options { IGNORE_CASE = false; OPTIMIZE_TOKEN_MANAGER = true; } PARSER_BEGIN(cal) import java.io.*; import java.util.*; public class cal { public static void main(String[] args) throws ParseException, FileNotFoundException { if ( args.length < 1 ) { System.out.println("Please pass in the filename for a parameter."); System.exit(1); } cal parser = new cal( new FileInputStream(args[0]) ); parser.calculator(); System.out.println("Parse completed."); } } PARSER_END(cal) SKIP: /* Whitespace */ { "\t" | "\n" | "\r" | " " } TOKEN: { <LPAREN: "("> | <RPAREN: ")"> | <ADD_OP: "+" | "-"> | <MULT_OP: "*" | "/"> | <NUMBER: (["0"-"9"])+> } void calculator() : {} { expression() < EOF> {System.out.println("Calculator");} } void expression() : {} { term() (<ADD_OP> term())* { System.out.println("Expression");} } void term(): {} { factor() (<MULT_OP> factor())* { System.out.println("Term");} } void factor(): {} { (<LPAREN> expression() <RPAREN> |number()) { System.out.println("Factor");} } void number() : {} { <NUMBER> { System.out.println("NUMBER");} }
NUMBER Factor NUMBER Factor Term NUMBER Factor Term Expression Factor NUMBER Factor Term NUMBER Factor Term Expression Calculator Parse completed.