-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
404 lines (314 loc) · 10.2 KB
/
parser.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
%{
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <getopt.h>
#include <linux/limits.h>
#include "ast.h"
#include "arc_utils.h"
#include "codegen.h"
#include "symbol_table.h"
#include "semantic.h"
#include "preprocessor.h"
#include "arc_options.h"
extern int yylex();
extern int yylex_destroy();
void yyerror(const char *s);
ast *abstract_tree = NULL;
symb_table table = NULL;
char *src = NULL;
char *exename = NULL;
char *include_path = NULL;
int is_dbg_mode = 0;
int print_tree = 0;
int print_table = 0;
int line_offset = 0;
int mem_size = 0;
char PROJECT_PATH[PATH_MAX];
FILE *fp_out;
%}
%union {
int nb;
char id[32];
struct ast *tree;
};
%define parse.error verbose
%locations
%token <nb> NB
%token <id> ID
%token PROGRAMME DEBUT FIN VAR ALGO
%token PROTO
%token AFFECT_SYMB "<-"
%token LE "<="
%token GE ">="
%token DIFF "!="
%token LIRE
%token ECRIRE
%token TQ
%token FAIRE
%token FTQ
%token SI
%token ALORS
%token SINON
%token FSI
%token POUR
%token DANS
%token FPOUR
%token RANGE_SYMB "..."
%token RETOURNER
%token ALLOUER
%token VRAI FAUX
%token '\n'
/* Priorités (du - prioritaire au + prioritaire) */
%right AFFECT_SYMB
%left OU
%left ET
%left NON
%nonassoc '<' '>' '=' "<=" ">=" "!="
%left '+' '-'
%left '*' '/' '%'
%token '(' ')'
/* Axiome */
%start PROGRAMME_ALGO
%type <tree> EXP
%type <tree> PROGRAMME_ALGO
%type <tree> CORPS_FUNC
%type <tree> LISTE_INSTR
%type <tree> INSTR
%type <tree> PROG_MAIN
%type <tree> AFFECTATION
%type <tree> DECLARATION
%type <tree> LIST_DECLA_VAR
%type <tree> STRUCT_SI
%type <tree> STRUCT_TQ
%type <tree> STRUCT_FAIRE_TQ
%type <tree> STRUCT_POUR
%type <tree> LIST_DECLA
%type <tree> FONCTION
%type <tree> LIST_ARGS
%type <tree> LIST_EXPR
%type <tree> APPEL_FUNC
%type <tree> LIRE_INSTR
%type <tree> ECRIRE_INSTR
%type <tree> AFFECT_TAB
%type <tree> ACCES_TAB
%type <tree> DECLA_TAB
%type <tree> ALLOC_INSTR
%type <tree> PROTO_FONCTION
%%
SEP : '\n'
| ';'
| SEP '\n'
| SEP ';'
;
END_FILE: SEP
| %empty
;
SEP_STRUCT: SEP
| %empty
;
DECLA_TAB: ID '[' NB ']' {$$ = create_arr_decla_node($1, $3, NULL);}
| ID '[' NB ']' "<-" '[' LIST_EXPR ']'
{$$ = create_arr_decla_node($1, $3, $7);}
;
LIST_DECLA_VAR: ID "<-" EXP {$$ = create_var_decla_node(create_id_leaf($1), $3, NULL, integer);}
| ID {$$ = create_var_decla_node(create_id_leaf($1), NULL, NULL, integer);}
| LIST_DECLA_VAR ',' ID {$$ = create_var_decla_node(create_id_leaf($3), NULL, $1, integer);}
| LIST_DECLA_VAR ',' ID "<-" EXP {$$ = create_var_decla_node(create_id_leaf($3), $5, $1, integer);}
| DECLA_TAB {$$ = create_var_decla_node($1, NULL, NULL, array);}
| LIST_DECLA_VAR ',' DECLA_TAB {$$ = create_var_decla_node($3, NULL, $1, array);}
| '@'ID {$$ = create_var_decla_node(create_id_leaf($2), NULL, NULL, pointer);}
| LIST_DECLA_VAR ',' '@'ID {$$ = create_var_decla_node(create_id_leaf($4), NULL, $1, pointer);}
;
DECLARATION: VAR LIST_DECLA_VAR SEP {$$ = create_decla_node($2, NULL);}
| FONCTION {$$ = create_decla_node($1, NULL);}
| PROTO_FONCTION {$$ = create_decla_node($1, NULL);}
;
LIST_DECLA: %empty {$$ = NULL;}
| DECLARATION LIST_DECLA {$1->decla_list.next = $2; $$ = $1;}
;
AFFECTATION: ID "<-" EXP {$$ = create_affect_node($1, $3, 0);}
| '*'ID "<-" EXP {$$ = create_affect_node($2, $4, 1);}
;
PROGRAMME_ALGO: SEP_STRUCT LIST_DECLA PROG_MAIN END_FILE
{$$ = create_prog_root($2, $3); abstract_tree = $$;}
;
PROG_MAIN: PROGRAMME '(' ')' SEP
LIST_DECLA
DEBUT SEP
CORPS_FUNC
FIN {$$ = create_function_node("PROGRAMME", NULL, $5, $8);}
;
LIST_ARGS: %empty {$$ = NULL;}
| ID {$$ = create_var_decla_node(create_id_leaf($1), NULL, NULL, integer);}
| LIST_ARGS ',' ID {$$ = create_var_decla_node(create_id_leaf($3), NULL, $1, integer);}
| '@'ID {$$ = create_var_decla_node(create_id_leaf($2), NULL, NULL, pointer);}
| LIST_ARGS ',' '@'ID {$$ = create_var_decla_node(create_id_leaf($4), NULL, $1, pointer);}
;
LIST_EXPR: %empty {$$ = NULL;}
| EXP {$$ = create_exp_list_node($1, NULL);}
| LIST_EXPR ',' EXP {$$ = create_exp_list_node($3, $1);}
;
PROTO_FONCTION: PROTO ID '(' LIST_ARGS ')' SEP {
/* Un exemple de comment gérer proprement les erreurs (très lourd) */
yylloc.first_line = @2.first_line;
yylloc.last_column = @5.last_column;
$$ = create_proto_node($2, $4);
}
;
FONCTION: ALGO ID '(' LIST_ARGS ')' SEP
LIST_DECLA
DEBUT SEP
CORPS_FUNC
FIN
SEP {$$ = create_function_node($2, $4, $7, $10);}
;
CORPS_FUNC: LISTE_INSTR {$$ = $1;}
;
APPEL_FUNC: ID '(' LIST_EXPR ')' {$$ = create_function_call_node($1, $3);}
;
ACCES_TAB: ID '[' EXP ']' {$$ = create_arr_access_node($1, $3, NULL);}
;
AFFECT_TAB: ID '[' EXP ']' "<-" EXP {$$ = create_arr_access_node($1, $3, $6);}
;
EXP: '(' EXP ')' {$$ = $2;}
| ACCES_TAB {$$ = $1;}
| ID {$$ = create_id_leaf($1);}
| NB {$$ = create_nb_leaf(yylval.nb);}
| NON EXP {$$ = create_u_op_node(NOT_OP, $2);}
| '-' EXP {$$ = create_u_op_node('-', $2);}
| '@' ID {$$ = create_u_op_node('@', create_id_leaf($2));}
| '*' ID {$$ = create_u_op_node('*', create_id_leaf($2));}
| EXP '+' EXP {$$ = create_b_op_node('+', $1, $3);}
| EXP '-' EXP {$$ = create_b_op_node('-', $1, $3);}
| EXP '*' EXP {$$ = create_b_op_node('*', $1, $3);}
| EXP '/' EXP {$$ = create_b_op_node('/', $1, $3);}
| EXP '%' EXP {$$ = create_b_op_node('%', $1, $3);}
| EXP '<' EXP {$$ = create_b_op_node('<', $1, $3);}
| EXP '>' EXP {$$ = create_b_op_node('>', $1, $3);}
| EXP '=' EXP {$$ = create_b_op_node('=', $1, $3);}
| EXP "<=" EXP {$$ = create_b_op_node(LE_OP, $1, $3);}
| EXP ">=" EXP {$$ = create_b_op_node(GE_OP, $1, $3);}
| EXP "!=" EXP {$$ = create_b_op_node(NE_OP, $1, $3);}
| EXP OU EXP {$$ = create_b_op_node(OR_OP, $1, $3);}
| EXP ET EXP {$$ = create_b_op_node(AND_OP, $1, $3);}
| VRAI {$$ = create_nb_leaf(1);}
| FAUX {$$ = create_nb_leaf(0);}
| APPEL_FUNC {$$ = $1;}
| LIRE_INSTR {$$ = $1;}
;
INSTR: EXP SEP {$$ = $1;}
| AFFECTATION SEP {$$ = $1;}
| AFFECT_TAB SEP {$$ = $1;}
| ECRIRE_INSTR SEP {$$ = $1;}
| STRUCT_TQ SEP {$$ = $1;}
| STRUCT_SI SEP {$$ = $1;}
| STRUCT_FAIRE_TQ SEP {$$ = $1;}
| STRUCT_POUR SEP {$$ = $1;}
| RETOURNER EXP SEP {$$ = create_return_node($2);}
| RETOURNER SEP {$$ = create_return_node(NULL);}
| ALLOC_INSTR SEP {$$ = $1;}
;
LISTE_INSTR: INSTR {$$ = create_instr_node($1, NULL);}
| LISTE_INSTR INSTR {$$ = create_instr_node($2, $1);}
;
// Autres structures
ALLOC_INSTR: ALLOUER '(' ID ',' EXP ')' {$$ = create_alloc_node($3, $5);}
;
LIRE_INSTR: LIRE '(' ')' {$$ = create_io_node(NULL, 'r');}
;
ECRIRE_INSTR: ECRIRE '(' EXP ')' {$$ = create_io_node($3, 'w');}
;
STRUCT_TQ: TQ EXP FAIRE SEP_STRUCT LISTE_INSTR FTQ {$$ = create_while_node($2, $5);}
;
STRUCT_FAIRE_TQ: FAIRE SEP_STRUCT
LISTE_INSTR TQ EXP {$$ = create_do_while_node($3, $5);}
;
STRUCT_POUR: POUR ID DANS EXP "..." EXP FAIRE SEP_STRUCT
LISTE_INSTR FPOUR {$$ = create_for_node($2, $4, $6, $9);}
;
STRUCT_SI:
SI EXP ALORS SEP_STRUCT
LISTE_INSTR
SINON SEP_STRUCT
LISTE_INSTR
FSI {$$ = create_if_node($2, $5, $8);}
| SI EXP ALORS SEP_STRUCT
LISTE_INSTR
FSI {$$ = create_if_node($2, $5, NULL);}
;
%%
int main(int argc, char **argv)
{
extern FILE *yyin;
/*
* Bricolage mais fonctionne:
* Permet d'obtenir le chemin vers le projet.
* Utilisé pour le chemin vers la librairie standard dans preprocessor.c
*
* __FILE__ contient le chemin du fichier passé en paramètre à gcc.
* C'est pour cette raison que l'on donne le chemin complet de parser.y
* dans le makefile.
*
* La 2ème ligne permet de supprimer le bout de chemin en trop.
*
* Ainsi, peu importe d'où l'exécutable est lancé, le chemin vers la
* librairie standard sera correct.
*/
strcpy(PROJECT_PATH, __FILE__);
PROJECT_PATH[strlen(PROJECT_PATH) - strlen("/src/parser.y")] = '\0';
/* Traitement des options de la ligne de commande */
handle_options(argc, argv);
/* Phase préprocesseur */
yyin = preprocessor(src, &line_offset);
/* Initialisation de la table des symboles */
table = init_symb_table("global");
abstract_tree = NULL;
/* Analyse lexicale / syntaxique */
yyparse();
/* Supprime le fichier intermédiaire utilisé */
system("rm ./__arc_PP.algo_pp");
/* Analyse sémantique */
semantic(abstract_tree);
second_turn_semantic(abstract_tree, NULL);
/* Affichage si demandé par l'utilisateur */
if (print_tree) ast_to_img(abstract_tree, "ast", "png");
if (print_table) symb_to_img(table, "table", "png");
/* Ouverture du fichier de sortie */
fp_out = fopen(exename, "w");
if (fp_out == NULL)
{
fatal_error("impossible d'ouvrir ~U%s~E", exename);
exit(F_INPUT_ERROR);
}
/* Génération du code */
init_ram_os();
codegen(abstract_tree);
fclose(yyin);
free_table(table);
fclose(fp_out);
if (is_dbg_mode)
{
/* Temporaire, pour vérifier que semantic marche bien */
char buff[256];
size_t codelen_total = abstract_tree->codelen + 7; // + 7 pour ramOS
sprintf(buff, "echo \"Codelen total: %ld\nNombre de lignes "\
"dans le fichier produit: \" && wc -l %s", codelen_total, exename);
system(buff);
}
/* Libération de la mémoire */
free(src);
free(exename);
free_ast(abstract_tree);
if (include_path != NULL) free(include_path);
/* Libère la mémoire non-libérée par bison */
yylex_destroy();
return 0;
}
void yyerror(const char *s)
{
set_error_info(yylloc);
fatal_error("%s", s);
exit(BISON_ERROR);
}