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.
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. |
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 | |
public class XXX extends Exception { }
See
~cs536-1/public/prog1/examples.java
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
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.
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:
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. |
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.