Studio 2: Recursive Descent Parsing

Studio Sessions Overview:

The results of your studio session are to be reported and documented in a file that you save in your workspace. You are to print and turn in one copy of that report for your group. In the descriptions of the studio exercises, verbs like report and document are indications of activities you should summarize and discuss in your report.

In your groups, take turns documenting results, looking over shoulders, and staffing the keyboard.

It is unacceptable to copy anything without understanding it. At any point, the TA or instructor can point to something you've done and ask you why it works, or change it and ask what would happen with the modification.


In each of the following sections, go as far as you can in the time allotted, but move on to the next section so you'll have a chance to work on each section while in studio.

Warm Up: (10 minutes)

  1. Suggestion: one person in the group could be working ahead on the analysis part of a grammar while somebody else is typing in the recursive-descent methods. Switch up the roles throughout the studio session.
  2. Have handy your book or my pages on computing First and Follow sets.
  3. Open eclipse and import (File..Import) this zip file using the Existing projects into workspace method (Do not choose the archive file method). After clicking on "Existing projects into workspace" select as the archive file the file you downloaded earlier.
    If this is your first time running eclipse, be sure your workspace is situated in your CEC filesystem space so that it survives this session.
  4. Open the following files
  5. You will probably not want to look at these files, but they are used to generate the scanner for this studio.
  6. Fill in the group's members' names in the Main.java file.
  7. You need to add the CUP runtime jar file to the build path for this project in eclipse:
    1. Select the project
    2. Choose File...Properties
    3. Chose the Java Build Path
    4. Add an external jar file
    5. The jar you want to add is situated in the Tools subdirectory of this project, and is called java-cup-11a.jar.
  8. Right-click on the build.xml file and run it as an Ant Build...

  9. The build should work and you should see no compilation errors. If something looks strange, ask for help.

    Hereafter, you can run this build by clicking on the arrow with the red toolbox.

  10. To run the program, right-click the Main class (found in the default package) and drag down to Run as....Java Application. No arguments need to be supplied.

    Hereafter, you can run the appolcation by clicking on the big arrow (without the red toolbox).
    You could also run it using the run target of the build.xml file. That target will make sure your project is built before it runs. If you want to do it this way, keep using the arrow with the red toolbox, and it will both build and run your program.
  11. This should produce some output in the console window.
  12. Look at the output, and look at the Example class given in Main.java

  13. If you have time, glance at the build.xml file. Discuss how it controls the build process you witnessed.

Main Problems

You will consider a success of grammars and use them to create recursive-descent parsers. All of your work should be done in Main.java, but you are welcome to create more test files if you wish.

For each rule of each grammar, you are asked which symbols predict that particular rule. Recall that the predict sets are computed primarily from First sets, but if a rule's right-hand side can derive the empty string, then the Follow set of the rule's left-hand side must also be included.


All of your work is given and can be done in the Main.java file. The information here is supplemental.

Before implementing any of these parsers, check your Predict sets with a neighboring group or the professor or TA.


Part 1 (20 minutes)

  1. MatchingParens A grammar that generates matching parentheses with an x in the middle. You should be able to write a recursive-descent parser for this grammar without changing the grammar at all.

  2. Problem1a Problem 1a from page 137 of the Crafting a Compiler text.
  3. Problem1d Problem 1d from page 137 of the Crafting a Compiler text.

Part 2 (20 minutes)

Do not comment out your work so far. Instead, keep it going forward so that at the end, everything you've done is still in the run.
  1. Declarations This is a grammar that generates declaration statements for a list of variables. Take a look at the test files to get an idea of the language. To find out the names of the terminal symbols, look in the sym.java file. For example, the keyword "int" is represented by sym.INT.

    This grammar has left recursiion, which you must eliminate using the principled techniques for doing so.

  2. IfThenElseFi This grammar generates if-then-else-fi statements. The "fi" terminal always ends and brackets the "if". The grammar has problems you must address before you can write a recursive-descent parser.

Part 3 (20 minutes)

Part 4 (time permitting)

Finally

Be sure to submit before leaving studio:

Last modified 13:54:24 CST 12 February 2008 by Ron K. Cytron