-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS.y
326 lines (253 loc) · 15.2 KB
/
S.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
%{
extern int yylineo;
int i=0,n,j=0,e=0,m;
int col=1;
char vars[20][9];
char types[20][9];
char buf[25];
char cstype[10];
char sigtype[10];
char *type;
int a=0;
%}
%union {
int entier;
float reel;
char* str;
}
%token mc_ident mc_data mc_prog mc_work mc_proc mc_stopr mc_et mc_ou mc_non mc_sup mc_inf mc_supe
mc_infe mc_equi mc_diff mc_const <str>mc_int <str>mc_float <str>mc_char <str>mc_str mc_line mc_size mc_acc
mc_disp mc_if mc_else mc_move mc_to mc_end <entier>csti <reel>cstf <str>cstch <str>cstst aff <str>add <str>sous
<str>mult <str>divi <str>idff point parG parD sepv virg deup gui1 gui2 doll ecom arob pcen hash <str>disp
%type <str> CSTE TYPE2 CST OPER
%left mc_ou
%left mc_et
%left mc_sup mc_inf mc_supe mc_infe mc_equi mc_diff
%left add sous
%left mult divi
%start S
%%
S : mc_ident mc_prog idff point mc_data mc_work LIST_DEC mc_proc LIST_INSTR mc_stopr { printf ("\n\n\t\t\t\t\t --- Programme syntaxiquement correct --- \n\n");
printf ("\n\n\t\t --------------------------- Fin de la compilation --------------------------- \n\n"); YYACCEPT;}
;
LIST_DEC : DEC LIST_DEC
| ;
DEC : DEC_VAR TYPE2 {for(n=0;n<i;n++) {
if (doubleDeclaration(vars[n])==0) insererTYPE(vars[n],$2);
else printf ("<< Erreur semantique ( Idf deja declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,vars[n]);
}
i=0;}
| DEC_CST
| DEC_TAB TYPE2 {for(n=0;n<i;n++) {
if (doubleDeclaration(vars[n])==0) insererTYPE(vars[n],$2);
else printf ("<< Erreur semantique ( Idf deja declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,vars[n]);
}
i=0;}
;
DEC_VAR : idff sepv DEC_VAR { strcpy(vars[i],$1);
i++;}
| idff { strcpy(vars[i],$1);
i++; }
;
DEC_CST : mc_const A;
A: idff TYPE2 { if (doubleDeclaration($1)==0) { insererTYPE($1,$2);
insererCODE($1); }
else printf ("<< Erreur semantique ( Idf deja declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$1);
}
| idff aff CST point { if (doubleDeclaration($1)==0) { insererTYPE($1,cstype);
insererCODE($1);
modifier_val($1,$3); }
else printf ("<< Erreur semantique ( Idf deja declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$1);
}
;
TYPE2 : mc_int {$$=$1;}
| mc_float {$$=$1;}
| mc_char {$$=$1;}
| mc_str {$$=$1;}
;
CST : csti {strcpy(cstype,"INTEGER.");
sprintf(buf,"%d",$1); $$=buf;}
| cstf {strcpy(cstype,"FLOAT.");
sprintf(buf,"%f",$1); $$=buf;}
| cstch {strcpy(cstype,"CHAR.");
$$=$1;}
| cstst {strcpy(cstype,"STRING.");
$$=$1;}
;
DEC_TAB : idff sepv DEC_TAB { strcpy(vars[i],$1);
i++; }
| idff mc_line csti virg mc_size csti { if (($3<0)||($6<1)) printf ("<< Erreur semantique ( Taille du tableau ou borne inferieure hors limites ), ligne %d, colonne %d : %s ou %s >>\n",yylineo,col,$3,$6);
strcpy(vars[i],$1);
i++;}
;
LIST_INSTR : INSTR LIST_INSTR
|
;
INSTR : AFFECTATION | IF_STAT | BOUCLE | LECTURE | ECRITURE
;
LECTURE : mc_acc parG gui1 SIGNE gui2 deup arob idff parD point { if (doubleDeclaration($8)==0) printf ("<< Erreur semantique ( Idf non declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$8);
if (strcmp((char*)TypeEntite($8),sigtype)!=0) printf ("<< Erreur semantique ( Incompatibilite de types dans ACCEPT), ligne %d, colonne %d : %s >>\n",yylineo,col,$8);
}
;
SIGNE : ecom { strcpy(sigtype,"STRING."); }
| hash { strcpy(sigtype,"CHAR."); }
| pcen { strcpy(sigtype,"FLOAT."); }
| doll { strcpy(sigtype,"INTEGER."); }
;
ECRITURE : mc_disp parG disp deup LIST_IDF parD point {
m=nbSIGNE($3,types);
if(m>0){
if(m!=j) printf("<< Erreur semantique (Le nombre de signes ne corresponds aux nombres de variables a afficher) ligne %d, colonne %d >>\n",yylineo,col);
else {
for(i=0;i<j;i++)
{
if(strcmp((char*)TypeEntite(vars[j-i-1]),types[i])!=0)
printf ("<< Erreur semantique ( Incompatibilite de types dans DISPLAY), ligne %d, colonne %d : sur l'entite %s et type %s >>\n",yylineo,col,vars[j-i-1],types[i]);
}
}
}
j=0;
m=0;
}
| mc_disp parG disp parD point {
m=nbSIGNE($3,types);
if (m!=0) {printf("<< Erreur semantique ( Idf manquant ), ligne %d, colonne %d ",yylineo,col);}
}
;
LIST_IDF : idff virg LIST_IDF { strcpy(vars[j],$1);
if (doubleDeclaration($1)==0) printf ("<< Erreur semantique ( Idf non declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$1);
j++;
}
| idff { strcpy(vars[j],$1);
if (doubleDeclaration($1)==0) printf ("<< Erreur semantique ( Idf non declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$1);
j++;
}
;
IF_STAT : mc_if parG CONDITIONS parD deup LIST_INSTR B
;
B : mc_end
| mc_else deup LIST_INSTR mc_end
;
BOUCLE : mc_move idff mc_to idff LIST_INSTR mc_end { if ((doubleDeclaration($2)==0)||(doubleDeclaration($4)==0)) printf ("<< Erreur semantique ( Idf non declaree ), ligne %d, colonne %d : %s ou %s >>\n",yylineo,col,$2,$4);}
| mc_move idff mc_to csti LIST_INSTR mc_end { if (doubleDeclaration($2)==0) printf ("<< Erreur semantique ( Idf non declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$2);}
| mc_move csti mc_to idff LIST_INSTR mc_end { if (doubleDeclaration($4)==0) printf ("<< Erreur semantique ( Idf non declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$4);}
| mc_move csti mc_to csti LIST_INSTR mc_end
;
AFFECTATION : idff aff EXPRESSION point { if (doubleDeclaration($1)==0) printf ("<< Erreur semantique ( Idf non declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$1);
else { if (verifIDF($1)==1) printf ("<< Erreur semantique ( Constante ne peut pas changer de valeur), ligne %d, colonne %d : %s >>\n",yylineo,col,$1);
else { if (j==1) {
if(strcmp((char*)TypeEntite($1),(char*)TypeEntite(types[0]))!=0) {
printf ("<< Erreur semantique ( Incompatibilite de types), ligne %d, colonne %d : entre %s et %s >>\n",yylineo,col,$1,types[0]);
}
else { modifier_val2($1,types[0]); }
strcpy(types[0],"");
}
else { for(n=0;n<j;n++) {
if(strcmp((char*)TypeEntite($1),(char*)TypeEntite(types[n]))!=0) {
printf ("<< Erreur semantique ( Incompatibilite de types), ligne %d, colonne %d : entre %s et %s >>\n",yylineo,col,$1,types[n]);
}
strcpy(types[n],"");
}
}
}
j=0;
}
}
;
EXPRESSION : C OPER EXPRESSION
| C
| C OPER parG EXPRESSION parD
;
OPER : add {$$=$1; }
| sous {$$=$1; }
| mult {$$=$1; }
| divi {$$=$1; a=1; }
;
C : idff { if (doubleDeclaration($1)==0) printf ("<< Erreur semantique ( Idf non declaree ), ligne %d, colonne %d : %s >>\n",yylineo,col,$1);
else {
strcpy(types[j],$1);
j++;
if(a==1) {
if(recheche_val($1)==0) { printf("<< Erreur semantique ( division par 0 ), ligne %d, colonne %d : %s >>\n",yylineo,col,$1);}
}
if(a==1) a=0;
}
}
| CSTE { strcpy(types[j],$1);
j++; }
;
CSTE : csti {
if(($1==0) && (a==1)) {
printf("<< Erreur semantique ( division par 0 ), ligne %d, colonne %d : %d >>\n",yylineo,col,$1);
a=0;
}
if(a==1) a=0;
sprintf(buf,"%d",$1); $$=buf;
}
| cstf {
if(($1==0) && (a==1)) {
printf("<< Erreur semantique ( division par 0 ), ligne %d, colonne %d : %f >>\n",yylineo,col,$1);
a=0;
}
if(a==1) a=0;
sprintf(buf,"%lf",$1); $$=buf;
}
;
CONDITIONS : CONDITION_SIMPLE | CONDITION_COMPLEXE
;
CONDITION_SIMPLE : CONDITION
;
CONDITION_COMPLEXE : parG CONDITION parD BETA CONDITION_COMPLEXE
| parG CONDITION parD
;
CONDITION : EXPRESSION EQ EXPRESSION { for(n=1;n<j;n++) {
if(strcmp((char*)TypeEntite(types[0]),(char*)TypeEntite(types[n]))!=0) {
printf ("<< Erreur semantique ( Incompatibilite de types), ligne %d, colonne %d : entre %s et %s >>\n",yylineo,col,types[0],types[n]);
}
strcpy(types[n],"");
}
strcpy(types[0],"");
j=0;
}
| EXPRESSION { for(n=1;n<j;n++) {
if(strcmp((char*)TypeEntite(types[0]),(char*)TypeEntite(types[n]))!=0) {
printf ("<< Erreur semantique ( Incompatibilite de types), ligne %d, colonne %d : entre %s et %s >>\n",yylineo,col,types[0],types[n]);
}
strcpy(types[n],"");
}
strcpy(types[0],"");
j=0;
}
| mc_non parG CONDITION parD { for(n=1;n<j;n++) {
if(strcmp((char*)TypeEntite(types[0]),(char*)TypeEntite(types[n]))!=0) {
printf ("<< Erreur semantique ( Incompatibilite de types), ligne %d, colonne %d : entre %s et %s >>\n",yylineo,col,types[0],types[n]);
}
strcpy(types[n],"");
}
strcpy(types[0],"");
j=0;
}
;
EQ : mc_sup
| mc_inf
| mc_supe
| mc_infe
| mc_equi
| mc_diff
;
BETA : mc_ou
| mc_et
;
%%
main ()
{
initialisation();
printf ("\n\n\t\t --------------------------- Debut de la compilation --------------------------- \n\n");
yyparse();
afficher();
}
yywrap()
{}
int yyerror (char *msg ) {
printf ("Erreur syntaxique, ligne %d, colonne %d \n",yylineo,col);
return 1; }