CS 112

Interpreter Assignment

Assigned: Wednesday, October 5

Program Due: Monday, October 24

For project 2, you will write an interpreter for the C-- language. This will build on the Lexer you wrote for Project 1.

An interpreter is a program that reads in statements in a language and executes those statements. It checks to make sure that the statemnts make sense and keeps track of variables.

About C--

C-- is a very small language. It consists only of assignment statements and expressions. Assignment statements take the form:

 (assignment) ::= (identifier) (assignmentOperator) (expression) ;

Where an expression is a sequence of numbers or identifiers added or subtracted together. For example:

We can write this more formally as:

(expression) ::= (numberOrID) { (+|-) (numberOrID) }*

This is an example of a grammar. A grammar is a compact way of specifying all legal constructs in a language. It's also helpful in thinking about how to build a program that can recognize assignment statements.


The bulk of your program will be devoted to parsing an input and determining whether it represents a valid assignment statement. In Project 1, you built a program that can break an input string into a sequence of tokens. Now, you'll build a program to determine whether or not those tokens are in a legal sequence.

We'll use a technique called recursive descent. This takes advantage of the grammatical structure we saw above. Recursive descent defines a method for each syntactic element in your program: identifier, numberOrID, assignment, plus, minus. Generally, these methods are called parseX, where X is the thing you're parsing.

So, to parse an assignment statement, parse an identifier, then an equals, then an IntegerOrId, then zero or more plus/integerOrId pairs. If you see anything else along the way, it's not a valid assignment statement.

To begin, just write a program that can parse a series of statements and indicate whether they are valid or not. Then worry about evaluating the expression properly.


Once you've got the parser working, you'll want to add in interpreting. This will consist of:

Symbol Table

In order to keep track of the variables, you'll need to create a symbol table. A symbol table stores the names of variables together with the addresses they've been assigned in memory. When a new variable is assigned a value, you should add an entry to the end of the symbol table, and assign it the next available address. The ith variable referred to in the source code should be assigned the address i in memory. Note that all C-- programs are restricted to having less than 10 variables.

Main Memory

Main memory should be coded as class with a data member which is an array of ten integers.

Building the Interpreter

Your program should use a "shell" to prompt the user for input. The user should enter a statement, and your program should read that statement into the InputString's buffer using its readInput() method. For each valid assignment statement, your program will:

  1. add an entry in the symbol table, if the variable on the left hand side has not been referred to before.
  2. evaluate the right-hand side of the assignment and store the result in a memory location associated with the left-hand side variable.
  3. If the statement is invalid, your program should print out an appropriate error message.
  4. If the statement is syntactically correct, your program should print out the symbol table and main memory:

Program Design

You will need three new classes, Symbol, SymbolTable, and MainMemory. You will also want to replace Lexer.java with Interpreter.java.

Much of the new work will be in your InputString class, where you will need to add parseX methods to deal with different program elements.


You will only be given credit for portions of the project that are fully completed.

No credit will be given for source code that does not compile/run.

Tasks will be given the following weights:

Please notice that documentation, formatting, and quality will also be graded on assignment 2.

1. Documentation and source format will be 5% of your grade. Does your header documentation include the author's name, the purpose of the program, and a description of how to use the program? Are the identifiers meaningful? Are any obscure constructs clearly explained? Does the method header documentation explain the purpose of the method, its parameters, pre- and post-conditions, and any changes to object member variables? Is the indentation consistent? Have blank lines been used so that the program is easy to read? Did you follow the style guidelines provided?

2. Quality of solution will be 5% of your grade. Are any of your methods more than 15 lines? Are there long or multipurpose methods? Is your solution too clever -- i.e., has the solution been condensed to the point where it's incomprehensible?


You must submit your source code to the cs112 submission directory by the due date. Bring a hard-copy of your program to your interactive grading session, which will occur at the lab following the due date.


It is OK for you to discuss solutions to this program with your classmates. However, no collaboration should ever involve looking at one of your classmate's source programs! It is usually extremely easy to determine that someone has copied a program, even when the individual doing the copying has changed identifiers and comments. If we discover that someone has copied a program, the authors of both programs will receive a 0 on the project and University sanctions.


You will most likely need the following methods:

In inputString:

In SymbolTable: In MainMemory : In Interpreter:

I also strongly recommend that you build a test method for each method that you write, and make sure that each piece of your program works independently before hooking everything together.