-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.y
301 lines (228 loc) · 7.39 KB
/
Main.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
%{
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include "symbol_table.h"
int yyerror(char *s);
int yylex();
int checkSwitch, def;
%}
/* bison declarations */
%union{
char str[1001];
int val;
}
%token MAIN IF ELSE ELSEIF PLUS MINUS MUL DIV MOD ARRAY INT FLOAT CHAR BS BE LOOP WHILE ODDEVEN PRINT FACTORIAL CASE DEFAULT SWITCH REVERSE_LOOP REVERSE_WHILE SIN COS TAN LOG LOG10
%token<val>NUMBER
%type<val>statement expression
%token<str>VAR
%nonassoc IFX
%nonassoc ELSEIF
%nonassoc ELSE
%left '<' '>'
%left MOD
%left PLUS MINUS
%left MUL DIV
%left '^'
/* Grammar rules and actions follow. */
%%
start_program: MAIN ':' BS body_inside BE {printf("\n:::: MAIN Function END ::::\n");}
;
body_inside: /* NULL */
| body_inside statement { }
;
statement: ';' { }
| declaration ';' { }
| expression ';' { printf("value of expression: %d\n", $1); $$=$1;
printf("\n\n");
}
| VAR '=' expression ';' {
printf("\nValue of the variable : %d\n",$3);
setVal($1,$3);
//data[$1]=$3;
$$=$3;
printf("\n\n");
}
| IF '(' expression ')' BS statement BE %prec IFX {
if($3){
printf("\nvalue of expression in IF : %d\n",$6);
}
else{
printf("\ncondition value zero in IF block\n");
}
printf("\n\n");
}
| IF '(' expression ')' BS statement BE ELSE BS statement BE {
if($3){
printf("value of expression in IF : %d\n",$6);
}
else{
printf("value of expression in ELSE : %d\n",$10);
}
printf("\n\n");
}
| IF '(' expression ')' BS statement BE ELSEIF '(' expression ')' BS statement BE ELSE BS statement BE {
if($3){
printf("value of expression in IF : %d\n",$6);
}
else if($10){
printf("value of expression in ELSE-IF : %d\n",$13);
}
else{
printf("value of expression in ELSE : %d\n",$17);
}
printf("\n\n");
}
| LOOP '(' NUMBER ',' NUMBER ',' NUMBER ')' BS statement BE {
int i;
printf("For Loop execution\n");
for(i=$3 ; i<$5 ; i=i+$7 )
{printf("\nvalue of i : %d expression value : %d\n", i,$10);}
printf("\n\n");
}
| REVERSE_LOOP '(' NUMBER ',' NUMBER ',' NUMBER ')' BS statement BE {
int i;
printf("Reverse For Loop execution\n");
for(i=$3 ; i>=$5 ; i=i-$7 )
{printf("\nvalue of i : %d expression value : %d\n", i,$10);}
printf("\n\n");
}
| PRINT '(' expression ')' ';' {printf("\nPrint Expression %d\n",$3);
printf("\n\n");}
| ODDEVEN '(' NUMBER ')' ';' {
printf("Odd Even Number detection \n");
if($3 %2 ==0){
printf("Number : %d is -> Even\n",$3);
}
else{
printf("Number is : %d is -> Odd\n",$3);
}
printf("\n\n");
}
| ARRAY TYPE VAR '[' NUMBER ']' ';' {
printf("\nARRAY Declaration [ ]\n");
printf("\nSize of the ARRAY is : %d\n",$5);
printf("\n\n");
}
| SWITCH '(' NUMBER ')' { checkSwitch = $3;
printf("\nCheck Switch : %d\n",checkSwitch);}
BS SWITCHCASE BE {
printf("\nSWITCH CASE Declaration & Case : %d executed\n",checkSwitch);
printf("\n\n");
}
| WHILE '(' NUMBER '<' NUMBER ')' BS statement BE {
int i;
printf("\n WHILE Loop execution \n");
for(i=$3 ; i<$5 ; i++) {printf("\niteration loop : %d \n", i);}
printf("\n\n");
}
| REVERSE_WHILE '(' NUMBER '>' NUMBER ')' BS statement BE {
int i;
printf("\n REVERSE WHILE Loop execution \n");
for(i=$3 ; i>$5 ; i--) {printf("\niteration loop : %d \n", i);}
printf("\n\n");
}
;
declaration : TYPE ID1 {printf("\nvariable Dection\n");
printf("\n\n");}
;
TYPE : INT {printf("interger declaration\n");}
| FLOAT {printf("float declaration\n");}
| CHAR {printf("char declaration\n");}
;
ID1 : ID1 ',' VAR {
int res = addNewVal($3);
if(res == 0){
printf("Compilation Error :: Varriable already declared\n");
exit(-1);
}
}
|VAR {
int res = addNewVal($1);
if(res == 0){
printf("Compilation Error :: Varriable already declared\n");
exit(-1);
}
}
;
SWITCHCASE: casegrammer
|casegrammer defaultgrammer
;
casegrammer: /*empty*/
| casegrammer casenumber
;
casenumber: CASE NUMBER ':' expression ';' { int check = $2;
if(check == checkSwitch)
{
printf("\nCase No : %d & expression value : %d \n",$2,$4);
def = -1000;
}
}
;
defaultgrammer: DEFAULT ':' expression ';' {
if(def != -1000)
{
printf("\nDefault case & expression value : %d",$3);
}
}
;
expression: NUMBER { $$ = $1; }
| VAR { $$ = getVal($1); }
| expression PLUS expression {printf("\nAddition : %d+%d = %d \n",$1,$3,$1+$3 ); $$ = $1 + $3;}
| expression MINUS expression {printf("\nSubtraction : %d-%d=%d \n ",$1,$3,$1-$3); $$ = $1 - $3; }
| expression MUL expression {printf("\nMultiplication : %d*%d \n ",$1,$3,$1*$3); $$ = $1 * $3; }
| expression DIV expression { if($3){
printf("\nDivision : %d/%d \n ",$1,$3,$1/$3);
$$ = $1 / $3;
}
else{
$$ = 0;
printf("\nDivision by zero\n\t");
}
}
| expression MOD expression { if($3){
printf("\nMod : %d MOD %d \n",$1,$3,$1 % $3);
$$ = $1 % $3;
}
else{
$$ = 0;
printf("\nMOD by zero\n");
}
}
| expression '^' expression {printf("\nPower : %d ^ %d \n",$1,$3,$1 ^ $3); $$ = pow($1 , $3);}
| expression '<' expression {printf("\nLess Than : %d < %d \n",$1,$3,$1 < $3); $$ = $1 < $3 ; }
| expression '>' expression {printf("\nGreater than : %d > %d \n ",$1,$3,$1 > $3); $$ = $1 > $3; }
| '(' expression ')' { $$ = $2; }
| SIN '(' expression ')' {printf("Value of Sin(%d) is %lf\n",$3,sin($3*3.1416/180)); $$=sin($3*3.1416/180);}
| COS '(' expression ')' {printf("Value of Cos(%d) is %lf\n",$3,cos($3*3.1416/180)); $$=cos($3*3.1416/180);}
| TAN '(' expression ')' {printf("Value of Tan(%d) is %lf\n",$3,tan($3*3.1416/180)); $$=tan($3*3.1416/180);}
| LOG10 '(' expression ')' {printf("Value of Log10(%d) is %lf\n",$3,(log($3*1.0)/log(10.0))); $$=(log($3*1.0)/log(10.0));}
| LOG '(' expression ')' {printf("Value of Log(%d) is %lf\n",$3,(log($3))); $$=(log($3));}
| FACTORIAL '(' NUMBER ')' {
printf("\nFACTORIAL declaration\n");
int i;
int f=1;
for(i=1;i<=$3;i++)
{
f=f*i;
}
printf("FACTORIAL of %d is : %d\n",$3,f);
printf("\n\n");
}
;
%%
int yyerror(char *s){
printf( "%s\n", s);
}
int yywrap()
{
return 1;
}
int main()
{
freopen("input_test.txt","r",stdin);
//freopen("output.txt","w",stdout);
yyparse();
return 0;
}