forked from anand-b/Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c.y
559 lines (551 loc) · 9.9 KB
/
c.y
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
%{
#include<stdio.h>
#include<stdlib.h>
#include<stddef.h>
#include<string.h>
#include<ctype.h>
#include "y.tab.h"
extern FILE *yyin;
extern int line;
//variables
int top=-1;
int result=1;
int startLoop,endLoop;
struct list
{
struct node *link;
int count;
}*lis;
struct node
{
struct node *link;
char *name;
int *addr;
};
typedef struct list l;
typedef struct node n;
int if_stack[20];
int if_top=-1;
void if_push();
int if_pop();
int* getAddr(l *,char *);
int getVal(l *,char *);
void freeList(l *);
void freeListUtil(n *);
void printUtil(l *,int);
void insert(l *,char *);
void removeUtil(l *,char *);
n* createNode(char *,int *);
int hashUtil(char *);
%}
%union
{
int number;
char *var_name;
}
%token INT NUMBER PLUS BMINUS MULTIPLY OP CP AND OR DO
%token COMMA DIVIDE MOD VAR FUNC_NAME PERIOD EQ
%token FOR THEN IF FI RETURN MAIN END BREAK CONTINUE
%token SRIGHT SLEFT BAND BOR BXOR BITCOMPLEMENT
%token LE GE L G NOT NE EE ELSE
%token DEC INC UMINUS OUT IN STRING LOOP TILL WITH
%left LE GE L G NOT NE EE
%left SRIGHT SLEFT AND OR
%left PLUS BMINUS MULTIPLY DIVIDE MOD
%left BAND BOR BXOR
%type <number> loop
%type <number> expr
%type <number> statement
%type <number> term
%type <number> factor
%type <number> shifts
%type <number> bitwise
%type <number> NUMBER
%type <number> logical
%type <number> compare
%type <number> pre
%type <number> post
%type <number> block
%type <number> init
%type <number> ifcond
%type <var_name> VAR
%type <var_name> STRING
%type <number> list
%type <number> initlist
%type <number> else_initlist
%type <number> else_init
%type <number> else_block
%%
st : MAIN statement {printf("\n---------------------------------------------\nProgram terminated with return value %d\n",$2);exit(0);}
;
statement : END {$$=0;}
| declaration statement {}
| init statement {}
| {if_push();} ifcond statement {}
| OUT STRING
{
printf("%s\n",$2);
}
PERIOD statement {}
| OUT logical PERIOD
{
printf("%d\n",$2);
}
statement {}
| IN VAR PERIOD
{
int *p=getAddr(lis,$2);
int tmp;
scanf("%d",&tmp);
*p=tmp;
}
statement {}
|{printf("%d\n",ftell(yyin));} loop statement {}
| PERIOD statement {}
;
declaration : INT list PERIOD
{
}
;
list : VAR COMMA list
{
insert(lis,$1);
}
| VAR EQ logical COMMA list
{
//printf("%s %d\n",$1,$3);
insert(lis,$1);
int *p;
p=getAddr(lis,$1);
*getAddr(lis,$1)=$3;
//printf("list 1: %s\n",$1);
int a=getVal(lis,$1);
//printf("%d\n",a);
$$=a;
//printUtil(lis,hashUtil($1));
}
| VAR EQ logical
{
//printf("%s %d\n",$1,$3);
insert(lis,$1);
*getAddr(lis,$1)=$3;
//printf("list 2: %s\n",$1);
int a=getVal(lis,$1);
//printf("%d\n",a);
}
| VAR
{
insert(lis,$1);
}
;
block : init block {$$=$1;}
| ifcondition block {}
| loop block {}
| strprint1 block {}
| varprint1 block {}
| input1 block {}
| init {}
| varprint1 {}
| strprint1 {}
| input1 {}
| ifcondition {}
| PERIOD block {}
| PERIOD {}
| loop
;
ifcondition : {if_push();} ifcond {}
input1 : IN VAR PERIOD
{
if(result==1){
int *p=getAddr(lis,$2);
int tmp;
scanf("%d",&tmp);
*p=tmp;
}
}
;
varprint1 : OUT logical PERIOD
{
if(result==1)
printf("%d\n",$2);
}
;
strprint1 : OUT STRING
{
if(result==1)
printf("%s\n",$2);
}
PERIOD {}
;
init : initlist PERIOD
{
}
| initlist COMMA init {}
;
initlist : VAR EQ logical
{
if(result==1)
{
//printf("Decl: %s\n",$1);
int *a=getAddr(lis,$1);
*a=$3;
$$=*a;
//printf("%d\n",*a);
}
}
;
else_block : else_init else_block {$$=$1;}
| ifcondition else_block {}
| loop block {}
| strprint0 else_block {}
| varprint0 else_block {}
| input0 else_block {}
| else_init {}
| varprint0 {}
| strprint0 {}
| input0 {}
| ifcondition {}
| PERIOD else_block {}
| PERIOD {}
| loop {}
;
varprint0 : OUT logical PERIOD
{
if(result==0)
printf("%d\n",$2);
}
;
input0 : IN VAR PERIOD
{
if(result==0){
int *p=getAddr(lis,$2);
int tmp;
scanf("%d",&tmp);
*p=tmp;
}
}
;
strprint0 :OUT STRING
{
if(result==0)
printf("%s\n",$2);
}
PERIOD {}
;
else_init : else_initlist PERIOD
{
}
;
else_initlist : VAR EQ logical
{
if(result==0)
{
//printf("Decl: %s\n",$1);
int *a=getAddr(lis,$1);
*a=$3;
$$=*a;
//printf("%d\n",*a);
}
}
;
ifcond : IF OP logical CP THEN block FI
{
if($3==1)
{
//$$=$7;
//printf("IF statement\n");
}
result=if_pop();
}
| IF OP logical CP THEN block ELSE else_block FI
{
//printf("condition: %d\n",$3);
if($3==1)
{
//$$=$6;
//printf("%d\n",$3);
//printf("IF statement\n");
}
else
{
//$$=$9;
//printf("ELSE statement\n");
}
result=if_pop();
}
;
loop : LOOP TILL logical DO block WITH logical PERIOD
{
printf("Loop\n");
}
;
logical : logical AND compare {$$=$1&&$3;result=$$;}
| logical OR compare {$$=$1||$3;result=$$;}
| compare {$$=$1;}
;
compare :compare GE bitwise
{
if($1 >= $3)
$$=1;
else
$$=0;
result=$$;
}
| compare LE bitwise
{
if($1 <= $3)
$$=1;
else
$$=0;
result=$$;
}
| compare G bitwise
{
if($1 > $3)
$$=1;
else
$$=0;
result=$$;
}
| compare L bitwise
{
if($1 < $3)
$$=1;
else
$$=0;
result=$$;
}
| compare NE bitwise
{
if($1 != $3)
$$=1;
else
$$=0;
result=$$;
}
| compare EE bitwise
{
if($1 == $3)
$$=1;
else
$$=0;
result=$$;
}
| bitwise {$$=$1;}
;
bitwise : bitwise BAND shifts {$$=$1&$3;}
| bitwise BOR shifts {$$=$1|$3;}
| bitwise BXOR shifts {$$=$1^$3;}
| shifts {$$=$1;}
;
shifts : shifts SRIGHT expr {$$=$1>>$3;}
| shifts SLEFT expr {$$=$1<<$3;}
| expr {$$=$1;}
;
expr : expr PLUS term {$$=$1+$3;}
| expr BMINUS term {$$=$1-$3;}
| term {$$=$1;}
;
term : pre MULTIPLY term {$$=$1*$3;}
| pre DIVIDE term {$$=$1/$3;}
| pre MOD term {$$=$1%$3;}
| pre {$$=$1;}
;
pre : INC pre {$$=++$2;}
| DEC pre {$$=--$2;}
| NOT pre {$$=!$2;}
| BITCOMPLEMENT pre {$$=~$2;}
| factor {$$=$1;}
;
factor : OP logical CP {$$=$2;}
| post {$$=$1;}
;
post : post INC {$$=$1++;}
| post DEC {$$=$1--;}
| NUMBER {$$=$1;}
| VAR {
//printf("%s\n",$1);
int *p=getAddr(lis,$1);
$$=*p;
}
;
%%
int yywrap()
{
return 1;
}
int yyerror(char *err)
{
printf("Error: ");
printf("Program terminated with return value %d\n",err);
}
int hashUtil(char *name)
{
int addition=0,i;
for(i=0;name[i]!='\0';i++)
addition+=(int)name[i];
return addition%200;
}
n* createNode(char *name,int *addr)
{
n *temp=(n*)malloc(sizeof(n));
temp->link=NULL;
temp->name=name;
temp->addr=addr;
return temp;
}
// 1 success 0 fail
void insert(l *list,char *name)
{
int *a=(int *) malloc(sizeof(int));
int index=hashUtil(name);
//printf("Inside insert\n");
list[index].count++;
if(list[index].link==NULL)
{
//printf("Inside insert link NLL\n");
list[index].link=createNode(name,a);
}
else
{
n *temp=list[index].link,*prev=list[index].link;
//printf("Inside insert insde else\n");
while(temp!=NULL && strcmp(temp->name,name))
{
//printf("%d\n",strcmp(temp->name,name));
//printf("Inside insert while\n");
prev=temp;
temp=temp->link;
}
if(!strcmp(temp->name,name))
{
printf("Oops: Duplicate variable declaration!\n");
exit(0);
}
else{
//printf("Inside insert\n");
prev->link=createNode(name,a);
}
}
//printf("Inside insert end\n");
}
void removeUtil(l *list,char *name)
{
int index=hashUtil(name);
n *temp,*prev;
if(list[index].count==1)
{
temp=list[index].link;
free(temp);
list[index].link=NULL;
}
else
{
if(list[index].link->name==name)
{
temp=list[index].link;
list[index].link=list[index].link->link;
free(temp);
}
else
{
temp=list[index].link->link;
prev=list[index].link;
while(temp!=NULL && temp->name!=name)
{
prev=temp;
temp=temp->link;
}
prev->link=temp->link;
free(temp);
}
}
list[index].count--;
}
void printUtil(l *list,int index)
{
n *temp=list[index].link;
while(temp!=NULL)
{
printf("%d \n",*temp->addr);
temp=temp->link;
}
printf("\n");
}
void freeListUtil(n *node)
{
if(node==NULL)
return;
else
{
freeListUtil(node->link);
}
n *temp=node;
free(temp);
}
void freeList(l *list)
{
int i;
for(i=0;i<200;i++)
{
freeListUtil(list[i].link);
list[i].link=NULL;
}
}
int* getAddr(l *list,char *name)
{
//printf("%s\n",name);
//printf("Inside getAddr\n");
int index=hashUtil(name);
n *temp=list[index].link;
//printf("Inside getAddr\n");
while(temp!=NULL && strcmp(temp->name,name) )
{
//printf("Inside getAddr while\n");
temp=temp->link;
}
if(temp==NULL)
{
printf("Error: No such variable declared\n");
exit(0);
}
else
{
//printf("Inside getAddr\n");
return temp->addr;
}
}
int getVal(l *list,char *name)
{
int index=hashUtil(name);
n *temp=list[index].link;
while(temp->name!=name)
{
temp=temp->link;
}
return *(temp->addr);
}
void if_push()
{
if_stack[++if_top]=result;
}
int if_pop()
{
return if_stack[if_top--];
}
int main(int argc,char *argv[])
{
int i;
int a,b,c;
yyin=fopen(argv[1],"r");
lis=(l*)malloc(sizeof(l)*200);
l *lis2=(l*)malloc(sizeof(l)*200);
for(i=0;i<200;i++)
{
lis[i].link=NULL;
lis[i].count=0;
}
yyparse();
fclose(yyin);
return 0;
}