next up previous

CSE 2320 Section 501 Fall 1998

Program 2

Due: October 13, 1998, 5:30pm (October 14, 1998, 5:00pm for -10%)

For this program you will implement a very small subset of the LISP programming language using singly-linked lists. You may write your code in C or C++. In addition to correctness, programming style is also important, so please write modular, well-documented code.

LISP stands for ``list processing'' and is used to define, query and modify lists of symbols. For us, a symbol is a string of no more than 32 uppercase alphanumeric characters (A-Z, 0-9). A list is denoted by a space-separated sequence of symbols surrounded by parentheses, e.g., ( CSE 2320 SECTION 501 ), with one space between each symbol and surrounding parentheses. The empty list is denoted by ( ).

The three LISP commands you are to implement are SETF, NTH and NTHREST as described in the following table, where n is a positive integer less than 100.

( SETF var list )

Sets variable var to list for possible future reference. Variable var can be any valid symbol. SETF returns the list.

( NTH n $\{list \vert var\}$ )

Returns the nth symbol in the given list or the list stored in variable var. If the list has fewer than n elements, then NTH returns the empty list ( ). If the variable var is undefined, then NTH returns ``Error: undefined variable''.

( NTHREST n $\{list \vert var\}$ )

Returns the list remaining after removing the first n symbols from the given list or the list stored in variable var. If the list has fewer than n symbols, then NTHREST returns the empty list ( ). If the variable var is undefined, then NTHREST returns ``Error: undefined variable''.


Implement data structures for the singly-linked list used to store the lists of symbols.

You may implement a global array to store the variables, but this is the only global variable allowed. There will be no more than 100 different vairables.

Be sure to garbage-collect the existing list when a variable is redefined. Garbage-collect all lists allocated just for NTH and NTHREST commands. Before ending the program, garbage-collect all other allocated memory.

The main part of your program will read in data from a file whose name is given as the first argument to your executable. The file will consist of one or more lines, each containing a valid command from among the three described above. Your main program should process each line one at a time and for each line, output the command read in, the result and a newline. See files pgm2in and pgm2out in the class directory on omega for an example.

Be sure to properly document your source code, as described in Program 1. After ensuring that your program compiles and executes correctly on the omega machine, run the program /public/cse/2320-501/handin2 to electronically hand in your source file. All source code must be in one file (i.e., no separate .h and .c files). Do not turn in a printout of your source code in class.

next up previous