CSC 533: Organization of Programming Languages
Fall 2003

HW4': Extending the SILLY Interpreter

For this assignment, you are to extend your SILLY interpreter to handle if-else statements, counter-driven loops, and local variables. The expanded grammar rules for the SILLY language are as follows:

<program> --> 'begin' { <statement> } 'end' <statement> --> <assignment> | <output> | <conditional> | <while> | <repeat> | <local> <assignment> --> <identifier> '=' <expression> <expression> --> <term> { ('+' | '-') <term> } <term> --> <integer> | <identifier> <output> ---> 'output' ( <string> | <expression> ) <if> --> 'if' <expression> <compare> <expression> { <statement> } [ 'else' { <statement> } ] 'end' <while> --> 'while' <expression> <compare> <expression> { <statement> } 'end' <repeat> --> 'repeat' <expression> { <statement> } 'end' <local> --> 'local' <identifier> { ',' <identifier> } <compare> --> '<' | '<=' | '>' | '>=' | '==' | '!=' <identifier> --> <letter> [ <digit> ] <string> --> '"' { <letter> | <digit> | ' ' } '"' <integer> --> <digit> { <digit> } <letter> --> 'a' | 'b' | 'c' | ... | 'z' | 'A' | 'B' | 'C' | ... | 'Z' <digit> --> '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'

As stated in HW3, variables are case sensitive, need not be declared, and are assumed to have value 0 if not otherwise assigned. Your extension to the interpreter should allow for an if statement to have an optional else case. In addition, a new repeat statement is added which performs counter-driver repetition. The keyword "repeat" is followed by a number or expression which specifies the number of repetitions. The sequence of statements that precede "end" are to be executed that number of times. Finally, the ability to declare local variables is added using a declaration statement. When a declaration statement appears within a conditional or loop, the variables listed after "local" are considered local to that conditional or loop. That is, bindings to those variables exist only inside that particular control statement. Static scoping rules apply, so any reference to a non-local variable will next try the enclosing statement.

As before, your SILLY interpreter should read the program from a file (as specified by the user) and display the output that would be produced by the SILLY program. If there are any syntax errors in the program, the interpreter should display "SYNTAX ERROR" and halt. For example:

SAMPLE PROGRAMOUTPUT
begin x = 10 if x < 10 output "foo" else local x, y y = 33 output x output y end output x end 0 33 10
begin r1 = 2 repeat r1 + r1 output "hello" end end hello hello hello hello

Note: since conditionals and loops are both statements, it is perfectly legal to nest these control statements. For example, a program might contain a conditional inside a loop, or a loop inside a conditional, or even a loop inside a conditional inside a loop! Be sure to consider the possibility of arbitrarily nested statements as you design your solution to this problem.

Your solution should be object-oriented, utilizing inheritance to define each type of statement. As a starting point, you may use the following code, which extends the interpreter from HW3 to also include conditionals and while loops.