COSC 152 Programming Languages

Fall 2007

Homework Assignment #8

Assignment and Statements
Due: Friday, Nov 16, 12:00PM CST
Submit: E-mail electronic copy to professor, with a subject header "COSC 152 HW#8".
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, extend the grammar from HW #7 with the grammar from Figure 3.23 on page 121 in EoPL, plus productions for Exercise 3.64 and 3.65 on page 122. The total grammar, in summary, should be:
<program>    ::=    <statement>
<statement>    ::=    <identifier> = < expression>
   |    print <expression>
   |    {{<statement>}*(;) }
   |    if <expression> < statement> <statement>
   |    while <expression> do <statement>
   |    var {<identifier> = <expression> }*(,) ; <statement>
   |    proc <identifier> ( { <identifier> }*(,) ) <statement>
   |    call <identifier> ( { <expression> }*(,) )
<expr>    ::=    <number>
   |    <symbol>
   |    <boolean>
   |    ( lambda ( {<symbol>}* ) <expr> )
   |    ( <expression> { <expression>}* )
   |    ( if <expr> <expr> <expr> )
   |    ( cond { ( <expr> <expr> ) }* ( else <expr> ) )
   |    ( let ( { ( <id> <expr> ) }* ) <expr> )
   |    ( let* ( { ( <id> <expr> ) }* ) <expr> )
   |    ( letrec ( { ( <id> ( lambda ( { <id> }* ) <expr> ) }* ) <expr> )
   |    ( set! <identifier> <expression> )

With primitive operators add, sub, mul, div, mod, cons, car, cdr, list, empty-list, empty?, equal, lesser, greater, and, or, xor.

Parser

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

You may modify your existing unparse function to work with the new abstract syntax and parse function, but the introduction of syntax outside of the normal Scheme format will make this more difficult. I encourage you to write an unparse function for your own debugging purposes, but this new function will probably need to rely more on the Scheme display primitive wherever it is impractical to return a list structure for concrete syntax.

Interpreter

Add an execute function for executing programs in the new language. Your existing evaluate function will still be useful for all of the expressions in the grammar.

Notes:

  • All of your existing testcases from previous phases of the interpreter can be easily retrofitted for the new interpreter by turning them into print statements. Thus, what was "(add 5 7)" can now be "print (add 5 7)".
  • The proc and call productions in the grammar are for defining and calling procedures, respectively. A procedure is much like a lambda function, except that its body is a statement rather than an expression. Calling a procedure does not return a value, and procedure calls should not appear on the right hand side of assignment statements. However, it is still possible to assign lambda functions to variables, and to assign the results of evaluating a lambda function to a variable.
  • On page 120 of EoPL, four testcases are given for the statement language. In our variant of the language, these testcases would look like this. The behavior of these cases is as described in the text.

  • Back
    [Revised 2007 Nov 12 19:50 DWB]