-
Notifications
You must be signed in to change notification settings - Fork 3
/
calculator.cup
31 lines (27 loc) · 1016 Bytes
/
calculator.cup
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
import java_cup.runtime.*;
import tree.*;
terminal PLUS, MINUS, MUL, DIV, ASSIGN;
terminal LPAREN, RPAREN;
terminal Integer NUMBER;
terminal String IDENT;
non terminal Tree expr;
precedence left PLUS, MINUS;
precedence left MUL, DIV;
precedence left ASSIGN;
expr ::= expr:e1 PLUS expr:e2
{: RESULT = new Plus(e1, e2); :}
| expr:e1 MINUS expr:e2
{: RESULT = new Minus(e1, e2); :}
| expr:e1 MUL expr:e2
{: RESULT = new Multiply(e1, e2); :}
| expr:e1 DIV expr:e2
{: RESULT = new Divide(e1, e2); :}
| LPAREN expr:e RPAREN
{: RESULT = e; :}
| IDENT:s ASSIGN expr:e
{: RESULT = new Assignment(s, e); :}
| IDENT:s
{: RESULT = new Identifier(s); :}
| NUMBER:n
{: RESULT = new Literal(n); :}
;