/* adder.jj Simple interpreter of mathematical expression. By Pat Troy, February 2007 */ options { STATIC = false; IGNORE_CASE = true; } PARSER_BEGIN(Adder) public class Adder { public static void main(String args[]) throws ParseException, TokenMgrError { Adder interpreter = new Adder(System.in); System.out.println("Welcome to the Natural Language Calculator!"); System.out.println("I'm waiting for commands. Type 'quit' or 'exit' to quit."); double result = interpreter.Start(); System.out.println ("Start returns: " + result); } } PARSER_END(Adder) SKIP : { " " | "\t" | "\r" } TOKEN : { } TOKEN : { < EOL : "\n" > } TOKEN : { < PLUS : "+" > } double Start() throws NumberFormatException : { double val; } { ( val = Expression () { System.out.println ("The result is: " + val); } )* {return -5.5; } } double Expression() throws NumberFormatException : { Token t; double i; double value; } { t = {i = Double.parseDouble (t.image); } {value = i;} ( t = {i = Double.parseDouble (t.image); } {value += i;} )* { return value; } }