-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleC.g4
56 lines (47 loc) · 1.57 KB
/
SimpleC.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
grammar SimpleC;
program: function+ ;
function: ID '(' formalParams? ')' '{' decl* stmt* '}' ;
formalParams: ID (',' ID)* ;
decl: type ID ';' ;
type: 'int' | 'bool' ;
stmt: 'input' ID ';' # Input
| 'output' expr ';' # Output
| ID '=' expr ';' # Assignment
| 'while' '(' expr ')' stmt # While
| 'if' '(' expr ')' stmt 'else' stmt # IfThenElse
| 'return' expr ';' # Return
| 'skip' ';' # Skip
| '{' stmt* '}' # Compound
;
expr: ID '(' actualParams? ')' # Call
| '-' expr # Negate
| '!' expr # Not
| expr op=('*' | '/') expr # MultDiv
| expr op=('+' | '-') expr # AddSub
| expr op=('<' | '>' | '<=' | '>=') expr # Relational
| expr op=('==' | '!=') expr # EqNeq
| expr op=('&&' | '||') expr # AndOr
| ID # Var
| NUM # Num
| '(' expr ')' # Parens
;
actualParams: expr (',' expr)* ;
//lexical spec
INT: 'int' ;
BOOL: 'bool' ;
ID: [a-zA-Z] ([a-zA-Z] | [0-9])* ;
NUM: [0-9]+ ;
MULT: '*' ;
DIV: '/' ;
PLUS: '+' ;
MINUS: '-' ;
AND: '&&' ;
OR: '||' ;
EQ: '==' ;
NEQ: '!=' ;
LT: '<' ;
GT: '>' ;
LE: '<=' ;
GE: '>=' ;
WS: [ \t\n\r]+ -> skip ;
SL_COMMENT: '//' .*? '\n' -> skip ;