PARSER_BEGIN(Calc3) public class Calc3 { public static void main (String args []) { Calc3 parser = new Calc3(System.in); for (;; jjtree.reset()) // restart tree builder try { switch (parser.expr()) { default: ((SimpleNode)jjtree.rootNode()).dump("\t"); case 0: break; case -1: System.exit(0); } } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } PARSER_END(Calc3) SKIP: // defines input to be ignored { " " | "\r" | "\t" } TOKEN: // defines token names { < EOL: "\n" > | < CONSTANT: ( )+ > // re: 1 or more | < #DIGIT: ["0" - "9"] > // private re } int expr(): // expr: sum \n {} // -1 at eof, 0 at eol/error { try { ( sum() { return 1; } | { return 0; } | { return -1; } ) } catch (ParseException pe) { System.err.println(pe); for (;;) switch (getNextToken().kind) { case EOF: return -1; case EOL: return 0; } } } void sum(): {} // sum: product { +- product } { product() ( ( "+" | "-" ) product() )* } void product(): {} // product: term { *%/ term } { term() ( ( "*" | "%" | "/" ) term() )* } void term(): {} // term: +term | -term | (sum) | number { "+" term() | "-" term() | "(" sum() ")" | }