Skip to content

Commit

Permalink
Let the parser construct abstract syntax trees
Browse files Browse the repository at this point in the history
  • Loading branch information
romildo committed Oct 10, 2016
1 parent 843b9dc commit ea9ff95
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/main/cup/parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parse;
import error.ErrorManager;
import java_cup.runtime.Symbol;
import java_cup.runtime.ComplexSymbolFactory.ComplexSymbol;
import absyn.*;

parser code {:
/* override error routines */
Expand Down Expand Up @@ -32,25 +33,25 @@ terminal Double LITNUM;
terminal PLUS, MINUS, TIMES, DIV;
terminal LPAREN, RPAREN;

non terminal Double exp;
non terminal Double term;
non terminal Double factor;
non terminal Exp exp;
non terminal Exp term;
non terminal Exp factor;

start with exp;

exp ::=
exp:x PLUS term:y {: RESULT = x + y; :}
| exp:x MINUS term:y {: RESULT = x - y; :}
exp:x PLUS term:y {: RESULT = new ExpBinOp(ExpBinOp.Op.PLUS, x, y); :}
| exp:x MINUS term:y {: RESULT = new ExpBinOp(ExpBinOp.Op.MINUS, x, y); :}
| term:x {: RESULT = x; :}
;

term ::=
| term:x TIMES factor:y {: RESULT = x * y; :}
| term:x DIV factor:y {: RESULT = x / y; :}
| term:x TIMES factor:y {: RESULT = new ExpBinOp(ExpBinOp.Op.TIMES, x, y); :}
| term:x DIV factor:y {: RESULT = new ExpBinOp(ExpBinOp.Op.DIV, x, y); :}
| factor:x {: RESULT = x; :}
;

factor ::=
LITNUM:x {: RESULT = x; :}
LITNUM:x {: RESULT = new ExpNum(x); :}
| LPAREN exp:x RPAREN {: RESULT = x; :}
;

0 comments on commit ea9ff95

Please sign in to comment.