5 public class SymbolTable {
7 private Hashtable table;
8 private SymbolTable parent;
10 public SymbolTable() {
11 table = new Hashtable();
15 public SymbolTable(SymbolTable parent) {
16 table = new Hashtable();
20 //public void add(String name, Descriptor d) {
24 public void add(Descriptor d) {
25 table.put(d.getSymbol(), d);
28 public void add(String name, Descriptor d) {
34 Enumeration e = getDescriptors();
35 while (e.hasMoreElements()) {
36 Descriptor d = (Descriptor) e.nextElement();
37 System.out.println(d.getSymbol());
40 System.out.println("parent:");
45 public Descriptor get(String name) {
46 Descriptor d = (Descriptor) table.get(name);
47 if (d == null && parent != null) {
48 return parent.get(name);
54 public Descriptor getFromSameScope(String name) {
55 return (Descriptor)table.get(name);
58 public Enumeration getNames() {
62 public Enumeration getDescriptors() {
63 return table.elements();
66 public Iterator descriptors() {
67 return table.values().iterator();
70 public Vector getAllDescriptors() {
75 d = parent.getAllDescriptors();
78 Enumeration e = getDescriptors();
79 while(e.hasMoreElements()) {
80 d.addElement(e.nextElement());
86 public boolean contains(String name) {
87 return (get(name) != null);
95 public int sizeAll() {
97 return parent.sizeAll() + table.size();
103 public SymbolTable getParent() {
107 public void setParent(SymbolTable parent) {
108 this.parent = parent;
112 * Adds contents of st2.table to this.table and returns a
113 * Vector of shared names, unless there are no shared names,
114 * in which case returns null.
116 public Vector merge(SymbolTable st2) {
117 Vector v = new Vector();
118 Enumeration names = st2.table.keys();
120 while (names.hasMoreElements()) {
121 Object o = names.nextElement();
123 if (table.containsKey(o)) {
126 table.put(o, st2.table.get(o));
137 public String toString() {
138 return "ST: " + table.toString();