forked from cheshire/antlr3-python-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEval.g
More file actions
43 lines (37 loc) · 811 Bytes
/
Eval.g
File metadata and controls
43 lines (37 loc) · 811 Bytes
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
tree grammar Eval;
options {
tokenVocab=Expr;
ASTLabelType=CommonTree;
language=Python;
}
// START:members
@members {
global memory, result
memory = dict()
result = list()
}
// END:members
// START:stat
prog: stat+ { return result};
stat: expr {
print $expr.value
result.append($expr.value)
}
| ^('=' ID expr) {
memory[$ID.getText()] = int($expr.value)
}
;
// END:stat
// START:expr
expr returns [int value]
: ^('+' a=expr b=expr) {$value = a+b;}
| ^('-' a=expr b=expr) {$value = a-b;}
| ^('*' a=expr b=expr) {$value = a*b;}
| ID {
value = memory.copy().pop($ID.getText());
}
| INT {
$value = int(str($INT));
}
;
// END:expr