CSC 533: Organization of Programming Languages
Spring 2008

HW3: Implementing a Simple Interpreter

For this assignment, you are to complete an interpreter for a simple, procedural programming language named SILLY (Simple, Interpreted, Limited Language for You). The grammar rules for the SILLY language are as follows:

<statement> --> <assignment> | <output> | <if> | <while> | <for> | <quit> <assignment> --> <identifier> '=' <expression> NEWLINE <expression> --> <term> { ('+' | '-' | '*' | '/' | '%') <term> } <term> --> <integer> | <identifier> <output> --> 'output' <expression> NEWLINE <if> --> 'if' <expression> NEWLINE { <statement> } [ 'else' NEWLINE { <statement> } ] 'end' NEWLINE <while> --> 'while' <expression> NEWLINE { <statement> } 'end' NEWLINE <for> --> 'for' <assignment> NEWLINE { <statement> } 'end' NEWLINE <quit> --> 'quit' NEWLINE <identifier> --> <letter> { <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'

Note that carriage returns are required at the end of each statement, as denoted by NEWLINE. For simplicity's sake, you may assume that each token in the language is separated by spaces and/or tabs.

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 previously assigned. An assignment statement assigns an integer value to a variable. An output statement displays a single value on a line by itself. If statements and while loops are controlled by an expression, where any nonzero value is considered to be true. A for loop iterates a set number of times, with the loop control variable counting down from its initial value until it reaches 0. Finally, a quit statement terminates the interpreter.

For example:

SAMPLE CODE (output in red)
  >>> x = 4
  >>> output x
  4
  >>> y = x + 1
  >>> x = 1 + 5 - 2
  >>> output y + x
  9
  >>> y = z + z + 1
  >>> output z
  0
  >>> output y
  1
  >>> val1 = 1023
  >>> val2 = 1024
  >>> output val1 % 2
  1  
  >>> output val2 / 10 % 2
  0  
  >>> output 65 - 1 / 8 / 2
  4  
  >>> quit
  BYE
  >>> num1 = 4
  >>> if num1
        output num1
      else
        output 0
      end
  4
  >>> x = 10
  >>> y = 1
  >>> while x - y
        x = x - 1
        y = y + 2
      end
  >>> output x
  7
  >>> for countdown = 10
          if countdown % 2
              output countdown
          end
      end
  9
  7
  5
  3
  1
  >>> quit
  BYE

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

To produce a fully functional interpreter for the SILLY language, you will need to augment/modify the provided code so that it:

  1. Allows for expressions to involve multiplication ('*'), integer division ('/'), and remainder ('%'). All of these operators have the same precedence and left associativity, so expressions can be evaluated in a simple left-to-right order.
  2. Allows for an else case in if statements. Note that there is only one 'end' keyword for an if statement - if there is an else case, the keyword 'else' separates the statements to be executed if the test expression evaluates to true (i.e., non-zero) or false (i.e., zero).
  3. Implements while loops. The class that defines a while loop should be similar to the if statement class due to their structural similarity. However, when executed a while loop should repeatedly execute the enclosed statements as long as the test expression evaluates to true (i.e., non-zero).
  4. Implements for loops. Note that a for loop assigns an initial value to a loop control variable, which is automatically decremented at the end of each loop pass. The for loop terminates when the loop control variable becomes zero.

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