-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.java
More file actions
73 lines (60 loc) · 1.48 KB
/
Copy pathSymbolTable.java
File metadata and controls
73 lines (60 loc) · 1.48 KB
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
66
67
68
69
70
71
72
73
package cop5556sp18;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Stack;
import cop5556sp18.Symbol;
import cop5556sp18.AST.Declaration;
public class SymbolTable {
int current, next;
HashMap<String, ArrayList<Symbol>> record = new HashMap<String, ArrayList<Symbol>>();
Stack<Integer> scopes = new Stack<Integer>();
Declaration dec;
public SymbolTable() {
this.current = 0;
this.next = 0;
scopes.push(0);
}
public void enterScope() {
current = ++next;
scopes.push(current);
}
public void exitScope() {
scopes.pop();
current = scopes.peek();
}
public void add(String ident, Symbol symbol) {
symbol.setScopeId(current);
if(!(record.containsKey(ident))) {
record.put(ident, new ArrayList<Symbol>());
}
record.get(ident).add(symbol);
}
public boolean duplicateCheck (Declaration dec) {
if(record.containsKey(dec.name)) {
ArrayList<Symbol> data = record.get(dec.name);
for(Symbol s : data) {
if(s.scopeId == current) {
return true;
}
}
}
return false;
}
public Declaration lookup(String ident) {
if (!record.containsKey(ident)) {
return null;
}
ArrayList<Symbol> data = record.get(ident);
int size = scopes.size();
if(!data.isEmpty()) {
for (int i = size-1; i >= 0; i--) {
for (Symbol sym: data) {
if (scopes.get(i) == sym.getScopeId()) {
return sym.getDeclaration();
}
}
}
}
return null;
}
}