-
Notifications
You must be signed in to change notification settings - Fork 1
/
symbol.h
387 lines (328 loc) · 10.4 KB
/
symbol.h
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#pragma once
#include "parser.h"
class Symbol
{
public:
string name;
boostvar type;
int scope_level;
Symbol()
{
this->scope_level = 0;
}
Symbol(string name)
{
this->name = name;
this->scope_level = 0;
}
Symbol(string name, boostvar type)
{
this->name = name;
this->type = type;
this->scope_level = 0;
}
};
class BuiltinTypeSymbol : public Symbol
{
public:
BuiltinTypeSymbol(string name)
{
this->name = name;
}
};
//To print the token in the correct format
ostream& operator<<(ostream& strm, const BuiltinTypeSymbol& builtInSymbol) {
return strm << builtInSymbol.name;
}
class VarSymbol : public Symbol
{
public:
VarSymbol(string name, boostvar type)
{
this->name = name;
this->type = type;
}
};
ostream& operator<<(ostream& strm, const VarSymbol& varSymbol) {
Type* type = boost::get<Type *>(varSymbol.type);
return strm << "<{" << varSymbol.name << " : " << type->value << "}>";
}
class ProcedureSymbol : public Symbol
{
public:
string name;
vector<boostvar> params;
boostvar block_node;
ProcedureSymbol(string name, vector<boostvar>params)
{
this->name = name;
this->params = params;
}
};
class ScopedSymbolTable
{
public:
unordered_map<string, boostvar> symbols;
string scope_name;
int scope_level;
//ScopedSymbolTable() {}
ScopedSymbolTable(string scope_name, int scope_level) {
this->scope_name = scope_name;
this->scope_level = scope_level;
_init_builtins();
}
void _init_builtins()
{
insert(new BuiltinTypeSymbol("INTEGER"));
insert(new BuiltinTypeSymbol("REAL"));
}
void error(string error_code, string name)
{
cout <<"ERROR:: "<< error_code << " -> (ID, " << name << ")" << endl;
_Exit(10);
}
void insert(boostvar symbol)
{
if (symbol.which() == 11)
{
BuiltinTypeSymbol* sym = boost::get<BuiltinTypeSymbol*>(symbol);
sym->scope_level = this->scope_level;
symbols[sym->name] = sym;
}
else if (symbol.which() == 12)
{
VarSymbol* sym = boost::get<VarSymbol*>(symbol);
sym->scope_level = this->scope_level;
symbols[sym->name] = sym;
}
else if (symbol.which() == 15)
{
ProcedureSymbol* sym = boost::get<ProcedureSymbol*>(symbol);
sym->scope_level = this->scope_level;
symbols[sym->name] = sym;
}
}
boostvar lookup(string name, bool procedue_check = false);
};
ostream& operator<<(ostream& strm, const ScopedSymbolTable& symbolTable) {
string information;
for (auto value : symbolTable.symbols)
information += " " + value.first;
return strm << "Symbols: {" + information;
}
stack<ScopedSymbolTable> enclosed_scopes;
boostvar ScopedSymbolTable :: lookup(string name, bool procedure_check)
{
//cout << "Lookup: " << name << endl;
if (symbols.find(name) != symbols.end())
{
boostvar symbol = symbols[name];
return symbol;
}
if (enclosed_scopes.size() > 1)
{
ScopedSymbolTable enclosed_scope = enclosed_scopes.top();
enclosed_scopes.pop();
boostvar symbol = enclosed_scope.lookup(name);
enclosed_scopes.push(enclosed_scope);
return symbol;
}
if (procedure_check)
{
cout << "ERROR:: No such function named '" << name<< "' exists." << endl;
_Exit(10);
}
else error("Variable not found", name);
}
class SemanticAnalyzer {
public:
ScopedSymbolTable current_scope = ScopedSymbolTable("", 0);
void error(string error_code)
{
cout << "ERROR:: " << error_code << endl;
_Exit(10);
}
void visit(boostvar node)
{
if (node.which() == 0)
visit_BinOp(boost::get<BinOp*>(node));
else if (node.which() == 1)
visit_Num(boost::get<Num*>(node));
else if (node.which() == 2)
visit_UnaryOp(boost::get<UnaryOp*>(node));
else if (node.which() == 3)
visit_Compound(boost::get<Compound*>(node));
else if (node.which() == 4)
visit_Assign(boost::get<Assign*>(node));
else if (node.which() == 5)
visit_Var(boost::get<Var*>(node));
else if (node.which() == 6)
visit_NoOp(boost::get<NoOp*>(node));
else if (node.which() == 7)
visit_Program(boost::get<Program*>(node));
else if (node.which() == 8)
visit_Block(boost::get<Block*>(node));
else if (node.which() == 9)
visit_VarDecl(boost::get<VarDecl*>(node));
else if (node.which() == 13)
visit_ProcedureDecl(boost::get<ProcedureDecl*>(node));
else if (node.which() == 16)
visit_Print(boost::get<Print*>(node));
else if (node.which() == 17)
visit_ProcedureCall(boost::get<ProcedureCall*>(node));
else if (node.which() == 18)
visit_Read(boost::get<Read*>(node));
else if (node.which() == 19)
visit_Condition(boost::get<Condition*>(node));
else if (node.which() == 20)
visit_Loop(boost::get<Loop*>(node));
else if (node.which() == 21)
visit_Message(boost::get<Message*>(node));
else
error("INVALID PARSING METHOD");
}
void visit_BinOp(BinOp* node)
{
visit(node->left);
visit(node->right);
}
void visit_Num(Num* node)
{
return;
}
void visit_UnaryOp(UnaryOp* node)
{
visit(node->expr);
}
void visit_Compound(Compound* node)
{
for (auto child : node->children)
visit(child);
}
void visit_NoOp(NoOp* node)
{
return;
}
void visit_Assign(Assign* node) {
visit(node->left);
visit(node->right);
if (node->right.which() == 5)
{
string var_name_right = boost::get<Var*>(node->right)->value;
VarSymbol* var_sym_right = boost::get<VarSymbol*>(this->current_scope.lookup(var_name_right));
string var_name_left = boost::get<Var*>(node->left)->value;
VarSymbol* var_sym_left = boost::get<VarSymbol*>(this->current_scope.lookup(var_name_left));
BuiltinTypeSymbol* type_val_left = boost::get<BuiltinTypeSymbol*>(var_sym_left->type);
BuiltinTypeSymbol* type_val_right = boost::get<BuiltinTypeSymbol*>(var_sym_right->type);
if (type_val_left->name != type_val_right->name)
{
cout << "ERROR:: Incompatible types: " << var_name_right << " assignment to " << var_name_left << endl;
_Exit(10);
}
}
}
void visit_Message(Message* node)
{
return;
}
void visit_Read(Read* node)
{
visit(node->var);
}
void visit_Print(Print* node)
{
for (auto message : node->messages)
visit(message);
}
void visit_Var(Var* node)
{
string var_name = node->value;
boostvar var_symbol = current_scope.lookup(var_name);
}
void visit_ProcedureDecl(ProcedureDecl* node)
{
string proc_name = node->proc_name;
ProcedureSymbol* proc_symbol = new ProcedureSymbol(proc_name, vector<boostvar>());
proc_symbol->block_node = node->block_node;
this->current_scope.insert(proc_symbol);
enclosed_scopes.push(current_scope);
//cout << "Enter scope: " << proc_name << endl;
ScopedSymbolTable procedure_scope = ScopedSymbolTable(proc_name, this->current_scope.scope_level + 1);
this->current_scope = procedure_scope;
for (auto param : node->params)
{
Param* param_node = boost::get<Param*>(param);
Type* type_node = boost::get<Type*>(param_node->type_node);
boostvar param_type = this->current_scope.lookup(type_node->value);
Var* var_node = boost::get<Var*>(param_node->var_node);
string param_name = var_node->value;
VarSymbol* var_symbol = new VarSymbol(param_name, param_type);
this->current_scope.insert(var_symbol);
proc_symbol->params.push_back(var_symbol);
}
visit(node->block_node);
//cout << "procedure_scope" << endl;
this->current_scope = enclosed_scopes.top();
enclosed_scopes.pop();
//cout << "Leave scope : " << proc_name << endl;
}
void visit_ProcedureCall(ProcedureCall* node)
{
string proc_name = node->proc_name;
boostvar proc = current_scope.lookup(proc_name, true);
if (proc.which() == 15)
{
ProcedureSymbol* proc_symbol = boost::get<ProcedureSymbol*>(proc);
node->proc_symbol = proc_symbol;
}
for (auto param_node : node->actual_params)
visit(param_node);
}
void visit_VarDecl(VarDecl* node)
{
Type* type_node = boost::get<Type*>(node->type_node);
string type_name = type_node->value;
boostvar type_symbol = current_scope.lookup(type_name);
Var* var_node = boost::get<Var*>(node->var_node);
string var_name = var_node->value;
if (current_scope.symbols.find(var_name) != current_scope.symbols.end())
{
cout << "ERROR:: Duplicate variable found -> " << var_node->token <<endl;
_Exit(10);
}
boostvar var_symbol = new VarSymbol(var_name, type_symbol);
current_scope.insert(var_symbol);
}
void visit_Condition(Condition* node)
{
visit(node->condition_node);
for (auto statement : node->if_statements)
visit(statement);
for (auto statement : node->else_statements)
visit(statement);
}
void visit_Loop(Loop* node)
{
visit(node->condition_node);
for (auto statement : node->statements)
visit(statement);
}
void visit_Block(Block* node)
{
for (auto declaration : node->declarations)
visit(declaration);
visit(node->compound_statement);
}
void visit_Program(Program* node)
{
//cout << "Enter Scope : GLOBAL" << endl;
ScopedSymbolTable global_scope = ScopedSymbolTable("GLOBAL", 1);
enclosed_scopes.push(current_scope);
this->current_scope = global_scope;
visit(node->block);
//cout << "global_scope" << endl;
this->current_scope = enclosed_scopes.top();
enclosed_scopes.pop();
//cout << "Leave scope : GLOBAL" << endl;
}
};