package step7; import step7.expr.yyLex; import step7.expr.yyTree.*; /** evaluate {@link expr} tree with integer arithmetic. This implementation demonstrates local state. */ public class stack implements jag.Visitor { /** result of current {@link #visit}. */ protected int result; %% Add: Object Object {{ $2; int right = result; $1; result += right; }}; Sub: Object Object {{ $2; int right = result; $1; result -= right; }}; Mul: Object Object {{ $2; int right = result; $1; result *= right; }}; Div: Object Object {{ $2; int right = result; $1; result /= right; }}; Mod: Object Object {{ $2; int right = result; $1; result %= right; }}; Minus: Object { $1; result = - result; }; Number: { result = $0.intValue(); }; %% /** read an expression from standard input and output it's value. */ public static void main (String[] args) throws Exception { stack s = new stack(); s.visit(new expr().yyparse(new yyLex(System.in))); System.out.println("\t"+s.result); } }