-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymTable.java
65 lines (52 loc) · 1.45 KB
/
SymTable.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import java.util.*;
public class SymTable {
private List<HashMap<String, Sym>> list;
public SymTable() {
list = new LinkedList<HashMap<String, Sym>>();
list.add(new HashMap<String, Sym>());
}
public void addDecl(String name, Sym sym)
throws DuplicateSymException, EmptySymTableException {
if (name == null || sym == null)
throw new IllegalArgumentException();
if (list.isEmpty())
throw new EmptySymTableException();
HashMap<String, Sym> symTab = list.get(0);
if (symTab.containsKey(name))
throw new DuplicateSymException();
symTab.put(name, sym);
}
public void addScope() {
list.add(0, new HashMap<String, Sym>());
}
public Sym lookupLocal(String name)
throws EmptySymTableException {
if (list.isEmpty())
throw new EmptySymTableException();
HashMap<String, Sym> symTab = list.get(0);
return symTab.get(name);
}
public Sym lookupGlobal(String name)
throws EmptySymTableException {
if (list.isEmpty())
throw new EmptySymTableException();
for (HashMap<String, Sym> symTab : list) {
Sym sym = symTab.get(name);
if (sym != null)
return sym;
}
return null;
}
public void removeScope() throws EmptySymTableException {
if (list.isEmpty())
throw new EmptySymTableException();
list.remove(0);
}
public void print() {
System.out.print("\n** Sym Table **\n");
for (HashMap<String, Sym> symTab : list) {
System.out.println(symTab.toString());
}
System.out.println();
}
}