options{ //DEBUG_PARSER = true; DEBUG_TOKEN_MANAGER = true; STATIC=false; } PARSER_BEGIN(Calculator) import java.util.Scanner; import java.io.StringReader; public class Calculator { private static double currentNumber; public static double fromString(String stringToParse) throws ParseException{ currentNumber = 0.0; Calculator parser = new Calculator(new StringReader(stringToParse)); parser.parse(); return currentNumber; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (true){ System.out.print("Type your expression: "); String expression = scanner.nextLine(); try{ System.out.println("Returned: " + fromString(expression)); }catch (Throwable t){ System.out.println(t); } } } } PARSER_END(Calculator) /** * For now only using the Hello World grammar */ SKIP : { " "|"\t"|"\n"|"\r" } TOKEN : { | | | | | | | | | } double factor() : { Token t; double i; } { t = { i = Double.parseDouble(t.image); currentNumber += i; return i; } } void parse() : { } { (factor())* }