-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEBNF.txt
48 lines (42 loc) · 1.71 KB
/
EBNF.txt
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
CompUnit ::= [CompUnit] (Decl | FuncDef);
FuncDef ::= FuncType IDENT "(" [FuncFParams] ")" Block;
FuncType ::= "void" | "int";
FuncFParams ::= FuncFParam {"," FuncFParam};
FuncFParam ::= BType IDENT;
Block ::= "{" {BlockItem} "}";
BlockItem ::= Decl | Stmt;
Stmt ::= OpenStmt | ClosedStmt
OpenStmt ::= OpenIf | OpenWhile
ClosedStmt ::= SimpleStmt | ClosedIf | ClosedWhile
OpenIf ::= "if" "(" Exp ")" Stmt
| "if" "(" Exp ")" ClosedStmt "else" OpenStmt
ClosedIf ::= "if" "(" Exp ")" ClosedStmt "else" ClosedStmt
OpenWhile ::= "while" "(" Exp ")" OpenStmt
ClosedWhile ::= "while" "(" Exp ")" ClosedStmt
SimpleStmt ::= LVal "=" Exp ";"
| [Exp] ";"
| Block
| "break" ";"
| "continue" ";"
| "return" [Exp] ";";
Decl ::= ConstDecl | VarDecl;
ConstDecl ::= "const" BType ConstDef {"," ConstDef} ";";
BType ::= "int";
ConstDef ::= IDENT "=" ConstInitVal;
ConstInitVal ::= ConstExp;
VarDecl ::= BType VarDef {"," VarDef} ";";
VarDef ::= IDENT | IDENT "=" InitVal;
InitVal ::= Exp;
Exp ::= EqExp;
ConstExp ::= Exp;
PrimaryExp ::= "(" Exp ")" | LVal | Number | CallExp;
LVal ::= IDENT;
Number ::= INT_CONST;
UnaryExp ::= PrimaryExp | UnaryOp UnaryExp;
UnaryOp ::= "+" | "-" | "!";
MulExp ::= UnaryExp | MulExp ("*" | "/" | "%") UnaryExp;
AddExp ::= MulExp | AddExp ("+" | "-") MulExp;
CallExp ::= IDENT "(" [FuncRParams] ")"
FuncRParams ::= Exp {"," Exp};
RelExp ::= AddExp | RelExp ("<" | ">" | "<=" | ">=") AddExp;
EqExp ::= RelExp | EqExp ("==" | "!=") RelExp;