(a) Write JavaCC code to create a parser for this subset of Tea . Add print statements after each production to print out the name of the nonterminal on the left-hand-side of the production.
(b) Run it on the assigned programs from the lexer assignment and a program of your choice.
(c) Using the printed output, create a parse tree outline by hand. (In the next assignment, we'll create internal abstract syntax trees.) If you have a lot of nodes for your example, you need not draw every node.
The following is a subset of the solution to this project:
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 tokens */ { <IF: "if"> | <ELSE: "else"> | <TYPE: "void"> ..... 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> | compoundstatement() | <LBRACE> statement_block() <RBRACE> { System.out.println("statement"); } } void assignmentstatement() : {} { <NAME> [<ASSIGNOP> expression()] { System.out.println("Assignment Statement"); } } ...Pass in:
Now is the time to add a Table of Contents to your project if you didn't do this for Assignments 1 and 2 . It should contain (at least) (1) references to the BNF, (2) the JavaCC code (which you most likely changed for this assignment), (3) the sample programs with their respective outputs and (4) your hand-drawn parse trees.
Send questions and comments to: Karen Lemone