forked from decaf-lang/minidecaf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniDecaf.g4
More file actions
57 lines (48 loc) · 2.22 KB
/
Copy pathMiniDecaf.g4
File metadata and controls
57 lines (48 loc) · 2.22 KB
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
grammar MiniDecaf;
import CommonLex;
prog
: (func | decl_global ';')* EOF
;
func
: type Identifier '(' (type Identifier ',')* (type Identifier)? ')' ('{' blockItem* '}' | ';')
;
blockItem
: stmt
| decl ';'
;
decl_global
: type Identifier ('=' Interger)? # globalVar
;
decl
: type Identifier ('=' expr)? # varDef
;
stmt
: 'return' expr ';' # returnStmt
| expr ';' # singleExpr
| 'if' '(' expr ')' stmt ('else' stmt)? # ifStmt
| '{' blockItem* '}' # block
| 'while' '(' expr ')' stmt # whileLoop
| 'do' stmt 'while' '(' expr ')' ';' # doWhile
| 'for' '(' (decl | expr)? ';' (expr)? ';' (expr)? ')' stmt # forLoop
| 'break' ';' # break
| 'continue' ';' # continue
| ';' # nop
;
expr
: Identifier '(' (expr ',')* (expr)? ')' # funcCall
| ('!' | '~' | '-') expr # unaryOp
| expr ('*' | '/' | '%') expr # mulDiv
| expr ('+' | '-') expr # addSub
| expr ('<' | '<=' | '>' | '>=') expr # lessGreat
| expr ('==' | '!=') expr # equal
| expr '&&' expr # land
| expr '||' expr # lor
| expr '?' expr ':' expr # condExpr
| '(' expr ')' # atomParen
| Identifier '=' expr # assign
| Identifier # Identifier
| Interger # integer
;
type
: 'int'
;