Adder.jj文件定义

/* adder.jj Adding up numbers */ options { STATIC = false ; } PARSER_BEGIN(Adder) import java.io.*; class Adder { public static void main( String[] args ) throws ParseException, TokenMgrError { Adder parser = new Adder( System.in ) ; parser.Start( System.out ) ; }

double previousValue = 0.0 ; } PARSER_END(Adder)

SKIP:{" "} TOKEN:{< EOL : "\n"|"\r"|"\r\n" >} TOKEN:{< PLUS : "+">} TOKEN :{ < MINUS : "-" > } TOKEN:{< TIMES : "*" > } TOKEN:{< DIVIDE : "/" > } TOKEN:{< OPEN_PAR : "(" > } TOKEN:{< CLOSE_PAR : ")" > } TOKEN:{< PREVIOUS : "$" > }

TOKEN:{< NUMBER : | "."  | "."|"."  >} TOKEN : {< #DIGITS : (["0"-"9"])+ >}

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 Primary throws NumberFormatException : { Token t; double d; } { t =  { return Double.parseDouble( t.image ); }  { return previousValue; }  d = Expression  { return d; }  d = Primary { return -d; } }

double Term throws NumberFormatException : { double i; double value; } { value = Primary (  i = Primary { value *= i;}  i = Primary  { value /= i; } )* { return value; } }