COSC 3410 Programming Languages

Homework Assignment #7

μ;

Due: April 8th, 2020 @ 11:59PM CDT

Submit: Turn in a Scheme source file called "hw7.scm" using the turnin command on the Systems Lab machines.

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)

The Grammar


Click here for our in-class demo of these productions

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.

Interpreter

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
Usage: (run "pgm") = v, the Scheme value that is the result of running the program "pgm" in your interpreter.

Examples:
> (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