forked from thufv/CMinor-Verifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMinorParser.g4
173 lines (140 loc) · 4.47 KB
/
CMinorParser.g4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* compatible with ACSL language grammar v1.17
*/
parser grammar CMinorParser;
options {
tokenVocab = CMinorLexer;
}
@header {#pragma warning disable 3021}
/* top level */
main: def* EOF;
def: funcDef | structDef | predDefs;
funcDef:
funcContract retVar '(' (paraVar (',' paraVar)*)? ')' '{' (
decl
| stmt
)* '}';
structDef: 'struct' IDENT '{' (atomicType IDENT ';')* '}' ';';
/* variable */
localVar:
atomicType IDENT
| 'struct' IDENT IDENT
| atomicType IDENT '[' INT_CONSTANT ']';
paraVar:
atomicType IDENT
| 'struct' IDENT IDENT
| atomicType IDENT '[' ']';
retVar:
atomicType IDENT
| 'struct' IDENT IDENT
| 'void' IDENT;
atomicType: 'int' | 'float';
logicParaVar:
logicAtomicType IDENT
| 'struct' IDENT IDENT
| logicAtomicType '[' ']' IDENT;
logicAtomicType: 'integer' | 'real' | 'boolean';
/* about statement */
stmt:
';' # EmptyStmt
| expr ';' # ExprStmt
| assign ';' # AssignStmt
| 'if' '(' expr ')' stmt ('else' stmt)? # IfStmt
| loopAnnot 'while' '(' expr ')' stmt # WhileStmt
| loopAnnot 'do' stmt 'while' '(' expr ')' # DoStmt
| loopAnnot 'for' '(' forInit? ';' expr? ';' forIter? ')' stmt # ForStmt
| 'break' ';' # BreakStmt
| 'continue' ';' # ContStmt
| 'return' expr? ';' # ReturnStmt
| assertion # AssertStmt
| '{' (stmt | decl)* '}' # BlockStmt;
forInit: localVar ('=' expr)? | assign;
forIter: assign | expr;
assign:
IDENT '=' expr # VarAssign
| IDENT '[' expr ']' '=' expr # SubAssign
| IDENT '.' IDENT '=' expr # MemAssign;
decl: localVar ('=' expr)? ';';
/* about expression */
expr:
IDENT # IdentExpr
| constant # ConstExpr
| IDENT '(' (expr (',' expr)*)? ')' # CallExpr
| '(' expr ')' # ParExpr
| expr '[' expr ']' # ArrAccessExpr
| expr '.' IDENT # MemExpr
| ('!' | '-') expr # UnaryExpr
| expr ('*' | '/' | '%') expr # MulExpr
| expr ('+' | '-') expr # AddExpr
| expr ('<' | '<=' | '>' | '>=') expr # OrdExpr
| expr ('==' | '!=') expr # EquExpr
| expr '&&' expr # AndExpr
| expr '||' expr # OrExpr;
/* annotation */
logicConstant:
INT_CONSTANT
| FLOAT_CONSTANT
| '\\true'
| '\\false';
arithTerm:
IDENT # IdentTerm
| '\\result' # ResTerm
| logicConstant # ConstTerm
| '{' arithTerm '\\with' '[' arithTerm ']' '=' arithTerm '}' # ArrUpdTerm
| '(' arithTerm ')' # ParArithTerm
| arithTerm '[' arithTerm ']' # ArrAccessTerm
| arithTerm '.' IDENT # MemTerm
| ('-' | '!') arithTerm # UnaryTerm
| arithTerm ('*' | '/' | '%') arithTerm # MulTerm
| arithTerm ('+' | '-') arithTerm # AddTerm;
term:
arithTerm # AriTerm
| '(' term ')' # ParTerm
| term ('<' | '<=' | '>' | '>=') term # OrdTerm
| term ('==' | '!=') term # EquTerm
| term '&&' term # AndTerm
| term '||' term # OrTerm;
pred:
'\\true' # TruePred
| '\\false' # FalsePred
| arithTerm (
('<' | '<=' | '>' | '>=' | '==' | '!=') arithTerm
)+ # CmpPred
| IDENT ('(' term (',' term)* ')')? # CallPred
| '(' pred ')' # ParPred
| pred '&&' pred # ConPred
| pred '||' pred # DisPred
| pred '==>' pred # ImpPred
| pred '<==>' pred # IffPred
| '!' pred # NegPred
| pred '^^' pred # XorPred
// 这里化简了 ACSL 中 location 和 tset 的概念 range 是个闭区间,其首尾的类型都只能是 integer
| '\\valid' '(' IDENT '+' '(' INT_CONSTANT '..' arithTerm ')' ')' # LengthPred
| ('\\forall' | '\\exists') binder (',' binder)* ';' pred # QuantiPred;
binder: logicAtomicType IDENT (',' IDENT)*;
funcContract:
'/*@' requiresClause* decreasesClause? ensuresClause* '*/'
| '//@' requiresClause* decreasesClause? ensuresClause* LINEEND;
requiresClause: 'requires' pred ';';
decreasesClause:
'decreases' (arithTerm | '(' arithTerm (',' arithTerm)+ ')') ';';
ensuresClause: 'ensures' pred ';';
assertion:
'/*@' 'assert' pred ';' '*/'
| '//@' 'assert' pred ';' LINEEND;
loopAnnot:
'/*@' ('loop' 'invariant' pred ';')* (
'loop' 'variant' (arithTerm | '(' arithTerm (',' arithTerm)+ ')') ';'
)? '*/'
| '//@' ('loop' 'invariant' pred ';')* (
'loop' 'variant' (arithTerm | '(' arithTerm (',' arithTerm)+ ')') ';'
)? LINEEND;
predDefs:
'/*@' predDef* '*/'
| '//@' predDef* LINEEND;
predDef:
'predicate' IDENT (
'(' logicParaVar (',' logicParaVar)* ')'
)? '=' pred ';';
/* miscellaneous */
constant: INT_CONSTANT | FLOAT_CONSTANT | 'true' | 'false';