PARSER_BEGIN(Q2) class Q2 { /** The size of the stack **/ static int stackSize = 0; /** 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 { parser.s(); System.out.println("Stack size is: "+stackSize); } catch (ParseException e) { System.out.println("Encountered errors during parse."); } } /* End of function "main" */ } PARSER_END(Q2) /******************/ /***** TOKENS *****/ /******************/ SKIP : /* WHITE SPACE */ { " " | "\t" | "\n" | "\r" } TOKEN : { < PUSH : "push" > | < POP : "pop"> | < SEMIC : ";" > | < ASSIGN : ":=" > | < NUM : (["0"-"9"])+ > | < ID : ["a"-"z"] > } /*************************/ /***** GRAMMAR RULES *****/ /*************************/ void s () : {} { a() ( a() )* } void a() : {} { { stackSize++; } | { if (stackSize>0) stackSize--; } }