CS 374 Compilers
Homework #1


Due: The beginning of class on Tuesday 1/31 (test cases on Thursday 1/26)

Note: This work is to be done in groups of 2.  Each group will submit one assignment.  Although you may divide the work, both team members should be able to present/describe their partner's work upon request. 

0. HW1 Preparation: Download the given starter code and read Chapter 1 thoroughly.  The test statements of SLPTest are difficult to read in their abstract syntax construction form, so consider initially compiling and running SLPTest to see (through use of recursive toString methods) these statements in their original source code form.

1. Maximum Print Arguments: Read Programming Problem 1 on p.12.  This method in SLPTest initiates a recursion over an abstract syntax tree to evaluate maxargs.  However, most maxargs methods have been trivially (and incorrectly) implemented as "return 0;".  Your challenge is to implement the maxargs methods of the abstract syntax classes correctly.  Note: If there are no print statements, the result should be 0. Also note that print statements can occur within print statement arguments through expression sequences (EseqExp). 

2. Interpreting Straight-Line Programs: Read Programming Problem 2 on pp. 12-14.  This method in SLPTest initiates a recursion over an abstract syntax tree to evaluate interp, that is, to execute the straight-line program.  However, most interp methods have been trivially (and incorrectly) implemented as "return null;".  Your challenge is to implement the interp methods of the abstract syntax classes correctly.

3. Extended Testing (due Thursday 1/26): Add at least five useful test statements to the statements array of SLPTest.  The correct output for the test statements supplied are as follows:

$ java SLPTest
Program:
a := 5 + 3; b := (print(a, a - 1), 10 * a); print(b)
Max Arguments: 2
Execution Output:
8 7
80
Program:
print(2, 3, 4)
Max Arguments: 3
Execution Output:
2 3 4
Program:
print((print(2, 3, 4), 1))
Max Arguments: 3
Execution Output:
2 3 4
1
Program:
print((print(1), 2), 3, 4)
Max Arguments: 3
Execution Output:
1
2 3 4
Program:
a := 1; print(a); a := 2; print(a)
Max Arguments: 1
Execution Output:
1
2
Program:
a := 1; print(a); print((a := 2, a)); print(a)
Max Arguments: 1
Execution Output:
1
2
2
Program:
print((a := 1, a), (a := a + 1, a), (a := a + 1, a))
Max Arguments: 3
Execution Output:
1 2 3

Supplemental Chapter Comments

(These comments are to supplement your reading.  Question(s) asked are for you to think about on your own and need not be turned in with the homework.)

Early on, skim Appendix A in anticipation of our implementation of a MiniJava compiler.  In what ways do you observe that MiniJava has been simplified from Java?  What is most surprising?  Are there alternative ways that MiniJava can accomplish the same computation as some familiar forms that are missing?

1.1: Don't be concerned if the jargon in Figure 1.1 doesn't mean much to you at this stage.  Think of it as being an approximate syllabus for the course.  You'll understand Figure 1.1 very well by the end of the course. The same goes for Table 1.2, but do read it as best you can at this stage.  Come back to this section regularly for a bird's eye view of what we're accomplishing.

1.2: There is considerable overlap in the topics of Chapters 2 and 3 and our theoretical computer science coverage of CS 201/301.  We'll avoid repetition of such material.  If you'll be taking the Computer Science GRE subject test before completing CS301, be sure to dig into the material we'll skim now.  I'll be glad to help you one-on-one.
We'll be using the tool JavaCC for this course.

1.3: We'll be covering the semantics (meaning) of straight-line programs in class.  Read the style and modularity conventions thoroughly.  The classes of Program 1.5 (along with toString methods) are supplied here.