CS 536 Program 1: The Sym and SymTab Classes

Due date: Tuesday, September 21 (by midnight)
Not accepted after midnight on Friday, September 24

Overview | Requirements | Announcements | Handin

Overview

In this assignment you will write several Java classes to be used later in the semester to represent a symbol table.  You will also get experience writing code to test the classes you have written and using a Makefile.

Requirements

For this assignment you will write four Java classes: SymTab, Sym, DuplicateException, and EmptySymTabException.

The SymTab class will be used by the compiler you write later in the semester to represent a symbol table. It will be implemented as a LinkedList of Hashtables, each of which stores a set of Syms.

The Sym class will be used to store information about each identifier that appears in a program (the variable and function names). For now, the only information will be the name of the identifier (a String); more information will be added for future assignments.

The DuplicateException and EmptySymTabException classes will define exceptions that can be thrown by methods of the SymTab class.

In addition to defining the four classes, you will write a main program to test your implementation, and a Makefile that controls the compilation of your main program. You will be graded both on the correctness of your Sym and SymTab classes and on how thoroughly your main program tests the SymTab class.

The Sym Class

The Sym class must be in a file named Sym.java. You must implement the following (public) Sym methods (and no other public methods):

Sym(String name) This is the constructor; it should initialize the Sym to have the given name.
String name() Return the name of the Sym.
String toString() Return the empty string.

The SymTab Class

The SymTab class must be in a file named SymTab.java. It must be implemented using a LinkedList of Hashtables (i.e., the SymTab class will have a field that is a LinkedList, and each element of that LinkedList will be a Hashtable). See below for more about LinkedLists and Hashtables.

You must implement the following (public) SymTab methods (and no other public methods):

SymTab() This is the constructor; it should initialize the SymTab's LinkedList field to contain a single, empty Hashtable.
void insert(Sym sym) throws DuplicateException, EmptySymTabException If the list is empty, throw an EmptySymTabException. If the first Hashtable in the list contains a Sym with the same name as the parameter sym, throw a DuplicateException. Otherwise, insert sym into the first Hashtable in the list (using the name as the hash key).
void addHashtab() Add a new, empty Hashtable to the front of the list.
Sym localLookup(String name) If the list is empty, return null. If the first Hashtable in the list contains a Sym with the given name, return that Sym. Otherwise, return null.
Sym globalLookup(String name) If any Hashtable in the list contains a Sym with the given name, return the first such Sym (i.e., the one from the Hashtable that is closest to the front of the list); otherwise, return null.
void removeHashtab() throws EmptySymTabException If the list is empty, throw an EmptySymTabException; otherwise, remove the Hashtable from the front of the list.
void print(PrintWriter p) This method is for debugging. For each Hashtable T in the list, print T.toString() to the given PrintWriter followed by a newline. Note that the print method should not close PrintWriter p; however, don't forget to close it in your test code (or you won't get any output).

The DuplicateException and EmptySymTabException Classes.

These two classes (which must be in files named DuplicateException.java, and EmptySymTabException.java) will simply define the two exceptions that can be thrown by the SymTab class. To define an exception named XXX, use code like this:
public class XXX extends Exception {
}

LinkedLists and Hashtables

The LinkedList and Hashtable classes are part of the java.util package; documentation can be found on-line for
the Hashtable class and the LinkedList class). (To access the full Java documentation, click here.)

See

~cs536-1/public/prog1/examples.java
for an example program that uses the LinkedList class.

The main program

To test your SymTab implementation, you will write a main program that exercises all of the SymTab methods. The program must be in a file named P1.java. Be sure to test:
  1. Every SymTab operation.
  2. Both "boundary" and non-boundary cases (e.g., operations on both an empty table and a non-empty table).
  3. All errors that cause an exception to be thrown (e.g., attempting to insert two symbols with the same name).

Your program should include comments that describe what is being tested. It must not require any input from the user (including command-line arguments). It may read from a file; in that case, the file name should be "hard coded" (you can name the file whatever you like, and don't forget to hand it in -- see What to Hand In below). You may use the code in the file

~cs536-1/public/prog1/IO.java to open files and to read words (e.g., names) from a file. See ~cs536-1/public/prog1/examples.java for examples of file I/O.

Note that your test code must work on our Sym and SymTab implementations, too, so it can't count on any methods other than the ones specified above, and it can't count on those classes having any particular fields.

You can decide what output your main program produces. However, a suggestion is to make it produce output only when it finds a problem with the code being tested. Note that you can make sure your methods throw exceptions when they are supposed to do so (without halting your test program) by putting those method calls inside try blocks in your P1.java code.

The Makefile

You must write a Makefile so that typing "make" compiles your main program, creating P1.class. See the file ~cs536-1/public/prog1/Makefile for an example Makefile and see the notes on Make for a more detailed description of how Makefiles work.

If you use the IO class, be sure to include a rule in your Makefile that compiles IO.java, and be sure to include IO.class in the rule that creates P1.class.

Note: To use jikes (the Java compiler) add:

setenv CLASSPATH ".:/s/java/jre/lib/rt.jar" to your .cshrc.local file. After you have added the definition of CLASSPATH to your .cshrc.local file, you must either log out and back in, or (in your top-level directory), type: source .cshrc If you type: printenv CLASSPATH the response should be: .:/s/java/jre/lib/rt.jar

Announcements

Includes: Additions, Revisions, and FAQs (Frequently Asked Questions).
Please check here frequently.

9/14/2004 Program released.  The handin directories have not yet been created - watch this space (and your email) for an update when they have been. 
9/16/2004 The handin directories have been created.  You can now hand in your code as described in the How to Submit Work Electronically section of the assignments page.


Handin

What to turn in

See the assignments page for information about how to submit your code.  The late policy is also found on the assignments page. 

Electronically submit all of the files that are needed to create P1.class (including your Makefile and a copy of IO.java if you use it), and a copy of your test data (if your main program reads from a file).  Do not copy any ".class" files, and do not create any subdirectories in your handin directory.

General information on program grading criteria can be found on the Grading Criteria for Programs page.