-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmytable.h
409 lines (325 loc) · 6.98 KB
/
mytable.h
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
#include<string.h>
int Index = 0;
struct symtab {
char name[10];
int value;
int scope;
int active;
char *type;
int isfunc;
int isdef;
char params[10];
int arrdim;
struct symtab *next;
};
struct stack{
char *items[10];
int top;
};
struct stack Stk = {.top = -1};
int label[10],ltop=0,lno = 0;
void push(char *ip){
if(Stk.top == 10)
{ printf("\n stack full");
return;
}
Stk.top++;
Stk.items[Stk.top] = (char *)malloc(strlen(ip) + 1);
strcpy(Stk.items[Stk.top],ip);
}
void dispStk(){
int i;
for(i = Stk.top-1;i>=0;i--)
printf("\n %s",Stk.items[i]);
}
char *pop()
{
int i;
if(Stk.top==-1)
{
printf("\nStack Empty!! \n");
exit(0);
}
char *str;
str = malloc(sizeof(char)*(strlen(Stk.items[Stk.top])));
strcpy(str,Stk.items[Stk.top]);
free(Stk.items[Stk.top]);
Stk.top--;
return(str);
}
struct Quadruple
{
char operator[5];
char operand1[10];
char operand2[10];
char result[10];
}QUAD[25];
void display_Quadruple()
{
int i;
printf("\n\n The INTERMEDIATE CODE Is : \n\n");
printf("\n\n The Quadruple Table \n\n");
printf("\n Result Operator Operand1 Operand2 ");
for(i=0;i<Index;i++)
printf("\n %d %s %s %s %s",i,QUAD[i].result,QUAD[i].operator,QUAD[i].operand1,QUAD[i].operand2);
}
void addQuadruple(char op[10],char op2[10],char op1[10],char res[10]){
strcpy(QUAD[Index].operator,op);
strcpy(QUAD[Index].operand2,op2);
strcpy(QUAD[Index].operand1,op1);
strcpy(QUAD[Index].result,res);
Index++;
}
struct symtab *symlook();
struct symtab *table = NULL;
void insertparams(char *fname,char *ps,char *ft){
struct symtab *p = table;
char t[7];
while(p!=NULL)
{ if(strcmp(p->name,fname)==0)
{ strcpy(p->params,ps);
//printf("Reached here\n");
p->isfunc = 1;
if(strcmp(ft,"i")==0)
strcpy(t,"int");
else if(strcmp(ft,"c")==0)
strcpy(t,"char");
else if(strcmp(t,"f")==0)
strcpy(t,"float");
p->type = malloc(strlen(t)*sizeof(char));
strcpy(p->type,t);
break;
}
p = p->next;
}
}
int find_type(char *nam){
struct symtab *p = table;
while(p!=NULL)
{ if(strcmp(p->name,nam)==0)
{if(p->type == NULL)
return 0;
if(p->type[0] == 'i' || p->type[0] == 'f')
return 1;
}
if(p->next==NULL)
break;
p = p->next;
}
return 0;
}
void prin()
{
if(table == NULL)
{ printf("\n empty");
return ;
}
struct symtab *s = table;
printf("\n-----------------------------------------\t\tSYMBOL TABLE\t\t----------------------------------------------\n");
printf("\n\t Name \t\tType \t\tScope \t\tStatus \t\tisFunc \t\tParams \t\tisDef \t\tArrdim\n");
printf("\n------------------------------------------------------------------------------------------------------------------------------\n");
while(1){
if(s->scope != -1 || s->isfunc == 1){
printf("\n\t %s",s->name);
printf("%*c", 15-strlen(s->name), ' ');
printf("%s",s->type);
printf("\t\t%d",s->scope);
printf("\t\t%d",s->active);
printf("\t\t%d",s->isfunc);
printf("\t\t%s",s->params);
printf("\t\t%d",s->isdef);
printf("\t\t%d",s->arrdim);
}
if(s->next==NULL)
break;
s = s->next;
}
printf("\n\n------------------------------------------------------------------------------------------------------------------------------\n");
printf("\n");
return;
}
int check_status(char *id){
struct symtab *s = table;
while(1){
if(strcmp(s->name,id) == 0 && s->active == 1 )
return 1;
if(s->next == NULL)
break;
s = s->next;
}
return 0;
}
int check_scope(char *id,int ne){
struct symtab *s = table;
int flag = 0;
while(1){
if(strcmp(s->name,id) == 0 && s->scope <= ne && s->active == 1 )
flag = 1;
if(s->next == NULL)
break;
s = s->next;
}
return flag;
}
void add_datatype(char *id,char *type,int sc){
struct symtab *s = table;
while(1){
if(strcmp(s->name,id)==0 && s->active == 1 && s->scope!= -1){
printf("\n redeclaration of %s",id);
exit(0);
return;
}
if(s->next == NULL)
break;
s = s->next;
}
s = table;
while(1){
if(strcmp(s->name,id)==0 && s->scope == -1)
{ //printf("found \n");
s->type = malloc(strlen(type)*sizeof(char));
strcpy(s->type,type);
s->scope = sc;
s->active = 1;
break;
}
if(s->next == NULL)
break;
s = s->next;
}
}
int get_arrdim(char *id){
struct symtab *s = table;
while(1){
if(strcmp(s->name,id) == 0 && s->active == 1)
return s->arrdim;
if(s->next == NULL)
break;
s = s->next;
}
return 0;
}
void scope(int ne){
struct symtab *s = table;
while(1){
if(s->scope == ne)
s->active = 0;
if(s->next == NULL)
break;
s = s->next;
}
}
void add_arrdim(char *id,int dim){
struct symtab *s = table;
while(1){
if(strcmp(s->name,id) == 0 && s->active == 1)
s->arrdim = dim;
if(s->next == NULL)
break;
s = s->next;
}
}
void insert(char *s){
struct symtab *p = table;
if(p == NULL){
p = malloc(sizeof(struct symtab));
int i;
strcpy(p->name,s);
p->scope = -1;
p->active = 0;
p->value = NULL;
p->next = NULL;
p->isfunc = 0;
p->isdef = 0;
p->arrdim = 0;
table = p;
return;
}
while(1){
if(p->next == NULL)
break;
p = p->next;
}
struct symtab *temp;
temp = malloc(sizeof(struct symtab));
strcpy(temp->name,s);
temp->value = NULL;
temp->next = NULL;
temp->isfunc = 0;
temp->scope = -1;
temp->active = 0;
temp->arrdim = 0;
temp->isdef = 0;
p->next = temp;
return;
}
int lookup(char *s,char *alist){
struct symtab *p = table;
while(p!=NULL)
{ if(strcmp(p->name,s)==0 && strcmp(p->params,alist) == 0){
p->isdef = 1;
return 1;
}
p = p->next;
}
return 0;
}
void label1()
{
char temp1[5];
char str[5];
strcpy(temp1,"t");
sprintf(str, "%d", temp_var);
strcat(temp1, str);
//dispstack();
printf("\t%s = not %s\n",temp1,Stk.items[Stk.top]);
printf("if %s goto L%d\n",temp1,lno);
temp_var++;
label[++ltop]=lno++;
}
void label2()
{
printf("\tgoto L%d\n",lno);
printf("\tL%d:\n",label[ltop]);
ltop--;
label[++ltop]=lno++;
}
void label3()
{
printf("\tL%d: \n", label[ltop--]);
}
void label4()
{
printf("\tL%d: \n",lno);
label[++ltop]=lno++;
}
void label5()
{
printf("\tgoto L%d \n",label[ltop-1]);
printf("\tL%d: \n",label[ltop]);
ltop -= 2;
}
void dispstack(){
int i;
for(i=Stk.top;i>0;i--){
printf("\n %s",Stk.items[i]);
}
}
void codegen()
{
char temp1[5];
char str[5];
strcpy(temp1,"t");
sprintf(str, "%d", temp_var);
strcat(temp1, str);
char op2[5];
strcpy(op2,pop());
char op[5];
strcpy(op,pop());
char op1[5];
strcpy(op1,pop());
printf("\t%s = %s %s %s\n", temp1, op1, op, op2);
push(temp1);
//strcpy(s[top].value, temp);
temp_var++;
}