-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.c
702 lines (617 loc) · 20.8 KB
/
ast.c
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
///////////////////////////////////////
// Author: Vishal Golcha
// Id: 2014B5A70717P
///////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include "astDefs.h"
int redundant[11];
char * red_names[9]={"MAIN","SQO","SQC","OP","CL","COMMA","SEMICOLON","eps"};
int ast_count=0;
int rem_redundant(int num){ //this is the symb_no
int i;
for(i=0;i<8;i++){
if(strcmp(symb_name[num],red_names[i])==0){
return 1;
}
}
}
int get_symb_no(char * x){
int i;
for(i=0;i<84;i++){
if(strcmp(x,symb_name[i])==0){
return i;
}
}
return -1;
}
tnode * tree_pruner(tnode* x,int par){
tnode * y =x->child;
int count =0;
// printf("check1\n");
if(x->child!=NULL){
if(rem_redundant(x->child->symb_no)==1){
tnode* z;
z=x->child->sibling;
// printf("%s \n",symb_name[z->symb_no]);
while(z!=NULL){
if(rem_redundant(z->symb_no)==1){
// printf("%s \n",symb_name[z->symb_no]);
z=z->sibling;
}
else{
break;
}
}
x->child = z;
// free memory
tnode * yy = y;
while(yy->sibling!=z){
yy=yy->sibling;
}
yy->sibling=NULL;
free(y);
}
}
// printf("check2\n");
if(x->child!=NULL){
tnode * n = x->child;
tnode * m = n;
// printf("%s \n",symb_name[x->child->symb_no]);
// printf("check1\n");
while(n->sibling!=NULL ){
// printf("check2\n");
if(rem_redundant(n->sibling->symb_no)==1){
tnode * zz = n->sibling;
// printf("%s \n",symb_name[n->sibling->symb_no]);
tnode *nm = n->sibling->sibling;
// printf("%s 2 \n",symb_name[zz->symb_no]);
// printf("%s 2 \n",symb_name[zz->symb_no]);
// printf("in here\n");
if(nm==NULL){
n->sibling=NULL;
n=nm;
break;
}
else{
n->sibling=n->sibling->sibling;
// n=n->sibling;
}
}
else{
n=n->sibling;
}
}
}
// x->child=m;
// printf("%s \n",symb_name[x->symb_no]);
// if(x->child->sibling!=NULL)
// printf("bacha sib %s \n",symb_name[x->child->sibling->symb_no]);
tnode *xx =x->child;
if(xx!=NULL){
count=1;
while(xx->sibling!=NULL){
// printf("boom\n");
// printf("%d",xx->symb_no);
// if(xx->sibling->symb_no!=-42)
// printf("%s --> ",symb_name[xx->sibling->symb_no]);
xx=xx->sibling;
count++;
}
}
// printf("child check fin \n");
x->ch_num=count;
// printf("%s \n",symb_name[x->symb_no]);
// printf("before sibs\n");
if(x->sibling!=NULL){
// printf("sibling %s \n",symb_name[x->sibling->symb_no]);
x->sibling=tree_pruner(x->sibling,2);
}
// printf("before kids\n");
if(x->child!=NULL)
x->child = tree_pruner(x->child,1);
/* ****** declaration statement pruning should work for left multi list as well *****
TYPE -- > VARLIST
|
ID , ID ,ID
******************************************* */
int ast_id =get_symb_no("ID");
if(x->symb_no==ast_id && x->sibling!=NULL && x->sibling->symb_no==get_symb_no("<more_ids>")){
x->sibling=x->sibling->child;
return x;
}
/* ****** conditional statement pruning *****
LT /GT etc
|
<constrainedVars Child > -> <constrainedVars Child >
******************************************* */
int bool_id =get_symb_no("<booleanExpression>");
// exclude the not case important
if(x->symb_no==bool_id ){
int cvars = get_symb_no("<constrainedVars>");
int sym_not = get_symb_no("NOT");
if(x->child->symb_no==cvars){
tnode * one = x->child->sibling->child;
tnode * two = x->child->child;
tnode * three = x->child->sibling->sibling->child;
three->sibling = NULL;
x->child= one ;
// x->child->sibling= NULL;
x->child->child = two;
x->child->child->sibling = three;
printf("%s %s %s \n",symb_name[x->child->symb_no],symb_name[x->child->child->symb_no],\
symb_name[x->child->child->sibling->symb_no]);
x->child->sibling = x->sibling;
printf("done\n");
// change to x->child to return x->child
}
else if(x->child->symb_no!=sym_not){
tnode * one = x->child->sibling->child;
tnode * two = x->child;
tnode * three = x->child->sibling->sibling;
x->child=one;
x->child->child = two;
x->child->child->sibling = three;
// printf("%s %s %s \n",symb_name[x->child->symb_no],symb_name[x->child->child->symb_no],\
symb_name[x->child->child->sibling->symb_no]);
// printf("done3\n");
x->child->sibling = x->sibling;
}
return x->child;
}
/************** parameter_list pruning *********
<parameter_list> ===> <type> ID <remainingList>
************************************************/
// there was ! here in x->sibling->symb_no != paralist_id
// which was wrong check other statements written while tests
int paralist_id =get_symb_no("<remainingList>");
if(x->symb_no == ast_id && x->sibling!=NULL && x->sibling->symb_no== paralist_id){
x->sibling = x->sibling->child;
return x;
}
/************** listVar pruning for inputParameterList in function*********
/* <var> <listVar> */
int var_id =get_symb_no("<var>");
int listvar_id = get_symb_no("<listVar>");
if(x->symb_no == var_id && x->sibling!=NULL && x->sibling->symb_no== listvar_id){
x->sibling = x->sibling->child->child;
return x;
}
/*
/************** rows pruning *********
<rows> ===> <row> <RowLit>
<RowLit> ===> SEMICOLON <rows>
*/
// occurence of an aritmetic expression term will now signify a row
int row_id = get_symb_no("<row>");
int rowLit_id = get_symb_no("<RowLit>");
if(x->symb_no == row_id && x->sibling!=NULL && x->sibling->symb_no== rowLit_id){
x->sibling = x->sibling->child->child;
return x;
}
/*
/************** accumulating column elements as a list *********
/* fiddles with <row> ===> NUM <remainingColElements>
*/
int num_id = get_symb_no("NUM");
int remcol_id = get_symb_no("<remainingColElements>");
if(x->symb_no == num_id && x->sibling!=NULL && x->sibling->symb_no== remcol_id){
x->sibling = x->sibling->child;
return x;
}
/*
/************** working on the arithmetic expressions now and pruning them *********
/*
*/
/*
<arithmeticExpression> ===> <arithmeticTerm> <ArithExpFactored>
<ArithExpFactored> ===> <operator_lowPrecedence> <arithmeticExpression>
<ArithExpFactored> ===> eps
<arithmeticTerm> ===> <factor> <ArithTermFactored>
<ArithTermFactored> ===> <operator_highPrecedence> <arithmeticTerm>
<ArithTermFactored> ===> eps
<factor> ===> OP <arithmeticExpression> CL
<factor> ===> <var>
*/
int arith_term = get_symb_no("<arithmeticTerm>");
int arith_id = get_symb_no("<arithmeticExpression>");
int arith_exp_fact = get_symb_no("<ArithExpFactored>");
int op_low = get_symb_no("<operator_lowPrecedence>");
int op_hi = get_symb_no("<operator_highPrecedence>");
int fact = get_symb_no("<factor>");
int arith_term_fact=get_symb_no("<ArithTermFactored>");
int mul =get_symb_no("MUL");
int div =get_symb_no("DIV");
int add =get_symb_no("PLUS");
int sub =get_symb_no("MINUS");
// printf("checker %s\n",symb_name[arith_term_fact]);
if(x->symb_no==op_hi || x->symb_no == op_low){
x->child->sibling=x->sibling;
return x->child;
}
/// base case 1 2 3
if(x->symb_no==arith_term && x->sibling!=NULL && x->sibling->symb_no==arith_exp_fact){
x->sibling = x->sibling->child;
// printf("%s ** \n",x->child->lexeme);
if(x->symb_no==arith_term && x->child!=NULL && x->child->sibling!=NULL && x->child->sibling->sibling!=NULL){
// printf("wooter\n");
// printf("%s \n",x->child->lexeme);
tnode * one , *two,*three;
one = x->child->sibling;
two = x->child;
three = x->child->sibling->sibling;
x->child=one;
x->child->child = two;
x->child->child->sibling = three;
x->child->sibling = x->sibling;
return x->child;
}
if(x->symb_no==arith_term && x->child!=NULL && x->child->sibling==NULL){
x->child->sibling = x->sibling;
return x->child;
}
return x;
}
if((x->symb_no== add ||x->symb_no== sub) && x->sibling!=NULL && x->sibling->symb_no==arith_exp_fact){
x->sibling = x->sibling->child;
return x;
}
if((x->symb_no== div ||x->symb_no== mul) && x->sibling!=NULL && x->sibling->symb_no==arith_exp_fact){
x->sibling = x->sibling->child;
return x;
}
// base cases for the factor ones
if(x->symb_no==fact && x->sibling!=NULL && x->sibling->symb_no==arith_term_fact){
x->sibling = x->sibling->child;
// printf("woot\n");
// matlit case hmm
int y = x->child->symb_no;
if(y==get_symb_no("ID")||y==get_symb_no("NUM")||y==get_symb_no("RNUM")||y==get_symb_no("STR") \
|| y==get_symb_no("<row>") ||y==get_symb_no("FUNID") || y == add ||y == sub \
|| y== mul ||y== div){
if(y==get_symb_no("ID") && x->child->sibling!=NULL){
// The curious case of matlit
x->child->sibling->sibling=x->sibling;
return x->child;
}
x->child->sibling=x->sibling;
return x->child;
}
return x;
}
if((x->symb_no== add ||x->symb_no== sub) && x->sibling!=NULL && x->sibling->symb_no==arith_term_fact){
x->sibling = x->sibling->child;
return x;
}
if((x->symb_no== mul ||x->symb_no== div) && x->sibling!=NULL && x->sibling->symb_no==arith_term_fact){
x->sibling = x->sibling->child;
return x;
}
// now let's resolve the arithemticTerm and Arithmetic Expression constructs needs some fixing with respect to factor wali computations
if(x->symb_no==arith_term && x->child!=NULL && x->child->sibling!=NULL && x->child->sibling->sibling!=NULL){
// printf("wooter\n");
// printf("%s \n",x->child->lexeme);
tnode * one , *two,*three;
one = x->child->sibling;
two = x->child;
three = x->child->sibling->sibling;
x->child=one;
x->child->child = two;
x->child->child->sibling = three;
x->child->sibling = x->sibling;
return x->child;
}
if(x->symb_no==arith_id && x->child!=NULL && x->child->sibling!=NULL && x->child->sibling->sibling!=NULL){
tnode * one , *two,*three;
one = x->child->sibling;
two = x->child;
three = x->child->sibling->sibling;
x->child=one;
x->child->child = two;
x->child->child->sibling = three;
x->child->sibling = x->sibling;
return x->child;
}
if(x->symb_no==arith_id && x->child!=NULL && x->child->sibling==NULL){
x->child->sibling = x->sibling;
return x->child;
}
if(x->symb_no==arith_term && x->child!=NULL && x->child->sibling==NULL){
// printf("woots\n");
x->child->sibling = x->sibling;
// printf("%s\n",x->child->lexeme);
return x->child;
}
if(count==0 && x->sibling==NULL){
if(x->symb_no<39){
if(x->symb_no==0){
return NULL;
}
return x; // pay careful attention here
}
else{
return NULL; // eps followed by eps looks impossible but think about it
}
}
else if(/* count==1 && */ par ==1 && x->sibling==NULL){
if(x->symb_no==row_id){
return x; // trying to preserve some structure helpful for the things ahead
}
if(x->symb_no==get_symb_no("<rows>")){
return x;
}
if(x->symb_no==get_symb_no("<declarationStmt>")){
return x;
}
return x->child;
}
return x;
/* xx =x->child;
if(xx!=NULL){
// count=1
xx=x->child->sibling;
while(xx!=NULL){
// printf("boom\n");
// printf("%s prints \n",symb_name[x->sibling->symb_no]);
xx=tree_pruner(xx,2);
xx=xx->sibling;
// count++;
}
}
// count again
xx=x->child;
if(xx!=NULL){
count=1;
while(xx->sibling!=NULL){
// printf("boom\n");
// printf("%s prints \n",symb_name[x->sibling->symb_no]);
xx=xx->sibling;
count++;
}
}
// if(count==1)
*/
}
void calculate_children(tnode* trv_node){
if(trv_node==NULL){
return;
}
if(trv_node->child!=NULL){
calculate_children(trv_node->child);
}
trv_node->ch_num=0;
if(trv_node->symb_no == -42){
trv_node->ch_num=1;
}
else{
tnode *temp=trv_node->child;
while(temp!=NULL){
trv_node->ch_num+=1;
if(trv_node->ch_num==0){
temp = trv_node->child->sibling;
}
else{
temp = temp->sibling;
}
}
}
if(trv_node->symb_no!=-42)
// printf("%s %d\n",symb_name[trv_node->symb_no],trv_node->ch_num);
;
else{
// printf("%s %d\n","$",trv_node->ch_num);
}
if(trv_node->child!=NULL){
tnode * x = trv_node->child->sibling;
while(x!=NULL){
calculate_children(x);
x = x->sibling;
}
}
}
ast_node* makenode(tnode * x){
// copy contents from tnode to x
// get the pointers from the kid
ast_node* temp = (ast_node*)malloc(sizeof(ast_node));
temp->sibling=NULL;
temp->child=NULL;
temp->symb_no=x->symb_no;
temp->lexeme = x->lexeme;// could be prone to errors of allocation
temp->line_num = x->line_num;
return temp;
}
ast_node* make_ast(tnode *x){
if(x==NULL){
return NULL;
}
if(x->symb_no==0){ // epsilon issues
return NULL;
}
if(x->ch_num==0 && x->sibling==NULL){
return makenode(x);
}
else if(x->ch_num==1 && x->sibling==NULL){
return make_ast(x->child);
}
else{
ast_node *temp =(ast_node *)malloc(sizeof(ast_node));
temp->symb_no=x->symb_no;
temp->lexeme = x->lexeme ; // check for errors here
temp->line_num = x->line_num;
if(x->child!=NULL)
temp->child = (make_ast(x->child));
else{
temp->child=NULL;
}
if(x->sibling!=NULL){
temp->sibling = (make_ast(x->sibling));
}
else{
temp->sibling =NULL;
}
// if(x->symb_no!=-42)
// printf("%s\n",symb_name[x->symb_no]);
return temp;
}
}
void update_ptree(char* inp){
init_lexer(inp);
init_grammar();
init_reverse_token_lookup_hash();
init_ptree_pstack();
parse();
close_file_lexer();
calculate_children(ptree);
}
void astraverse(ast_node *trv_node){
if(trv_node==NULL){
return;
}
if(trv_node->child!=NULL){
// printf("1\n");
// printf("%s \n",symb_name[trv_node->symb_no]);
// // printf("%s\n",trv_node->child->lexeme);
// printf("2\n");
if(trv_node->child == NULL){
printf("null hai bete\n");
}
astraverse(trv_node->child);
}
if(trv_node->symb_no == -42){
printf("dollar\n");
}
else{
printf("%s \n",symb_name[trv_node->symb_no]);
}
if(trv_node->child!=NULL){
// printf("in here %s ")
ast_node * x = trv_node->child->sibling;
while(x!=NULL){
// printf("not null\n");
// printf("%s par %s \n",x->lexeme,trv_node->lexeme);
// printf("%s\n",x->lexeme);
// printf("3\n");
astraverse(x);
x = x->sibling;
}
}
}
void ptraverse(tnode *trv_node){
if(trv_node==NULL){
return;
}
if(trv_node->child!=NULL){
// printf("not null\n");
// printf("%s par %s \n",trv_node->child->lexeme,trv_node->lexeme);
// printf("%s\n",trv_node->child->lexeme);
ptraverse(trv_node->child);
}
// printf("%d\n",trv_node->symb_no);
if(trv_node->symb_no == -42){
printf("$\n");
}
else{
printf("%s \n",symb_name[trv_node->symb_no]);
}
if(trv_node->child!=NULL){
// printf("in here %s ")
tnode * x = trv_node->child->sibling;
while(x!=NULL){
// printf("not null\n");
// printf("%s par %s \n",x->lexeme,trv_node->lexeme);
// printf("%s\n",x->lexeme);
ptraverse(x);
x = x->sibling;
}
}
}
void inorder_final_ast(tnode* trv_node){
// lexemeCurrentNode lineno token valueIfNumber parentNodeSymbol
// isLeafNode(yes/no) NodeSymbol
if(trv_node==NULL){
return;
}
if(trv_node->child!=NULL){
// printf("not null\n");
// printf("%s par %s \n",trv_node->child->lexeme,trv_node->lexeme);
// printf("%s\n",trv_node->child->lexeme);
inorder_final_ast(trv_node->child);
}
// printf("%d\n",trv_node->symb_no);
if(trv_node->symb_no == -42){
// printf("----\n");
printf("%25.25s %6d %25.25s %25.25s %25.25s %6.6s %25.25s\n","----",-1,"----","----","----","NO","$");
ast_count++;
}
else{
if(strcmp(trv_node->lexeme,"<mainFunction>")==0){
// printf("parent $\n");
printf("%25.25s %6d %25.25s %25.25s %25.25s %6.6s %25.25s\n","----",-1,"----","----","$","NO","<mainFunction>");
}
else if(trv_node->symb_no==0){
// printf("%s\n",trv_node->parent->lexeme);
printf("%25.25s %6d %25.25s %25.25s %25.25s %6.6s %25.25s\n","eps",-1,"eps","----",trv_node->parent->lexeme,"YES","----");
}
else if(strcmp(symb_name[trv_node->symb_no],"RNUM")==0 ||strcmp(symb_name[trv_node->symb_no],"NUM")==0){
printf("%25.25s %6d %25.25s %25.25s %25.25s %6.6s %25.25s\n",trv_node->lexeme,trv_node->line_num,symb_name[trv_node->symb_no]
,trv_node->lexeme,trv_node->parent->lexeme,"YES","----");
}
else if(trv_node->symb_no <=38){
printf("%25.25s %6d %25.25s %25.25s %25.25s %6.6s %25.25s\n",trv_node->lexeme,trv_node->line_num,symb_name[trv_node->symb_no]
,"----",trv_node->parent->lexeme,"YES","----");
}
else if(trv_node->symb_no>38){
printf("%25.25s %6d %25.25s %25.25s %25.25s %6.6s %25.25s\n","----",-1,"----","----",trv_node->parent->lexeme,"NO",trv_node->lexeme);
}
ast_count++;
// else if()
}
if(trv_node->child!=NULL){
// printf("in here %s ")
tnode * x = trv_node->child->sibling;
while(x!=NULL){
// printf("not null\n");
// printf("%s par %s \n",x->lexeme,trv_node->lexeme);
// printf("%s\n",x->lexeme);
inorder_final_ast(x);
x = x->sibling;
}
}
}
void ast_constructer(char *inp){
// rem_redundant();
update_ptree(inp);
// ast_node * ast_root = make_ast(ptree);
// printf("%s \n",symb_name[ptree->child->symb_no]);
ptree->child=tree_pruner(ptree->child,1);
inorder_final_ast(ptree);
// printf("pruned\n");
// printf("%s \n",symb_name[ptree->child->symb_no]);
// printf("%s \n",symb_name[ptree->child->child->sibling->symb_no]);
// printf("%d \n",ptree->symb_no);
// ptraverse(ptree);
// int symb = ptree->child->sibling->sibling->sibling->symb_no;
// printf("%s \n",symb_name[symb]);
// astraverse(ast_root);
}
void ast_constructer_wo_print(char *inp){
update_ptree(inp);
// ast_node * ast_root = make_ast(ptree);
// printf("%s \n",symb_name[ptree->child->symb_no]);
ptree->child=tree_pruner(ptree->child,1);
}
// int main(){
// // rem_redundant();
// update_ptree("./revised_testcases/testcase5.txt","addres.txt");
// // ast_node * ast_root = make_ast(ptree);
// // printf("%s \n",symb_name[ptree->child->symb_no]);
// ptree->child=tree_pruner(ptree->child,1);
// printf("pruned\n");
// // printf("%s \n",symb_name[ptree->child->symb_no]);
// // printf("%s \n",symb_name[ptree->child->child->sibling->symb_no]);
// printf("%d \n",ptree->symb_no);
// ptraverse(ptree);
// // int symb = ptree->child->sibling->sibling->sibling->symb_no;
// // printf("%s \n",symb_name[symb]);
// // astraverse(ast_root);
// }