-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbolTable.java
More file actions
82 lines (70 loc) · 1.93 KB
/
SymbolTable.java
File metadata and controls
82 lines (70 loc) · 1.93 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
74
75
76
77
78
79
80
81
82
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package symboltable;
/**
*
* @author tquart1
*/
public class SymbolTable {
private IdentifierHeaderNode[] table;
public SymbolTable() {
table = new IdentifierHeaderNode[50];
initTable(table);
}
public void add(String identifier, int line_num) {
int row;
row = findIdentifier(identifier);
LineNumNode temp;
if (row == -1) {
row = nextAvailRow();
table[row] = new IdentifierHeaderNode(identifier, new LineNumNode(line_num, null));
} else {
temp = table[row].getNext();
while (temp.getNext() != null) {
temp = temp.getNext();
}
temp.setNext(new LineNumNode(line_num, null));
}
}
private int findIdentifier(String ident) {
int i = 0;
while (table[i] != null) {
if (table[i].getIdentifier().equals(ident)) {
return i;
}
i = i + 1;
}
return -1;
}
private int nextAvailRow() {
int i = 0;
while (table[i] != null) {
i = i + 1;
}
return i;
}
private void initTable(IdentifierHeaderNode[] table) {
for (int i = 0; i < 50; i++) {
table[i] = null;
}
}
public void display() {
LineNumNode temp;
int i = 0;
while (table[i] != null) {
System.out.print(table[i].getIdentifier());
System.out.print(" ");
temp = table[i].getNext();
while (temp != null) {
System.out.println(temp.getLineNum());
System.out.print("");
temp = temp.getNext();
}
i++;
System.out.println();
}
}
}