-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.lark
54 lines (39 loc) · 1.35 KB
/
grammar.lark
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
?start: program
program : statement* return_statement
?statement : r_exp | assignment | if_statement | while_statement | return_statement
assignment : l_exp [":" ident] "=" r_exp ";"
return_statement : "return" r_exp ";"
while_statement : "while" log_exp "{" (statement)* "}"
if_statement : "if" log_exp "{" (statement)* "}" else_statement
| "if" log_exp "{" (statement)* "}"
else_statement : "elif" log_exp "{" (statement)* "}" else_statement
| "elif" log_exp "{" (statement)* "}"
| "else" "{" (statement)* "}"
log_exp: r_exp "==" r_exp -> equals
| r_exp "<=" r_exp -> less_equals
| r_exp "<" r_exp -> less_than
| r_exp ">=" r_exp -> great_equals
| r_exp ">" r_exp -> great_than
| r_exp "and" r_exp -> and_
| r_exp "or" r_exp -> or_
| "not" r_exp -> not_
| "(" r_exp ")"
| NUMBER
r_exp : sum
l_exp : IDENT -> var
?sum: product
| sum "+" product -> add
| sum "-" product -> sub
?product: atom
| product "*" atom -> mul
| product "/" atom -> div
?atom: NUMBER -> number
| IDENT -> ident
| "-" atom -> neg
| "(" sum ")"
number: NUMBER
NUMBER: /[0-9]+/
ident: IDENT
IDENT: /[_a-zA-Z][_a-zA-Z0-9]*/
%import common.WS
%ignore WS