CSC 533: Organization of Programming Languages
Spring 2005

HW4: Extending the SILLY Interpreter

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

<program> --> { <subroutine> } { <statement> } <subroutine> --> 'subroutine' <identifier> '(' { <identifier> } ')' { <local> } { <statement> } 'end' <local> --> 'local' <identifier> <statement> --> <assignment> | <input> | <output> | <if> | <while> | <repeat> | <call> <assignment> --> <identifier> '=' <expression> <expression> --> <term> { ('+' | '-') <term> } <term> --> <integer> | <identifier> <input> --> 'input' <identifier> <output> --> 'output' ( <string> | <expression> ) <if> --> 'if' <expression> { <statement> } ( 'else' { <statement> } ) 'end' <while> --> 'while' <expression> { <statement> } 'end' <repeat> --> 'repeat' <expression> { <statement> } 'end' <call> --> 'call' <identifier> '(' { <expression> } ')' <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. You are to make the following additions:

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 errors in the program, e.g., calling an undefined subroutine or specifiying the wrong number of parameters in the call, the interpreter should display "SYNTAX ERROR" and halt. For example:

SAMPLE PROGRAMOUTPUT
x = 3 repeat x + 1 output "foo" end input y while y output y y = y - 1 end foo foo foo foo 3 2 1
SAMPLE PROGRAMOUTPUT
subroutine s1 ( ) output "inside s1" output a end subroutine s2 ( x ) local a local b a = x + 1 b = a + 1 output "inside s2" output a output b call s1 ( ) end a = 5 call s2 ( a ) output "done" inside s2 6 7 inside s1 5 done

You are strongly encouraged to add language features incrementally. Credit will be awarded based on the following guidelines:

25%    repeat statement
25% input statement
20% subroutines & calls with no local variables or parameters
15% subroutines & calls with local variables
15% subroutines & calls with parameters