CS170

Project Assignment 2

DUE : Thursday 10/29.

The goal of this assignment is to begin the semantic analysis function of your interpreter. The basic functions quote, car, cdr, cons, and symbol? will be implemented.

This project assignment is in two phases. Do not go on to phase II until you have completed phase I.

Phase I: Internal Representation of Lists

Modify your S_Expression() function so that it returns the underlying "cons-cell" structure of the list that it parses. For example, if your interpreter reads the list (a b c), then S_Expression() should return the structure:

                                          (links image)

To complement this, write a function printList() that, given a pointer to such a cons-cell structure, will print out the list in parenthesized form. Thus, given a pointer to the above structure, printList() will print out:

                                         (a b c)

Note that what you have now is a way to read the list in and represent it as the cons-cell structure, and a way to write that cons-cell structure back out so that the list looked like what you put in. This may seem silly, but trust me... the alternative is an extremely unwieldy interpreter. At this point, your interpreter should just take an s-expression and echo it back out, as in the sample run given right here. Of course, there is a trivial way to get the same effect (simply echo the string), but it's what happens "behind the scenes" that counts in this phase.

A few tips on the cons-cell structure:


Phase II: Interpreting Simple Functions

Add an eval() function that can evaluate a list, given its internal representation. For now, just implement the basic functions quote, car, cdr, cons and symbol?. All these functions are one to three lines long with the exception of cons, which is perhaps 6 or 7 lines long. Each of these functions (eval, car, cdr, quote) should have a List parameter (cons has two) and return a List. When this phase is complete, a sample run might look like this.

A few tips on evaluation:

Back to CS170 Assignments.