Previous    Next

EXERCISES

4.1  

Write a package of Java classes to express the abstract syntax of regular expressions.

 

4.2  

Extend Grammar 3.15 such that a program is a sequence of either assignment statements or print statements. Each assignment statement assigns an expression to an implicitly-declared variable; each print statement prints the value of an expression. Extend the interpreter in Program 4.1 to handle the new language.

 

4.3  

Write a JavaCC version of the grammar from Exercise 4.2. Insert Java code for interpreting programs, in the style of Program 4.2.

 

4.4  

Modify the JavaCC grammar from Exercise 4.3 to contain Java code for building syntax trees, in the style of Program 4.4. Write two interpreters for the language: one in object-oriented style and one that uses visitors.

 

4.5  

In $MINIJAVA/chap4/handcrafted/visitor, there is a file with a visitor PrettyPrintVisitor.java for pretty printing syntax trees. Improve the pretty printing of nested if and while statements.

 

4.6  

The visitor pattern in Program 4.7 has accept methods that return int. Ifone wanted to write some visitors that return integers, others that return class A, and yet others that return class B, one could modify all the classes in Program 4.7 to add two more accept methods, but this would not be very modular. Another way is to make the visitor return Object and cast each result, but this loses the benefit of compile-time type-checking. But there is a third way.

Modify Program 4.7 so that all the accept methods return void, and write two extensions of the Visitor class: one that computes an int for each Exp, and the other that computes a float for each Exp. Since the accept method will return void, the visitor object must have an instance variable into which each accept method can place its result. Explain why, if one then wanted to write a visitor that computed an object of class C for each Exp, no more modification of the Exp subclasses would be necessary.


JaVaScreenshot Previous    Next
Comments