-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog.y
More file actions
273 lines (254 loc) · 7.61 KB
/
prog.y
File metadata and controls
273 lines (254 loc) · 7.61 KB
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
%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "functions.c"
int yyparse();
int yylex();
int yyerror();
int ifdone[1000];
int ifptr=0;
struct ll_identifier *root=NULL,*last=NULL;
%}
%union {
char text[1000];
int val;
int lineval;
}
%token <text> ID
%token <val> NUM
%token <text> STR
%type <val> expression
%left LT GT LE GE
%left PLUS MINUS
%left MULT DIV
%token INT DOUBLE CALCULATE WHILE FOR CHAR VOID MAIN DOT PB PE BB BE VARD VARC SM CM ASGN PRINT PRINTLN MINUS MULT DIV LT GT LE GE IF ELSE ELSEIF COL FUNCTION EQU NEQU INPUT CA
%nonassoc IFX
%nonassoc ELSE
%left SH
%%
starhere : function program function {
printf("\n Compilation Successful\n");
}
;
program : INT MAIN PB PE BB statement BE
;
statement : /* empty */
| statement declaration
| statement print
| statement ifelse
| statement mathexpr
| statement assign
| statement whilestmt
| statement forloopstmt
;
mathexpr : expression SM { printf("Value of expression :: %d\n",$1);}
declaration : type variables SM {
//printf("came hear declaration\n");
}
;
type : INT | DOUBLE | CHAR {
//printf("got type\n");
}
;
variables : variable CM variables {
//printf("variable , variables\n");
}
| variable {
//printf("variable\n");
}
;
variable :ID
{
// printf("ok2 %s\n",$1);
int res = addNewVal(&root,&last,$1,"");
if(!res)
{
printf("Compilation Error :: Varribale %s is already declared\n",$1);
exit(-1);
}
}
|ID ASGN expression
{
//printf("ok1 %s %d\n",$1,$3);
int n = log10($3) + 1;
char *numberArray = calloc(n, sizeof(char));
sprintf(numberArray,"%ld",$3);
int res = addNewVal(&root,&last,$1,numberArray);
if(!res)
{
printf("Compilation Error :: Varribale %s is already declared\n",$1);
exit(-1);
}
}
;
assign : ID ASGN expression CM assign {
//printf("came assign\n");
}
| ID ASGN expression SM
{
//printf("Came here\n");
struct ll_identifier* decl = isDeclared(&root,$1);
if(decl==NULL)
{
printf("Compilation Error :: Varribale %s is not declared\n",$1);
exit(-1);
}
else
{
int n = log10($3) + 1;
char *numberArray = calloc(n, sizeof(char));
sprintf(numberArray,"%ld",$3);
setVal(&root,&last,$1,numberArray);
}
}
;
print :PRINT PB expression PE SM
{
printf("Value of expression :: %f\n",$3);
}
|PRINT PB ID PE SM
{
struct ll_identifier* decl = isDeclared(&root,$3);
if(decl==NULL)
{
printf("Compilation Error :: Varribale %s is not declared\n",$3);
exit(-1);
}
else
{
print(decl->data);
//printf("Value of %s :: %d\n",$3,decl->data.intval);
}
}
| PRINT PB STR PE SM
{
printf("Print String :: %s\n",$3);
}
| PRINTLN PB PE SM
{
printf("\n");
}
;
expression :NUM {$$=$1; printf("output: %d\n",$$);}
| ID
{
struct ll_identifier* res = isDeclared(&root,$1);
if(res==NULL)
{
printf("Compilation Error :: Varribale %s is not declared\n",$1);
exit(-1);
}
else
{
if(res->data.type==1)
$$ = res->data.intval;
else if(res->data.type==2)
$$ = res->data.doubleval;
}
}
| expression PLUS expression
{
$$ = $1+$3;
}
| expression MINUS expression
{
$$ = $1-$3;
}
| expression MULT expression
{
$$ = $1*$3;
}
| expression DIV expression
{
if($3==0)
{
printf("Compilation Error :: Divide by Zero Occured\n");
exit(-1);
}
$$ = $1/$3;
}
| expression LT expression
{
$$ = ($1<$3);
}
| expression GT expression
{
$$ = ($1>$3);
}
| expression LE expression
{
$$ = ($1<=$3);
}
| expression GE expression
{
$$ = ($1>=$3);
}
| expression EQU expression
{
$$ = ($1==$3);
}
| expression NEQU expression
{
$$ = ($1!=$3);
}
| PB expression PE
{
$$ = $2;
}
;
ifelse : IF PB expression PE BB statement BE {
ifptr++;
if($3>0){
printf("IF Executed\n");
ifdone[ifptr]=1;
}
} elseif {
ifptr--;
}
;
elseif : /* empty */
| ELSEIF PB expression PE BB statement BE
{
if(ifdone[ifptr]==0 && $3>0){
ifdone[ifptr]=1;
printf("ELSE IF executed\n");
}
} elseif
| elseif ELSE BB statement BE
{
if(ifdone[ifptr]==0)
printf("ELSE Executed\n");
}
;
function : /* empty */
| function func
;
func :type FUNCTION PB fparameter PE BB statement BE
{
printf("\nfunction declared\n");
}
;
fparameter : /* empty */
| type ID fsparameter
;
fsparameter : /* empty */
| fsparameter CM type ID
;
whilestmt : WHILE PB expression PE BB statement BE{
printf("while condition executed for %d\n",$3);
}
;
forloopstmt : FOR PB forassign SM expression SM forassign PE BB statement BE{
printf("for loop executed\n");
}
;
forassign :ID ASGN expression CM forassign
| ID ASGN expression
%%
extern char yytext[];
extern int column;
int yyerror(char *s){
fflush(stdout);
printf("\n%*s\n%*s\n", column, "^", column, s);
}