PARSER_BEGIN(Calc2) public class Calc2 { public static void main (String args []) { Calc2 parser = new Calc2(System.in); for (;;) try { if (parser.expr() == -1) System.exit(0); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } } PARSER_END(Calc2) 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() throws Exception: // expr: sum \n { int e; } // prints result; -1 at eof, 0 at eol/error { try { ( e = sum() { System.out.println("\t"+e); return 1; } | { return 0; } | { return -1; } ) } catch (Exception err) { if (err instanceof ParseException || err instanceof ArithmeticException || err instanceof NumberFormatException) { System.err.println(err); for (;;) switch (getNextToken().kind) { case EOF: return -1; case EOL: return 0; } } throw err; } } int sum() throws NumberFormatException: // sum: product { +- product } { int s, r; } // returns value { s = product() ( "+" r = product() { s += r; } | "-" r = product() { s -= r; } )* { return s; } } int product() throws NumberFormatException: // product: term { *%/ term } { int p, r;} // returns value { p = term() ( "*" r = term() { p *= r; } | "%" r = term() { p %= r; } | "/" r = term() { p /= r; } )* { return p; } } int term() throws NumberFormatException: // term: +term | -term | (sum) | number { int t; } // returns value { "+" t = term() { return t; } | "-" t = term() { return -t; } | "(" t = sum() ")" { return t; } | { return Integer.parseInt(token.image); } }