-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_grammar.txt
44 lines (37 loc) · 1.97 KB
/
parser_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
program = { statement } ;
(* Statement *)
statement = block
| variable_declaration
| expression_statement
| print_statement
| if_statement
| while_statement
| return_statement ;
block = "{" , { statement } , "}" ;
variable_declaration = ( "let" | "const" ) , identifier , [ "=" , expression ] ;
expression_statement = expression ;
print_statement = "print" , expression ;
if_statement = "if" , "(" , expression , ")" , statement , [ "else" , statement ] ;
while_statement = "while" , "(" , expression , ")" , statement ;
return_statement = "return" , [ expression ] ;
(* Expression *)
expression = assignment_expression ;
assignment_expression = conditional_expression , [ assignment_operator , assignment_expression ] ;
conditional_expression = logical_or_expression , [ "?" , expression , ":" , conditional_expression ] ;
(* BinaryExpression *)
logical_or_expression = logical_and_expression , { "|" , logical_and_expression } ;
logical_and_expression = equality_expression , { "&" , equality_expression } ;
equality_expression = relational_expression , { ( "==" | "!=" ) , relational_expression } ;
relational_expression = additive_expression , { ( "<" | "<=" | ">" | ">=" ) , additive_expression } ;
additive_expression = multiplicative_expression , { ( "+" | "-" ) , multiplicative_expression } ;
multiplicative_expression = unary_expression , { ( "*" | "/" ) , unary_expression } ;
(* UnaryExpression *)
unary_expression = postfix_expression | ( ( "-" | "!" ) , unary_expression ) ;
(* PostfixExpression *)
postfix_expression = primary_expression , { "[" , expression , "]" | "." , identifier | "(" , [ argument_list ] , ")" } ;
argument_list = expression , { "," , expression } ;
primary_expression = identifier | literal | "(" , expression , ")" ;
assignment_operator = "=" | "+=" | "-=" | "*=" | "/=" ;
identifier = letter , { letter | digit | "_" } ;
literal = NUMBER | STRING | BOOLEAN | ARRAY | NULL ;
(* Pending add variables *)