options { IGNORE_CASE = false; OPTIMIZE_TOKEN_MANAGER = true; } PARSER_BEGIN(tea) import java.io.*; public class tea { 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); } tea parser = new tea( new FileInputStream(args[0]) ); parser.program(); System.out.println("Parse completed."); } } PARSER_END(tea) SKIP: /* Whitespace */ { "\t" | "\n" | "\r" | " " } TOKEN: /* Most Things */ { <IF: "if"> | <ELSE: "else"> | <TYPE: "void"> | <VARIABLE_TYPE: "int"> | <LPAREN: "("> | <RPAREN: ")"> | <LBRACE: "{"> | <RBRACE: "}"> | <COMMA: ","> | <SEMICOLON: ";"> | <WHILE: "while"> | <DO: "do"> | <FOR: "for"> } TOKEN: /* Expression Tokens */ { <ADDOP: "+" | "-"> | <MULOP: "*" | "/" | "%"> | <ASSIGNOP: "="> | <NOT: "!"> | <AND: "&&"> | <OR: "||"> | <RELOP: "!=" | "=="> | <LTGT: ">" | "<" | ">=" | "<="> } TOKEN: /* Generic Name & Number */ { <NUMBER: (["0"-"9"])+> | <NAME: ["a"-"z","A"-"Z"] (["a"-"z","A"-"Z","0"-"9","_"])*> } void program() : {} { method_declaration() <EOF> } void method_declaration() : {} { <TYPE> <NAME> <LPAREN> <RPAREN> <LBRACE> statement_block() <RBRACE> { System.out.println("method_declaration"); } } void statement_block() : {} { ( statement() )* } void statement() : {} { simplestatement() <SEMICOLON> {System.out.println("SimpleStatement");} | compoundstatement() {System.out.println("CompoundStatement");} | <LBRACE> statement_block() <RBRACE> } void simplestatement() : {} { declarativestatement() | assignmentstatement() } void declarativestatement() : {} { <VARIABLE_TYPE> assignmentstatement() (<COMMA> assignmentstatement())* { System.out.println("Declarative Statement"); } } void assignmentstatement() : {} { <NAME> [<ASSIGNOP> expression()] { System.out.println("Assignment Statement"); } } void compoundstatement() : {} { ifstatement() { System.out.println("ifstatement"); } | loopstatement() { System.out.println("loopstatement"); } } void ifstatement() : {} { <IF> condition() statement() [<ELSE> statement()] } void condition() : {} { <LPAREN> expression() <RPAREN> { System.out.println("Condition"); } } void loopstatement() : {} { whilestatement() { System.out.println("WhileStatement"); } | dowhilestatement() { System.out.println("DoWhileStatement"); } | forstatement() { System.out.println("ForStatement"); } } void whilestatement() : {} { <WHILE> condition() statement() } void dowhilestatement() : {} { <DO> statement() <WHILE> condition() <SEMICOLON> } void forstatement() : {} { <FOR> <LPAREN> [ for_expression() ] <SEMICOLON> [expression()] <SEMICOLON> [ for_expression() ] <RPAREN> statement() } void for_expression() : {} { declarativestatement() | assignmentstatement() (<COMMA> assignmentstatement())* } /* Expression Statements */ void expression() : {} { or_expression() { System.out.println("expression"); } } void or_expression() : {} { and_expression() (<OR> and_expression())* } void and_expression() : {} { relop_expression() (<AND> relop_expression())* } void relop_expression() : {} { ltgt_expression() (<RELOP> ltgt_expression())* } void ltgt_expression() : {} { addop_expression() (<LTGT> addop_expression())* } void addop_expression() : {} { mulop_expression() (<ADDOP> mulop_expression())* } void mulop_expression() : {} { term() (<MULOP> term())* } void term() : {} { <NOT> value() | <ADDOP> value() | value() } void value() : {} { <NAME> | <NUMBER> | <LPAREN> expression() <RPAREN> }
Send questions and comments to: Karen Lemone