This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ifcc.g4
70 lines (58 loc) · 2.4 KB
/
ifcc.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
grammar ifcc;
axiom : prog EOF ;
prog : function+ ;
function : return_type VAR '(' parameters ')' '{' statement* '}' ;
return_type : TYPE | VOID ;
parameters : (TYPE VAR (',' TYPE VAR)*)? ;
statement
: 'return' expr? ';' # ret
| TYPE declareAssign (',' declareAssign)* ';' # declaration
| TYPE ( arrayDecl (',' arrayDecl)* ) ';' # arrayDeclaration
| '{' statement* '}' # block
| 'if' '(' expr ')' if_block=statement ('else' else_block=statement)? # if
| 'while' '(' expr ')' body=statement # while
| 'break' ';' # break
| 'continue' ';' # continue
| expr ';' # expression
;
declareAssign : VAR ('=' expr)? ;
arrayDecl : VAR '[' CONST ']' ('=' '{' expr (',' expr)* '}')? ;
expr
: '(' expr ')' # par
| VAR '['CONST']' # arrayAccess
| VAR INCREMENT # incrementAfter
| ADD_SUB expr # sign
| UNARY_OP expr # unary
| INCREMENT VAR # incrementBefore
| expr MUL_DIV expr # mulDiv
| expr ADD_SUB expr # addSub
| expr COMP expr # comparison
| expr BIT_AND expr # bitAnd
| expr BIT_XOR expr # bitXor
| expr BIT_OR expr # bitOr
| expr LOGIC expr # logicOp
| VAR assignmentop=('=' | '+=' | '-=' | '*=' | '/=') expr # assignment
| VAR '[' CONST ']' assignmentop=('=' | '+=' | '-=' | '*=' | '/=') expr # arrayAssignment
| VAR '(' (expr (',' expr)*)? ')' # callFunction
| CONST # const
| CHAR # char
| VAR # var
;
ADD_SUB : '+' | '-' ;
MUL_DIV : '*' | '/' | '%' ;
INCREMENT : '++' | '--' ;
UNARY_OP : '!' ;
LOGIC : '||' | '&&' ;
COMP : '==' | '<=' | '>=' | '<' | '>' | '!=';
BIT_AND : '&' ;
BIT_OR : '|' ;
BIT_XOR : '^' ;
TYPE : 'int' | 'char';
VOID : 'void' ;
CONST : [0-9]+ ;
CHAR : '\'' . '\'' ;
VAR : [_a-zA-Z][_a-zA-Z0-9]* ;
COMMENT : '/*' .*? '*/' -> skip ;
COMMENT2 : '//' .*? ('\n' | EOF) -> skip ;
DIRECTIVE : '#' .*? '\n' -> skip ;
WS : [ \t\r\n] -> channel(HIDDEN);