-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymbolTable.c
527 lines (450 loc) · 11.8 KB
/
symbolTable.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
/*
-=-=-=-=-=-=-=-=-=-=-=-=-=
BATCH 26
-=-=-=-=-=-=-=-=-=-=-=-=-=
AAYUSH AHUJA 2010A7PS023P
MAYANK GUPTA 2010A7PS022P
-=-=-=-=-=-=-=-=-=-=-=-=-=
symbolTable.c
-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#include<fcntl.h>
#include"lexerDef.h"
#include"lexer.h"
#include"parserDef.h"
#include"parser.h"
#include"symbolTable.h"
#include"semantic.h"
extern bool any_error;
void insertft(variable GT[],funTable FT[],char *fname, symbol type, int recindex,char *name,int ion , int *offset)
{
if(checkMultigtN(GT,recindex,name) && checkMultiftN(FT,fname,recindex,name))
{
printf("ERROR: Variable %s being a global variable, cannot be declared more than once\n", name);
any_error=1;
return;
}
if(checkMultigtN(GT,recindex,name))
return;
int hval,hval2,hkey=100;
struct paralist *curr;
struct paralist *in = (struct paralist*)malloc(sizeof(struct paralist));
variable t;
//printf("adding to function %s:%s\n",fname,name);
hval = hash(fname,hkey);
strcpy(FT[hval].fname,fname);
t.type = type;
t.recindex = -1;
strcpy(t.name,name);
t.ion = ion;
t.offset = *offset;
in->item = t;
in->next = NULL;
if(ion == 0)
{
curr = FT[hval].inputList;
if(curr == NULL)
FT[hval].inputList = in;
else
{
//curr = FT[hval].inputList;
while(curr->next!=NULL)
curr = curr->next;
curr->next = in;
}
}
if(ion == 1)
{
if(FT[hval].outputList==NULL)
FT[hval].outputList = in;
else
{
curr = FT[hval].outputList;
while(curr->next!=NULL)
curr = curr->next;
curr->next = in;
}
}
hval2 = hash(name,hkey);
strcpy(FT[hval].table[hval2].name,name);
FT[hval].table[hval2].recindex = recindex;
FT[hval].table[hval2].type = type;
FT[hval].table[hval2].ion = ion;
FT[hval].table[hval2].offset = *offset;
FT[hval].table[hval2].filled = 1;
FT[hval].filled = 1;
}
//RT,temp2->next[1]->t->lexeme,temp3->next[1]->t->s,temp3->next[2]->t->lexeme
void insertrt(recTable RT[],char *rname, symbol type, char *name)
{
if(checkMultirt(RT,rname,name))
{
return;
}
int hval,hval2,hkey=100;
//printf("adding to record %s:%s\n",rname,name);
hval = hash(rname,hkey);
strcpy(RT[hval].rname,rname);
if(type == TK_INT)
{
strcat(RT[hval].str,(" int"));
RT[hval].size+=2;
}
if(type == TK_REAL)
{
strcat(RT[hval].str,(" real"));
RT[hval].size+=4;
}
hval2 = hash(name,hkey);
strcpy(RT[hval].table[hval2].name,name);
RT[hval].table[hval2].type = type;
RT[hval].table[hval2].filled = 1;
RT[hval].filled = 1;
}
//insert(GT,temp2->next[1]->t->s,temp2->next[2]->t->lexeme)
void insertgt(variable GT[],symbol type, int recindex, char *name)
{
if(checkMultigt(GT,recindex,name))
{
return;
}
int hval,hkey=100;
//printf("adding to global %s\n",name);
hval = hash(name,hkey);
strcpy(GT[hval].name,name);
GT[hval].recindex = recindex;
GT[hval].type = type;
GT[hval].filled = 1;
}
void initSymbolTable(variable GT[], funTable FT[], recTable RT[])
{
int i,j;
for(i=0;i<100;i++)
{
GT[i].filled = 0;
GT[i].recindex = -1;
RT[i].filled = 0;
RT[i].str[0] = '\0';
RT[i].size = 0;
FT[i].filled = 0;
FT[i].inputList = NULL;
FT[i].outputList = NULL;
for(j=0;j<100;j++)
{
FT[i].table[j].filled = 0;
RT[i].table[j].filled = 0;
RT[i].table[j].recindex = -1;
FT[i].table[j].recindex = -1;
}
}
}
void printFT(funTable FT[], recTable RT[])
{
int i,j;
//printf("FUNCTION NAMES:\n");
for(i=0;i<100;i++)
{
if(FT[i].filled == 1)
{
//printf("function:%s\n",FT[i].fname);
for(j=0;j<100;j++)
if(FT[i].table[j].filled == 1)
{
if(FT[i].table[j].type == TK_RECORD)
{
//RT[FT[i].table[j].recindex].str
printf("%-20s%-30s%-20s%-5d\n",FT[i].table[j].name,RT[FT[i].table[j].recindex].str,FT[i].fname,FT[i].table[j].offset);
}
else if (FT[i].table[j].type == TK_INT)
{
printf("%-20s%-30s%-20s%-5d\n",FT[i].table[j].name,"int",FT[i].fname,FT[i].table[j].offset);
}
else if (FT[i].table[j].type == TK_REAL)
{
printf("%-20s%-30s%-20s%-5d\n",FT[i].table[j].name,"real",FT[i].fname,FT[i].table[j].offset);
}
//printf("%-30s %-30s %d %d\n",toStr(FT[i].table[j].type),FT[i].table[j].name,FT[i].table[j].offset,FT[i].table[j].ion);
}
}
}
}
void printGT(variable GT[],recTable RT[])
{
int i,j;
//printf("GLOBAL NAMES:\n");
for(i=0;i<100;i++)
{
if(GT[i].filled == 1)
{
if(GT[i].type == TK_RECORD)
{
//RT[GT[i].table[j].recindex].str
printf("%-20s%-30s%-20s%-5s\n",GT[i].name,RT[GT[i].recindex].str,"global","-");
}
else if (GT[i].type == TK_INT)
{
printf("%-20s%-30s%-20s%-5s\n",GT[i].name,"int","global","-");
}
else if (GT[i].type == TK_REAL)
{
printf("%-20s%-30s%-20s%-5s\n",GT[i].name,"real","global","-");
}
}
}
}
void createInputParameterTable(variable GT[],funTable FT[],recTable RT[],parseTree A,char *fname, int *offset)
{
parseTree temp;
int i,j;
//printf("INPUT PARS A = %s\n",toStr(A->t->s));
if(A->t->s == TK_INT)
{
temp = A->parent; //temp = parameter_list
//void insertft(funTable FT[],char *fname, symbol type, int recindex,char *name,int ion , int *offset)
insertft(GT,FT,fname,temp->next[0]->t->s,-1,temp->next[1]->t->lexeme,0,offset);
*offset = *offset + 2;
}
else if(A->t->s == TK_REAL)
{
temp = A->parent; //temp = parameter_list
//void insertft(GT,funTable FT[],char *fname, symbol type, int recindex,char *name,int ion , int *offset)
insertft(GT,FT,fname,temp->next[0]->t->s,-1,temp->next[1]->t->lexeme,0,offset);
*offset = *offset + 4;
}
else if(A->t->s == TK_RECORD)
{
temp = A->parent->parent; //parameter_list>(constructed_datatypes>(record id & tk_record) == tk_id)
int hkey = 100,recindex;
recindex = hash(temp->next[0]->next[1]->t->lexeme,hkey); // = tk_recordid
insertft(GT,FT,fname,temp->next[0]->next[0]->t->s,recindex,temp->next[1]->t->lexeme,0,offset);
*offset = *offset + RT[recindex].size;
}
else
{
for(i=0;A->next[i]!=NULL;i++)
{
temp = A->next[i];
createInputParameterTable(GT,FT,RT,temp,fname,offset);
}
}
}
void createOutputParameterTable(variable GT[],funTable FT[],recTable RT[],parseTree A,char *fname, int *offset)
{
parseTree temp;
int i,j;
//printf("OUTPUT PARS A = %s\n",toStr(A->t->s));
if(A->t->s == TK_INT)
{
temp = A->parent; //temp = parameter_list
//void insertft(GT,funTable FT[],char *fname, symbol type, int recindex,char *name,int ion , int *offset)
insertft(GT,FT,fname,temp->next[0]->t->s,-1,temp->next[1]->t->lexeme,1,offset);
*offset = *offset + 2;
}
else if(A->t->s == TK_REAL)
{
temp = A->parent; //temp = parameter_list
//void insertft(GT,funTable FT[],char *fname, symbol type, int recindex,char *name,int ion , int *offset)
insertft(GT,FT,fname,temp->next[0]->t->s,-1,temp->next[1]->t->lexeme,1,offset);
*offset = *offset + 4;
}
else if(A->t->s == TK_RECORD)
{
temp = A->parent->parent; //parameter_list>(constructed_datatypes>(record id & tk_record) == tk_id)
int hkey = 100,recindex;
recindex = hash(temp->next[0]->next[1]->t->lexeme,hkey); // = tk_recordid
insertft(GT,FT,fname,temp->next[0]->next[0]->t->s,recindex,temp->next[1]->t->lexeme,1,offset);
*offset = *offset + RT[recindex].size;
}
else
{
for(i=0;A->next[i]!=NULL;i++)
{
temp = A->next[i];
createOutputParameterTable(GT,FT,RT,temp,fname,offset);
}
}
}
createFieldRecordsTable(recTable RT[], parseTree A, char *rname)
{
int i,j;
parseTree temp;
//printf("FILED REC A = %s\n",toStr(A->t->s));
if(A->t->s ==TK_TYPE)
{
temp = A->parent;
//void insertrt(recTable RT[],char *rname, symbol type, char *name)
insertrt(RT,rname,temp->next[1]->t->s,temp->next[2]->t->lexeme);
}
else
{
for(i=0;A->next[i]!=NULL;i++)
{
temp = A->next[i];
createFieldRecordsTable(RT,temp,rname);
}
}
}
void createFunctionDeclareTable(variable GT[],funTable FT[],recTable RT[],parseTree A,char * fname,int *offset)
{
parseTree temp;
int i,j;
//printf("FUNCTION DECLARATIONS A = %s\n",toStr(A->t->s));
if(A->t->s == TK_INT)
{
temp = A->parent; //temp = declaration
//void insertft(GT,funTable FT[],char *fname, symbol type, int recindex,char *name,int ion , int *offset)
insertft(GT,FT,fname,temp->next[1]->t->s,-1,temp->next[2]->t->lexeme,2,offset);
*offset = *offset + 2;
}
else if(A->t->s == TK_REAL)
{
temp = A->parent; //temp = declaration
//void insertft(GT,funTable FT[],char *fname, symbol type, int recindex,char *name,int ion , int *offset)
insertft(GT,FT,fname,temp->next[1]->t->s,-1,temp->next[2]->t->lexeme,2,offset);
*offset = *offset + 4;
}
else if(A->t->s == TK_RECORD)
{
temp = A->parent->parent; //declarartion>tk_type == (constructed_datatypes>(record id & tk_record) == tk_id)
int hkey = 100,recindex;
recindex = hash(temp->next[1]->next[1]->t->lexeme,hkey); // = tk_recordid
insertft(GT,FT,fname,temp->next[1]->next[0]->t->s,recindex,temp->next[2]->t->lexeme,2,offset);
*offset = *offset + RT[recindex].size;
}
else
{
for(j=0;A->next[j]!=NULL;j++)
{
temp = A->next[j];
createFunctionDeclareTable(GT,FT,RT,temp,fname,offset);
}
}
}
void createFunctionTable(variable GT[], funTable FT[], recTable RT[],parseTree A, char * fname, int *offset)
{
if(A->t->s == TK_FUNID)
if(checkMultiFun(FT, fname))
return;
parseTree temp;
int i,j;
//int offset = 1;
//printf("FUNCTION TABLE A = %s\n",toStr(A->t->s));
if(A->t->s == TK_INPUT)
{
//printf("INPUT = %s\n",toStr(A->t->s));
temp = A->parent; //parent of tk_ input = input_par
createInputParameterTable(GT,FT,RT,temp,fname,offset);
}
else if(A->t->s == TK_OUTPUT) ////parent of tk_output = output_par
{
temp = A->parent;
createOutputParameterTable(GT,FT,RT,temp,fname,offset);
}
else if(A->t->s == declaration)
{
temp = A; //parent of tk_type = declaration
createFunctionDeclareTable(GT,FT,RT,temp,fname,offset);
}
else
{
for(i=0;A->next[i]!=NULL;i++)
{
temp = A->next[i];
createFunctionTable(GT,FT,RT,temp,fname,offset);
}
}
}
void createMainFunctionTable(variable GT[], funTable FT[], recTable RT[], parseTree A,int *offset)
{
parseTree temp;
int i,j;
//printf("MAIN FUNCTION TABLE A = %s\n",toStr(A->t->s));
char fname[] = "_main";
//int offset;
if(A->t->s == declaration)
{
temp = A; // declaration
createFunctionDeclareTable(GT,FT,RT,temp,fname,offset);
}
else
{
for(i=0;A->next[i]!=NULL;i++)
{
temp = A->next[i];
createMainFunctionTable(GT,FT,RT,temp,offset);
}
}
}
void createSymbolTable(variable GT[], funTable FT[], recTable RT[],parseTree A)
{
parseTree temp;
int i,j,k;
//printf("SYMBOL TABLE A = %s\n",toStr(A->t->s));
if(A->t->s == TK_FUNID)
{
temp = A->parent; //parent of funid = function
int offset = 0;
createFunctionTable(GT,FT,RT,temp,A->t->lexeme,&offset);
}
else if(A->t->s == TK_MAIN)
{
temp = A->parent;
int offset = 0;
createMainFunctionTable(GT,FT,RT,temp,&offset);
}
else
{
for(i=0;A->next[i]!=NULL;i++)
{
temp = A->next[i];
createSymbolTable(GT,FT,RT,temp);
}
}
}
createRecordTable(recTable RT[], parseTree A)
{
parseTree temp;
int i,j;
if(A->t->s == typedefinition)
{
temp = A;
createFieldRecordsTable(RT,temp,temp->next[1]->t->lexeme);
}
else
for(i=0;A->next[i]!=NULL;i++)
{
temp = A->next[i];
createRecordTable(RT,temp);
}
}
createGlobalTable(variable GT[],parseTree A)
{
parseTree temp;
int i,j;
if(A->t->s == TK_GLOBAL)
{
temp = A->parent; //parent = declaration
if(temp->next[1]->t->s == TK_INT || temp->next[1]->t->s == TK_REAL)
{
//void insertgt(variable GT[],symbol type, int recindex, char *name)
insertgt(GT,temp->next[1]->t->s,-1,temp->next[2]->t->lexeme);
}
else
{
//if TK_RECORD
int recindex,hkey = 100;
recindex = hash(temp->next[1]->next[1]->t->lexeme,hkey); //temp->next[1]= constructed_datatype
insertgt(GT,temp->next[1]->next[0]->t->s,recindex,temp->next[2]->t->lexeme);
}
}
else
for(i=0;A->next[i]!=NULL;i++)
{
temp = A->next[i];
createGlobalTable(GT,temp);
}
}