-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax.lark
51 lines (41 loc) · 1.33 KB
/
syntax.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
start:com
aexp: term
| aexp "+" term -> add
| aexp "-" term -> sub
term: factor
| term "*" factor -> mul
| term "/" factor -> div
factor: NUM
| VAR
| VAR "(" (aexp ",")* ")" -> call
| "(" aexp ")"
| "<input>" -> input // only supported for compiler
bexp: band
| bexp "or" band -> or
band: bnot
| band "and" bnot -> and
bnot: batom
| "not" bnot -> not
batom: TRUE
| FALSE
| aexp "<" aexp -> lt // only supported for interpreter
| aexp "<=" aexp -> le // only supported for compiler
| aexp "=" aexp -> eq
| "(" bexp ")"
com: SKIP
| VAR ":=" aexp -> assign
| com ";" com -> seq
| "if" bexp "then" com "else" com "end" -> ifelse
| "while" bexp "do" com "end" -> while
| "print" aexp -> print
| "def" VAR "(" (VAR ",")* ")" "{" com "return" aexp "}" -> def
%import common.CNAME -> NAME
%import common.INT -> INT
%import common.WS
%ignore WS
%ignore /(#).*/ //comment
VAR: /(?!(if|then|else|end|while|do|print|not|and|or|true|false|def|return)\b)[a-zA-Z_]\w*/
NUM: INT | "-" INT
TRUE: "true"
FALSE: "false"
SKIP : "skip"