Homework 2



This homework is due on Thursday October 24st.

It deals with Lexical and Syntactic Analysis of JAY (see Appendix B of the textbook). You will implement the Recursive Descent Parser algorithm for JAY.

This homework is to be done alone or in a group of two students. If you decide to do it by 2 you have to follow the rules of 'Extreme Mathematical Thinking' and 'Pair Programming' (to be discussed in class).

Students have to hand-in a hard-copy with their succinct explanations and the listing of all their JAVA code. The JAVA code must be accessible from matrix in the directory called CS361/HW2/. The JAVA code must be clear and commented (The given code is not commented but should have been!!! Add comments). All the explanations to access the code on matrix must be provided in your hard-copy. Your JAVA code will be tested with different inputs.

All the names (names of directory, classes, methods, ...) must be respected.


1. Give 3 examples of JAY programs. Give 3 examples of JAY programs with syntax errors.


2. Do exercise 2.3 on page 48 of your text. The skeleton of the TokenStream.java file is given.


3. Write a class called TokenStreamDemo.java to test your TokenStream.java file. TokenStreamDemo must print, for a given program, the list of all its token types and values.


4. Develop a complete recursive descent parser for Jay, using the guidelines introduced in section 2.3, the TokenStream class you completed in 1., and the following additional on-line resources:

a. the skeleton recursive descent parser ConcreteSyntax.java,

b. the complete Jay abstract syntax Program.java. Program.java is equivalent to AbstractSyntax.java but it contains added display methods to facilitate debugging and experimentation. (Program.java presents the abstract syntax of JAY as a collection of the Java classes as in Appendix B).

c. the driver program parser.java, and

d. the Jay program program.jay from Figure 2.19 as test one input.

Copy all these files to your CS361/HW2/ directory, complete the coding of ConcreteSyntax.java, and then run the driver program with the command:

% java parser program.jay

That is, the file name "program.jay" is provided as a run-time parameter. To see if the program is correct, compare its output with the file program.jay.output, which shows the complete abstract syntax tree for this Jay program. Test your program with other inputs.


5. Extra-exercise: Provide a graphical interface to your program to provide a graphical view of the Abstract syntax tree.