-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
475 lines (418 loc) · 10.3 KB
/
parser.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define YYSTYPE char *
int yylex();
void yyerror(const char *s);
int sym[26];
typedef struct node{
int scope;
int value;
char name[100];
char dtype[50];
int line_num;
int valid;
}node;
typedef struct table{
node* head;
}table;
// int count = 0;
extern node symTable[1000];
extern int yylineno;
int valid=1;
extern int SymTable[100];
extern int scope;
extern int count;
extern void displaySymTable();
extern int find(int scope, char *yytext);
extern void update(char* name, int value, int scope);
extern int insert(int* idx, int scope, char* dtype, char* val, int line_num);
extern void decrScope();
extern void incrScope();
extern char tdType[50];
%}
%token T_MAIN T_INT T_FLOAT T_DOUBLE T_CHAR T_VOID T_Terminator T_INC T_DEC T_EQ T_NE T_GT T_LT T_GTE T_LTE T_AND T_OR T_NOT T_BAND T_BOR T_BXOR T_ADDEQ T_MULEQ T_DIVEQ T_MODEQ T_LSEQ T_RSEQ T_ANDEQ T_XOREQ T_OREQ T_H
%token T_B T_OB T_SB T_CSB T_OSB T_CB T_OCB T_ID T_ENDL T_CIN T_COUT T_RETURN T_FOR T_IF T_ELSE T_OUT T_IN T_NUM T_SUBEQ T_COMMA T_BREAK T_CONTINUE T_OFB T_CFB T_VMAIN T_STRING T_STRIN T_STROUT T_DEQ T_STR T_INCLUDE T_TRUE T_FALSE
%token T_CLASS T_PUBLIC T_PRIVATE T_PROTECTED T_ARRAY
%left T_ADD T_SUB
%left T_MUL T_DIV
%%
S: START {printf("\n\nPROGRAM IS VALID\n\n"); displaySymTable(); exit(0);}
START: T_INCLUDE Prog
| Prog
;
Prog: DECL BODY
| BODY
;
BODY: T_MAIN T_OFB STMT T_CFB
| T_VMAIN T_OFB STMT T_CFB
| T_CLASS T_ID T_OFB CLASS_STMT T_CFB BODY
| FUNC BODY
|
;
CLASS_STMT: T_PUBLIC STMT CLASS_STMT
|T_PRIVATE STMT CLASS_STMT
|T_PROTECTED STMT CLASS_STMT
|
;
STMT: DECL STMT
|ASSIGN_EXPR STMT
|T_IN INOUT T_Terminator STMT
|T_OUT INOUT T_Terminator STMT
|FORSTMT STMT
|SELECTSTMT STMT
|JUMPSTMT STMT
|T_OFB STMT T_CFB STMT
|UNARYEXP T_Terminator STMT
| FUNC STMT
|
;
FUNC: TYPE T_ID T_OB PARAMLIST T_CB T_OFB STMT T_CFB { scope++;}
;
PARAMLIST: TYPE T_ID PARAMLIST{
if(!insert(&count, scope + 1, $1, $2, yylineno)){
yyerror("Variable redeclared");
}
}
| TYPE T_ID T_EQ E PARAMLIST{
if(!insert(&count, scope + 1, $1, $2, yylineno)){
yyerror("Variable redeclared");
}
update($2, atoi($4), scope + 1);
}
| T_COMMA T_ID T_EQ E PARAMLIST
{
if(!insert(&count, scope + 1, tdType, $2, yylineno)){
yyerror("Variable redeclared");
}
update($2, atoi($4), scope);
}
| T_COMMA TYPE T_ID PARAMLIST{
if(!insert(&count, scope+1, $2, $3, yylineno)){
yyerror("Variable redeclared");
}
}
|
;
INOUT: T_STRIN T_ID
|T_STROUT T_ID INOUT
|T_STROUT T_ENDL INOUT
|T_STROUT T_STRING INOUT
|
;
TYPE: T_INT {strcpy(tdType,$1);}
|T_FLOAT {strcpy(tdType,$1);}
|T_CHAR {strcpy(tdType,$1);}
|T_DOUBLE {strcpy(tdType,$1);}
|T_VOID {strcpy(tdType,$1);}
;
DECL: TYPE T_ID T_EQ E VARLIST T_Terminator {
if(!insert(&count, scope, $1, $2, yylineno)){
yyerror("Variable redeclared");
}
update($2, atoi($4), scope);
}
| TYPE T_ID VARLIST T_Terminator {
if(!insert(&count, scope, $1, $2, yylineno)){
yyerror("Variable redeclared");
}
}
| TYPE T_ARRAY T_Terminator
| TYPE T_ARRAY T_EQ T_OFB ARRELE T_CFB T_Terminator
;
ARRELE: T_NUM T_COMMA ARRELE
| T_STRING T_COMMA ARRELE
| T_NUM
| T_STRING
|
;
VARLIST: T_COMMA T_ID VARLIST {
if(!insert(&count, scope, tdType, $2, yylineno)){
yyerror("Variable redeclared");
}
}
|T_ID {
if(!insert(&count, scope, tdType, $1, yylineno)){
yyerror("Variable redeclared");
}
}
|T_COMMA T_ID T_EQ E VARLIST {
if(!insert(&count, scope, tdType, $2, yylineno)){
yyerror("Variable redeclared");
}
update($2, atoi($4), scope);
}
|
;
ASSIGN_EXPR: T_ID ASSIGNOP T_ID T_Terminator {
if(find(scope, $1) == -1 && find(scope, $3) == -1){
yyerror("Variables not declared");
}
int idx1 = find(scope, $1);
int val1 = symTable[idx1].value;
int idx2 = find(scope, $3);
int val2 = symTable[idx2].value;
if(strcmp($2, "=")){
update($1, val2, scope);
}
else if(strcmp($2, "+=")){
val1 += val2;
update($1, val1, scope);
}
else if(strcmp($2, "-=")){
val1 -= val2;
update($1, val1, scope);
}
else if(strcmp($2, "*=")){
val1 *= val2;
update($1, val1, scope);
}
else if(strcmp($2, "/=")){
val1 /= val2;
update($1, val1, scope);
}
else if(strcmp($2, "%=")){
val1 %= val2;
update($1, val1, scope);
}
else if(strcmp($2, ">>=")){
val1 >>= val2;
update($1, val1, scope);
}
else if(strcmp($2, "<<=")){
val1 <<= val2;
update($1, val1, scope);
}
else{
yyerror("Invalid operation");
}
}
| T_ID ASSIGNOP T_NUM T_Terminator {
if(find(scope, $1) == -1){
yyerror("Variable not declared");
}
int idx1 = find(scope, $1);
int val1 = symTable[idx1].value;
int scope1 = symTable[idx1].scope;
char dtype1[50];
strcpy(dtype1,symTable[idx1].dtype);
printf("scope=%d\nscope1=%d\n",scope,scope1);
if(scope != scope1){
if(!insert(&count, scope, dtype1, $1, yylineno)){
yyerror("Variable redeclared");
}
update($1, atoi($3), scope);
}
if(strcmp($2, "=")){
update($1, atoi($3), scope);
}
else if(strcmp($2, "+=")){
val1 += atoi($3);
update($1, val1, scope);
}
else if(strcmp($2, "-=")){
val1 -= atoi($3);
update($1, val1, scope);
}
else if(strcmp($2, "*=")){
val1 *= atoi($3);
update($1, val1, scope);
}
else if(strcmp($2, "/=")){
val1 /= atoi($3);
update($1, val1, scope);
}
else if(strcmp($2, "%=")){
val1 %= atoi($3);
update($1, val1, scope);
}
else if(strcmp($2, ">>=")){
val1 >>= atoi($3);
update($1, val1, scope);
}
else if(strcmp($2, "<<=")){
val1 <<= atoi($3);
update($1, val1, scope);
}
else{
yyerror("Invalid operation");
}
}
| T_ID ASSIGNOP T_OB E T_CB T_Terminator {
if(find(scope, $1) == -1){
yyerror("Variable not declared");
}
int idx1 = find(scope, $1);
int val1 = symTable[idx1].value;
if(strcmp($2, "=")){
update($1, atoi($4), scope);
}
else if(strcmp($2, "+=")){
val1 += atoi($4);
update($1, val1, scope);
}
else if(strcmp($2, "-=")){
val1 -= atoi($4);
update($1, val1, scope);
}
else if(strcmp($2, "*=")){
val1 *= atoi($4);
update($1, val1, scope);
}
else if(strcmp($2, "/=")){
val1 /= atoi($4);
update($1, val1, scope);
}
else if(strcmp($2, "%=")){
val1 %= atoi($4);
update($1, val1, scope);
}
else if(strcmp($2, ">>=")){
val1 >>= atoi($4);
update($1, val1, scope);
}
else if(strcmp($2, "<<=")){
val1 <<= atoi($4);
update($1, val1, scope);
}
else{
yyerror("Invalid operation");
}
}
;
E: E T_ADD T {
int val = atoi($1) + atoi($3);
sprintf($$, "%d", val);
}
|E T_SUB T {
int val = atoi($1) - atoi($3);
sprintf($$, "%d", val);
}
|T {$$ = $1;}
;
T: T T_MUL F {
int val = atoi($1) * atoi($3);
sprintf($$, "%d", val);
}
|T T_DIV F {
int val = atoi($1) / atoi($3);
sprintf($$, "%d", val);
}
|F {$$ = $1;}
F: T_ID {
if(find(scope, $1) == -1){
yyerror("Variable not declared");
}
int idx = find(scope, $1);
sprintf($$, "%d", symTable[idx].value);
}
|T_NUM {$$ = $1;}
| T_OB E T_CB {$$ = $2;}
| BOOL {
$$ = $1;
}
| UNARYEXP
;
FORSTMT: T_FOR T_OB INITFOR COND T_Terminator ITRCHANGE T_CB T_OFB STMT T_CFB
;
INITFOR: T_ID T_Terminator { scope--; }
|ASSIGN_EXPR { scope--; }
|DECL { scope--; }
|T_Terminator { scope--; }
;
COND: OPERATION
;
OPERATION: E LOGICOP E
|E RELOP E
|E BITOP E
|E RELOP E LOGICOP E RELOP E
|T_NOT T_OB E RELOP E T_CB
|T_NOT E RELOP E
|T_NOT E
|E
|
;
ITRCHANGE: UNARYEXP
| ASSIGN_EXPR
|
;
SELECTSTMT: T_IF T_OB COND T_CB T_OFB STMT T_CFB
|T_IF T_OB COND T_CB T_OFB STMT T_CFB T_ELSE T_OFB STMT T_CFB
;
JUMPSTMT: T_RETURN T_ID T_Terminator
| T_RETURN T_NUM T_Terminator
| T_RETURN T_STRING T_Terminator
| T_RETURN T_Terminator
| T_CONTINUE T_Terminator
| T_BREAK T_Terminator
;
UNARYEXP: T_INC T_ID {
if(find(scope, $2) == -1){
yyerror("Variable not declared");
}
int idx = find(scope, $2);
sprintf($$, "%d", ++symTable[idx].value);
update($2, atoi($$), scope);
}
| T_ID T_INC {
if(find(scope, $1) == -1){
yyerror("variable not declared");
}
int idx = find(scope, $1);
sprintf($$, "%d", symTable[idx].value++);
update($1, atoi($$), scope);
}
| T_DEC T_ID {
if(find(scope, $2) == -1){
yyerror("variable not declared");
}
int idx = find(scope, $2);
sprintf($$, "%d", --symTable[idx].value);
update($2, atoi($$), scope);
}
| T_ID T_DEC {
if(find(scope, $1) == -1){
yyerror("variable not declared");
}
int idx = find(scope, $1);
sprintf($$, "%d", symTable[idx].value--);
update($1, atoi($$), scope);
}
;
LOGICOP: T_OR {$$ = $1;}
| T_AND {$$ = $1;}
;
BITOP: T_BAND {$$ = $1;}
| T_BOR {$$ = $1;}
| T_BXOR {$$ = $1;}
;
RELOP: T_DEQ {$$ = $1;}
| T_LT {$$ = $1;}
| T_GT {$$ = $1;}
| T_GTE {$$ = $1;}
| T_LTE {$$ = $1;}
| T_NE {$$ = $1;}
;
ASSIGNOP: T_EQ {$$ = $1;}
| T_ADDEQ {$$ = $1;}
| T_MULEQ {$$ = $1;}
| T_DIVEQ {$$ = $1;}
| T_MODEQ {$$ = $1;}
| T_LSEQ {$$ = $1;}
| T_RSEQ {$$ = $1;}
| T_SUBEQ {$$ = $1;}
;
BOOL: T_TRUE {$$ = "1";}
| T_FALSE {$$ = "0";}
;
%%
void yyerror(const char *s) {
printf("\nERROR: %s on line %d\n",s, yylineno);
}
int main()
{
printf("LINE\tTOKENS\n");
yyparse();
return 0;
}