Have the lexical analyzer continue to print out tokens as before. In addition have the parser print out each successful reduction. For example, when a "compound_statement" is recognized, print "compound_statement". Indent these grammatical symbols so that the structure of the parse tree is evident in your output (simply indent one space each time you descend one level of recursion; decrease the amount of indenting upon return from a procedure). Do not attempt to construct the parse tree.
Run your parser on the example of assignment 1 as well as one program of your own. Your output should look something like this, where the input file "thesource.txt" means the sample source code given in assignment 1. The indentation and format doesn't have to match that exactly, but it should be substantially similar and the indenting (and undenting!!) should be readable and consistent.
Do not alter the grammar in your implementation.
Using javacc, the purely recognition functionality of your program should prove to be quite straightforward. The javacc code directly reflects the grammar. For example, the code for simple_expression might look like this:
void simple_expression () : {} { term() ( ADDOP term() )* }Please put the BNF production (given in assignment 1) as a comment above each rule such as the one just given.
So, you might say in response to the above hint, what's there to do?
That takes a little more work.
For one, if you haven't done so already, you may need to modify
your lexical analyzer to account
for such specific tokens as { and }. More
importantly, you need to fold actions into the grammar to get the output
behavior specified above. To this end, note that you can associate parameters
with the nonterminals (e.g., think "simple_expression(int n)" in
place of "simple_expression()" as above). Such parameters behave
exactly like local variables within the scope of the rule. For this assignment,
the statements that implement the printing actions you need to perform could be
placed in the brackets {}, which up to now have been empty, just after the
header of the rule. Ultimately, your nonterminal will be returning values
(that is, the "void" above will be replaced by something else), but don't
worry about that just yet.
Please submit your file(s), in a .zip, at this location.
Back to CS 230 Home Page