PARSER_BEGIN(Q2) class Q2 { /** Standard code to call the parser */ public static void main(String args[]) { Q2 parser; System.out.println("Reading from standard input . . ."); parser = new Q2(System.in); try { int res = parser.s(); System.out.println("The answer is: "+res); } catch (ParseException e) { System.out.println("Encountered errors during parse."); } } /* End of function "main" */ } PARSER_END(Q2) /******************/ /***** TOKENS *****/ /******************/ SKIP : /* WHITE SPACE */ { " " | "\t" | "\n" | "\r" | "\f" } TOKEN : { < PLUS : "+" > | < TIMES : "*" > | < NUM : ()+ > | < #DIGIT : ["0"-"9"] > | < LBR : "(" > | < RBR : ")" > } /*************************/ /***** GRAMMAR RULES *****/ /*************************/ int s() : { int i; } { i=e() { return i; } } int e() : { int i1, i2; } { i1=t() ( i2=t() { i1 += i2; } )* { return i1; } } int t() : { int i1, i2; } { i1=f() ( i2=f() { i1 *= i2; } )* { return i1; } } int f() : { Token t; int i; } { t= { return Integer.parseInt(t.image); } | i=e() { return i; } }