CSC 533: Organization of Programming Languages
Spring 2007
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:
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
the
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
the
keyword "end". Static scoping is assumed, so any variables referenced within these statements
that
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"
followed
by the name of the subroutine and a series of expressions contained in parentheses. Again, you
may
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
into
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.
For example:
SAMPLE PROGRAM
OUTPUT
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 PROGRAM
OUTPUT
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