CS230

Compiler Design


Project Assignment #2

Syntax Analysis

 Write a recursive descent parser using the grammar and your lexical analyzer of assignment 1.

 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.

 Do not alter the grammar in your implementation.

  Using javacc, this 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? For one, you may need to modify the lexical part of the grammar to account for such specific tokens as begin and end. 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.


Back to CS 230 Home Page