-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.y
425 lines (378 loc) · 11.7 KB
/
syntax.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Struct.c"
extern FILE *yyin;
extern FILE *yyout;
extern int yylineno;
extern char *yytext;
extern int yylex();
extern void yyerror(const char *s);
int currentColumnNum = 1;
Line *Table_sym = NULL;
int i =25;
int ri=1;
typedef struct quadruplet{
char op[20];
char opr1[20];
char opr2[20];
char res[20];
}quadruplet;
int sauv_if_fin = 0;
int sauv_else =0;
int sauv_else_fin=0;
int sauv_for_test=0;
int sauv_for_init_id=0;
quadruplet quadruplets[1000];
int taille;
char pas[20];
%}
%union {
int val_int;
double val_float;
char val_str[255];
char val_char;
}
%token PROG
%token L_FOR L_WHILE
%token INPUT OUTPUT
%token IF ELSE
%token DOT
%token <val_char> COMMA
%token <val_char> SEMICOLON
%token OPENINGPARENTHESIS CLOSINGPARENTHESIS OPENINGBRACE CLOSINGBRACE OPENINGBRACKET CLOSINGBRACKET
%token EQUAL NONEQUAL AND OR NON INFERIOR SUPERIOR INFERIOREQUAL SUPERIOREQUAL
%token ADD SUB MULT DIV MOD POWER
%token ASSIGN
%token <val_str> BOOL
%token <val_int> INTEGER
%token <val_float> FLOAT
%token <val_char> CHAR
%token <val_str> INTDEC
%token <val_str> STRDEC
%token <val_str> BOOLDEC
%token <val_str> FLTDEC
%token <val_str> CHRDEC
%token <val_str> STRUCTDEC
%token INLINECOMMENT
%token BLOCCOMMENT
%token NEWLINE
%token <val_str> STR
%token <val_str> ID
%left OPENINGPARENTHESIS CLOSINGPARENTHESIS OPENINGBRACKET CLOSINGBRACKET DOT ;
%left NON;
%right POWER;
%left MULT DIV MOD;
%left ADD SUB;
%left INFERIOR INFERIOREQUAL SUPERIOR SUPERIOREQUAL;
%left EQUAL NONEQUAL;
%left AND;
%left OR;
%right ASSIGN;
%left COMMA;
%start Axiom;
%%
/* The grammar */
Axiom: | PROG ID OPENINGBRACE code CLOSINGBRACE ;
code: code code | affectation | commentaire | tabledeclaration | structdeclaration | declaration | statements | expression | read | write ;
declaration: type ID SEMICOLON {
if(get_id(Table_sym,$2)!=NULL){
printf("Variable already declared : %s \n ",$2);
YYERROR;
}else{
insertColumn(Table_sym,$<val_str>1,$2,"");
}
} ;
type: INTDEC | FLTDEC | STRDEC | BOOLDEC | CHRDEC | structure ;
structdeclaration: STRUCTDEC structure OPENINGBRACE declarations CLOSINGBRACE ;
declarations : declaration | declaration declarations;
structure: ID ;
tableelement : ID OPENINGBRACKET item CLOSINGBRACKET;
tabledeclaration: type OPENINGBRACKET tablesize CLOSINGBRACKET ID SEMICOLON;
tablesize: ID | expressionAR | INTEGER;
statements : if_stmt | if_else_stmt | for_stmt | while_stmt ;
if_stmt: B1 OPENINGBRACE code CLOSINGBRACE {
/* sauvegarder la fin du bloc "if" */
sprintf(quadruplets[sauv_if_fin].opr1,"%d",taille+1);
};
B1:IF expressionLG{
sauv_if_fin = taille-1;
};
if_else_stmt: A1 ELSE OPENINGBRACE code CLOSINGBRACE {
sprintf(quadruplets[sauv_else_fin].opr1,"%d",taille+1);
} ;
A1: A2 OPENINGBRACE code CLOSINGBRACE {
sprintf(quadruplets[sauv_else].opr1,"%d",taille+2);
strcpy(quadruplets[taille].op,"BR");
strcpy(quadruplets[taille].opr1,"");
strcpy(quadruplets[taille].opr2,"");
strcpy(quadruplets[taille].res,"");
sauv_else_fin = taille;
taille++;
} ;
A2:IF OPENINGPARENTHESIS expressionLG CLOSINGPARENTHESIS{
sauv_else=taille-1;
};
for_stmt: R1 OPENINGBRACE code CLOSINGBRACE{
char res[10];
strcpy( quadruplets[taille].op,"+");
strcpy( quadruplets[taille].opr1,quadruplets[sauv_for_init_id].res);
strcpy( quadruplets[taille].opr2,pas);
sprintf(res, "R%d", ri);
strcpy( quadruplets[taille].res,res);
taille++;
ri++;
strcpy( quadruplets[taille].op,"=");
sprintf(quadruplets[taille].opr1,"%s",res);
strcpy( quadruplets[taille].opr2,"");
strcpy( quadruplets[taille].res,quadruplets[sauv_for_init_id].res);
taille++;
strcpy( quadruplets[taille].op,"BR");
sprintf( quadruplets[taille].opr1,"%d", sauv_for_test + 1);
strcpy( quadruplets[taille].opr2,"");
strcpy( quadruplets[taille].res,"");
taille++;
sprintf(quadruplets[sauv_for_test].opr1,"%d",taille+1);
};
R1:R2 SEMICOLON INTEGER CLOSINGPARENTHESIS{
sprintf(pas,"%d",$<val_int>3);
};
R2:R3 SEMICOLON expressionLG {
sauv_for_test = taille-1;
};
R3:L_FOR OPENINGPARENTHESIS ID ASSIGN expressionAR{
if(get_id(Table_sym,$<val_str>3)!=NULL){
strcpy( quadruplets[taille].op,"=");
strcpy(quadruplets[taille].opr1,$<val_str>5);
strcpy( quadruplets[taille].opr2,"");
strcpy( quadruplets[taille].res,$<val_str>3);
sauv_for_init_id =taille;
taille++;
}else{
printf("Undeclared variable (first use in this operation) : %s\n",$<val_str>3);
YYERROR;
}
};
while_stmt: C1 OPENINGBRACE code CLOSINGBRACE{
};
C1:L_WHILE OPENINGPARENTHESIS expressionLG CLOSINGPARENTHESIS;
expression : expressionAR | expressionLG;
read: INPUT OPENINGPARENTHESIS ID CLOSINGPARENTHESIS SEMICOLON {
if(get_id(Table_sym,$<val_str>3)!=NULL){
strcpy( quadruplets[taille].op,"READ");
strcpy( quadruplets[taille].opr1,$<val_str>3);
strcpy( quadruplets[taille].opr2,"");
strcpy( quadruplets[taille].res,"");
taille++;
}else{
printf("Undeclared variable (first use in this operation) : %s\n",$<val_str>3);
YYERROR;
}
};
write: OUTPUT OPENINGPARENTHESIS expressionAR CLOSINGPARENTHESIS SEMICOLON {
strcpy( quadruplets[taille].op,"WRITE");
strcpy( quadruplets[taille].opr1,$<val_str>3);
strcpy( quadruplets[taille].opr2,"");
strcpy( quadruplets[taille].res,"");
taille++;
};
commentaire: commentaire commentaire | INLINECOMMENT | BLOCCOMMENT ;
expressionAR :OPENINGPARENTHESIS expressionAR CLOSINGPARENTHESIS {
strcpy( $<val_str>$,$<val_str>2);
}
| expressionAR ADD expressionAR {
char res[10];
strcpy( quadruplets[taille].op,"+");
strcpy( quadruplets[taille].opr1,$<val_str>1);
strcpy( quadruplets[taille].opr2,$<val_str>3);
sprintf(res, "R%d", ri);
strcpy( quadruplets[taille].res,res);
taille++;
ri++;
strcpy( $<val_str>$,res);
}
| expressionAR SUB expressionAR {
char res[10];
strcpy( quadruplets[taille].op,"-");
strcpy( quadruplets[taille].opr1,$<val_str>1);
strcpy( quadruplets[taille].opr2,$<val_str>3);
sprintf(res, "R%d", ri);
strcpy( quadruplets[taille].res,res);
taille++;
ri++;
strcpy( $<val_str>$,res);
}
| SUB expressionAR {
char res[10];
strcpy( quadruplets[taille].op,"-");
strcpy( quadruplets[taille].opr1,"0");
strcpy( quadruplets[taille].opr2,$<val_str>2);
sprintf(res, "R%d", ri);
strcpy( quadruplets[taille].res,res);
taille++;
ri++;
strcpy( $<val_str>$,res);
}| expressionAR MULT expressionAR {
char res[10];
strcpy( quadruplets[taille].op,"*");
strcpy( quadruplets[taille].opr1,$<val_str>1);
strcpy( quadruplets[taille].opr2,$<val_str>3);
sprintf(res, "R%d", ri);
strcpy( quadruplets[taille].res,res);
taille++;
ri++;
strcpy( $<val_str>$,res);
}| expressionAR DIV expressionAR {
char res[10];
strcpy( quadruplets[taille].op,"/");
strcpy( quadruplets[taille].opr1,$<val_str>1);
strcpy( quadruplets[taille].opr2,$<val_str>3);
sprintf(res, "R%d", ri);
strcpy( quadruplets[taille].res,res);
taille++;
ri++;
strcpy( $<val_str>$,res);
} | expressionAR MOD expressionAR {
char res[10];
strcpy( quadruplets[taille].op,"\%");
strcpy( quadruplets[taille].opr1,$<val_str>1);
strcpy( quadruplets[taille].opr2,$<val_str>3);
sprintf(res, "R%d", ri);
strcpy( quadruplets[taille].res,res);
taille++;
ri++;
strcpy( $<val_str>$,res);
}| expressionAR POWER expressionAR {
char res[10];
strcpy( quadruplets[taille].op,"**");
strcpy( quadruplets[taille].opr1,$<val_str>1);
strcpy( quadruplets[taille].opr2,$<val_str>3);
sprintf(res, "R%d", ri);
strcpy( quadruplets[taille].res,res);
taille++;
ri++;
strcpy( $<val_str>$,res);
}| item;
item: ID {
if(get_id(Table_sym,$<val_str>1)!=NULL){
strcpy($<val_str>$, $<val_str>1);
}else{
printf("Undeclared variable (first use in this operation) : %s\n",$<val_str>1);
YYERROR;
}
}
| INTEGER {
sprintf($<val_str>$,"%d",$<val_int>1);
}
| tableelement ;
champenreg: ID DOT ID ;
expressionLG: OPENINGPARENTHESIS expressionLG CLOSINGPARENTHESIS
{
strcpy($<val_str>$,$<val_str>2);
}
| expressionLG AND expressionLG
| expressionLG OR expressionLG
| NON expressionLG
| element ;
element : ID | BOOL | tableelement | champenreg
| expressionAR EQUAL expressionAR {
strcpy( quadruplets[taille].op,"BNE");
strcpy( quadruplets[taille].opr1,"");
strcpy( quadruplets[taille].opr2,$<val_str>1);
strcpy( quadruplets[taille].res,$<val_str>3);
taille++;
}
| expressionAR NONEQUAL expressionAR {
strcpy( quadruplets[taille].op,"BE");
strcpy( quadruplets[taille].opr1,"");
strcpy( quadruplets[taille].opr2,$<val_str>1);
strcpy( quadruplets[taille].res,$<val_str>3);
taille++;
}
| expressionAR INFERIOR expressionAR {
strcpy( quadruplets[taille].op,"BGE");
strcpy( quadruplets[taille].opr1,"");
strcpy( quadruplets[taille].opr2,$<val_str>1);
strcpy( quadruplets[taille].res,$<val_str>3);
taille++;
}
| expressionAR INFERIOREQUAL expressionAR {
strcpy( quadruplets[taille].op,"BG");
strcpy( quadruplets[taille].opr1,"");
strcpy( quadruplets[taille].opr2,$<val_str>1);
strcpy( quadruplets[taille].res,$<val_str>3);
taille++;
}
| expressionAR SUPERIOR expressionAR {
strcpy( quadruplets[taille].op,"BLE");
strcpy( quadruplets[taille].opr1,"");
strcpy( quadruplets[taille].opr2,$<val_str>1);
strcpy( quadruplets[taille].res,$<val_str>3);
taille++;
}
| expressionAR SUPERIOREQUAL expressionAR {
strcpy( quadruplets[taille].op,"BL");
strcpy( quadruplets[taille].opr1,"");
strcpy( quadruplets[taille].opr2,$<val_str>1);
strcpy( quadruplets[taille].res,$<val_str>3);
taille++;
};
affectation : ID ASSIGN expressionAR SEMICOLON {
if(get_id(Table_sym,$1)!=NULL){
strcpy( quadruplets[taille].op,"=");
strcpy(quadruplets[taille].opr1,$<val_str>3);
strcpy( quadruplets[taille].opr2,"");
strcpy( quadruplets[taille].res,$1);
taille++;
}else{
printf("Undeclared variable (first use in this operation) :%s\n",$1);
YYERROR;
}
}| type ID ASSIGN expressionAR SEMICOLON{
if(get_id(Table_sym,$2)==NULL){
insertColumn(Table_sym,$<val_str>1,$2,"");
strcpy( quadruplets[taille].op,"=");
strcpy(quadruplets[taille].opr1,$<val_str>4);
strcpy( quadruplets[taille].opr2,"");
strcpy( quadruplets[taille].res,$2);
taille++;
}else{
printf("Variable already declared :%s\n",$2);
YYERROR;
}
};
%%
void printSymbolTable() {
Line *line = Table_sym;
while (line != NULL) {
Column *column = line->Columns;
while (column != NULL) {
printf(" Type: %s\n", column->typeToken);
printf(" Name: %s\n", column->nameToken);
printf(" Value: %s\n", column->valeurToken);
column = column->suivC;
}
line = line->suivL;
}
}
int main(int argc, char **argv) {
Table_sym = insertLine(&Table_sym);
yyin = fopen(argv[1], "r");
yyout = fopen("quadruplets.txt", "w+");
int value = yyparse();
// stocker les quadruplets dans un fichiers
for(i=0 ; i<taille ; i++){
fprintf(yyout,"%d-(%s,%s,%s,%s)\n",i+1,quadruplets[i].op,quadruplets[i].opr1,quadruplets[i].opr2,
quadruplets[i].res);
}
if(value==1){
printf("\nError in line :%d and column : %d\n",yylineno,currentColumnNum);
}else{
printf("Compilation finished successfully!\n");
}
printSymbolTable();
fclose(yyin);
fclose(yyout);
return 0;
}