-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathastgen.cpp
559 lines (536 loc) · 16.4 KB
/
astgen.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
AUTHORS : Hrishikesh S.
DEVELOPER COMMENTS : \/\* for describing code
\/\/ for removing code
vn v - version, n - version number
*/
#include<bits/stdc++.h>
#include "astgen.h"
// core
// make a node for AST
ast_node *make_ast_node(std::string op, struct ast_node *left_leaf, struct ast_node *right_leaf){
ast_node *temp_node = new ast_node;
temp_node->symbol = op;
// for the binary tree
/*
temp_node->left = left_leaf;
temp_node->right = right_leaf;
*/
// for the generic tree
temp_node->children.push_back(left_leaf);
temp_node->children.push_back(right_leaf);
std::cout << "######AST : node created at " << op << std::endl;
return temp_node;
}
// core
// make a leaf for AST
ast_node *make_ast_leaf(std::string val){
ast_node *temp_leaf = new ast_node;
temp_leaf->symbol = val;
// for binary tree
/*
temp_leaf->left = NULL;
temp_leaf->right = NULL;
*/
// for generic tree
//temp_leaf->children.push_back(NULL);
temp_leaf->children.push_back(NULL);
std::cout << "######AST : leaf created at " << val << std::endl;
return temp_leaf;
}
// core
// Prints the branch in pre-order like fashion
void LevelOrderTraversalEACHROOT(ast_node *root){
if (root==NULL)
return;
std::queue<ast_node *> q;
std::cout << "||" << std::endl;
std::cout << "||" << std::endl;
std::cout << "||" << std::endl;
std::cout << "||" << std::endl;
q.push(root);
while (!q.empty()){
int n = q.size();
// If this node has children
while (n > 0){
ast_node *p = q.front();
q.pop();
if(q.size() > 1)
std::cout << p->symbol << " ";
else
std::cout << p->symbol << " ";
/*
else
std::cout << p->symbol << std::endl;
*/
// if there are not children, move to next sibling
if((p->children)[0] == NULL)
break;
// Enqueue all children of the dequeued item
for (int i=0; i< p->children.size(); i++)
q.push(p->children[i]);
n--;
}
/*
if(q.size() == 0)
std::cout << std::endl;
else
std::cout << " ";
*/
std::cout << std::endl;
/*
std::cout << "|" << std::endl;
std::cout << "|" << std::endl;
std::cout << "|" << std::endl;
std::cout << "|" << std::endl;
*/
}
//std::cout << std::endl;
}
// deprecated
// create a branches on node
ast_node *create_branch(ast_node *i_root, ast_node *branch){
std::cout << "######BRANCH CREATION : " << branch->symbol << std::endl;
i_root->children.push_back(branch);
LevelOrderTraversalEACHROOT(i_root);
std::cout << std::endl;
return i_root;
}
// core, expression evaluation
// central node creation of exp evaluation
ast_node *central_node_creation(std::string op){
std::cout << "######NODE CREATION : RL EXPRESSION EVALUATION central_node_creation()" << std::endl;
ast_node *left_leaf = S_ast.front();
S_ast.pop_front();
ast_node *right_leaf = S_ast.front();
S_ast.pop_front();
std::cout << "######Popping " << right_leaf->symbol << " and " << left_leaf->symbol << std::endl;
ast_node *central_node = make_ast_node(op, left_leaf, right_leaf);
return central_node;
}
// core
// alternate central node creation for exp evaluation
ast_node *central_node_creation_exp(std::string op){
std::cout << "######NODE CREATION : LR EXPRESSION EVALUATION central_node_creation_exp()" << std::endl;
ast_node *right_leaf = S_ast.front();
S_ast.pop_front();
ast_node *left_leaf = S_ast.front();
S_ast.pop_front();
std::cout << "######Popping " << right_leaf->symbol << " and " << left_leaf->symbol << std::endl;
ast_node *central_node = make_ast_node(op, left_leaf, right_leaf);
return central_node;
}
// core
// declaration and expression
ast_node *central_node_creation_declaration(std::string op){
std::cout << "######NODE CREATION : DECLR & EXPRESSION EVALUATION central_node_creation_declaration()" << std::endl;
ast_node *right_leaf = S_ast.front();
S_ast.pop_front();
ast_node *left_leaf = S_ast.front();
S_ast.pop_front();
ast_node *next_leaf = S_ast.front();
S_ast.pop_front();
std::cout << "######Popping " << right_leaf->symbol << " and " << left_leaf->symbol << " and " << next_leaf->symbol << std::endl;
ast_node *central_node = make_ast_node(op, left_leaf, right_leaf);
central_node->children.push_back(next_leaf);
return central_node;
}
// pre-order traversal for binary tree
/*
void printPreorder(struct ast_node *node){
if (node == NULL)
return;
std::cout << node->symbol << " ";
printPreorder(node->left);
printPreorder(node->right);
}
*/
// post-order traversal for binary tree
/*
void printPostorder(struct ast_node *node){
if(node == NULL)
return;
printPostorder(node->left);
printPostorder(node->right);
std::cout << node->symbol << " ";
}
*/
// core
// branch-wise traversal for syntax tree
void LevelOrderTraversalAST(){
std::cout << "SYNTAX TREE (BRANCH-WISE - LevelOrder)" << std::endl;
std::cout << syn_root->head_symbol << std::endl;
for(auto i:syn_root->children){;
LevelOrderTraversalEACHROOT(i);
std::cout << std::endl;
}
/*
std::cout << "SYNTAX TREE (BRANCH - WISE - postOrder)" << std::endl;
std::cout << syn_root->head_symbol << std::endl;
for(auto i:syn_root->children){
std::cout << i->symbol << " ";
printPostorder(i);
std::cout << std::endl;
}
*/
}
// to print stack elements
// useful for logging
void print_stack_elements(){
std::cout << "stack contents" << std::endl;
std::cout << "-----------------" << std::endl;
if(!S_ast.empty()){
std::cout << "not empty" << std::endl;
for(auto i: S_ast){
std::cout << i->symbol << std::endl;
}
}
else{
std::cout << "empty" << std::endl;
}
std::cout << "-----------------" << std::endl;
}
void declare_and_assign_branch(int if_flag, int for_flag, int while_flag){
std::string op = "DECLR_STAT";
std::cout << "######DECLR_AND_ASSIGN BRANCH declare_and_assign_branch()" << std::endl;
// pop int, a, value
// so there are three pops
ast_node *first_child = S_ast.front();
S_ast.pop_front();
ast_node *second_child = S_ast.front();
S_ast.pop_front();
ast_node *third_child = S_ast.front();
S_ast.pop_front();
// create NODE with value
std::cout << "######LEAFS : " <<first_child->symbol << " " << second_child->symbol << " " << third_child->symbol << std::endl;
ast_node *temp_node = new ast_node;
temp_node->symbol = op;
temp_node->children.push_back(first_child);
temp_node->children.push_back(second_child);
temp_node->children.push_back(third_child);
std::cout << "######AST : node created at " << op << std::endl;
if(if_flag > 0 || for_flag > 0 || while_flag > 0){
S_ast.push_front(temp_node);
std::cout << "node pushed into stack" << std::endl;
}
else{
syn_root->children.push_back(temp_node);
std::cout << "node pushed into tree" << std::endl;
}
}
void declare_assign_node_creation(){
std::string op = "DECLR_STAT";
std::cout << "######DECLR_AND_ASSIGN BRANCH declare_assign_node_creation()" << std::endl;
// pop int, a, value
// so there are three pops
ast_node *first_child = S_ast.front();
S_ast.pop_front();
ast_node *second_child = S_ast.front();
S_ast.pop_front();
ast_node *third_child = S_ast.front();
S_ast.pop_front();
// create NODE with value
std::cout << first_child->symbol << " " << second_child->symbol << " " << third_child->symbol << std::endl;
ast_node *temp_node = new ast_node;
temp_node->symbol = op;
temp_node->children.push_back(first_child);
temp_node->children.push_back(second_child);
temp_node->children.push_back(third_child);
S_ast.push_front(temp_node);
std::cout << "######DECLR_AND_ASSIGN NODE created" << std::endl;
}
void general_declaration_in_loop(){
std::cout << "######General declaration in loop" << std::endl;
std::string declr = "DECLR_STAT";
ast_node *temp = new ast_node;
temp->symbol = declr;
ast_node *branch1 = S_ast.front();
S_ast.pop_front();
ast_node *branch2 = S_ast.front();
S_ast.pop_front();
temp->children.push_back(branch1);
temp->children.push_back(branch2);
S_ast.push_front(temp);
}
// core
// if
void IF_Alternate(int if_cond_flag){
std::cout << "######Creating new branch" << std::endl;
std::deque<ast_node*> temp;
// start with popping
ast_node *branch1 = S_ast.front();
S_ast.pop_front();
std::cout << branch1->symbol << std::endl;
// pop till we find for
while(branch1->symbol != "if"){
std::cout << "adding " << branch1->symbol << std::endl;
temp.push_front(branch1);
if(S_ast.size() > 0){
branch1 = S_ast.front();
std::cout << "popping ";
std::cout << branch1->symbol << std::endl;
S_ast.pop_front();
}
}
// branch1 will contain 'if'
ast_node *if_struct = new ast_node;
if_struct->symbol = "IF_STRUCT";
// for condition
ast_node *cond = new ast_node;
cond->symbol = "COND";
ast_node *branch2 = temp.front();
cond->children.push_back(branch2);
temp.pop_front();
// attach all children 'if'
// add the statements
ast_node *statement = new ast_node;
statement->symbol = "STATEMENT";
ast_node *tmp_branch = new ast_node;
std::cout << temp.size() << std::endl;
while(temp.size() > 0){
tmp_branch = temp.front();
temp.pop_front();
std::cout << "popping and adding " << tmp_branch->symbol << std::endl;
statement->children.push_back(tmp_branch);
}
if_struct->children.push_back(branch1);
if_struct->children.push_back(cond);
if_struct->children.push_back(statement);
if(if_cond_flag > 1)
S_ast.push_front(if_struct);
else
syn_root->children.push_back(if_struct);
std::cout << "######IF added to the tree" << std::endl;
return;
// popping 'if'
/*
ast_node *branch1 = S_ast.front();
S_ast.pop_front();
// IF_STRUCT is the header of 'if' statement
ast_node *if_struct = new ast_node;
if_struct->symbol = "IF_STRUCT";
if_struct->children.push_back(branch1);
// popping statements till relop
std::string statement_symbol = "STATEMENT";
// new node for statement branch
ast_node *statement = new ast_node;
statement->symbol = statement_symbol;
ast_node *branch2 = S_ast.front();
S_ast.pop_front();
// pushing statement to STATEMENT node
statement->children.push_back(branch2);
LevelOrderTraversalEACHROOT(statement);
std::cout << std::endl;
// popping condition
ast_node *branch3 = S_ast.front();
S_ast.pop_front();
while(branch3->symbol != "=="){
if(branch3->symbol == "int"){
S_ast.push_front(branch3);
ast_node *temp = general_declaration_in_loop();
statement->children.push_back(temp);
}
else{
statement->children.push_back(branch3);
}
branch3 = S_ast.front();
S_ast.pop_front();
}
// COND is the header of condition
std::string condition_symbol = "COND";
// STATEMENT is the header of statement
// new node for condition branch
ast_node *cond = new ast_node;
cond->symbol = condition_symbol;
std::cout << "######Added node names" << std::endl;
/*
ast_root->children.push_back(create_branch(statement, branch2));
ast_root->children.push_back(create_branch(cond, branch3));
*/
// pushing condition to COND node
/*
cond->children.push_back(branch3);
LevelOrderTraversalEACHROOT(if_struct);
std::cout << std::endl;
*/
/*
LevelOrderTraversalEACHROOT(branch2);
LevelOrderTraversalEACHROOT(branch3);
*/
/*
ast_root->children.push_back(branch2);
ast_root->children.push_back(branch3);
*/
/*
if_struct->children.push_back(cond);
if_struct->children.push_back(statement);
syn_root->children.push_back(if_struct);
std::cout << "######IF added to the tree" << std::endl;
*/
}
// for branch execution
void for_creation(int for_flag){
std::cout << "######BRANCH EXECUTION" << std::endl;
std::deque<ast_node*> temp;
// start with popping
ast_node *branch1 = S_ast.front();
S_ast.pop_front();
std::cout << branch1->symbol << std::endl;
// pop till we find for
while(branch1->symbol != "for"){
std::cout << "adding " << branch1->symbol << std::endl;
temp.push_front(branch1);
if(S_ast.size() > 0){
branch1 = S_ast.front();
std::cout << "popping ";
std::cout << branch1->symbol << std::endl;
S_ast.pop_front();
}
}
// branch1 will contain 'for'
ast_node *for_struct = new ast_node;
for_struct->symbol = "FOR_STRUCT";
// for init expression in 'for' loop
ast_node *init_expr = new ast_node;
init_expr->symbol = "INIT";
ast_node *branch2 = temp.front();
init_expr->children.push_back(branch2);
temp.pop_front();
// for condition expression in 'for' loop
ast_node *cond_expr = new ast_node;
cond_expr->symbol = "COND";
ast_node *branch3 = temp.front();
cond_expr->children.push_back(branch3);
temp.pop_front();
// for unary expression in 'for' loop
/*
ast_node *uop_expr = new ast_node;
uop_expr->symbol = "UNARYOP"
*/
ast_node *branch4 = temp.front();
/*
uop_expr->children.push_back(branch4);
*/
temp.pop_front();
// attach all children 'for'
for_struct->children.push_back(branch1);
for_struct->children.push_back(init_expr);
for_struct->children.push_back(cond_expr);
for_struct->children.push_back(branch4);
// add the statements
ast_node *statement = new ast_node;
statement->symbol = "STATEMENT";
ast_node *tmp_branch = new ast_node;
std::cout << temp.size() << std::endl;
while(temp.size() > 0){
tmp_branch = temp.front();
temp.pop_front();
std::cout << "popping and adding " << tmp_branch->symbol << std::endl;
statement->children.push_back(tmp_branch);
}
for_struct->children.push_back(statement);
if(for_flag > 1)
S_ast.push_front(for_struct);
else
syn_root->children.push_back(for_struct);
std::cout << "######FOR added to the tree" << std::endl;
return;
}
// to create nodes for unary expression (post-fix & pre-fix)
void unary_expression_branch(std::string identifier, std::string uop){
ast_node *unary_expr = new ast_node;
unary_expr->symbol = "UNARY EXPR";
unary_expr->children.push_back(make_ast_leaf(identifier));
unary_expr->children.push_back(make_ast_leaf(uop));
S_ast.push_front(unary_expr);
std::cout << "######UOP added to stack" << std::endl;
return;
}
void while_creation(int while_flag){
std::cout << "######Creating new branch" << std::endl;
std::deque<ast_node*> temp;
// start with popping
ast_node *branch1 = S_ast.front();
S_ast.pop_front();
std::cout << branch1->symbol << std::endl;
// pop till we find for
while(branch1->symbol != "while"){
std::cout << "adding " << branch1->symbol << std::endl;
temp.push_front(branch1);
if(S_ast.size() > 0){
branch1 = S_ast.front();
std::cout << "popping ";
std::cout << branch1->symbol << std::endl;
S_ast.pop_front();
}
}
// branch1 will contain 'if'
ast_node *while_struct = new ast_node;
while_struct->symbol = "WHILE_STRUCT";
// for condition
ast_node *cond = new ast_node;
cond->symbol = "COND";
ast_node *branch2 = temp.front();
cond->children.push_back(branch2);
temp.pop_front();
// attach all children 'if'
// add the statements
ast_node *statement = new ast_node;
statement->symbol = "STATEMENT";
ast_node *tmp_branch = new ast_node;
std::cout << temp.size() << std::endl;
while(temp.size() > 0){
tmp_branch = temp.front();
temp.pop_front();
std::cout << "popping and adding " << tmp_branch->symbol << std::endl;
statement->children.push_back(tmp_branch);
}
while_struct->children.push_back(branch1);
while_struct->children.push_back(cond);
while_struct->children.push_back(statement);
if(while_flag > 1)
S_ast.push_front(while_struct);
else
syn_root->children.push_back(while_struct);
std::cout << "######WHILE added to the tree" << std::endl;
return;
}
void else_creation(){
std::cout << "######Creating new branch" << std::endl;
std::deque<ast_node*> temp;
// start with popping
ast_node *branch1 = S_ast.front();
S_ast.pop_front();
std::cout << branch1->symbol << std::endl;
// pop till we find for
while(branch1->symbol != "else"){
std::cout << "adding " << branch1->symbol << std::endl;
temp.push_front(branch1);
if(S_ast.size() > 0){
branch1 = S_ast.front();
std::cout << "popping ";
std::cout << branch1->symbol << std::endl;
S_ast.pop_front();
}
}
// branch1 will contain 'if'
ast_node *else_struct = new ast_node;
else_struct->symbol = "ELSE_STRUCT";
// attach all children 'if'
// add the statements
ast_node *statement = new ast_node;
statement->symbol = "STATEMENT";
ast_node *tmp_branch = new ast_node;
std::cout << temp.size() << std::endl;
while(temp.size() > 0){
tmp_branch = temp.front();
temp.pop_front();
std::cout << "popping and adding " << tmp_branch->symbol << std::endl;
statement->children.push_back(tmp_branch);
}
else_struct->children.push_back(branch1);
else_struct->children.push_back(statement);
S_ast.push_front(else_struct);
std::cout << "######IF added to the tree" << std::endl;
return;
}