CS 4536/CS 536 Homework 1: Basic Interpreters

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.

  1. Binary arithmetic operators

    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 (e.g., function or data structure) 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).

  2. Multi-armed 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.

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?)])

What to Turn In

Turn in two files: one (interp.ss) containing your parser, interpreter and tests of helper functions, and another (tests.ss) containing your parse and interp tests. We will run the second file against everyone else's first file during grading.


FAQ