Work is to be completed in teams of two. Your source file should be in the EoPL dialect ("#lang eopl" at top of file) supported by DrRacket, and should not make use of non-standard features like comment boxes that produce an XML file rather than a flat text .scm file.
At the bottom of your hw7.scm, please include the line:
(provide scan&parse run)
Extend your grammar from the previous assignment to include the following productions:
<program> | ::= | <statement> |
a-program (stmt) | ||
<statement> | ::= | <identifier> = < expression> |
assign-stmt (var rhs) | ||
| | print <expression> | |
print-stmt (exp) | ||
| | {{<statement>}*(;) } | |
compound-stmt (stmts) | ||
| | if <expression> then < statement> else <statement> | |
if-stmt (test-exp then-stmt else-stmt) | ||
| | while <expression> do <statement> | |
while-stmt (test-exp body-stmt) | ||
| | var {<identifier> = <expression> }*(,) ; <statement> | |
block-stmt (vars init-exps body-stmt) |
The top level production now states that a program consists of a statement. Statements may include expressions, which are defined by the grammar from the previous assignment. All of the primitive operators defined in the previous assignment should also be in force.
This language is a close relative to the IMPLICIT-REFS language of EoPL Chapter 4, with more familiar Scheme syntax, and a variation on the statement language extensions of exercise 4.22 and 4.25.
Augment your μREC interpreter code from the previous assignment with code from EXPLICIT-REFS interpreter in EoPL section 4.2, (particularly pages 110-113,) and the IMPLICIT-REFS interpreter in EoPL section 4.3 (particularly pages 118-119,) to interpret the μ; language.
Your interpreter should still be executed with the run function:
run : String → SchemeVal > (run "var x = 5; print x")
5
run-complete
> (run "var x = 3; var y = 4;
print(add x y)")
7
run-complete
> (run "var x = 3; var y = 4; var z = 0;
{while (greater x 0) do
{z = (add z y);
x = (sub x 1)};
print z}")
12
run-complete
> (run "var x = 3;
{print x;
var x = 4;
{print x};
print x}")
3
4
3
run-complete
> (run "var f = (lambda (x y)
(mul x y));
var x = 3;
print(f 4 x)")
12
run-complete