-
Notifications
You must be signed in to change notification settings - Fork 2
/
calc.y
358 lines (289 loc) · 5.26 KB
/
calc.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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int linenum;
int yylex();
int search(char name[]);
void insert(char name[], int type, char value[]);
void printTab();
int yyerror(char *s);
void yyerror2(int type, char *s, char b[]);
void insertVal(char value[], int posi);
void valAssign(int modifier, char s1[], char s3[]);
struct symtab{
char name[20];
int type; // 1 is int, 2 is float
char value[20];
};
struct symtab tab[20];
int ptr = 0;
%}
%token TOK_SEMICOLON
TOK_ADD TOK_SUB TOK_MUL TOK_DIV TOK_EQ
TOK_INT TOK_FLOAT TOK_IDENT
TOK_PRINTID TOK_PRINTEX TOK_EXIT TOK_MAIN TOK_OPENCURLY TOK_CLOSECURLY
TOK_IDENT_ERR
%token <variable> TOK_NUM TOK_FNUM
%union{
char name[20];
int int_val;
struct strVariable{
int intVar;
float floatVar;
}variable;
}
%type <variable> expr RAW
%type <name> TOK_IDENT
%type <name> TOK_IDENT_ERR
%left TOK_ADD TOK_SUB
%left TOK_MUL TOK_DIV
%%
prog:
TOK_MAIN TOK_OPENCURLY vardefs stmts TOK_CLOSECURLY
| {
char *t ="Parsing Error";
yyerror2(3,t,"");
}
;
vardefs:
| vardef TOK_SEMICOLON vardefs
;
vardef:
TOK_INT TOK_IDENT {
int flag;
char *t;
flag = search($2);
if(flag == -1){
insert($2, 1, "0");
}else{
t = "Identifier already defined - ";
yyerror2(3,t, $2);
}
}
| TOK_INT TOK_IDENT_ERR
{
char *t ="Invalid Identifier - ";
yyerror2(3,t,$2);
}
| TOK_FLOAT TOK_IDENT {
int flag;
char *t;
flag = search($2);
if(flag == -1){
insert($2, 2, "0.0");
}else{
t = "Identifier already defined - ";
yyerror2(3,t,$2);
}
}
| TOK_FLOAT TOK_IDENT_ERR
{
char *t ="Invalid Identifier - ";
yyerror2(3,t,$2);
}
;
stmts:
| stmt TOK_SEMICOLON stmts
;
stmt:
TOK_IDENT TOK_EQ expr
{
int modifier;
char val[15];
if($3.intVar != 0) {
modifier = 1;
sprintf(val, "%d", $3.intVar);
}else if($3.floatVar != 0){
modifier = 2;
sprintf(val, "%f", $3.floatVar);
}
valAssign(modifier, $1, val);
}
| TOK_PRINTID TOK_IDENT
{
//todo change print to file
int flag;
char *t;
flag = search($2);
if(flag == -1){
t = "Identifier not defined - ";
yyerror2(3,t, $2);
}else{
printf("%s\n", tab[flag].value);
}
}
| TOK_PRINTEX expr
{
if($2.intVar != 0){
printf("%d", $2.intVar);
}
else{
printf("%f", $2.floatVar);
}
}
;
expr:
expr TOK_ADD expr
{
char *t;
t = " + ";
yyerror2(2,t, NULL);
}
|
expr TOK_DIV expr
{
char *t;
t = " / ";
yyerror2(2,t, NULL);
}
| expr TOK_SUB expr
{
struct strVariable temp2;
int flag;
if($1.intVar != 0 && $3.intVar != 0) {
temp2.floatVar = 0;
temp2.intVar = $1.intVar - $3.intVar;
}else if($1.floatVar != 0 && $3.floatVar != 0) {
temp2.intVar = 0;
temp2.floatVar = $1.floatVar - $3.floatVar;
}else {
char *t = "Type mismatch";
yyerror2(3,t, "");
}
$$ = temp2;
}
| expr TOK_MUL expr
{
//$$ = $1 * $3;
struct strVariable temp;
int flag;
if($1.intVar != 0 && $3.intVar != 0) {
temp.floatVar = 0;
temp.intVar = $1.intVar * $3.intVar;
}else if($1.floatVar != 0 && $3.floatVar != 0) {
temp.intVar = 0;
temp.floatVar = $1.floatVar * $3.floatVar;
}else {
char *t = "Type mismatch";
yyerror2(3,t, "");
}
$$ = temp;
}
| TOK_IDENT
{
int flag;
char *t;
flag = search($1);
struct strVariable temp;
if(flag == -1){
t = "Identifier not defined - ";
yyerror2(3,t, $1);
}else{
if(tab[flag].type == 1){
temp.intVar = atoi(tab[flag].value);
temp.floatVar = 0;
}
else {
temp.intVar = 0;
temp.floatVar = atof(tab[flag].value);
}
}
$$ = temp;
}
| RAW
;
RAW:
TOK_NUM
{
$$ = $1;
}
| TOK_FNUM
{
$$ = $1;
}
;
%%
void valAssign(int modifier, char s1[], char s3[]){
int flag,flag2 = 0;
char temp[20];
flag = search(s1);
if(flag == -1){
char *t ="Identifier Not present - ";
yyerror2(3,t,s1);
}else{
switch(modifier){
case 1:
if(tab[flag].type == 1) flag2 = 1;
break;
case 2:
if(tab[flag].type == 2) flag2 = 1;
break;
}
if(flag2 == 1){
insertVal(s3, flag);
}else{
char *t ="Wrong Type Error - ";
yyerror2(3,t,s3);
}
}
}
int yyerror(char *s)
{
printf("Syntax Error on line %d\n%s\n",linenum, s);
return 0;
}
void yyerror2(int type, char *s, char b[])
{
//todo change print to file
switch(type){
case 1:
printf("Syntax Error on line - %d\n%s\n",linenum, s);
break;
case 2:
printf("Lexical Error on line - %d: %s\n",linenum, s);
break;
case 3:
printf("Line - %d: %s%s\n",linenum, s, b);
break;
}
exit(0);
}
int search(char name[]){
int i;
int flag = -1;
for(i = 0; i < ptr; i++){
if(strcmp(tab[i].name, name) == 0){
flag = i;
break;
}
}
return flag;
}
void insert(char name[], int type, char value[]){
strcpy(tab[ptr].name, name);
strcpy(tab[ptr].value, value);
tab[ptr].type = type;
ptr++;
printTab();
}
void insertVal(char value[], int posi){
strcpy(tab[posi].value, value);
printTab();
}
void printTab(){
int i;
for(i = 0; i < ptr; i++){
//use for testing
/*printf("Name - %s, value - %s, type - ", tab[i].name, tab[i].value);
if(tab[i].type == 1) printf("int\n");
else if(tab[i].type == 2) printf("float\n");*/
}
}
int main()
{
//todo remove clear
system("clear");
yyparse();
return 0;
}