PARSER_BEGIN(Expression) import java.io.InputStreamReader; import java.io.IOException; import java.io.ObjectOutputStream; import java.util.Vector; import ck.Node; /** recognizes, stores, and evaluates arithmetic expressions using a parser generated with javacc. */ public class Expression { /** reads lines from standard input, parses, and evaluates them. or writes them as a Vector to standard output if -c is set. @param args if -c is specified, a Vector is written. */ public static void main (String args []) { boolean cflag = args.length > 0 && args[0].equals("-c"); try { Vector v = (Vector)new Expression(new InputStreamReader(System.in)) .lines(); if (cflag) { ObjectOutputStream out = new ObjectOutputStream(System.out); out.writeObject(v); out.close(); } else for (int n = 0; n < v.size(); ++ n) System.out.println(((Number)v.elementAt(n)).floatValue()); } catch (IOException e) { System.err.println(e); } catch (ParseException e) { } // cannot happen } } PARSER_END(Expression) SKIP: // defines input to be ignored { " " | "\r" | "\t" | < "#" (~ ["\n"])* > // comment from # to end of line } // re: many of negated character class TOKEN: // defines token names { < EOL: "\n" > | < INT: ( )+ > // re: some | < LONG: ( )+ ["l", "L"] > // re: class | < DOUBLE: ( )+ "." ( )* ( )? // re: many | "." ( )+ ( )? // re: alt | ( )+ > | < #DIGIT: ["0" - "9"] > // private re | < #EXP: ["e", "E"] ( ["+", "-"] )? ( )+ > // re: opt } Vector lines (): // lines: [{ [ sum ] "\n" }]; // returns vector of trees { Vector v = new Vector(); Number t; } { ( try { ( t = sum() { v.addElement(t); } | | { return v; } ) } catch (ParseException err) { System.err.println(err); skip: for (;;) switch (getNextToken().kind) { case EOF: return v; case EOL: break skip; } } )* } Number sum (): // sum: product [{ ("+"|"-") product }]; { Number s, r; } // returns tree { s = product() ( "+" r = product() { s = new Node.Add(s, r); } | "-" r = product() { s = new Node.Sub(s, r); } )* { return s; } } Number product (): // product: term [{ ("*"|"%"|"/") term }]; { Number p, r;} // returns value { p = term() ( "*" r = term() { p = new Node.Mul(p, r); } | "%" r = term() { p = new Node.Mod(p, r); } | "/" r = term() { p = new Node.Div(p, r); } )* { return p; } } Number term (): // term: "+"term | "-"term | "("sum")" | Number; { Number t; } // returns value { "+" t = term() { return t; } | "-" t = term() { return new Node.Minus(t); } | "(" t = sum() ")" { return t; } | { return new Integer(token.image); } | { return new Long(token.image.substring(0, token.image.length()-1)); } | { return new Double(token.image); } }