| Code: | pa2.ss |
| Tests: | pa2-tests.ss |
The purpose of this assignment is to learn about interpreting by writing and extending an interpreter for the WAE language in PLAI. It is to be completed individually according to the course collaboration policy.
You can work on this assignment using DrScheme or MzScheme on ACS machines or on your own machine. Wherever you work, you first have to install the PLAI language. Once installed, it will work in both DrScheme and MzScheme, and you will find documentation for the PLAI language by selecting "Help Desk" from the Help menu in DrScheme, and then following the link labeled Programming Languages: Application and Interpretation under the heading "Languages".
First, write a parser and interpreter for the WAE language in PLAI. The book will be of great assistance; it provides the beginnings of a parser, an abstract syntax datatype, and an interpreter. Once you've written and tested the parser and interpreter for WAE, extend the language with these features:
The parse and calc functions in the book
use separate rules for addition and subtraction.
You will replace these separate rules
with a generic implementation for binary arithmetic operators.
binop
datatype variant of WAE containing the procedure for that operator.
binop variant (in the abstract syntax),
use the procedure value you stored while parsing.
Once you have implemented addition and subtraction this way, you can add other binary operators without modifying your functions; you need modify only your data structure. To illustrate this, add associations for the symbols * and / to procedures that implement multiplication and division, and write your own automated tests for this functionality.
with
The WAE language in the book includes a with construct
that binds a single identifier to the value of an expression.
You will modify the with construct
to bind any number of identifiers to values, as with Scheme's let.
with will have a list of bindings
together with a single expression as its body.
with.
with,
but not to other identifiers bound in the same binding list.
Here is a BNF grammar for the resulting WAE language:
<WAE> ::= <num>
| {<binop> <WAE> <WAE>}
| {with {<binding> ...} <WAE>}
| <id>
<binop> ::= + | - | * | /
<binding> ::= {<id> <WAE>}
The ellipsis (...) means
the previous non-terminal is present zero or more times.
Your parser will need to detect illegal syntax.
The tests include an example of each error your parser must detect,
and the message it should pass to Scheme's error function.
As with the previous assignment, you will need to think carefully about where your code might fail and add your own test cases to the test file. Once again your submission will be tested against a more comprehensive suite of tests than we gave you and we may also assess the quality of your tests.