-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.c
706 lines (619 loc) · 24.7 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
703
704
705
706
#include "stackTree.h"
extern char *map_nonterminals_to_strings[TOTAL_NUM_NT];
extern char *map_terminals_to_strings[TOTAL_NUM_TOKENS];
//root->addr holds the address of ast subtree
void createAst(TreeNode root)
{
if(root == NULL){
printf("root null, returning \n");
return;
}
TreeNode temp = root->child;
/* if(temp == NULL){
printf("null found termid: %s ruleid = %d, lineno = %d\n", (root->grammar_node->t == 1) ? map_nonterminals_to_strings[root->grammar_node->s.nonterm_type] : map_terminals_to_strings[root->grammar_node->s.term_type], root->rule_id, root->line);
// printf("ruleid : %d\n", root->rule_id);
// if(root->rule_id == 32){
// printf("garbage s*ala : %d\n", (int) (root->grammar_node == NULL));
// }
} */
switch(root->rule_id)
{
//terminals
case -2:
//root->addr = NULL;
// printf("null found termid: %s ruleid = %d, lineno = %d\n", (root->grammar_node->t == 1) ? map_nonterminals_to_strings[root->grammar_node->s.nonterm_type] : map_terminals_to_strings[root->grammar_node->s.term_type], root->rule_id, root->line);
// printf("returning\n");
return;
//rules where addr is simply passed on to the parent and no new node is created
//1st non terminal child
case 9:
case 10:
case 31 ... 35: //rules for stmt
case 62:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = root->child->addr;
root->addr->tlist = root->child->addr->tlist;
return;
//rules where addr of 2nd child passed to parent
case 14:
case 41:
case 43:
case 61:
case 86:
case 87:
case 90:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = root->child->right->addr;
return;
//special (taking from 4th child)
case 5:
case 6:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = root->child->right->right->right->right->addr;
return;
//rules giving eps
case 3:
case 7:
case 15:
case 18:
case 23:
case 25:
case 28:
case 30:
case 39:
case 42:
case 47:
case 54:
case 71:
case 88:
case 91:
return;
//mainFunction ===> TK_MAIN stmts TK_END
case 1:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->child = root->child->right->addr;
return;
//program ===> otherFunctions mainFunction
case 0:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root);
/* root->addr->child = root->child->addr;
if(root->addr->child == NULL){ //otherfuns is null
root->addr->child = root->child->right->addr;
}
else{ //otherfuns is not null
root->addr->child->right = root->child->right->addr;
} */
//otherfuns node
root->addr->child = createAstNode(root->child);
root->addr->child->child = root->child->addr;
//mainfun node
root->addr->child->right = root->child->right->addr;
// root->addr->child->right->child = ;
return;
//otherFunctions(1) ===> function otherFunctions(2)
case 2:
//typeDefinitions(1) ===> typeDefinition typeDefinitions(2)
case 17:
//declarations(1) ===> declaration declarations(2)
case 24:
//otherStmts(1) ===> stmt otherStmts(2)
case 29:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = root->child->addr;
root->addr->right = root->child->right->addr;
return;
//moreFields(1) ===> fieldDefinition moreFields(2)
case 22:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = root->child->addr;
root->addr->right = root->child->right->addr;
if(root->child->right->addr == NULL){
root->addr->tlist->next = NULL;
}
else{
root->addr->tlist->next = root->child->right->addr->tlist;
}
return;
//function ===> TK_FUNID input_par output_par TK_SEM stmts TK_END
case 4:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
//FUNID node
root->addr = createAstNode(root->child);
//input_par node
root->addr->child = createAstNode(root->child->right);
root->addr->child->child = root->child->right->addr;
//output_par node
root->addr->child->right = createAstNode(root->child->right->right);
root->addr->child->right->child = root->child->right->right->addr;
//stmts node
root->addr->child->right->right = root->child->right->right->right->right->addr;
return;
//parameter_list ===> dataType TK_ID remaining_list ///datatype to be handled
case 8:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child->right);
root->addr->tlist = (TypeList) malloc(sizeof(struct typeList)); //assign type
switch (root->child->addr->s.term_type)
{
case TK_INT:
root->addr->tlist->vtype = intType;
break;
case TK_REAL:
root->addr->tlist->vtype = realType;
break;
case TK_RECORDID:
root->addr->tlist->vtype = recordType;
break;
default:
root->addr->tlist->vtype = errType;
break;
}
//inserAtHead of linked list of types
if(root->child->right->right->addr == NULL){
root->addr->tlist->next = NULL;
}
else{
root->addr->tlist->next = root->child->right->right->addr->tlist;
}
root->addr->right = root->child->right->right->addr;
return;
//stmts ===> typeDefinitions declarations otherStmts returnStmt
case 16:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root);
/* root->addr->child = root->child->addr;
if(root->addr->child == NULL){ //typedefs is null
root->addr->child = root->child->right->addr;
if(root->addr->child == NULL){ //declarations is null
root->addr->child = root->child->right->right->addr;
if(root->addr->child == NULL){ //otherstmts is null
root->addr->child = root->child->right->right->right->addr;
}
else{ //otherstmts is not null
root->addr->child->last->right = root->child->right->right->right->addr;
}
}
else{ //delclarations is not null
root->addr->child->right = root->child->right->right->addr;
if(root->addr->child->right == NULL){ //otherstmts is null
root->addr->child->right = root->child->right->right->right->addr;
}
else{ //otherstms is not null
root->addr->child->right->right = root->child->right->right->right->addr;
}
}
}
else{ //typedefs is not null
root->addr->child->right = root->child->right->addr;
if(root->addr->child->right == NULL){ //declarations is null
root->addr->child->right = root->child->right->right->addr;
if(root->addr->child->right == NULL){ //otherstmts is null
root->addr->child->right = root->child->right->right->right->addr;
}
else{ //otherstmts is not null
root->addr->child->right->right = root->child->right->right->right->addr;
}
}
else{ //delclarations is not null
root->addr->child->right->right = root->child->right->right->addr;
if(root->addr->child->right->right == NULL){ //otherstmts is null
root->addr->child->right->right = root->child->right->right->right->addr;
}
else{ //otherstms is not null
root->addr->child->right->right->right = root->child->right->right->right->addr;
}
}
} */
//typedefs node
root->addr->child = createAstNode(root->child);
root->addr->child->child = root->child->addr;
//declarations node
root->addr->child->right = createAstNode(root->child->right);
root->addr->child->right->child = root->child->right->addr;
//otherstmts node
root->addr->child->right->right = createAstNode(root->child->right->right);
root->addr->child->right->right->child = root->child->right->right->addr;
//returnstmts node
root->addr->child->right->right->right = createAstNode(root->child->right->right->right);
root->addr->child->right->right->right->child = root->child->right->right->right->addr;
return;
//typeDefinition ===> TK_RECORD TK_RECORDID fieldDefinitions TK_ENDRECORD TK_SEM
case 19:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child->right);
root->addr->child = root->child->right->right->addr;
root->addr->tlist = root->child->right->right->addr->tlist; //handling type list
return;
//fieldDefinitions ===> fieldDefinition(1) fieldDefinition(2) moreFields
case 20:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = root->child->addr;
root->addr->right = root->child->right->addr;
root->addr->right->right = root->child->right->right->addr;
//create type linked list using fd etc
root->addr->tlist = root->child->addr->tlist;
root->addr->tlist->next = root->child->right->addr->tlist;
if(root->child->right->right->addr == NULL){
root->addr->tlist->next->next = NULL;
}
else{
root->addr->tlist->next->next = root->child->right->right->addr->tlist;
}
return;
//fieldDefinition ===> TK_TYPE primitiveDatatype TK_COLON TK_FIELDID TK_SEM ///handle datatype!!!
case 21:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child->right->right->right);
root->addr->tlist = (TypeList) malloc(sizeof(struct typeList)); //assign type
strncpy(root->addr->tlist->fieldName, root->addr->value, MAX_LEXEME_SIZE + 1);
root->addr->tlist->next = NULL;
switch (root->child->right->addr->s.term_type)
{
case TK_INT:
root->addr->tlist->vtype = intType;
break;
case TK_REAL:
root->addr->tlist->vtype = realType;
break;
default:
root->addr->tlist->vtype = errType;
break;
}
return;
//declaration ===> TK_TYPE dataType TK_COLON TK_ID global_or_not TK_SEM ///handle type
case 26:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child->right->right->right);
if(root->child->right->right->right->right->rule_id == 27){
root->addr->globalFlg = 1;
}
root->addr->tlist = (TypeList) malloc(sizeof(struct typeList)); //assign type
root->addr->tlist->next = NULL;
switch (root->child->right->addr->s.term_type)
{
case TK_INT:
root->addr->tlist->vtype = intType;
break;
case TK_REAL:
root->addr->tlist->vtype = realType;
break;
/* case TK_RECORDID:
root->addr->tlist->vtype = recordType;
break; */
default:
root->addr->tlist->vtype = errType;
break;
}
return;
//assignmentStmt ===> singleOrRecId TK_ASSIGNOP arithmeticExpression TK_SEM ///handle types
case 36:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child->right);
root->addr->child = root->child->addr;
root->addr->child->right = root->child->right->right->addr;
return;
//singleOrRecId ===> TK_ID new24
case 37:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->child = root->child->right->addr;
return;
//funCallStmt ===> outputParameters TK_CALL TK_FUNID TK_WITH TK_PARAMETERS inputParameters TK_SEM
case 40:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root);
/* root->addr->child = root->child->addr;
if(root->addr->child == NULL){ //outputparams is null
root->addr->child = createAstNode(root->child->right->right);
root->addr->child->right = root->child->right->right->right->right->right->addr;
}
else{ //outputparams is not null
root->addr->child->right = createAstNode(root->child->right->right);
root->addr->child->right->right = root->child->right->right->right->right->right->addr;
} */
//outputparams node
root->addr->child = createAstNode(root->child);
root->addr->child->child = root->child->addr;
//funid node
root->addr->child->right = createAstNode(root->child->right->right);
root->addr->child->right->child = root->child->right->right->addr;
//inputparameters node
root->addr->child->right->right = createAstNode(root->child->right->right->right->right->right);
root->addr->child->right->right->child = root->child->right->right->right->right->right->addr;
return;
//iterativeStmt ===> TK_WHILE TK_OP booleanExpression TK_CL stmt otherStmts TK_ENDWHILE
case 44:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root);
root->addr->child = root->child->right->right->addr;
/* //root->addr->right = root->child->right->right->addr;
root->addr->right->right = root->child->right->right->right->right->addr;
root->addr->right->right->right = root->child->right->right->right->right->right->addr; */
root->addr->child->right = root->child->right->right->right->right->addr;
root->addr->child->right->right = root->child->right->right->right->right->right->addr;
return;
//conditionalStmt ===> TK_IF TK_OP booleanExpression TK_CL TK_THEN stmt otherStmts elsePart
case 45:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
//conditionalStmt node
root->addr = createAstNode(root);
root->addr->child = root->child->right->right->addr;
//then stmts are siblings of boolEx
root->addr->child->right = createAstNode(root->child->right->right->right->right->right);
root->addr->child->right->child = root->child->right->right->right->right->right->addr;
root->addr->child->right->child->right = root->child->right->right->right->right->right->right->addr;
//else statements are also siblings of boolEx
root->addr->child->right->right = root->child->right->right->right->right->right->right->right->addr;
return;
//elsePart ===> TK_ELSE stmt otherStmts TK_ENDIF
case 46:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->child = root->child->right->addr;
root->addr->child->right = root->child->right->right->addr;
return;
//ioStmt ===> TK_READ TK_OP singleOrRecId TK_CL TK_SEM
case 48:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->child = root->child->right->right->addr;
return;
//ioStmt ===> TK_WRITE TK_OP allVar TK_CL TK_SEM
case 49:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->child = root->child->right->right->addr;
return;
//allVar ===> TK_ID newVar
case 50:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->child = root->child->right->addr;
return;
//arithmeticExpression ===> term expPrime
case 55:
//term ===> factor termPrime
case 58:
createAst(temp);
temp->right->inh = temp->addr;
createAst(temp->right);
root->addr = temp->right->syn;
return;
//expPrime(1) ===> lowPrecedenceOperators term expPrime(2)
case 56:
//termPrime(1) ===> highPrecedenceOperators factor termPrime(2)
case 59:
createAst(temp);
createAst(temp->right);
root->child->right->right->inh = temp->addr;
temp->addr->child = root->inh;
temp->addr->child->right = root->child->right->addr;
createAst(root->child->right->right);
root->syn = root->child->right->right->syn;
return;
//expPrime ===> eps
case 57:
//termPrime ===> eps
case 60:
root->syn = root->inh;
return;
//all ===> TK_ID temp
case 69:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->child = root->child->right->addr;
return;
//booleanExpression(1) ===> TK_OP booleanExpression(2) TK_CL logicalOp TK_OP booleanExpression(3) TK_CL
case 72:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = root->child->right->right->right->addr;
root->addr->child = root->child->right->addr;
root->addr->child->right = root->child->right->right->right->right->right->addr;
return;
//booleanExpression ===> var(1) relationalOp var(2)
case 73:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = root->child->right->addr;
root->addr->child = root->child->addr;
root->addr->child->right = root->child->right->right->addr;
return;
//booleanExpression(1) ===> TK_NOT TK_OP booleanExpression(2) TK_CL
case 74:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->child = root->child->right->right->addr;
return;
//idList ===> TK_ID more_ids
case 89:
while(temp!=NULL){
createAst(temp);
temp=temp->right;
}
root->addr = createAstNode(root->child);
root->addr->right = root->child->right->addr;
return;
//Nonterms to a single term
//primitiveDatatype ===> TK_INT
case 11:
//primitiveDatatype ===> TK_REAL
case 12:
//highPrecedenceOperators ===> TK_MUL
case 63:
//highPrecedenceOperators ===> TK_DIV
case 64:
//lowPrecedenceOperators ===> TK_PLUS
case 65:
//lowPrecedenceOperators ===> TK_MINUS
case 66:
//var ===> TK_ID ///symbol table lookup for type?
case 75:
//logicalOp ===> TK_AND
case 78:
//logicalOp ===> TK_OR
case 79:
//relationalOp ===> TK_LT
case 80:
//relationalOp ===> TK_LE
case 81:
//relationalOp ===> TK_EQ
case 82:
//relationalOp ===> TK_GT
case 83:
//relationalOp ===> TK_GE
case 84:
//relationalOp ===> TK_NE
case 85:
root->addr = createAstNode(root->child);
//printf("Test: %s\n", root->addr->value);
return;
//type handled here
//allVar ===> TK_NUM
case 51:
//allVar ===> TK_RNUM
case 52:
//all ===> TK_NUM
case 67:
//all ===> TK_RNUM
case 68:
//var ===> TK_NUM
case 76:
//var ===> TK_RNUM
case 77:
root->addr = createAstNode(root->child);
root->addr->tlist = (TypeList) malloc(sizeof(struct typeList));
root->addr->tlist->next = NULL;
switch (root->child->grammar_node->s.term_type)
{
case TK_NUM:
root->addr->tlist->vtype = intType;
break;
case TK_RNUM:
root->addr->tlist->vtype = realType;
default:
root->addr->tlist->vtype = errType;
break;
}
//printf("Test: %s\n", root->addr->value);
return;
//2nd child terminal
//constructedDatatype ===> TK_RECORD TK_RECORDID ///symbol table lookup for type?
case 13:
//global_or_not ===> TK_COLON TK_GLOBAL
case 27:
root->addr = createAstNode(root->child->right);
return;
//new24 ===> TK_DOT TK_FIELDID ///symbol table lookup for type?
case 38:
//temp ===> TK_DOT TK_FIELDID
case 70:
//newVar ===> TK_DOT TK_FIELDID
case 53:
root->addr = createAstNode(root->child->right);
//root->addr->tlist = createTypeList();
return;
}
}
void printAst(AstNode root){
if(root == NULL){
return;
}
if(root->t == NON_TERM){
printf("Non-term: %s\n", map_nonterminals_to_strings[root->s.nonterm_type]);
}
else{
if(root->tlist != NULL){
printf("Term: %s\tValue: %s, Type: %d\n", map_terminals_to_strings[root->s.term_type], root->value, root->tlist->vtype);
}
else{
printf("Term: %s\tValue: %s, Type: NULL\n", map_terminals_to_strings[root->s.term_type], root->value);
}
}
AstNode temp = root->child;
while(temp!=NULL){
printAst(temp);
temp = temp->right;
}
}