-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
433 lines (413 loc) · 10.4 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* @file
* @brief Archivo de entrada para `bison`.
*
* Esta versión soluciona el problema de múltiples `stmt` en una línea. Ej:
*
* if 1 {print(2); print(4)} fn F(n) { n + 1 }
*/
%require "3.8.2"
// https://www.gnu.org/software/bison/manual/html_node/Token-Decl.html
// https://www.gnu.org/software/bison/manual/html_node/Precedence-Decl.html
// https://www.gnu.org/software/bison/manual/html_node/Shift_002fReduce.html
// https://www.gnu.org/software/bison/manual/html_node/Destructor-Decl.html
%{
#include "bison.h"
YYLTYPE g_prev_loc;
int g_scope = 0;
int g_loops = 0;
int g_fns = 0;
%}
%locations
%define api.location.type {YYLTYPE}
%define parse.error custom // Mensajes de error personalizados
// %define parse.error verbose // Información extra en los mensajes de error
// %define parse.error detailed
/* %define parse.lac full // Ayuda con la información extra en errores */
/* %define api.token.prefix {TK_} */
%union {
AstNode *ast;
AstNodeT ast_t;
}
%token
LET "let"
IF "if"
ELIF "elif"
ELSE "else"
WHILE "while"
FOR "for"
IN "in"
FN "fn"
RETURN "return"
BREAK "break"
CONTINUE "continue"
EXIT "exit"
DEL "del"
OR "or"
AND "and"
NOT "not"
EQ "=="
NE "!="
LE "<="
GE ">="
ADD_A "+="
SUB_A "-="
MUL_A "*="
DIV_A "/="
EOL "fin de línea"
%token <ast>
ID "identificador"
ATOM_LIT "átomo"
INT_LIT "entero"
DOTS "..."
%type <ast>
stmt_list
stmt
block
fn
params
if
elif
for
while
decl
assign
assignable
assignable_list
id_list
expr_list
del
del_target_list
del_target
insert
return
expr
and
not
cmp
sum
term
factor
primary
call
args
elem
%type <ast_t>
assign_op
%destructor {
print_dbg("%%destructor: <ast>");
ast_free($$);
} <ast>
%expect 2 // token: error
%start program
%%
program
: stmt_list eos { g_ast = ast_reverse_ll($1); }
;
stmt_list
: stmt_list eos stmt {
// printf("stmt_list -> stmt_list eos stmt\n");
if($1) $$ = $[stmt] ? ast_add_next_ll_n(ast_new_ll_n($[stmt], @[stmt]), $1) : $1;
else $$ = ast_new_ll_n($[stmt], @[stmt]);
// Si no se está en un bloque
if($[stmt] && !g_scope) {
if(g_repl) repl_eval($[stmt]);
// En archivos también se evalúa stmt por stmt para un mejor reporte de errores
else file_eval($[stmt]);
}
CHECK_EXIT($$);
}
| stmt {
// printf("stmt_list -> stmt\n");
$$ = ast_new_ll_n($[stmt], @[stmt]);
if($[stmt] && !g_scope) {
if(g_repl) repl_eval($[stmt]);
else file_eval($[stmt]);
}
CHECK_EXIT($$);
}
| stmt_list error eos stmt { // Captura errores y evita perder el AST
// printf("stmt_list -> stmt_list error eos stmt\n");
if($1) $$ = $[stmt] ? ast_add_next_ll_n(ast_new_ll_n($[stmt], @[stmt]), $1) : $1;
else $$ = ast_new_ll_n($[stmt], @[stmt]);
// Si no se está en un bloque
if($[stmt] && !g_scope) {
if(g_repl) repl_eval($[stmt]);
// En archivos también se evalúa stmt por stmt para un mejor reporte de errores
else file_eval($[stmt]);
}
CHECK_EXIT($$);
}
| error { $$ = NULL; }
;
eos
: EOL { yyerrok; repl_prompt(); }
| ';' { yyerrok; }
;
stmt
: %empty { $$ = NULL; }
| decl
| assign
| del
| insert
| if
| for
| while
| fn
| expr { $$ = ast_new_exprst($1); }
| "exit" { $$ = ast_new_exit(); }
| "break" {
if(g_loops <= 0) {
yyerror("'break' fuera de un ciclo.");
YYERROR;
} else $$ = ast_new_break();
}
| "continue" {
if(g_loops <= 0) {
yyerror("'continue' fuera de un ciclo.");
YYERROR;
} else $$ = ast_new_continue();
}
| return {
if(g_fns <= 0) {
yyerror("'return' fuera de una función.");
YYERROR;
} else $$ = $1;
}
;
/*
debug
: '.' ID { dbg_stmt(NULL, $2, false); }
| expr '.' ID { dbg_stmt($1, $3, false); }
| stmt '.' ID { dbg_stmt($1, $3, false); }
| ID '.' ID { dbg_stmt($1, $3, false); }
| expr '.' ID '!' { dbg_stmt($1, $3, true); }
| stmt '.' ID '!' { dbg_stmt($1, $3, true); }
| ID '.' ID '!' { dbg_stmt($1, $3, true); }
;
*/
block
: '{' { g_scope++; } stmt_list '}' { g_scope--;
$$ = ast_reverse_ll($[stmt_list]);
}
;
fn
: "fn" ID '(' { g_scope++; } params ')' { g_scope--; g_fns++; } block {
g_fns--;
if(g_scope > 0) { yyerror("Definición de función dentro de un bloque"); YYERROR; }
else $$ = ast_new_fndef($[ID], ast_reverse_ll($[params]), $[block]);
}
;
params
: id_list { $$ = $1; }
| id_list ',' "..." {
$$ = ast_add_next_ll_n(ast_new_ll_n($3, @3), $1);
}
| "..." { $$ = ast_new_ll_n($1, @1); }
| %empty { $$ = NULL; }
;
if
: "if" expr block elif {
$$ = ast_new_if($2, $3, $4);
}
;
elif
: "elif" expr block elif {
$$ = ast_new_ll_n(ast_new_if($2, $[block], $4), @1);
}
| "else" block { $$ = $2; }
| %empty { $$ = NULL; }
;
for
: "for" { g_scope++; } ID { g_scope--; } "in" expr { g_loops++; } block {
g_loops--;
$$ = ast_new_for($[ID], $[expr], $[block]);
}
;
while
: "while" expr { g_loops++; } block {
g_loops--;
$$ = ast_new_while($2, $[block]);
}
;
decl
: "let" id_list { $$ = ast_new_decl(ast_reverse_ll($2), NULL); }
// Declaración e inicialización
| "let" id_list '=' expr_list {
$$ = ast_new_decl(ast_reverse_ll($2), ast_reverse_ll($4));
}
;
assign
: assignable_list assign_op expr_list {
$$ = ast_new_assign(ast_reverse_ll($1), $2, ast_reverse_ll($3));
}
;
assignable_list
: assignable_list ',' assignable {
// La lista se construye en orden inverso
$$ = ast_add_next_ll_n(ast_new_ll_n($3, @3), $1);
}
| assignable { $$ = ast_new_ll_n($1, @1); }
;
assignable
: ID
/* | ID '[' sum ']' { $$ = ast_new_getelem($1, $3); } */
| elem
;
insert
: primary '[' '!' expr ']' '=' expr { $$ = ast_new_insert($1, $4, -1, $7); }
| primary '[' expr '!' ']' '=' expr { $$ = ast_new_insert($1, $3, 1, $7); }
;
id_list
: id_list ',' ID {
// La lista se construye en orden inverso
$$ = ast_add_next_ll_n(ast_new_ll_n($3, @3), $1); // ID nunca es NULL
}
| ID { $$ = ast_new_ll_n($1, @1); }
;
expr_list
: expr_list ',' expr {
// La lista se construye en orden inverso
$$ = ast_add_next_ll_n(ast_new_ll_n($3, @3), $1); // expr nunca es NULL
}
| expr { $$ = ast_new_ll_n($1, @1); }
;
assign_op
: '=' { $$ = ASSIGN_AST; }
| "+=" { $$ = ADD_AST; }
| "-=" { $$ = SUB_AST; }
| "*=" { $$ = MUL_AST; }
| "/=" { $$ = DIV_AST; }
;
del
: "del" del_target_list { $$ = ast_new_del($2); }
;
del_target_list
: del_target_list ',' del_target { $$ = ast_add_next_ll_n(ast_new_ll_n($3, @3), $1); }
| del_target { $$ = ast_new_ll_n($1, @1); }
;
del_target
: ID
| elem
| cmp "in" sum {
$$ = ast_new_in($1, $3);
}
;
return
: "return" expr { $$ = ast_new_return($2); }
| "return" { $$ = ast_new_return(NULL); }
;
expr
: expr "or" and { $$ = ast_new_or($1, $3); }
| and
;
and
: and "and" not { $$ = ast_new_and($1, $3); }
| not
;
not
: "not" not { $$ = ast_new_not($2); }
| cmp
;
cmp
: cmp "==" sum { $$ = ast_new_equal($1, $3); }
| cmp "!=" sum { $$ = ast_new_not(ast_new_equal($1, $3)); }
| cmp '<' sum { $$ = ast_new_less($1, $3); }
| cmp '>' sum { $$ = ast_new_less($3, $1); }
| cmp "<=" sum { $$ = ast_new_not(ast_new_less($3, $1)); }
| cmp ">=" sum { $$ = ast_new_or(ast_new_less($3, $1), ast_new_equal($3, $1)); }
| cmp "not" "in" sum {
// Negación de pertenencia (permite "x not in X" en vez de "not x in X")
$$ = ast_new_not(ast_new_in($1, $4));
}
| cmp "in" sum { // Pertenencia | Posición de un elemento
$$ = ast_new_in($1, $3);
}
| sum
;
sum
: sum '+' term { // Suma | Unión | Concatenación
$$ = ast_new_add($1, $3);
}
| sum '-' term { // Resta | Diferencia
$$ = ast_new_sub($1, $3);
}
| term
;
term
: term '*' factor { // Producto | Intersección
$$ = ast_new_mul($1, $3);
}
| term '/' factor { $$ = ast_new_div($1, $3); }
| factor
;
factor
: '-' factor { $$ = ast_new_neg($2); } // Opuesto
| '#' primary { $$ = ast_new_len($2); } // Cardinal | Largo
| primary
;
primary
: '(' expr ')' { $$ = $2; }
| call
| elem
| ID
| INT_LIT
| ATOM_LIT
// Literal "iterable", "secuencia" o "colección"
| '[' { g_scope++; } args']' { g_scope--; // Literal lista
$$ = ast_new_lst($[args]);
}
| '{' { g_scope++; } args '}' { g_scope--; // Literal conjunto
$$ = ast_new_set($[args]);
}
;
call
: primary '(' args ')' { $$ = ast_new_fncall($1, $3); } // Llamada a función
;
args
: expr_list { $$ = ast_reverse_ll($1); }
| %empty { $$ = NULL; }
;
elem
: primary '[' expr ']' { // Elemento según posición
$$ = ast_new_getelem($1, $3);
}
;
%%
static int yyreport_syntax_error(const yypcontext_t *ctx) {
int res = 0;
char *msg = str_dup("");
// Indica el token inesperado
yysymbol_kind_t lookahead = yypcontext_token(ctx);
if(lookahead != YYSYMBOL_YYEMPTY) {
msg = str_concat(msg, "Inesperado: ");
bool dots = yysymbol_name(lookahead)[0] == '.';
if(dots) msg = str_concat(msg, "'");
msg = str_concat(msg, yysymbol_name(lookahead));
if(dots) msg = str_concat(msg, "'");
}
// Indica los tokens esperados
enum { TOKENMAX = 5 };
yysymbol_kind_t expected[TOKENMAX];
int n = yypcontext_expected_tokens(ctx, expected, TOKENMAX);
if(n < 0) {
// Informa el error a yyparse
res = n;
} else {
for(int i = 0; i < n; ++i) {
if(i == 0) {
msg = str_concat(msg, ". Se esperaba: ");
msg = str_concat(msg, yysymbol_name(expected[i]));
/* msg = str_concat(msg, "'"); */
} else {
msg = str_concat(msg, ", ");
msg = str_concat(msg, yysymbol_name(expected[i]));
/* msg = str_concat(msg, "'"); */
}
}
}
msg = str_concat(msg, ".");
yyerror(msg);
free(msg);
return res;
}