-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbol-table.c
686 lines (587 loc) · 21 KB
/
symbol-table.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
///////////////////////////////////////
// Author: Vishal Golcha
// Id: 2014B5A70717P
///////////////////////////////////////
#include "symboltable.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
sym_node * sroot;
sym_node * scur;
int sub1,div1,add1,mul1,mat1,row1,col1,func1;
void var_init(){
mul1 = get_symb_no("MUL");
div1 = get_symb_no("DIV");
add1 = get_symb_no("PLUS");
sub1 = get_symb_no("MINUS");
mat1 = get_symb_no("MATRIX");
row1=0;
col1=0;
func1=get_symb_no("FUNCTION");
}
sym_node* create_sym_node(){
sym_node* x= (sym_node*)malloc(sizeof(sym_node));
x->htlink = create_hashtable_symtable(30);
x->parent = NULL;
x->child=NULL;
x->sibling=NULL;
x->scope_name= (char *)malloc(sizeof(char)*50);
return x;
}
typedef struct llh_pair{
llhnode * x,*y;
}llhnode_pair;
llhnode_pair* llhnode_find_sym_htable(htable_sym* z,tnode *y){
unsigned long h = hash(symb_name[y->symb_no]);
int x = h%(z->lcnt);
// printf("hash value %d\n",x );
hnode *t = (z->hlist[x]).head;
while(t!=NULL){
// printf("%d\n",y->val);
if(strcmp(t->lexeme,y->lexeme)==0 \
&& t->symb_no ==y->symb_no){
llhnode_pair* temp = (llhnode_pair*)malloc(sizeof(llhnode_pair));
temp->x=t->inpt;
temp->y=t->outt;
return temp ; // retrieve this and index-1 on calls
}
t=t->next;
}
return NULL;
}
llhnode_pair *repeated_find_get_list(tnode *y){
sym_node * x = scur;
while(x!=NULL){
llhnode_pair * res = llhnode_find_sym_htable(x->htlink,y);
if(res!=NULL){
return res;
}
else{
x=x->parent;
}
}
return NULL;
}
char * repeated_find(tnode *y){
sym_node * x = scur;
while(x!=NULL){
char * res = find_sym_htable(x->htlink,y);
if(res!=NULL){
return res;
}
else{
x=x->parent;
}
}
return NULL;
}
// used to check it it exists in the hash table
// if it does then return matrix's row and column
pair * repeated_find_retrieve_rc_(tnode *y){
sym_node * x = scur;
while(x!=NULL){
pair * res = retrieve_hash_table_rc(scur->htlink,y);
if(res!=NULL){
return res;
}
else{
x=x->parent;
}
}
return NULL;
}
int repeated_find_modify_rc_(tnode *y,pair *k){
sym_node * x = scur;
while(x!=NULL){
int res= modify_hash_table_rc(scur->htlink,y,k);
if(res!=0){
return res;
}
else{
x=x->parent;
}
}
return 0;
}
pair* matrix_size_check(tnode *y){
tnode * x = y;
tnode *xc = y->child;
int c = 0;
int r = 0;
// printf("issue here 1\n");
pair * np = (pair *)malloc(sizeof(pair));
while(xc!=NULL){
c++;
xc=xc->sibling;
}
// printf("issue here 2\n");
while(x!=NULL){
r++;
tnode *t = x->child;
int temp=0;
while(t!=NULL){
temp++;
t=t->sibling;
}
if(temp!=c){
np->c=-1;
np->r=-1;
return np;
}
x=x->sibling;
}
// printf("issue here 3\n");
np->r=r;
np->c=c;
return np;
}
// in case you find a proper matching rhs you have to assign / modify the value of
// matrix's number of columns in the hashtable
int mat_rhs_traversal_helper(char* typ,tnode *x,char * ac1,char* ac2){
// ensure the passed pointer is not ArithExp
// printf("in here %s \n",symb_name[x->symb_no]);
int a=1,b=1,c=1;
if(x->child!=NULL){
a = mat_rhs_traversal_helper(typ,x->child,ac1,ac2);
}
int y =x->symb_no;
char *z = symb_name[x->symb_no];
if(y==mul1 || y==add1 || y==sub1 || y==div1){
if(y==mul1 || y==div1){
printf("Error on line no %d",x->line_num);
printf("matrix division or multiplication is not allowed \n");
}
b=1;
}
else if(strcmp(z,ac1)==0){
// look for the id in the symbol table
char * j=repeated_find(x);
pair * k=repeated_find_retrieve_rc_(x);
if(j==NULL){
printf("Error on line %d undeclared variable %s \n",x->line_num,x->lexeme);
// printf("here\n");
}
else{
// k cant be NULL since we found the id in the hash
if(strcmp(j,typ)==0){
b=1;
if(k->r==-1){
printf("Error on line %d in %s",x->line_num,x->lexeme);
printf("unknown matrix size , invalidates computaion ,\
please assign the matrix variable a value .\n");
b=0;
}
else if(row1==0 && col1==0){
// assign row value and col to this global variable
row1=k->r;
col1=k->c;
}
else if(row1!=k->r || col1!=k->c){
printf("Error on line %d in %s",x->line_num,x->lexeme);
printf("dissimilar matrix sizes for computation \n");
b=0;
}
}
else{
b=0;
printf("Error on line %d in %s",x->line_num,x->lexeme);
printf(" incompatible type addition \n");
}
}
}
else if(strcmp(z,ac2)==0){
// the key matches to that of arithmetic expression hence the child contains a matrix
// get the size of the matrix below
b=1;
pair *k =matrix_size_check(x->child); // this row->row->row with their respective col kids below
// printf("m size %d %d \n",k->c,k->r);
// printf("segment2\n");
if(k->r==-1 || k->c==-1){
printf("Error on line %d ",x->child->line_num);
printf("non uniform matrix encountered in computation evry row should have same number of cols\n");
b=0;
}
else if(row1==0 && col1==0){
row1 = k->r;
col1 = k->c;
}
else if(row1!=k->r || col1!=k->c){
printf("Error on line %d ",x->child->line_num);
printf("dissimilar matrix types in computation\n");
b=0;
}
}
else{
if(strcmp(z,"<row>")==0 || strcmp(z,"NUM")==0){
b=1;
}
else{
b=0;
printf("we hit this\n");
}
}
if(x->child!=NULL){
tnode *k = x->child->sibling;
while(k!=NULL){
c=c*mat_rhs_traversal_helper(typ,k,ac1,ac2);
k=k->sibling;
}
}
return a*b*c;
}
int rhs_traversal_helper(char* typ,tnode *x,char * ac1,char* ac2){
// ensure the passed pointer is not rows
int a=1,b=1,c=1;
if(x->child!=NULL){
a = rhs_traversal_helper(typ,x->child,ac1,ac2);
}
int y =x->symb_no;
char *z = symb_name[x->symb_no];
if(y==mul1 || y==add1 || y==sub1 || y==div1){
b=1;
}
else if(strcmp(z,ac1)==0){
// look for the id in the symbol table
char * j=repeated_find(x);
if(j==NULL){
printf("Error on line %d undeclared variable %s \n",x->line_num,x->lexeme);
printf("here\n");
}
else{
if(strcmp(j,typ)==0){
b=1;
}
else{
b=0;
}
}
}
else if(strcmp(z,ac2)==0){
b=1;
}
else{
b=0;
}
if(x->child!=NULL){
tnode *k = x->child->sibling;
while(k!=NULL){
c=c*rhs_traversal_helper(typ,k,ac1,ac2);
k=k->sibling;
}
}
return a*b*c;
}
int non_matrix_rhs_traverse(char *typ,tnode *rhs){
char *ac1 = (char*)malloc(sizeof(char)*30);
char *ac2 = (char*)malloc(sizeof(char)*30);
if(strcmp(typ,"INT")==0){
strcpy(ac1,"ID");
strcpy(ac2,"NUM");
}
else if(strcmp(typ,"REAL")==0){
strcpy(ac1,"ID");
strcpy(ac2,"RNUM");
}
else if(strcmp(typ,"STRING")==0){
strcpy(ac1,"ID");
strcpy(ac2,"STR");
}
// printf("in here\n");
// printf("%s %s",ac1,ac2);
int ch = rhs_traversal_helper(typ,rhs,ac1,ac2);
return ch;
}
/**************************************************************************************************/
/**************************************************************************************************/
void traverse_and_construct(tnode* x){
if(x->child!=NULL)
traverse_and_construct(x->child);
// printf("%s \n",x->lexeme);
int dec_stm = get_symb_no("<declarationStmt>");
/******************* declaration contruct insertions *********************************************/
/************************************************************************************************/
if(x->symb_no==dec_stm){
// printf("in dec \n");
// there was an error here
// could get erroneous check thorughly
char * typ = symb_name[x->child->child->symb_no];
tnode *list = x->child->sibling->child;
while(list!=NULL){
char* check =repeated_find(list);
if(check!=NULL){
// printf("here\n");
printf("Error on line %d redeclared variable %s \n",list->line_num,symb_name[list->symb_no]);
}
else{
// printf("inserted\n");
scur->htlink=insert_sym_htable(scur->htlink,list,typ,NULL,NULL);
}
list=list->sibling;
}
}
/******************************* Function insertions ******************************************/
/************************************************************************************************/
// accumulates types and ids as different linked lists of hnodes
if(x->symb_no == func1){
// FUNCTION ->paralist->=->FUNID->paralist-><stmtsAndFunctionDefs>
// paralist needs <type's> kid <type> followed by <id> and so on .
llhnode * list1_types = (llhnode*)malloc(sizeof(llhnode));
llhnode * list2_ids = (llhnode*)malloc(sizeof(llhnode));
// pointer declarations from the heap help in populating inptypes and outypes
tnode * out_temp = x->sibling->child;
// printf("%s \n",symb_name[out_temp->sibling->symb_no]);
int cnt1=0;
while(out_temp!=NULL){
if(cnt1%2==0){
list1_types = hnode_attach(list1_types,out_temp->child,\
symb_name[out_temp->child->symb_no],NULL,NULL);
}
else{
list2_ids = hnode_attach(list2_ids,out_temp,\
symb_name[out_temp->symb_no],NULL,NULL);
}
cnt1++;
out_temp=out_temp->sibling;
}
char *func_name = x->sibling->sibling->sibling->lexeme;
tnode * func_node = x->sibling->sibling->sibling;
tnode * inp_temp = x->sibling->sibling->sibling->sibling->child;
// printf("%s \n",symb_name[inp_temp->symb_no]);
llhnode * list1_types2 = (llhnode*)malloc(sizeof(llhnode));
llhnode * list2_ids2 = (llhnode*)malloc(sizeof(llhnode));
cnt1=0;
while(inp_temp!=NULL){
if(cnt1%2==0){
// printf("in here\n");
// printf("%s \n",symb_name[inp_temp->child->symb_no]);
list1_types2 = hnode_attach(list1_types2,inp_temp->child,\
symb_name[inp_temp->child->symb_no],NULL,NULL);
}
else{
list2_ids2 = hnode_attach(list2_ids2,inp_temp,\
symb_name[inp_temp->symb_no],NULL,NULL);
}
cnt1++;
inp_temp=inp_temp->sibling;
}
// printf("done \n");
sym_node *tr;
if(scur->child==NULL){
sym_node * tempp = create_sym_node();
// printf("in here \n");
tempp->scope_name=func_name;
// printf("in here 3\n");
tempp->parent=scur;
scur->child=tempp;
// printf("in here 3\n");
tr =tempp;
}
else{
tr = scur->child;
while(tr->sibling!=NULL){
tr=tr->sibling;
}
sym_node * tempp = create_sym_node();
tempp->scope_name=func_name;
tempp->parent=scur;
tr->sibling = tempp;
tr =tr->sibling;
}
// printf("done2\n");
// hnode_llst_traverse(list2_ids2);
/*******************************************************************/
/******************* put function details inside current htable*****/
// need an if find construct here also
if(find_sym_htable(scur->htlink,func_node)==NULL){
// printf("%s\n",symb_name[func_node->symb_no]);
scur->htlink=insert_sym_htable(scur->htlink,func_node,"FUNID",\
list1_types,list1_types2);
}
else{
printf("Error on line %d func redefinition function already exists in the scope \n",\
func_node->line_num);
}
scur=tr;
// printf("inserted \n");
// change scope
hnode* p1 = list2_ids->head;
hnode* p2 = list1_types->head;
// now we need to insert these parameter variables into the current hashtable
// printf("poop\n");
// printf("%s ",scur->scope_name);
while(p1!=NULL){
scur->htlink = insert_sym_htable(scur->htlink,p1->ele,symb_name[p2->symb_no]\
,NULL,NULL);
p1=p1->next;
p2=p2->next;
}
// printf("done3");
p1 = list2_ids2->head;
p2 = list1_types2->head;
while(p1!=NULL){
scur->htlink = insert_sym_htable(scur->htlink,p1->ele,symb_name[p2->symb_no]\
,NULL,NULL);
p1=p1->next;
p2=p2->next;
}
}
if(x->symb_no == get_symb_no("END") && scur->parent!=NULL){
// printf("hitting the end of life \n");
scur= scur->parent;
// printf("%s",scur->scope_name);
}
/*****************************************************************************************/
/***********************************Handling func calls***********************************/
/***********************************and LHS multi list ***********************************/
// lhs has varlist below which has list
if(x->symb_no==get_symb_no("<leftHandSide_listVar>")){
tnode * vtyp_list = x->child->child;
llhnode * list1 = (llhnode*)malloc(sizeof(llhnode));
llst_str* list1_types = (llst_str*)malloc(sizeof(llst_str));
int lflag=1;
while(vtyp_list!=NULL){
char * temp_typ =repeated_find(vtyp_list);
if(temp_typ==NULL){
printf("Error on line num %d in var %s",vtyp_list->line_num,vtyp_list->lexeme);
printf("Not declared before \n");
*list1_types = attach_str(*list1_types," ",0);
}
else{
*list1_types = attach_str(*list1_types,temp_typ,0);
}
list1= hnode_attach(list1,vtyp_list,\
symb_name[vtyp_list->symb_no],NULL,NULL);
vtyp_list=vtyp_list->sibling;
}
tnode *fun_list = x->sibling->sibling->child;
llhnode_pair * func_pair;
llhnode *list2;
llst_str *list2_types;
if(fun_list->symb_no = get_symb_no("FUNID")){
func_pair = repeated_find_get_list(fun_list);
if(func_pair==NULL){
printf("Error on line no %d ",fun_list->line_num);
printf("function not declared before use .");
// printf("%s\n",scur->scope_name);
}
/*
// the following block was being used for matchine variale types in functions but
// gave seg faults in the end
else{
// accumulate input variables also
list2 = (llhnode*)malloc(sizeof(llhnode));
list2_types = (llst_str*)malloc(sizeof(llst_str));
int lflag=1;
tnode * ftyp_list = fun_list->sibling->child;
printf("%s",symb_name[ftyp_list->symb_no]);
while(ftyp_list!=NULL){
char * temp_typ =repeated_find(ftyp_list);
if(temp_typ==NULL){
printf("Error on line num %d in var %s",ftyp_list->line_num,ftyp_list->lexeme);
printf("Not declared before \n");
*list2_types = attach_str(*list2_types," ",0);
}
else{
*list2_types = attach_str(*list2_types,temp_typ,0);
}
list2= hnode_attach(list2,ftyp_list,\
symb_name[ftyp_list->symb_no],NULL,NULL);
ftyp_list=ftyp_list->sibling;
}
// printf("in here\n");
// input parameters check
if(func_pair->y->cnt == list2->cnt){
printf("Error on line num %d in var %s",ftyp_list->line_num,ftyp_list->lexeme);
printf("Incorrect number of parameters \n");
}
else{
str_node *tr2;
hnode * tr1 ;
tr1 = func_pair->y->head;
tr2 = list2_types->head;
while(tr1!=NULL){
if(strcmp(tr1->lexeme,tr2->str_val)==0){
;
}
else{
printf("Error on line num %d in var %s",ftyp_list->line_num,ftyp_list->lexeme);
printf(" parameter type mismatch \n");
}
tr1=tr1->next;
tr2=tr2->next;
}
}
}*/
}
}
/*******************************LHS 1 = RHS1 semantic checks*******************************/
/******************************************************************************************/
int rhs1 = get_symb_no("<rightHandSide_type1>");
int lhs1 = get_symb_no("<leftHandSide_singleVar>");
// char * typ_lhs = repeated_find(lhs);
// if(typ_lhs==NULL){
// printf("lhs not declared before");
// } use this to find lhs type and divert ways for matrix and nm
if(x->symb_no==lhs1){
char* check = repeated_find(x->child);
// printf("check print %s",check);
if(x->sibling->sibling->child->symb_no != get_symb_no("FUNID")\
|| x->sibling->sibling->child->symb_no != get_symb_no("SIZE")){
if(check==NULL){
// printf("here\n");
printf("Error on line %d undeclared variable %s \n",x->child->line_num,\
x->child->lexeme);
}
else if(strcmp(check,"MATRIX")!=0){
int ch2=non_matrix_rhs_traverse(check,x->sibling->sibling->child);
if(ch2==0){
printf("non - similar data types in computation on line %d \n",x->line_num);
}
}
else{
// ast constructed such that row variable has a parent rows else
// rows would not be in the ast;
// printf("%s \n",symb_name[x->sibling->sibling->child->symb_no]);
int ch2 = mat_rhs_traversal_helper(check,x->sibling->sibling->child,"ID","<rows>");
if(ch2==1){
// modify hashtable to add sizes to the matrix;
// printf("computed \n");
pair * kk = (pair*)malloc(sizeof(pair));
kk->r = row1;
kk->c = col1;
repeated_find_modify_rc_(x->child,kk);
printf("matrix size assigned to %s \n",x->child->lexeme);
row1=0;
col1=0;
}
else{
printf("Error on line %d variable could note be computed because of thr above erros %s\
\n",x->child->line_num,\
x->child->lexeme);
}
}
}
}
tnode * z = x->child;
if(z!=NULL){
tnode *y = z->sibling;
while(y!=NULL){
traverse_and_construct(y);
y=y->sibling;
}
}
}
void make_sym_table(tnode *root){
sroot= create_sym_node();
strcpy(sroot->scope_name,"MAIN");
scur=sroot;
traverse_and_construct(root);
}
// int main(){
// ast_constructer("add.txt","addout.txt");
// printf("******************************\n");
// // printf("%s\n",symb_name[ptree->child->symb_no]);
// // printf("******************************\n");
// var_init();
// make_sym_table(ptree);
// }