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