CSC 533: Organization of Programming Languages
Fall 2002

HW4: Extending the SILLY Interpreter

For this assignment, you are to extend your SILLY interpreter to handle conditionals and loops. The expanded grammar rules for the SILLY language are as follows:

<program> --> 'begin' { <statement> } 'end' <statement> --> <assignment> | <output> | <conditional> | <loop> <assignment> --> <identifier> '=' <expression> <expression> --> <term> { '+' <term> } <term> --> <integer> | <identifier> <output> ---> 'output' ( <string> | <expression> ) <conditional> --> 'if' <expression> < <expression> { <statement> } 'endif' <loop> --> 'while' <expression> < <expression> { <statement> } 'endwhile' <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, the SILLY language is case sensitive, and variables are not explicitly declared and are assumed to have value 0 if not otherwise assigned. The new conditional statement performs a less-than comparison, executing the statement(s) preceding "endif" if the comparison evaluates to true. Similarly, the new loop statement performs a less-than comparison, repeatedly executing the statement(s) preceding "endwhile" as long as the comparison evaluates to true.

As before, your SILLY interpreter should read the program from standard input (i.e., cin) 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
start x = 10 if x < 20 output "x is" output x endif output "done" end x is 10 done
start L = 1 s = 0 while L < 6 s = s + L L = L + 1 output s endwhile end 1 3 6 10 15

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.