/* FinalCalculator.jj calculadora simples para as 4 operações básicas e parenteses*/ options { STATIC = false; } PARSER_BEGIN(FinalCalculator) import java.io.PrintStream; class FinalCalculator { public static void main (String args[]) throws ParseException, TokenMgrError, NumberFormatException { FinalCalculator parser = new FinalCalculator(System.in); parser.Start(System.out); } double previousValue = 0.0; } PARSER_END(FinalCalculator) SKIP: {" "} TOKEN: { < EOL: "\n"|"\r"|"\r\n" > } TOKEN: { < PLUS: "+" > } TOKEN: { < OPEN_PAR: "(" > } TOKEN: { < CLOSE_PAR: ")" > } TOKEN: { < PREVIOUS: "$" > } TOKEN: { < VEZES: "*" > } TOKEN: { < DIVIDE: "/" > } TOKEN: { < MENOS: "-" > } 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 = Term() ( i = Term() {value +=i; } | i = Term() {value -= i; } )* {return value; } } double Term() throws NumberFormatException : { double i; double value; } { value = Primary() ( i = Primary() { value *= i; } | i = Primary() { value /= i; } )* { return value; } } double Primary() throws NumberFormatException : { Token t; double d; } { t = {return Double.parseDouble( t.image );} | t = {return previousValue;} | d = Expression() {return d;} | d = Primary() { return -d;} }