CSC 533: Organization of Programming Languages
Spring 2005

HW3: Implementing a Simple Interpreter

For this assignment, you are to complete an interpreter for a simple programming language named SILLY (Simple, Interpreted, Limited Language for You). For now, the SILLY language only contains three types of statements: assignments, output statements, and if statements. The grammar rules for the SILLY language are as follows:

<program> --> { <statement> } <statement> --> <assignment> | <output> | <if> <assignment> --> <identifier> '=' <expression> <expression> --> <term> { ('+' | '-') <term> } <term> --> <integer> | <identifier> <output> --> 'output' ( <string> | <expression> ) <if> --> <if> <expression> { <statement> } ( 'else' { <statement> } ) 'end' <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'

The SILLY language is case sensitive, so variables a and A are considered unique. Variables are not explicitly declared, and are assumed to have initial values of 0 if not otherwise assigned. An assignment statement assigns an integer value to a variable. An output statement displays a single value (string or integer) on a line by itself. An if statement is controlled by an expression, where any nonzero value is considered to be true. Within the program file, whitespace (any sequence of spaces, tabs, and returns) separates the individual tokens (language elements) in a program.

For example:

x = 4 output "the numbers are" output x p2 = x + 2 output p2 - x + 5 output "done" the numbers are 4 7 done
x0 = y + 1 output "x0 and y are" output x0 output y if x0 output "yes" else output "no" end x4 = x0 + output "done" x0 and y are 1 0 yes SYNTAX ERROR: malformed expression

An incomplete version of the SILLY interpreter is provided for you via the following classes/files:

The current interpreter implementation is incomplete. You will need to modify code to add the following functionality.

  1. Allow for a string to be printed by an output statement, as well as numerical expressions.
  2. Allow for expressions to involve addition and/or subtraction operators.
  3. Allow for an else case in if statements.

Note that in making these additions, the main progam file (interp.cpp) need not be modified at all. Instead, you will primarily be making changes to the various Statement classes, with some minor modifications to Token and Tokenizer. As you add new features, be sure that syntax errors result in an informative message being displayed.

Once you have the interpreter working correctly on programs involving all three types of statements, add an additional statement type: the while loop. The grammar rule for the new while loop is as follows:

<while> --> <while> <expression> { <statement> } 'end'

A while loop performs conditional repetitions: as long as the loop test evaluates to true (i.e., the expression is nonzero), the statements inside the loop will be executed in sequence.