// ********************************************************************** // The SymTab class implements a symbol table, which is a LinkedList // of Hashtables, each containing zero or more Syms. // // Public Methods // ============== // // constructor // ----------- // SymTab() -- initialize the table to be empty (i.e., to // be a LinkedList containing one, empty Hashtable) // // modifiers // --------- // void insert(Sym sym) -- add sym to the first table in the LinkedList // (error if the LinkedList is empty or if // there is already a symbol with the // same name) // // void addHashtab() -- add a new, empty Hashtable to the front of // the LinkedList // // void removeHashtab() -- remove the Hashtable from the front of // the LinkedList (error if the LinkedList is // already empty) // // other operations // ---------------- // Sym localLookup(String k) -- if there is a symbol with the given name // in the first hashtable in the LinkedList, // then return it; // otherwise, return null // Sym globalLookup(String k) -- if there is a symbol with the given name // in ANY hashtable in the LinkedList, // then return the first such Sym; // otherwise, return null // void print(PrintWriter p) -- print all of the Hashtables in the // LinkedList, one per line, to the given // PrintWriter import java.io.*; import java.util.*; class SymTab { /*** private fields ***/ LinkedList myList; // the LinkedList of hashtables /*** public methods ***/ // ********************************************************************** // constructor // ********************************************************************** public SymTab() { myList = new LinkedList(); addHashtab(); } // ********************************************************************** // insert // ********************************************************************** public void insert(Sym sym) throws DuplicateException, EmptySymTabException { Hashtable tab; if (myList.size() == 0) throw new EmptySymTabException(); if (localLookup(sym.name()) != null) throw new DuplicateException(); try { tab = (Hashtable)(myList.getFirst()); tab.put(sym.name(), sym); } catch (NoSuchElementException ex) { // shouldn't get here System.err.println("unexpected NoSuchElementException in SymTab.insert"); System.exit(-1); } } // ********************************************************************** // addHashtab // ********************************************************************** public void addHashtab() { myList.addFirst(new Hashtable()); } // ********************************************************************** // removeHashtab // ********************************************************************** public void removeHashtab() throws EmptySymTabException { try { myList.removeFirst(); } catch (NoSuchElementException ex) { throw new EmptySymTabException(); } } // ********************************************************************** // localLookup // ********************************************************************** public Sym localLookup(String str) { Hashtable tab; if (myList.size() == 0) return null; try { tab = (Hashtable)(myList.getFirst()); return (Sym)(tab.get(str)); } catch (NoSuchElementException ex) { // shouldn't get here System.err.println("unexpected NoSuchElementException in SymTab.insert"); System.exit(-1); return null; // compiler requires a return here } } // ********************************************************************** // globalLookup // ********************************************************************** public Sym globalLookup(String str) { Hashtable tab; Sym tmp; Iterator it = myList.iterator(); try { while (it.hasNext()) { tab = (Hashtable)(it.next()); tmp = (Sym)(tab.get(str)); if (tmp != null) return tmp; } } catch (NoSuchElementException ex) { // shouldn't get here System.err.println("unexpected NoSuchElementException in SymTab.globalLookup"); System.exit(-1); } return null; } // ********************************************************************** // print // ********************************************************************** public void print(PrintWriter p) { Hashtable tab; Iterator it = myList.iterator(); try { while (it.hasNext()) { tab = (Hashtable)(it.next()); p.println(tab.toString()); } } catch (NoSuchElementException ex) { // shouldn't get here System.err.println("unexpected NoSuchElementException in SymTab.print"); System.exit(-1); } } }