-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrammar.txt
49 lines (25 loc) · 1.63 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
<program> := <declaration>* EOF
<declaration> := <varDeclaration> | <funcDeclaration> | <statement>;
<funcDeclaration> := "function" <TYPE> <function>;
<varDeclaration> := <TYPE> <IDENTIFIER> ("=" expression>)? ";"
<statement> := <expressionStatement> | <printStatement> | <block> | <ifStatement> | <whileStatement | forStatement
<expressionStmt> := <expression> ";"
<print> := "print" <expression> ";"
<block> := "{" + <declaration> + "}"
<expression> := <assignment>
<assignment> := <IDENTIFIER> "=" <assignment> | <logicOr>
<logicOr> := <logicAnd> ( "or" <logicAnd> )
<logicAnd> := <equality> ( "or" <equality> )
<equality> := <comparison> ( ( "!=" | "==" ) <comparison> )*
<comparison> := <term> ( ( ">" | ">=" | "<" | "<=" ) <term> )*
<term> := <factor> ( ( "-" | "+" ) <factor> )*
<factor> := <unary> ( ( "/" | "*" | "%" ) <unary> )*
<unary> := ( "-" | "!" ) <unary> | <call>
<primary> := <NUMBER> | <STRING> | "true" | "false" | "nil" | "(" <expression> ")" | <IDENTIFIER>
<ifStatement> := "if" "(" <expression> ")" <statement> ( "else" <statement> )?
<whileStatement> := "while" "(" <expression> ")" <statement>
<forStatement> := "for" "(" ( <varDeclaration> | <expressionStatement> | ";" ) <expression>? ";" <expression>? ")" <statement>
<call> := <IDENTIFIER> ( "(" <arguments>? ")" )*
<function> := "function" <TYPE> <IDENTIFIER> "(" <parameters>? ")" <block>;
<arguments> := <IDENTIFIER> ( "," <IDENTIFIER> )*
<parameters> := <type> <IDENTIFIER> ( "," <type> <IDENTIFIER> )*