-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.y
321 lines (267 loc) · 5.48 KB
/
grammar.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
/* Simple grammar rules*/
%{
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
int count1 = 0;
int loop_count = 0;
int go_to_default =1;
int variable_value = 5;
int if_condition_is_true=0;//0 for no condition, 1 for true, 2 for false
int dont_do_this_else=0;
char *variable[50];
int value[50];
int check_for_duplicate(char* term){
int i;
for(i=0;i<count1;i++)
{
if(strcmp(term, variable[i])==0)
return 1;
}
variable[count1]=term;
count1++;
return 0;
}
int assign_value(char* term, int a) {
int i;
for(i=0;i<count1;i++)
{
if(strcmp(term, variable[i])==0)
{
value[i]=a;
return 1;
}
}
return 0;
}
int get_variable_value (char* term) {
int i;
for(i=0;i<count1;i++)
{
if(!strcmp( term, variable[i]))
{
return value[i];
}
}
printf("%s not declared. May result to unintended output", term);
return 0;
}
%}
%union {
double DBL;
int INTG;
char* VAR;
}
%token <DBL> NUMBER
%token <VAR> VARIABLE
%token <VAR> STRING
%token INT FLOAT CHARACTER INC DEC TILL IF ELSE LB RB LOOP WHILE SWITCH CASE DEFAULT PRINT START END END_OF_STATEMENT PLUS MINUS MULT DIV ISGRTR ISLESS ISGRTREQU ISLESSEQU EQU
%left ASSIGN
%nonassoc IFX
%nonassoc ELSE
%start START_POINT
%type <INTG> EXPRESSION STATEMENT TERM FACTOR CONDITION SINGLE_CASE CASES
%%
START_POINT: START PROGRAM END{
printf("Compiled Successfully");
}
;
PROGRAM:/*empty*/
|PROGRAM STATEMENT
;
STATEMENT: END_OF_STATEMENT{
$$ = 1;
}
| DECLARATION END_OF_STATEMENT {$$=1;}
| EXPRESSION END_OF_STATEMENT{
$$ = 1;
}
| PRINT STRING {
printf("%s\n", $2);
}
| PRINT VARIABLE {
int i;
for(i=0;i<count1;i++)
{
if(strcmp( $2, variable[i])==0)
{
printf("%s value is: %d\n",$2, value[i]);
break;
}
}
}
| VARIABLE ASSIGN EXPRESSION END_OF_STATEMENT
{
int i=0;
i= assign_value($1, $3);
if(i)
printf("Value Assigned\n");
else
printf("Variable not declared\n");
}
|IF_ELSE END_OF_STATEMENT{
$$ = 1;
}
| WHILE LB CONDITION RB LOOP LB PROGRAM RB {
printf("Inside loop\n");
}
| SWITCH EXPRESSION LB CASES RB END_OF_STATEMENT {
}
;
CASES : SINGLE_CASE
| CASES SINGLE_CASE
| CASES DEFAULT_CASE
;
SINGLE_CASE: CASE EXPRESSION LB PROGRAM RB END_OF_STATEMENT{
if (variable_value == $2)
{
go_to_default = 0;
printf("Currently in case number %d\n", $2);
}
}
;
DEFAULT_CASE : DEFAULT LB PROGRAM RB END_OF_STATEMENT {
if (go_to_default)
{
printf("Inside default case.\n");
}
else
go_to_default = 1;
}
;
DECLARATION : TYPE VARIABLE
{
int i;
char* temp;
temp = $2;
i = check_for_duplicate(temp);
if(i==1)
printf("Duplicate declaration of variable %s\n", temp);
else
printf("Variable %s declared\n", temp);
};
TYPE : INT
| FLOAT
| CHARACTER
;
EXPRESSION: EXPRESSION PLUS TERM {
$$ = $1 + $3;
}
|EXPRESSION MINUS TERM{
$$ = $1 - $3;
}
|TERM
;
TERM: TERM MULT FACTOR{
$$ = $1 * $3;
}
|TERM DIV FACTOR{
$$ = $1 / $3;
}
|FACTOR
;
FACTOR: NUMBER {
$$ = $1; }
|VARIABLE {
variable_value = get_variable_value($1);
$$ = get_variable_value($1);
}
|"(" EXPRESSION ")"{
$$ = $2;
}
|"-" FACTOR {
$$ = -$2;
}
;
IF_ELSE: IF CONDITION LB PROGRAM RB ELSE LB PROGRAM RB {
if($2)
{
printf("Inside IF \n");
if_condition_is_true = 1;
}
else
{
printf("Inside ELSE \n");
if_condition_is_true = 2;
}
}
|IF CONDITION LB PROGRAM RB %prec IFX {
if($2)
{
printf("Inside SINGLE IF \n");
}
else{
printf("SINGLE IF condition not true\n");
}
}
;
CONDITION: EXPRESSION ISGRTR EXPRESSION {
$$ = $1 > $3? 1:0;
}
|EXPRESSION ISLESS EXPRESSION {
$$ = $1 < $3? 1:0;
}
|EXPRESSION ISGRTREQU EXPRESSION {
$$ = $1 >= $3? 1:0;
}
|EXPRESSION ISLESSEQU EXPRESSION {
$$ = $1 <= $3? 1:0;
}
|EXPRESSION EQU EXPRESSION{
$$ = ($1 == $3)? 1:0;
}
|VARIABLE ASSIGN EXPRESSION INC TILL EXPRESSION {
int i=0, j;
printf ("I am here\n");
i= assign_value($1, $3);
if(i)
{
printf("Value INITIALIZED.. Entering into loop\n");
for(j=$3; j<=$6; j++)
{
loop_count++;
}
i=0;
i= assign_value($1, j);
printf("Loop ENDED.. Value: %d\n", j);
if(i)
{
printf("Loop Ended Successfully. Total loop count: %d\n", loop_count);
loop_count = 0;
}
else
printf("Variable not declared\n");
}
}
|VARIABLE ASSIGN EXPRESSION DEC TILL EXPRESSION {
int i=0, j;
i= assign_value($1, $3);
if(i)
{
printf("Value INITIALIZED.. Entering into loop\n");
for(j=$6; j>=$3; j--)
{
loop_count++;
}
i=0;
i= assign_value($1, j);
printf("Loop ENDED.. Value: %d\n", j);
if(i)
{
printf("Loop Ended Successfully. Total loop count: %d\n", loop_count);
loop_count = 0;
}
else
printf("Variable not declared\n");
}
}
;
%%
extern char* yytext;
int yywrap() {
return 1;
}
yyerror(char* s) {
printf("%s\n", s);
}