Due: Friday, 11 January 2008.
Write the two methods interp and maxargs described on page 12 of the textbook. Use the following class as a template. Please, use customary Java style conventions. Package names should be lower case, class names should be upper case. (File names must be the same as class names.)
import java.io.IOException;
class Interp {
public static void interp(Stm s) { /* you write this part */ }
public static int maxargs(Stm s) { /* you write this part */ }
public static void main (Stm s) throws IOException {
System.out.println (maxargs(s));
interp(s);
}
public static void main(String args[]) throws IOException {
main (Example.a_program);
main (Example.another_program);
}
}
Also, add another interesting example program to the class Example below. Include comments giving the concrete syntax of the program (Stm) and the output of the interpretor on the program, just as in the example a_program.
class Example {
// Stm :: a:=5+3; b := (print (a, a-1), 10*a); print(b)
// Out ::
// 8 7
// 80
static Stm a_program =
new CompoundStm(new AssignStm("a",new OpExp(new NumExp(5), OpExp.Plus, new NumExp(3))),
new CompoundStm(new AssignStm("b",
new EseqExp(new PrintStm(new PairExpList(new IdExp("a"),
new LastExpList(new OpExp(new IdExp("a"), OpExp.Minus, new NumExp(1))))),
new OpExp(new NumExp(10), OpExp.Times, new IdExp("a")))),
new PrintStm(new LastExpList(new IdExp("b")))));
// Stm :: /* fill in your program here */
// Out :: /* tell me what the output should be */
static Stm another_program = /* you come up with another program */
}
The data structures for the programming language are given to you slp.java. Do not change this file; do not turn this file in. Compiling this file gives 10 warnings and programming this way is not advised. Note the most egregious error is that all instance variables really should be final. In this regard, consider Appel's statement on page 11:
Data structures are initialized when they are created (by the constructor functions), and are never modified after that (until they are eventually discarded).
You are encouraged to use java.util.HashMap instead of building a class Table as suggested in the textbook. Although the discussion of Table and Exericse 1.1 are quite interesting.
The program will be compiled and run as follows:
javac *.javaand run by
java Interp
|
Course=cse4251 Project=asgn1 |