-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraversal.cpp
394 lines (365 loc) · 16.7 KB
/
traversal.cpp
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
388
389
390
391
392
393
394
#include "traversal.h"
#include <fstream>
template <typename S>
std::ostream &operator<<(std::ostream &os, const std::vector<S> &vector)
{
// Printing all the elements
// using <<
for (auto element : vector)
{
os << element << " ";
}
os << "\n";
return os;
}
int isUniformSize(Initializer *init, std::vector<int> &vec)
{
if (init->isScalar == true)
{
return 1;
}
int size = init->val.InitializerList->size();
std::vector<int> tmp;
for (auto i : *init->val.InitializerList)
{
tmp.push_back(isUniformSize(i, vec));
}
if (std::all_of(tmp.begin(), tmp.end(), [&](int i)
{ return i == tmp[0]; }))
{
// vec.push_back(size);
return size;
}
else
{
// std::cout << "Fatal: Tensor dimensions not uniform" << std::endl;
return -1;
}
}
void ShapeTensor(Initializer *init, std::vector<int> &vec)
{
if (init->isScalar == true)
{
return;
}
int size = init->val.InitializerList->size();
vec.push_back(size);
ShapeTensor(init->val.InitializerList->at(0), vec);
return;
}
//Semantic Analysis for Declarations part of the program
void traverse_declarations(Start *root)
{
std::vector<Decl *> *DeclList = root->DeclList;
// Check for invalid data type declarations
for (auto decl : *DeclList)
{
switch (decl->DataType)
{
case TypeSpecifier::INT:
break;
case TypeSpecifier::BOOL:
break;
case TypeSpecifier::FLOAT:
break;
case TypeSpecifier::TENSOR:
break;
default:
std::cout << "Fatal: Type not found" << std::endl;
break;
}
}
// Check if correct values are being assigned to variables or not
for (auto decl : *DeclList)
{
// std::cout << decl->InitDeclaratorList->declarator->name << " " << decl->InitDeclaratorList->declarator->Dimensions.size() << std::endl;
if (decl->DataType != TypeSpecifier::TENSOR)
{
// Check if tensor value is being assigned to a scalar variable or not
if (decl->InitDeclaratorList->initializer != NULL)
{
if (decl->InitDeclaratorList->initializer->isScalar == false)
{
// Find the row and col from symbol table
SymTabItem *symTabItem = search(root->symbolTable, decl->InitDeclaratorList->declarator->name);
std::string err_msg = "Type mismatch, Tensor value assigned to scalar variable " + decl->InitDeclaratorList->declarator->name;
semantic_error(err_msg, symTabItem->rowNum, symTabItem->colNum, true);
// std::cout << "Fatal: Type mismatch at "<< symTabItem->rowNum<< ":"<<symTabItem->colNum <<"\n\tTensor value being assigned to scalar: " << decl->InitDeclaratorList->declarator->name << std::endl;
// exit(0);
}
}
// Checks if the scalar variable declared is of scalar type and not tensor
if (decl->InitDeclaratorList->declarator->Dimensions.size() != 0)
{
SymTabItem *symTabItem = search(root->symbolTable, decl->InitDeclaratorList->declarator->name);
std::string err_msg = "Type mismatch, Scalar variable " + decl->InitDeclaratorList->declarator->name + " declared as tensor";
semantic_error(err_msg, symTabItem->rowNum, symTabItem->colNum, true);
// std::cout << "Fatal: Type mismatch at "<< symTabItem->rowNum<< ":"<<symTabItem->colNum <<"\n\tScalar variable is not arrayable(tensor) type: " << decl->InitDeclaratorList->declarator->name << std::endl;
// exit(0);
}
}
// Check if float value is being assigned to non float type variable
if (decl->DataType == TypeSpecifier::INT || decl->DataType == TypeSpecifier::BOOL)
{
if (decl->InitDeclaratorList->initializer != NULL)
{
if (decl->InitDeclaratorList->initializer->val.cvalue != NULL)
{
if (decl->InitDeclaratorList->initializer->isScalar == true && decl->InitDeclaratorList->initializer->val.cvalue->isInt == false)
{
SymTabItem *symTabItem = search(root->symbolTable, decl->InitDeclaratorList->declarator->name);
std::string err_msg = "Type mismatch, Float value assigned to non float type variable " + decl->InitDeclaratorList->declarator->name;
semantic_error(err_msg, symTabItem->rowNum, symTabItem->colNum, true);
// std::cout << "Fatal: Type mismatch at "<< symTabItem->rowNum<< ":"<<symTabItem->colNum <<"\n\tFloat value being assigned to non float variable: " << decl->InitDeclaratorList->declarator->name << std::endl;
// exit(0);
}
}
}
}
// Check for tensors
if (decl->DataType == TypeSpecifier::TENSOR)
{
//Check if tensor shape is declared
if (decl->InitDeclaratorList->declarator->Dimensions.size() == 0)
{
SymTabItem *symTabItem = search(root->symbolTable, decl->InitDeclaratorList->declarator->name);
std::string err_msg = "Tensor shape not declared for " + decl->InitDeclaratorList->declarator->name;
semantic_error(err_msg, symTabItem->rowNum, symTabItem->colNum, true);
// std::cout << "Fatal: Type mismatch at "<< symTabItem->rowNum<< ":"<<symTabItem->colNum <<"\n\tTensor variable shape not declared: " << decl->InitDeclaratorList->declarator->name << std::endl;
// exit(1);
}
else if (decl->InitDeclaratorList->initializer != NULL)
{
//Check if Tensor shape is uniform
std::vector<int> tensor_declare_shape = decl->InitDeclaratorList->declarator->Dimensions;
std::vector<int> tensor_init_shape;
int size = isUniformSize(decl->InitDeclaratorList->initializer, tensor_init_shape);
if (size == -1)
{
SymTabItem *symTabItem = search(root->symbolTable, decl->InitDeclaratorList->declarator->name);
std::string err_msg = "Tensor shape not uniform for " + decl->InitDeclaratorList->declarator->name;
semantic_error(err_msg, symTabItem->rowNum, symTabItem->colNum, true);
// std::cout << "Fatal: Type mismatch at "<< symTabItem->rowNum<< ":"<<symTabItem->colNum <<"\n\tTensor dimensions not uniform: " << decl->InitDeclaratorList->declarator->name << std::endl;
// exit(0);
}
// std::cout << decl->InitDeclaratorList->declarator->name << " " << size << std::endl;
if (size != -1)
{
ShapeTensor(decl->InitDeclaratorList->initializer, tensor_init_shape);
// for (int i = 0; i < tensor_init_shape.size(); i++)
// {
// std::cout << tensor_init_shape[i] << " ";
// }
// std::cout << decl->InitDeclaratorList->declarator->name <<"\n";
// for (int i = 0; i < tensor_declare_shape.size(); i++)
// {
// std::cout << tensor_declare_shape[i] << " ";
// }
// std::cout << decl->InitDeclaratorList->declarator->name <<"\n";
if(tensor_declare_shape != tensor_init_shape)
{
SymTabItem *symTabItem = search(root->symbolTable, decl->InitDeclaratorList->declarator->name);
std::string err_msg = "Tensor shape mismatch for " + decl->InitDeclaratorList->declarator->name + " declared as " + std::to_string(tensor_declare_shape[0]) + "x" + std::to_string(tensor_declare_shape[1]) + " but initialized as " + std::to_string(tensor_init_shape[0]) + "x" + std::to_string(tensor_init_shape[1]);
semantic_error(err_msg, symTabItem->rowNum, symTabItem->colNum, true);
// std::cout << "Fatal: Type mismatch at "<< symTabItem->rowNum<< ":"<<symTabItem->colNum <<"\n\tTensor dimensions not equal: " << decl->InitDeclaratorList->declarator->name << std::endl;
// exit(0);
}
}
}
else
{
}
}
}
}
void traverse_operations(Start *root)
{
std::vector<AssgnStmt *> *AssgnStmtList = root->AssgnStmtList;
// should be equal to number of assignment statements
// std::cout << "Traversing operations: " << AssgnStmtList->size() << std::endl;
for (auto assgn_stmt : *AssgnStmtList)
{
std::string var_name = assgn_stmt->name;
SymTabItem *symTabItem = search(root->symbolTable, var_name);
if (symTabItem == NULL)
{
std::string error = "Variable" + var_name + " not declared";
semantic_error(error, assgn_stmt->row_num, assgn_stmt->col_num, true);
// std::cout << "Fatal: Variable " << var_name << " not found" << std::endl;
// exit(0);
}
assgn_stmt->expr->initialize_expression_node_info(root->symbolTable);
// std::cout << "Dimensions of expr " << assgn_stmt->expr->dimensions << std::endl;
if (symTabItem->dataType == "int")
{
if (assgn_stmt->expr->DataType != TypeSpecifier::INT)
{
std::string error = "RHS and LHS not of same type, LHS is int and RHS is non int";
semantic_error(error, symTabItem->rowNum, symTabItem->colNum);
// std::cout << "Fatal: RHS and LHS not of same type\n";
// std::cout << "LHS of type: " << symTabItem->dataType << "\n";
// exit(0);
// TODO: handle shorthand
// TODO: add errors for bool and char
}
}
else if (symTabItem->dataType == "float")
{
if (assgn_stmt->expr->DataType == TypeSpecifier::TENSOR)
{
std::string error = "RHS and LHS not of same type, LHS: float, RHS: Tesnor";
semantic_error(error, assgn_stmt->row_num, assgn_stmt->col_num, true);
// std::cout << "Fatal: RHS and LHS not of same type\n";
// std::cout << "LHS of type: " << symTabItem->dataType << "\n";
// exit(0);
}
}
else if (symTabItem->dataType == "Tensor")
{
if (assgn_stmt->expr->DataType != TypeSpecifier::TENSOR)
{
std::string error = "RHS and LHS not of same type, LHS is Tensor, RHS is not Tensor";
semantic_error(error,assgn_stmt->row_num, assgn_stmt->col_num, true);
// std::cout << "Fatal: RHS and LHS not of same type\n";
// std::cout << "LHS of type: " << symTabItem->dataType << "\n";
// exit(0);
}
if (assgn_stmt->expr->dimensions != symTabItem->Dims)
{
std::string error = "RHS and LHS not of same dimensions, LHS is " + std::to_string(symTabItem->Dims[0]) + "x" + std::to_string(symTabItem->Dims[1]);
semantic_error(error,assgn_stmt->row_num, assgn_stmt->col_num, true);
// std::cout << "Fatal: RHS and LHS not of same dimensions\n";
// exit(0);
}
}
}
}
// Semantic Analysis for Gradient part of the program
void traverse_gradient(Start *root)
{
std::vector<GradStmt *> *GradStmtList = root->GradStmtList;
bool anyError = false;
// std::cout << "Gradient Semantic Analysis" << std::endl;
// checking if gradient is being done for vars only
for (auto gradStmt : *GradStmtList)
{
std::string gradName = gradStmt->name;
//Checking if variable exists
SymTabItem *symTabItem = search(root->symbolTable, gradName);
if (symTabItem == NULL)
{
std::string error = "Variable " + gradName + " not found";
semantic_error(error);
// std::cout << "Fatal: Variable " << gradName << " not found" << std::endl;
// exit(0);
// anyError = true;
}
else
{
//Checking if variable is a constant as its gradient cannot be taken
if (symTabItem->type == "cns" && gradStmt->grad_type != GradType::PRINT)
{
SymTabItem *symTabItem = search(root->symbolTable, gradStmt->name);
std::string error = "Cannot take gradient of constant " + gradName;
semantic_error(error);
// std::cout << "Fatal: Cannot take gradient of a constant: " << gradName << std::endl;
// exit(0);
// anyError = true;
}
}
}
// checking if backward and then gradient is being done
// std::cout << "Gradient Semantic Analysis" << std::endl;
if (anyError == false)
{
// std::map<std::string, GradType> gradMap;
bool backward = false;
for(auto gradStmt : *GradStmtList)
{
std::string gradStmtName = gradStmt->name;
SymTabItem *symTabItem = search(root->symbolTable, gradStmt->name);
GradType gradStmtType = gradStmt->grad_type;
if (gradStmtType == GradType::BACKWARD)
{
if(symTabItem->type == "cns")
{
std::string error_msg = "Cannot take gradient of a constant: " + gradStmtName;
semantic_error(error_msg, symTabItem->rowNum, symTabItem->colNum);
// std::cout << "Fatal: Cannot take backward of a constant: " << gradStmtName << std::endl;
// exit(0);
}
else{
// gradient for 1x1 tensor, int, float is allowed
if(symTabItem->dataType == "Tensor")
{
if(symTabItem->Dims != std::vector<int>(2,1))
{
std::string error_msg = "Cannot take backward of a tensor of dimensions other than 1x1: " + gradStmtName;
semantic_error(error_msg, symTabItem->rowNum, symTabItem->colNum);
// std::cout << "Fatal: Gradient of 1x1 Tensor is supported only: " << gradStmtName << std::endl;
// exit(0);
}
else
{
backward = true;
}
}
backward = true;
}
}
else if (gradStmtType == GradType::GRAD)
{
if (backward == false)
{
std::string error_msg = "Cannot take gradient without backward of " + gradStmtName;
semantic_error(error_msg, symTabItem->rowNum, symTabItem->colNum);
// std::cout << "Fatal: Gradient cannot be taken before backward pass" << std::endl;
// std::cout << "Cannot take gradient of: '" << gradStmtName << "' without taking backward first" << std::endl;
// anyError = true;
}
else
{
// if(symTabItem->dataType == "int" || symTabItem->dataType == "float")
// {
// std::cout << "Fatal: Gradient of int or float is not supported: " << gradStmtName << std::endl;
// exit(0);
// }
}
}
else
{
//Do nothing
}
}
}
// std::cout << "Gradient Semantic Analysis Over" << std::endl;
}
int semantic_error(std::string msg, int row, int col, bool print_line)
{
std::string basename = std::string(filename).substr(std::string(filename).find_last_of("/\\") + 1);
if (row && col)
{
std::cerr <<basename << ":" << row << ":" << col << ":"
<< "\e[1;31m Fatal: \e[0m" << msg << std::endl;
if (print_line)
{
std::cerr << std::string(basename.length(), ' ') << "| " <<get_line(row) << "\n"<< std::endl;
}
}
else
{
std::cerr << "\e[1;31m Fatal: \e[0m" << msg << std::endl;
}
exit(1);
}
std::string get_line( int row)
{
std::ifstream file(filename);
std::string line;
for (int i = 0; i < row; i++)
{
std::getline(file, line);
}
return line;
file.close();
}