Due: Thursday Jan 17, 11:59pm via turnin (assignment
name hwk1)
Collaboration Policy:
Pairs Permitted
Write a parser and interpreter for the WAE language (discussed in class), extended with the following two language features. The textbook can be of great assistance in this assignment; it provides the beginnings of a parser, an abstract syntax datatype and an interpreter.
In place of having separate rules for + and
-, define a single syntactic rule for all binary
arithmetic operators. Parse these into a binop datatype
variant. Define a table that maps operator names (symbols) to actual
functions (Scheme procedures) that perform the corresponding
operation. Having a single rule like this, accompanied by a table,
makes your language easier to extend: once you have modified your
parser and interpreter once to support binary operators, you won't
need to touch either one to add any number of new ones. To
demonstrate this, define multiplication and division (using
* and / to represent them in the language's
concrete syntax).
with
Each identifier bound by the with expression is
bound only in its body. There will be zero or more identifiers bound
by each with expression. If there are multiple bindings
of the same identifier in a single with
expression’s bindings list, your parser should halt with an
error message. An example:
{with {{x 2}
{y 3}}
{with {{z {+ x y}}}
{+ x z}}
will evaluate to 7, while
{with {{x 2}
{x 3}}
{+ x 2}}
will halt with an error message (have it contain the substring "multiple").
Your solution should contain define at least the following two
functions: (1) parse, which consumes an expression in the
language's concrete syntax and returns the abstract syntax
representation of that expression, and (2) interp, which
consumes an abstract syntax expression (as returned by the
parse function) and returns a number. Include a
contract for every function that you write and test cases that
amply exercise all of your code. Use PLAI-Pretty Big as the language
level for this assignment.
The following grammar captures the concrete syntax of the WAE language with the additional features:
<WAE> ::= <num>
| {+ <WAE> <WAE>}
| {- <WAE> <WAE>}
| {* <WAE> <WAE>}
| {/ <WAE> <WAE>}
| <id>
| {with {{<id> <WAE>} ...} <WAE>}
In this grammar, the ellipsis (...) means that the
previous non-terminal is present zero or more times.
The following define-types capture the abstract syntax (use these to get same the variant names as our reference implementation):
(define-type Binding [binding (name symbol?) (named-expr WAE?)]) (define-type WAE [num (n number?)] [binop (op procedure?) (lhs WAE?) (rhs WAE?)] [with (lob (listof Binding?)) (body WAE?)] [id (name symbol?)])
For test-jousting, submit tests for the parse and interp functions only, not any internal functions that you wrote.
Turn in two files: one containing your parser, interpreter and
tests of helper functions, and another containing your
parse and interp tests. We will run the
second file against everyone else's first file during grading.
Code that does not run against our test suite will not be
graded!! This should be a good incentive to run your code
through the jousting server at least once before submitting.
You must submit your final files via turnin. We will not take your tests or code from the jousting server.
What should we use as the error message for multiple
bindings to a variable?
Anything that contains the substring
"multiple" (case sensitive). If you want your message to also report
the name of the multiply-defined variable (not required), use the
format operation. For example, (format "hi ~a" 'mom)
yields the string "hi mom".
What should we use as the error message for unknown
operators?
Anything that contains the substring "unknown"
(case sensitive).
What should we use as the error message for unbound
identifiers?
Anything that contains the substring "unbound"
(case sensitive).
What does the requirement that we shouldn't need to change the
parser or interpreter to add new binops mean?
This means that
you shouldn't have to edit the parse or interp functions to add new
binops. You may need to make changes to a helper, but those changes
should be localized to the helper.
Is there any way to trace the inputs and outputs of calls to a
function?
Yes. Let's say you wanted to see a trace of all
calls to subst (including return values) to aid in debugging. To do
this, you need to load the tracer and tell Scheme which functions to
trace. The following two lines do this:
(require (lib "trace.ss")) (trace subst)You can put these in either DrScheme window. Next time you run an expression that invokes subst, each call to subst will print tracing info in the interactions window.