CIS 505, Programming-Language Paradigms, Fall 2010.

Assignment #5 (10 points). Due on Saturday, November 13, 2010, 11pm

Use SML/NJ to write an interpreter for a simple imperative language with procedures that take parameters. Its syntax is given by the grammar
  P : Program
  C : Command
  E : Expression
  D : Declaration
  I : Identifier
  N : Numeral

  P ::=  D ; C

  C ::=  C1 ; C2  |  I = E  |  if E { C } | while E { C }  
      |  call I ( E ) | print ( E )

  E ::=  N  | I | ( E1 + E2 )  | (E1 * E2) | (E1 - E2) 

  D ::=  D1 ; D2  |  int I  |  proc I1 ( I2 ) { C }

  N ::=  strings of digits

  I ::=  strings of letters, not including keywords
Your interpreter should take a program written in that syntax, and return a list of the values printed. To allow you to focus on the semantic aspects, a parser has been provided for you. It converts an input string into operator trees that are represented using SML datatypes.

As an example program, consider the following that computes the factorial of 4:

   int x; proc f(n) {if n {x = (x * n); call f((n - 1))}}; x = 1; print(x); call f(4); print(x)
Given the above program as input, your interpreter should thus return the list [1,24].

As another example, consider the following program that also computes the factorial of 4:

   int x; proc f(n) {print(n); if n {call f((n-1)); print(n); x = (x * n)}}; x = 1; call f(4); print(x)
Here your interpreter should return the list [4,3,2,1,0,1,2,3,4,24].

You need to make several decisions about the semantics of the language. In comments, you should explain and motivate your choices, but you must allow mutual recursion and must not allow an integer variable to be called. If the input program contains one or more errors, your interpreter should still terminate normally but with an informative message as designed by you (use exceptions for that).

You may do this exercise solo or with a partner. If you choose the latter, you and your partner will submit one and the same program.

Submit to K-State Online an .sml-file containing the interpreter. You should also construct some test programs (in addition to the one above), and include a text-file log of those and their outputs.


Torben Amtoft