-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar.txt
63 lines (33 loc) · 1.51 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# This is the grammar understood by the Parser
program -> declaration* EOF
declaration -> variableDeclaration | functionDeclaration | statement;
variableDeclaration -> 'var' IDENTIFIER ('=' expression)? ';'
functionDeclaration -> 'fun' function
statement -> expressionStatement | printStatement | block | ifStatement | whileStatement | forStatement
function -> IDENTIFIER '(' parameters? ')' block
parameters -> IDENTIFIER ( ',' IDENTIFIER)*
printStatement -> 'print' expression ';'
expressionStatement -> expression ';'
block -> '{' declaration* }'
ifStatement -> 'if' '(' expression ')' statement ( 'else' statement )?
whileStatement -> 'while' '(' expression ')' statement
forStatement -> 'for' '(' ( variableDeclaration | expressionStatement | ';' ) expression? ';' expression? ')' statement
expression -> commaSeparated
commaSeparated -> assignment ( "," 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 | functionCall
functionCall -> primary ( '(' arguments? ')' )*
primary -> NUMBER
| STRING
| "false"
| "true"
| "nil"
| "(" commaSeparated ")"
| IDENTIFIER
arguments -> expression ( ',' expression )*