Assignment 2

CS 550 Programming Languages
Instructor: Jeremy Johnson 
Due: Thursday April 25 by 9am

Support for List Processing in Mini Language (100 points)

This assignment extends the mini language and interpreter from Mini Language Lecture. It also uses the list grammar from the Lecture on Grammars and Parsing. You are to extend the mini language to support lists. That is variables may be assigned lists, lists may be returned by functions, and lists can occur in expressions. First you must change the symbol table so that variables may be bound to either integers or lists (use a class called Element which can be an Integer or List). Second you must extend the syntax of the language to support list constants (use "[" and "]" to enclose lists so as not to be confused with "(" and ")" which are used to group arithmetic expressions). E.G. [1,2,[3,4]].

You must also support the following functions

to allow construction and access to lists.

In Part I, these functions can be implemented using list functions from your implementation language. Finally, you should introduce an operator, denoted by two consecutive vertical bars, ||, for list concatenation.

Note that not all operators can be applied to integers or lists. E.G. +, -, and * only apply to integers, while || only applies to lists. Similarly, car, cdr, and null? require lists as inputs, while cons(x,L) can have x as either an integer or a list, but L must be a list. This error checking can be performed at runtime (i.e. when the program is interpreted).

In Part II, you must implement the underlying dynamic memory management used to support lists. Dynamic memory management should use garbage collection with a mark and sweep algorithm. The implementation of dynamic memory will assume that all allocated blocks are the same size (i.e. will hold either an int or a pointer to a list cell) and will follow the implementation outlined in Lecture 3. Use a vector or array of elements (ints or pointers, which are indices, to list cells). An element is stored in a cons cell which has two fields: a car field and a cdr field. cons cells also need bits to distinguish between pointers and ints and should also have a bit, for marking, to be used by garbage collection. You will need to reimplement cons, from Part I so that it checks to see if there are any available cells and if so returns a cell and updates the available cell list, and if not calls garbage collection. Recall that the available cells in the heap (a vector of cons cells) are linked together in an available cell list. Garbage collection works by first marking all active lists, i.e. those accessible from variables in the active environments, and then reclaiming, i.e. putting in the available cell list, those cells in the heap that are not marked. Afterwards, cells need to be unmarked. Make sure that temporary cells, e.g. those used to build list constants, or those used when concatenating lists, are accessible to the garbage collector. You will also need to reimplement car, cdr, and null? to access the car and cdr fields of cons cells and to check for lists that are null (list with index -1).

This assignment may be written in any of the following languages: C, C++, java, python, and their corresponding parser generators, though the current mini language interpreter provided from me is written in C++ and bison(there are java and python versions written by a former TA and Professor Schmidt respectively).

Background Information

Background information is available in the lectures Grammars and Parsing, Mini Language Interpreter and Memory Allocation and Garbage Collection.

What to do (Part I)

Extend the mini language and interpreter from Mini Language Lecture Part 3 to:

  1. Allow variables to be either integers or lists.
  2. to support list constants denoted by comma separated lists surrounded by [ and ]. You may use the grammar from Assignment 1 with ( and ) replaced by [ and ].
  3. to support the list functions cons(e,L), car(L), cdr(L), nullp(L), intp(e), and listp(e).
  4. to support a list concatenation operator denoted by ||

Use the extended language to write and call a function to compute the length of a list (implement this function iteratively and recursively). Execute programs that define and call these functions using your interpreter.

What to do (Part II)

Redo the list processing functions in Assignment 2 to use your own implementation of dynamic memory allocation and garbage collection.
  1. Provide a heap of cons cells (use a vector or array). Organize available cells into an available cell list.
  2. Reimplement cons, car, cdr, and null? to use and access cells in the heap. All allocation should ultimately be done through cons. The cons function should get cells from the available cell list, and if none are available call garbage collection. If cells are reclaimed by garbage collection, the processing of cons should resume, otherwise an error message printed.
  3. Implement a mark and sweep garbage collector. Accessible cells are those that can be reached from variables in the active environments. You also need to make sure to handle temporary variables properly, i.e. those used to construct list constants and in support of the concatenation operator.
Make sure that you thoroughly test your garbage collection routines by executing several scenarios where garbage collection is required.

Grading

Points will be assigned as follows.
5 part 1.1 [Correctness]
5 part 1.2 [Correctness]
10 part 1.3 [Correctness]
10 part 1.4 [Correctness]
20 part 2.1 [Correctness]
20 part 2.2 [Correctness]
20 part 2.3 [Correctness]
10 README
For evaluation of correctness, students will receive full credit if the program is working properly for all cases and if the program is partially working students receive 3/4 credit. If the submited program is not working but the right idea has been attempted, students receive 1/2 credit and if the solution is completely off base no credit will be received.

How to submit

Students should submit their solution electronically using BbVista. Only one submission is required per group. The group leader for the assignment should submit the assignment.

Submit a gzipped tar file, called A2.tar.gz (the tar file should contain a directory called A2 which contains the files). The tar file should contain source code, instructions how to run your programs, sample input and output files, and a README file. Code should be documented (clear specifications and comments for any tricky parts of the code). The program should succesfully build and run on the CS compute server tux.

Since you can do this in a number of languages, submit a makefile . Here is a sample makefile. Please ask for help w/makefiles.

Submit the following files to Bb Vista:

Note: Please pay attention to file names. E.g., makefile is not Makefile, and README is not README.txt.