-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticAnalyzer.java
More file actions
276 lines (231 loc) · 8.56 KB
/
Copy pathSemanticAnalyzer.java
File metadata and controls
276 lines (231 loc) · 8.56 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import absyn.*;
import absyn.Declaration.*;
import absyn.Expressions.*;
import absyn.Statements.*;
import java.util.HashMap;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class SemanticAnalyzer implements AbsynVisitor {
final static int SPACES = 4;
private FileOutputStream outFile;
private boolean printSymbols;
private HashMap<String, ArrayList<NodeType>> map = new HashMap<>();
private FuncDeclaration currFunc = null;
public SemanticAnalyzer(Absyn result, FileOutputStream out) {
this.outFile = out;
this.printSymbols = out != null;
printMessage("Entering the global scope:", 0);
int level = 0;
result.accept(this, ++level, false);
for(ArrayList<NodeType> arr: map.values()) {
for(NodeType type: arr) {
if(type.dec instanceof FuncDeclaration) {
String funcStr = type.name + ": (";
DeclarationList p = ((FuncDeclaration)(type.dec)).params;
if(p == null) {
funcStr += "void";
}
while(p != null && p.head != null) {
funcStr += p.head.type;
if(p.tail != null)
funcStr += ",";
p = p.tail;
}
funcStr += (") -> " + type.dec.type);
printMessage(funcStr, 1);
}else if(type.dec instanceof VarDeclaration) {
if(((VarDeclaration)(type.dec)).size != null){
printMessage(type.name + ": " + type.dec.type + "[" + ((VarDeclaration)(type.dec)).size + "]", 1);
}else {
printMessage(type.name + ": " + type.dec.type, 1);
}
}
}
}
printMessage("Leaving global scope", 0);
}
private void printMessage(String message, int level) {
try{
if(this.printSymbols) {
for( int i = 0; i < level * SPACES; i++ ) this.outFile.write(" ".getBytes());
this.outFile.write((message + "\n").getBytes());
}
}catch(IOException e){
e.printStackTrace();
}
}
private void insert(NodeType node) {
if(map.get(node.name) == null) {
map.put(node.name, new ArrayList<NodeType>());
}
map.get(node.name).add(0, node);
}
private NodeType lookup(String name) {
NodeType n = null;
ArrayList<NodeType> arr = map.get(name);
if(arr == null) {
return null;
}
for(NodeType node: arr) {
if(node.name.equals(name)) {
n = node;
break;
}
}
return n;
}
private void delete(int level) {
for(ArrayList<NodeType> arr: map.values()) {
Iterator i = arr.iterator();
while(i.hasNext()) {
NodeType n = (NodeType) i.next();
if(n.level == level) {
i.remove();
}
}
}
}
public void visit( DeclarationList expList, int level, boolean isAddr ) {
while( expList != null && expList.head != null) {
expList.head.accept( this, level, false );
expList = expList.tail;
}
}
public void visit(VarDeclaration node, int level, boolean isAddr) {
NodeType type = new NodeType(node.name, node, level);
insert(type);
if(level > 1)
printMessage(node.name + ": " + node.type, level);
}
public void visit( FuncDeclaration exp, int level, boolean isAddr ) {
printMessage( "Entering the function scope for " + exp.name + ":", level);
insert(new NodeType(exp.name, exp, level));
this.currFunc = exp;
level++;
DeclarationList dl = exp.params;
while(dl != null && dl.head != null) {
dl.head.accept(this, level, false);
dl = dl.tail;
}
exp.funcBody.accept( this, level, false );
this.currFunc = null;
delete(level);
printMessage("Leaving function scope", --level);
}
public void visit( StatementList expList, int level, boolean isAddr ) {
while( expList != null && expList.head != null) {
expList.head.accept( this, level, false );
expList = expList.tail;
}
}
public void visit( VarExpression exp, int level, boolean isAddr ) {
String varStr = exp.name;
NodeType type = lookup(varStr);
if(type == null) {
System.err.println("Error: variable does not exist\n");
}else {
if(type.dec.type.equals("int") && exp.index != null) {
System.err.println("Error: Variable type is not defined (line "
+ (exp.row + 1) + ", col " + exp.index.col + ")");
}
exp.dtype = type.dec;
}
// Check if index for arrays is an integer type
if(exp.index != null) {
exp.index.accept(this, ++level, false);
if(!exp.index.dtype.type.equals("int")){
System.err.println("Error: Array index must be of type int (line "
+ (exp.index.row + 1) + ", col " + exp.index.col + ")");
}
}
}
public void visit( AssignExpression exp, int level, boolean isAddr ) {
exp.lhs.accept( this, level, false );
exp.rhs.accept( this, level, false );
if(exp.lhs.dtype.type.equals("void") || exp.lhs.dtype.type != exp.rhs.dtype.type) {
System.err.println("Error: Cannot assign type " + exp.rhs.dtype.type + " to type " +
exp.lhs.dtype.type + " (line " + (exp.row + 1) + ", col " + exp.col + ")");
}
}
public void visit( IfStatement exp, int level, boolean isAddr ) {
exp.test.accept( this, level, false );
if (exp.test.dtype.type != "int" && exp.test.dtype.type != "boolean") {
System.err.println("Error: If statement test must be an int or int comparison (line "
+ (exp.test.row + 1) + ", col " + exp.test.col + ")");
}
printMessage("Entering a new block:", level);
exp.ifblock.accept( this, ++level, false );
delete(level);
printMessage("Leaving the block", --level);
if (exp.elseblock != null ) {
printMessage("Entering a new block:", level);
exp.elseblock.accept( this, ++level, false );
delete(level);
printMessage("Leaving the block", --level);
}
}
public void visit( IntExpression exp, int level, boolean isAddr ) {
exp.dtype = new VarDeclaration(exp.row, exp.col, "int", "", null);
}
public void visit( OpExpression exp, int level, boolean isAddr ) {
exp.left.accept( this, level, false );
exp.right.accept( this, level, false );
if(exp.left.dtype.type.equals("int") && exp.right.dtype.type.equals("int")
&& (exp.op >= 0 && exp.op <= 3)) {
exp.dtype = new VarDeclaration(exp.row, exp.col, "int", "", null);
}else if(exp.left.dtype.type.equals("int") && exp.right.dtype.type.equals("int")
&& exp.op >= 4) {
exp.dtype = new VarDeclaration(exp.row, exp.col, "boolean", "", null);
} else {
System.err.println("Error: Cannot evaluate expression (line "
+ (exp.row + 1) + ", col " + exp.col + ")");
exp.dtype = new VarDeclaration(exp.row, exp.col, "error", "", null);
}
}
public void visit( WhileStatement exp, int level, boolean isAddr ) {
exp.test.accept( this, level, false );
if (exp.test.dtype.type != "int" && exp.test.dtype.type != "boolean") {
System.err.println("Error: While statement test must be an int or int comparison (line "
+ (exp.test.row + 1) + ", col " + exp.test.col + ")");
}
printMessage("Entering a new block:", level);
exp.exps.accept( this, ++level, false );
delete(level);
printMessage("Leaving the block", --level);
}
public void visit( ReturnStatement exp, int level, boolean isAddr ) {
if(currFunc == null) {
System.err.println("Error: Return can only be used inside a function body (line "
+ (exp.row + 1) + ", col " + exp.col + ")");
}
exp.exp.accept( this, ++level, false );
NodeType func = lookup(currFunc.name);
if(!exp.exp.dtype.type.equals(func.dec.type)) {
System.err.println("Error: Return type doesn't match type specified in function declaration (line "
+ (exp.row + 1) + ", col " + exp.col + ")");
}
}
public void visit( FuncExpression exp, int level, boolean isAddr ) {
if(exp.args != null)
exp.args.accept(this, ++level, false);
NodeType n = lookup(exp.funcName);
// Set the type of this expression
if(n == null && exp.funcName == "input") {
exp.dtype = new FuncDeclaration(exp.row, exp.col, "int", "input", null, null);
}else if(n == null && exp.funcName == "output") {
exp.dtype = new FuncDeclaration(exp.row, exp.col, "void", "output", null, null);
}else{
exp.dtype = n.dec;
}
}
public void visit(CompoundStatement node, int level, boolean isAddr) {
if(node.local_decl != null) {
node.local_decl.accept(this, level, false);
}
if(node.statements != null) {
node.statements.accept(this, level, false);
}
}
}