-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.py
45 lines (38 loc) · 1.68 KB
/
grammar.py
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
GRAMMAR = r"""
program : declaration*
declaration : fun_decl | statement
fun_decl : "fun" function "nuf"
statement : expression_stmt | return_stmt | if_stmt | for_stmt | break_stmt | continue_stmt
expression_stmt : expression? ";"
return_stmt : "return" expression? ";"
if_stmt : "if" "(" expression ")" declaration* "fi"
for_stmt : "for" "(" expression_stmt expression_stmt expression ")" declaration* "rof"
break_stmt : "break" ";"
continue_stmt : "continue" ";"
expression : assignment
assignment : identifier "=" assignment | logic_or
logic_or : logic_and ( "or" logic_and )*
logic_and : equality ( "and" equality )*
equality : comparison ( ( "!=" | "==" ) comparison )*
comparison : term ( ( ">" | ">=" | "<" | "<=" ) term )*
term : factor ( ( "-" | "+" ) factor )*
factor : unary ( ( "/" | "*" ) unary )*
unary : ( "!" | "-" ) unary | call
call : primary ( "(" arguments? ")" )*
primary : "true" -> true | "false" -> false | "nil" -> nil
| NUMBER -> number | ESCAPED_STRING -> string
| identifier | "(" expression ")"
function : identifier "(" parameters? ")" declaration*
parameters : identifier ( "," identifier )*
arguments : expression ( "," expression )*
identifier : CNAME
%import common.ESCAPED_STRING
%import common.LETTER
%import common.DIGIT
%import common.NUMBER
%import common.CNAME
%import common.WS
%ignore WS
%import common.SH_COMMENT
%ignore SH_COMMENT
"""