forked from muhamza/Jalebi-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.y
309 lines (303 loc) · 7.81 KB
/
compiler.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
%{
void yyerror (char *s);
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
int yylineno;
int scopeY = -1;
int tCounter = 0;
char *tempString;
bool FindVariableSymbolTable(char *ident);
bool IsVariableInitialized(char *ident);
bool IsVariableInteger(char *ident);
%}
%union{
int iVal;
float fVal;
char* sVal;
}
%token markazi se chalo jabtak
%token agar agarwarna warna likho
%token hindsa jumla booliyayi aasharia
%token <sVal> khaali sahih ghalat
%token <sVal> stringliteral
%token <sVal> identifier
%token <fVal> decimal
%token <iVal> integer
%token comma semicolon
%type <sVal> ADDEXPRESSION MULTIPLYEXPRESSION TERMINALEXPRESSION
%type <sVal> RELATIONALEXPRESSION LOGICALEXPRESSION BOOLOPTIONS RELATIONALOPERATORS LOGICALOPERATORS NOTOPERATOR
%type <sVal> SELECTIONSTATEMENT IFSTATEMENT ELSEIFSTATEMENT ELSESTATEMENT
%start START
%left LRP RRP LCP RCP
%right INO DCO
%left MUL DIV MOD PLS MIS
%left <sVal> LT LTE GT GTE IEQ NEQ
%left <sVal> NOT AND OR
%right EA AA SA MA DA
%%
START: markazi LRP RRP COMPOUNDSTATEMENT
;
/* assignment operators like +=, -=, *=, /= */
ASSIGNMENTOPERATORS: MA | DA | AA | SA
;
/* relational operators like >, <, >=, <=, ==, != */
RELATIONALOPERATORS: GT | LT | GTE | LTE | IEQ | NEQ
;
LOGICALOPERATORS: AND | OR
;
NOTOPERATOR: NOT
;
/* increment and decrement operators like ++,-- */
INCDECOPERATORS: INO | DCO
;
BOOLTYPE: booliyayi
;
BOOLOPTIONS: sahih | ghalat
;
FLOATTYPE: aasharia
;
NUMBERTYPE: hindsa
;
STRINGTYPE: jumla
;
NUMBERS: integer | decimal
;
COMPOUNDSTATEMENT: LCP MULTIDECLARATION MULTISTATEMENT RCP {/*printf("Increment Scope = %d\n", ++scopeY);*/}
;
MULTIDECLARATION: DECLARATION MULTIDECLARATION
|
;
MULTISTATEMENT: STATEMENT MULTISTATEMENT
|
;
DECLARATION: NUMBERTYPE identifier semicolon
| STRINGTYPE identifier semicolon
| FLOATTYPE identifier semicolon
| BOOLTYPE identifier semicolon
| NUMBERTYPE identifier EA integer semicolon {printf("%s = %d\n", $2, $4);}
| STRINGTYPE identifier EA stringliteral semicolon {printf("%s = %s\n", $2, $4);}
| FLOATTYPE identifier EA decimal semicolon {printf("%s = %f\n", $2, $4);}
| BOOLTYPE identifier EA BOOLOPTIONS semicolon {printf("%s = %s\n", $2, $4);}
;
STATEMENT: SELECTIONSTATEMENT
| ITERATIONSTATEMENT
| EXPRESSIONSTATEMENT
| PRINTSTATEMENT
;
PRINTSTATEMENT: likho LRP stringliteral RRP semicolon {printf("print %s\n", $3);}
| likho LRP stringliteral comma identifier RRP semicolon {
int ret = FindVariableSymbolTable($5);
if(!ret){
yyerror("Variable not declared!");
exit(1);
}
printf("print %s, %s\n", $3, $5);
}
| likho LRP identifier RRP semicolon {
int ret = FindVariableSymbolTable($3);
if(!ret){
yyerror("Variable not declared!");
exit(1);
}
printf("print %s\n", $3);
}
;
SELECTIONSTATEMENT: IFSTATEMENT ELSEIFSTATEMENT ELSESTATEMENT
;
IFSTATEMENT: agar LRP LOGICALEXPRESSION RRP COMPOUNDSTATEMENT {
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("if %s goto st+3\n", $3);
printf("%s = %s\n",temp, "0");
printf("goto st+2\n");
printf("%s = %s\n",temp, "1");
$$ = strdup(temp);
}
;
ELSEIFSTATEMENT: agarwarna LRP LOGICALEXPRESSION RRP COMPOUNDSTATEMENT {
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("if %s goto st+3\n", $3);
printf("%s = %s\n",temp, "0");
printf("goto st+2\n");
printf("%s = %s\n",temp, "1");
$$ = strdup(temp);
}
| {}
;
ELSESTATEMENT: warna COMPOUNDSTATEMENT {}
| {}
;
ITERATIONSTATEMENT: jabtak LRP LOGICALEXPRESSION RRP COMPOUNDSTATEMENT
| chalo LRP integer se integer RRP COMPOUNDSTATEMENT
;
LOGICALEXPRESSION: LRP RELATIONALEXPRESSION RRP LOGICALOPERATORS LRP RELATIONALEXPRESSION RRP {
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s %s %s\n", temp, $2, $4, $6);
$$ = strdup(temp);
}
| NOTOPERATOR LRP LOGICALEXPRESSION RRP { //RELATIONALEXPRESSION
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s %s\n", temp, $1, $3);
$$ = strdup(temp);
}
| RELATIONALEXPRESSION { $$ = $1; }
;
RELATIONALEXPRESSION: identifier RELATIONALOPERATORS integer {
bool ret1 = FindVariableSymbolTable($1);
if (!ret1){
yyerror("Variable not declared!");
exit(1);
}
bool ret2 = IsVariableInteger($1);
if (!ret2){
yyerror("Incompatible datatypes!");
exit(1);
}
char num[5];
sprintf(num, "%d\0", $3);
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s %s %s\n",temp, $1, $2 ,num);
$$ = strdup(temp);
}
| identifier RELATIONALOPERATORS identifier {
bool ret1 = FindVariableSymbolTable($1);
if (!ret1){
yyerror("Variable not declared!");
exit(1);
}
bool ret2 = IsVariableInteger($1);
if (!ret2){
yyerror("Incompatible datatypes!");
exit(1);
}
bool ret3 = FindVariableSymbolTable($1);
if (!ret3){
yyerror("Variable not declared!");
exit(1);
}
bool ret4 = IsVariableInteger($1);
if (!ret4){
yyerror("Incompatible datatypes!");
exit(1);
}
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s %s %s\n",temp, $1, $2, $3);
$$ = strdup(temp);
}
| integer RELATIONALOPERATORS integer {
char* num[5];
sprintf(num, "%d\0", $1);
char* num2[5];
sprintf(num, "%d\0", $3);
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s %s %s\n",temp, num, $2, num2);
$$ = strdup(temp);
}
| BOOLOPTIONS {
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s\n",temp, $1);
$$ = strdup(temp);
}
;
/* Note:
1) the first two rules of EXPRESSIONSTATEMENT will cater all assignment operators except for =;
2) the last rule will cater assignment (=)
*/
EXPRESSIONSTATEMENT: identifier ASSIGNMENTOPERATORS identifier semicolon
| identifier ASSIGNMENTOPERATORS NUMBERS semicolon
| identifier INCDECOPERATORS semicolon
| INCDECOPERATORS identifier semicolon
| identifier EA ADDEXPRESSION semicolon {
bool ret1 = FindVariableSymbolTable($1);
if (!ret1){
yyerror("Variable not declared!");
exit(1);
}
bool ret2 = IsVariableInteger($1);
if (!ret2){
yyerror("Incompatible datatypes!");
exit(1);
}
InitializeVariable($1);
//UpdateVariableValue($1, atoi($3));
//printf("Total = %d\n", $3);
printf("%s = %s\n", $1, $3);
}
;
ADDEXPRESSION: ADDEXPRESSION PLS MULTIPLYEXPRESSION {
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s + %s\n",temp, $1, $3);
$$ = strdup(temp);
}
| ADDEXPRESSION MIS MULTIPLYEXPRESSION {
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s - %s\n",temp, $1, $3);
$$ = strdup(temp);
}
| MULTIPLYEXPRESSION {$$ = $1;}
;
MULTIPLYEXPRESSION: MULTIPLYEXPRESSION MUL TERMINALEXPRESSION {
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s * %s\n",temp, $1, $3);
$$ = strdup(temp);
}
| MULTIPLYEXPRESSION DIV TERMINALEXPRESSION {
char temp[5];
sprintf(temp,"t%d",tCounter++);
printf("%s = %s / %s\n",temp, $1, $3);
$$ = strdup(temp);
}
| TERMINALEXPRESSION {$$ = $1;}
;
TERMINALEXPRESSION: integer {
char* temp[5];
sprintf(temp, "%d\0", $1);
$$ = strdup(temp);
}
| decimal {
char* temp[5];
sprintf(temp, "%f\0", $1);
$$ = strdup(temp);
}
| identifier {
int ret = FindVariableSymbolTable($1);
int ret2 = IsVariableInitialized($1);
if(ret && ret2){
$$ = $1;
}
else{
yyerror("Variable not declared or initialized!");
exit(1);
}
}
| LRP ADDEXPRESSION RRP {$$ = ($2);}
;
%%
#include "lex.yy.c"
#include <string.h>
int main(int argc, char *argv[])
{
yyin = fopen(argv[1], "r");
if(!yyparse())
printf("\nParsed Correctly!\n");
else
printf("\nParsing Failed\n");
fclose(yyin);
PrintSymbolTable();
return 0;
}
void yyerror (char *s) {
printf("\nLine = %d : Masla = %s\n", (yylineno), s);
}