-
Notifications
You must be signed in to change notification settings - Fork 0
/
bison.y
375 lines (291 loc) · 7.04 KB
/
bison.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define DEFAULT 0
#define FFOR 1
#define FWHILE 2
int flag = DEFAULT;
int i,j;
char c;
int k = 0 ;
int k2 = 0;
FILE *fp ;
int loopcounter=0;
int linesWithError[100];
int startvalue=0;
int addvalue=0;
int endvalue=0;
char *Index;
char *printer;
char *copier;
char* tmp = "tmp";
static char buffer[200];
int errors=0;
extern int lines;
extern char* yytext;
extern int yylineno;
extern int yylval;
extern FILE *yyin;
extern FILE *yyout;
void copyPrint(const char s[],int choice);
void yyerror(const char* );
void copyFunc(char* );
int yylex(void);
extern FILE *output;
%}
%define parse.error verbose
%token EXTERN
%token VOID
%token BEG
%token END
%token IF
%token ELSE
%token RETURN
%token INT
%token BOOL
%token STRING
%token TRUE
%token FALSE
%token OPENBRACKET
%token CLOSEBRACKET
%token OPENBRACE
%token CLOSEBRACE
%token AMPERSAND
%token AND
%token OR
%token NOT
%token EQUAL
%token NON_EQUAL
%token LESS
%token GREATER
%token L_EQUAL
%token GR_EQUAL
%token ADD
%token SUB
%token MUL
%token DIV
%token MOD
%token SEMICOLON
%token COMMA
%token ID
%token STATHERASUMBOLOSEIRA
%token STATHERAAKERAIOU
%token WHILE
%token FOR
%left NOT
%left MUL DIV MOD
%left SUB ADD
%left EQUAL NON_EQUAL
%left GREATER GR_EQUAL LESS L_EQUAL
%left OR AND
%right ASSIGN
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
%start programma
%%
programma: ekswterikesdilwseis kefalidaprogrammatos tmimaorismwn tmimaentolwn;
ekswterikesdilwseis: ekswterikoprwtotupo2;
ekswterikoprwtotupo2: %empty
| ekswterikoprwtotupo ekswterikoprwtotupo2;
ekswterikoprwtotupo: EXTERN prwtotuposunartisis;
kefalidaprogrammatos: VOID ID OPENBRACKET CLOSEBRACKET
| VOID ID OPENBRACKET error CLOSEBRACKET {yyclearin; yyerrok;};
tmimaorismwn: orismos2;
orismos2: %empty
| orismos orismos2;
orismos: orismosmetablitwn
| orismossunartisis
| prwtotuposunartisis;
orismosmetablitwn: tuposdedomenwn_metavlites SEMICOLON;
tuposdedomenwn_metavlites: INT ID
| BOOL ID
| STRING ID
| tuposdedomenwn_metavlites COMMA ID;
orismossunartisis: kefalidasunartisis tmimaorismwn tmimaentolwn;
prwtotuposunartisis: kefalidasunartisis SEMICOLON;
kefalidasunartisis: typos_synartisis_metavlites OPENBRACKET c CLOSEBRACKET
| typos_synartisis_metavlites OPENBRACKET error CLOSEBRACKET{yyclearin; yyerrok;};
typos_synartisis_metavlites:INT ID
| BOOL ID
| VOID ID;
c: %empty
| listatupikwnparametrwn;
listatupikwnparametrwn: tupikesparametroi ntp;
ntp: %empty
| ptp ntp;
ptp: COMMA tupikesparametroi;
tupikesparametroi: tuposdedomenwn ID
| tuposdedomenwn AMPERSAND ID;
tuposdedomenwn: INT
| BOOL
| STRING;
tmimaentolwn: BEG command END;
command: %empty
| entoli command;
entoli: aplientoli SEMICOLON
| domimenientoli
| sunthetientoli
| entolifor
| entoliwhile;
sunthetientoli: OPENBRACE command CLOSEBRACE;
domimenientoli: entoliif;
aplientoli: anathesi
| klisisunartisis
| entolireturn
| entolinull;
entoliif: IF OPENBRACKET genikiekfrasi CLOSEBRACKET entoli %prec LOWER_THAN_ELSE
| IF OPENBRACKET genikiekfrasi CLOSEBRACKET entoli ELSE entoli;
anathesi: ID ASSIGN genikiekfrasi
| ID error genikiekfrasi
| ID ASSIGN genikiekfrasi error ';' {yyclearin; yyerrok; };
klisisunartisis: ID OPENBRACKET CLOSEBRACKET
| ID OPENBRACKET listapragmatikwnparametrwn CLOSEBRACKET;
listapragmatikwnparametrwn: pragmatikiparametros npp
| pragmatikiparametros error {yyclearin; yyerrok; };
npp: %empty
| ppp npp;
ppp: COMMA pragmatikiparametros;
pragmatikiparametros: genikiekfrasi;
entolireturn: RETURN
| RETURN genikiekfrasi;
entolinull: " ";
genikiekfrasi: genikosoros ngo;
ngo: %empty
| pgo ngo;
pgo: OR genikosoros;
genikosoros: genikosparagontas ngp;
ngp: %empty
| pgp ngp;
pgp: AND genikosparagontas;
genikosparagontas: genikosprwtparag
| NOT genikosprwtparag;
genikosprwtparag: apliekfrasi
| apliekfrasi tmimasugkrisis;
tmimasugkrisis: sugkritikostelestis apliekfrasi;
sugkritikostelestis: EQUAL
| NON_EQUAL
| LESS
| GREATER
| L_EQUAL
| GR_EQUAL;
apliekfrasi: aplosoros nao1;
nao1: %empty
| pao1 nao1;
pao1: addsub aplosoros;
addsub: ADD
| SUB;
aplosoros: aplosparagontas nao2;
nao2: %empty
| pao2 nao2;
pao2: muldivmod aplosparagontas;
muldivmod: MUL
| DIV
| MOD;
aplosparagontas: aplosprwtoros
| addsub aplosprwtoros;
aplosprwtoros: ID
| stathera
| klisisunartisis
| OPENBRACKET genikiekfrasi CLOSEBRACKET;
stathera: STATHERAAKERAIOU
| STATHERASUMBOLOSEIRA
| TRUE
| FALSE;
entolifor: FOR OPENBRACKET
ID {Index = strdup(yytext);} ASSIGN STATHERAAKERAIOU {startvalue=atoi(yytext);} SEMICOLON
ID LESS STATHERAAKERAIOU {endvalue=atoi(yytext);} SEMICOLON
ID ASSIGN ID ADD STATHERAAKERAIOU {addvalue=atoi(yytext);}
CLOSEBRACKET
{
loopcounter =(endvalue-startvalue)/addvalue;
if(loopcounter>3)
{
k = 1 ;
flag = FWHILE;
fprintf(output,"%s = %d ;\n",Index,startvalue);
fprintf(output,"while (%s < %d) ",Index,endvalue);
fprintf(output,"{ \n %s = %s + %d;",Index,Index,addvalue);
flag=DEFAULT;
}
else
{
k2=1;
k=1;
flag = FFOR;
}
}
OPENBRACE
command {k=1;}
CLOSEBRACE {
if (flag == FFOR)
copyPrint(tmp,2);
flag=DEFAULT;
};
entoliwhile: WHILE OPENBRACKET ID sugkritikostelestis STATHERAAKERAIOU CLOSEBRACKET entoli;
%%
void yyerror(char const *s)
{
fprintf (stderr, "%s\n", s);
linesWithError[errors]=yylineno;
errors++;
yyclearin;
}
int main(int argc, char *argv[])
{
fp = fopen (argv[1],"r");
output = fopen("output.txt", "w");
if(!fp)
{
printf("Can't open file \n");
return -1;
}
yyin = fp;
do {
yyparse();
}
while (!feof(yyin));
if(errors==0)
printf("Program Parsed Successfully! \n");
else
{
printf("There were %d parsing errors \n",errors);
for(i=0;i<errors;i++)
printf("Syntax Error on line %d \n",linesWithError[i]);
}
fclose(fp);
fclose(output);
}
void copyPrint(const char s[],int choice)
{
char *temp;
char *tokenize[1000];
int position=0;
if(choice == 1)
{
copier = strdup(s);
strcat(buffer,copier);
strcat(buffer," ");
}
else if(choice == 2)
{
int met = 0,j = 0;
temp = strdup(buffer);
printer= strtok(temp," ");
while(printer!=NULL)
{ tokenize[met] = printer;
met++;
printer = strtok(NULL," ");
}
for(i=startvalue;i<endvalue;i=i+addvalue)
{
for (j=0;j<met;j++)
{
if (!strcmp(Index,tokenize[j]) && strcmp("=",tokenize[j+1]))
fprintf(output,"%d ",i);
else
fprintf(output,"%s ",tokenize[j]);
}
}
}
}