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(Descriptor d) {
21 add(d.getSymbol(), d);
24 public void add(String name, Descriptor d) {
29 Enumeration e = getDescriptors();
30 while (e.hasMoreElements()) {
31 Descriptor d = (Descriptor) e.nextElement();
32 System.out.println(d.getSymbol());
35 System.out.println("parent:");
40 public Descriptor get(String name) {
41 Descriptor d = (Descriptor) table.get(name);
42 if (d == null && parent != null) {
43 return parent.get(name);
49 public Descriptor getFromSameScope(String name) {
50 return (Descriptor)table.get(name);
53 public Enumeration getNames() {
57 public Iterator getNamesIterator() {
58 return table.keySet().iterator();
61 public Enumeration getDescriptors() {
62 return table.elements();
65 public Iterator getDescriptorsIterator() {
66 return table.values().iterator();
69 public Iterator descriptors() {
70 return table.values().iterator();
73 public Vector getAllDescriptors() {
78 d = parent.getAllDescriptors();
81 Enumeration e = getDescriptors();
82 while(e.hasMoreElements()) {
83 d.addElement(e.nextElement());
89 public boolean contains(String name) {
90 return (get(name) != null);
98 public int sizeAll() {
100 return parent.sizeAll() + table.size();
106 public SymbolTable getParent() {
110 public void setParent(SymbolTable parent) {
111 this.parent = parent;
115 * Adds contents of st2.table to this.table and returns a
116 * Vector of shared names, unless there are no shared names,
117 * in which case returns null.
119 public Vector merge(SymbolTable st2) {
120 Vector v = new Vector();
121 Enumeration names = st2.table.keys();
123 while (names.hasMoreElements()) {
124 Object o = names.nextElement();
126 if (table.containsKey(o)) {
129 table.put(o, st2.table.get(o));
140 public String toString() {
141 return "ST: " + table.toString();