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
cons( e, L )- returns a new list, with element e prepended to the front of list L
car( L )- returns the first element in the list
cdr( L )- returns the rest of the list (minus the first element)
nullp( L )- returns 1 if L is null, 0 otherwise
intp( e )- returns 1 if e is an integer, 0 otherwise
listp( e )- returns 1 if e is a list, 0 otherwise
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 is available in the lectures Grammars and Parsing, Mini Language Interpreter and Memory Allocation and Garbage Collection.
Extend the mini language and interpreter from Mini Language Lecture Part 3 to:
]. You may use the grammar from Assignment 1 with
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.
|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]|
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:
moreutility) all source code for part 1
moreutility) your iterative length function in the mini language
moreutility) your recursive length function in the mini language
moreutility) all source code for part 2
Note: Please pay attention to file names. E.g.,
makefile is not
README is not