-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
36 lines (36 loc) · 1.52 KB
/
grammar.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
program -> declaration* EOF ;
declaration -> varDecl | statement ;
varDecl -> "var" IDENTIFIER ( "=" expression)? ";" ;
statement -> expressionStmt
| forStmt
| ifStmt
| printStmt
| whileStmt
| blockStmt ;
expressionStmt -> expression ";" ;
forStmt -> "for" "("
( varDecl | exprStmt | ";" ) # initializer
expression? ";" # condition
expression? # incrementer
")" statement ;
ifStmt -> "if" "(" expression ")" statement ( "else" statement )? ;
printStmt -> "print" expression ";" ;
whileStmt -> "while" "(" expression ")" statement ;
blockStmt -> "{" declaration* "}" ;
expression -> assignment ;
assignment -> IDENTIFIER "=" assignment
| logic_or ;
logic_or -> logic_and ( "or" logic_and )* ;
logic_and -> equality ( "and" equality )* ;
equality -> comparison ( ( "!=" | "==" ) comparison )* ;
comparison -> addition ( ( ">" | ">=" | "<" | "<=" ) addition )* ;
addition -> multiplication ( ( "-" | "+" ) multiplication )* ;
multiplication -> unary ( ( "/" | "*" ) unary )* ;
unary -> ( "!" | "-" ) unary | primary ;
primary -> NUMBER
| STRING
| "false"
| "true"
| "nil"
| "(" expression ")"
| IDENTIFIER ;