COSC 152 Programming Languages

Fall 2007

Homework Assignment #5

Conditionals
Due: Friday, Oct 19, 12:00PM CDT
Submit: E-mail electronic copy to professor, with a subject header "COSC 152 HW#5".
Work may be completed in pairs. Each team should submit only once. Be certain to put both team member names on work submitted. It would be courteous to carbon-copy your partner when e-mailing the final submission.

The Grammar

For this assignment, ammend the grammar from HW #4 to include the following productions:
<expr>    ::=    ( if <bool-expr> <expr> <expr> )
   |    ( cond { ( <bool-expr> <expr> ) }* ( else <expr> ) )
<bool-expr>    ::=    #t
   |    #f
   |    <compare-expr>
   |    <logical-expr>
<compare-expr>    ::=    ( equal <expr> <expr> )
   |    ( greater <expr> <expr> )
   |    ( lesser <expr> <expr> )
<logical-expr>    ::=    ( and <bool-expr> <bool-expr> )
   |    ( or <bool-expr> <bool-expr> )
   |    ( xor <bool-expr> <bool-expr> )

Parser

Use the SLLGEN parser generator system to specify your lexical and syntax rules, and automatically build your parse function.

Modify your existing unparse function to work with the new abstract syntax and parse function.

Your scanner specification must now accept Boolean literals "#t" and "#f"; note that these literals must scan into distinct tokens, or you will not be able to tell them apart later. (This is contrary to the impromptu scanner definition I scrawled on the board toward the end of Monday's lecture, which recognized either Boolean literal as the same token.

Interpreter

Modify your evaluate function to operate over the new syntax.

The cond construct should evaluate to only the first expression that corresponds to a true condition. If none of the clauses has a true condition, the else clause is always taken to be true.

Notes:

  • Although our input grammar includes lambda terms and applications, for now your evaluate function may throw an error if you encounter a lambda term or an application of anything other than a primitive operator. Note that this implies there will be no variable definitions.
  • Check rigorously for errors in the input, or for invalid expressions.
  • Appropriate errors should be thrown if your interpreter encounters any trouble. See the eopl:error construct used in the text. Think carefully about what kinds of errors the interpreter can encounter.

  • Back
    [Revised 2007 Oct 10 11:50 DWB]