/* Calculator.jj calculadora simples para adição*/ options { STATIC = false; } PARSER_BEGIN(Calculator0) import java.io.PrintStream; class Calculator0 { public static void main (String args[]) throws ParseException, TokenMgrError, NumberFormatException { Calculator0 parser = new Calculator0(System.in); parser.Start(System.out); } double previousValue = 0.0; } PARSER_END(Calculator0) SKIP: {" "} TOKEN: {< EOL: "\n"|"\r"|"\r\n" > } TOKEN: { < PLUS: "+" > } TOKEN: { < NUMBER: |"."||"."|"." > } TOKEN: { < #DIGITS: (["0"-"9"])+ > } //declaração de um token "local", por isso o '#' void Start(PrintStream printStream) throws NumberFormatException : {} { ( previousValue = Expression() { printStream.println(previousValue); } )* } double Expression() throws NumberFormatException : { double i; double value; } { value = Primary() ( i = Primary() {value +=i; } )* {return value; } } double Primary() throws NumberFormatException : { Token t; } { t = {return Double.parseDouble( t.image );} }