/** * JavaCC file */ options { JDK_VERSION = "1.5"; } PARSER_BEGIN(eg1) package org.simtk.parser; public class eg1 { public static void main(String args[]) throws ParseException { eg1 parser = new eg1(System.in); while (true) { System.out.println("Reading from standard input..."); System.out.print("Enter an expression like \"1+(2+3)*4;\" :"); try { switch (eg1.one_line()) { case 0: System.out.println("OK."); break; case 1: System.out.println("Goodbye."); break; default: break; } } catch (Exception e) { System.out.println("NOK."); System.out.println(e.getMessage()); eg1.ReInit(System.in); } catch (Error e) { System.out.println("Oops."); System.out.println(e.getMessage()); break; } } } } PARSER_END(eg1) SKIP : { " " | "\r" | "\t" | "\n" } TOKEN : /* OPERATORS */ { < PLUS: "+" > | < MINUS: "-" > | < MULTIPLY: "*" > | < DIVIDE: "/" > } TOKEN : { < CONSTANT: ( )+ > | < #DIGIT: ["0" - "9"] > } int one_line() : {} { sum() ";" { return 0; } | ";" { return 1; } } void sum() : {} { term() (( | ) term())* } void term() : {} { unary() (( | ) unary())* } void unary() : {} { element() | element() } void element() : {} { | "(" sum() ")" }