-
Notifications
You must be signed in to change notification settings - Fork 0
/
EBNF.txt
46 lines (38 loc) · 1.4 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
Program --> (Function)* MainFunction
MainFunction --> "num" "main" "(" ")" Block
Function --> Type Identifier "(" FormalList ")" Block
FormalList --> VarDecl ("," VarDecl)*
Statement --> Block | If | While | VarDecl ";" | Assignment ";" | Return ";"
Block --> "{" (Statement)* "}"
If --> "if" "(" Expression ")" Statement "else" Statement
While --> "while" "(" Expression ")" Statement
VarDecl --> Type Identifier
Assignment --> Identifier "=" Expression
Return --> "return" Expression
Expression --> AndOrExpression
AndOrExpression --> CompExpression ( Cond Expression )?
CompExpression --> CondExpression ( Comp Expression )?
CondExpression --> AddSubExpression ( "?" Expression ":" Expression )?
AddSubExpression --> MultDivExpression ( AddSub Expression )?
MultDivExpression --> PrimaryExpression ( MulDiv CondExpression )?
PrimaryExpression -->
Identifier
| FunctionCall
| NumLiteral
| BooleanLiteral
| NotExpression
| "-" PrimaryExpression
| "(" Expression ")"
NotExpression --> "!" Expression
FunctionCall --> Identifier "(" ExpressionList ")"
ExpressionList --> Expression ("," Expression)*
Type --> "void" | "num" | "bool"
Identifier --> Letter (Letter | "_" | Digit)*
NumLiteral --> ("-")? (Digit)* (".")? (Digit)*
BooleanLiteral --> "true" | "false"
AddSub --> "+" | "-"
MulDiv --> "*" | "/"
Cond --> "&&" | "||"
Comp --> "<" | "<=" | "==" | ">=" | ">"
Letter --> ["A" - "Z", "a" - "z"]
Digit --> ["0" - "9"]