1 20 21 22 import java.util.*; 23 24 32 public class SymTab { 33 Hashtable h; 36 SymTab pred; 38 public SymTab() { 39 this(null); 40 } 41 42 public SymTab(SymTab p) { 43 h = new Hashtable(); 44 pred = p; 45 } 46 47 public boolean enter(String s, SymtabEntry e) { 48 Object value = lookup(s); 49 h.put(s, e); 50 return(value==null); 51 } 52 53 public SymtabEntry lookup(String s) { 54 Object value = h.get(s); 55 if (value==null && pred!=null) 56 value = pred.lookup(s); 57 return ((SymtabEntry)value); 58 } 59 60 public String toString() { String res = "symbol table\n=============\n"; 62 Enumeration e = h.keys(); 63 String key; 64 65 while(e.hasMoreElements()) { 66 key = (String )e.nextElement(); 67 res += key+" \t"+h.get(key)+"\n"; 68 } 69 70 if (pred!=null) res+="++ predecessor!\n"; 71 return(res); 72 } 73 74 public int size() { 75 return(h.size()); 76 } 77 } 78 79 | Popular Tags |