CSC 533: Organization of Programming Languages
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
SILLY language are as follows:
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:
Add a new repeat statement that performs
counter-driven repetition. The keyword "repeat" is followed by a number
or expression that specifies the number of repetitions. The sequence of
statements that precede "end" are to be executed that number of times.
If the number specified is zero or negative, the statements will not be executed at all.
Add a new input statement that reads a number from standard input. The keyword
"input" is followed by a variable name, and the number entered by the user
(from standard input) is to be stored in that variable.
Add simple subroutine declarations that can appear at the beginning of the program. A
subroutine begins with the keyword "subroutine" followed by its name and zero or more parameters
enclosed in parentheses. Note: you can assume that spaces will surround the parentheses to make
parsing easier. Next, there can be a sequence of local variable declarations, each of which
consists of the keyword "local" followed by a variable name. Local variables exist only inside
subroutine, and can override global variables with the same names. Finally, the sequence of
statements that are to be executed as a result of calling the subroutine follow, terminated by
keyword "end". Static scoping is assumed, so any variables referenced within these statements
are not parameters or declared local are assumed to be global variables.
Finally, add a new subroutine call statement. A call begins with the keyword "call"
by the name of the subroutine and a series of expressions contained in parentheses. Again, you
assume that spaces will surround the parentheses to make parsing easier. By-value parameter
passing is assumed, so the values of the expressions are passed to the subroutine and copied
the parameters (which behave the same as local variables).
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.
x = 3
repeat x + 1
y = y - 1
subroutine s1 ( )
output "inside s1"
subroutine s2 ( x )
a = x + 1
b = a + 1
output "inside s2"
call s1 ( )
a = 5
call s2 ( a )
You are strongly encouraged to add language features incrementally. Credit will be awarded
based on the following guidelines:
subroutines & calls with no local variables or parameters