-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymTab.java
77 lines (63 loc) · 1.57 KB
/
SymTab.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
66
67
68
69
70
71
72
73
74
75
76
77
package symtab;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
public class SymTab{
SymTab parentScope;
private ArrayList<SymTab> childScope;
//First entry is name, second is type and last value -- default = 0
HashMap<String, Pair> entries;
public SymTab() {
entries = new HashMap<String, Pair>();
childScope = new ArrayList<>();
}
public SymTab enterScope(SymTab ps) {
SymTab newChild = new SymTab();
ps.childScope.add(newChild); //Check
newChild.parentScope = ps;
return newChild;
}
public SymTab exitScope(SymTab curr) {
Set<String> keys = curr.entries.keySet();
for(String s:keys) {
System.out.print(s);
System.out.println("\t" + curr.entries.get(s).getKind() + "\t" + curr.entries.get(s).getTypeOfVar());
}
System.out.println();
return curr.parentScope;
}
public boolean checkBack(String name, SymTab curr)
{
SymTab tmp = new SymTab();
tmp = curr;
while(tmp != null)
{
if(tmp.entries.containsKey(name) == true)
{
return true;
}
else
{
tmp = tmp.parentScope;
System.out.println("NOT FOUND");
System.out.println(tmp.entries);
}
}
return false;
}
public void insert(String name, String kind, String typeOfVar) {
if(this.entries.containsKey(name) == false) {
entries.put(name, new Pair(typeOfVar, kind));
}
else if(this.entries.containsKey(name) == true)
{
System.out.println(name + " already declared.");
System.exit(-1);
}
else
{
System.out.println("Variable "+name+" declaration not found.");
System.exit(-1);
}
}
}