]>
Commit | Line | Data |
---|---|---|
1 | # Executing Actions. -*- Autotest -*- | |
2 | ||
3 | # Copyright (C) 2001-2012 Free Software Foundation, Inc. | |
4 | ||
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU General Public License as published by | |
7 | # the Free Software Foundation, either version 3 of the License, or | |
8 | # (at your option) any later version. | |
9 | # | |
10 | # This program is distributed in the hope that it will be useful, | |
11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | # GNU General Public License for more details. | |
14 | # | |
15 | # You should have received a copy of the GNU General Public License | |
16 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | ||
18 | AT_BANNER([[User Actions.]]) | |
19 | ||
20 | ## ------------------ ## | |
21 | ## Mid-rule actions. ## | |
22 | ## ------------------ ## | |
23 | ||
24 | AT_SETUP([Mid-rule actions]) | |
25 | ||
26 | # Bison once forgot the mid-rule actions. It was because the action | |
27 | # was attached to the host rule (the one with the mid-rule action), | |
28 | # instead of being attached to the empty rule dedicated to this | |
29 | # action. | |
30 | ||
31 | AT_BISON_OPTION_PUSHDEFS | |
32 | AT_DATA_GRAMMAR([[input.y]], | |
33 | [[%error-verbose | |
34 | %debug | |
35 | %{ | |
36 | ]AT_YYERROR_DECLARE[ | |
37 | ]AT_YYLEX_DECLARE[ | |
38 | %} | |
39 | %% | |
40 | exp: { putchar ('0'); } | |
41 | '1' { putchar ('1'); } | |
42 | '2' { putchar ('2'); } | |
43 | '3' { putchar ('3'); } | |
44 | '4' { putchar ('4'); } | |
45 | '5' { putchar ('5'); } | |
46 | '6' { putchar ('6'); } | |
47 | '7' { putchar ('7'); } | |
48 | '8' { putchar ('8'); } | |
49 | '9' { putchar ('9'); } | |
50 | { putchar ('\n'); } | |
51 | ; | |
52 | %% | |
53 | ]AT_YYERROR_DEFINE[ | |
54 | ]AT_YYLEX_DEFINE(123456789)[ | |
55 | int | |
56 | main (void) | |
57 | { | |
58 | return yyparse (); | |
59 | } | |
60 | ]]) | |
61 | AT_BISON_OPTION_POPDEFS | |
62 | ||
63 | AT_BISON_CHECK([-d -v -o input.c input.y]) | |
64 | AT_COMPILE([input]) | |
65 | AT_PARSER_CHECK([./input], 0, | |
66 | [[0123456789 | |
67 | ]]) | |
68 | ||
69 | AT_CLEANUP | |
70 | ||
71 | ||
72 | ||
73 | ||
74 | ||
75 | ## ---------------- ## | |
76 | ## Exotic Dollars. ## | |
77 | ## ---------------- ## | |
78 | ||
79 | AT_SETUP([Exotic Dollars]) | |
80 | ||
81 | AT_BISON_OPTION_PUSHDEFS | |
82 | AT_DATA_GRAMMAR([[input.y]], | |
83 | [[%error-verbose | |
84 | %debug | |
85 | %{ | |
86 | ]AT_YYERROR_DECLARE[ | |
87 | ]AT_YYLEX_DECLARE[ | |
88 | # define USE(Var) | |
89 | %} | |
90 | ||
91 | %union | |
92 | { | |
93 | int val; | |
94 | }; | |
95 | ||
96 | %type <val> a_1 a_2 a_5 | |
97 | sum_of_the_five_previous_values | |
98 | ||
99 | %% | |
100 | exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5 | |
101 | sum_of_the_five_previous_values | |
102 | { | |
103 | USE (($1, $2, $<foo>3, $<foo>4, $5)); | |
104 | printf ("%d\n", $6); | |
105 | } | |
106 | ; | |
107 | a_1: { $$ = 1; }; | |
108 | a_2: { $$ = 2; }; | |
109 | a_5: { $$ = 5; }; | |
110 | ||
111 | sum_of_the_five_previous_values: | |
112 | { | |
113 | $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4; | |
114 | } | |
115 | ; | |
116 | ||
117 | %% | |
118 | ]AT_YYERROR_DEFINE[ | |
119 | ]AT_YYLEX_DEFINE([])[ | |
120 | int | |
121 | main (void) | |
122 | { | |
123 | return yyparse (); | |
124 | } | |
125 | ]]) | |
126 | ||
127 | AT_BISON_CHECK([-d -v -o input.c input.y], 0) | |
128 | AT_COMPILE([input]) | |
129 | AT_PARSER_CHECK([./input], 0, | |
130 | [[15 | |
131 | ]]) | |
132 | ||
133 | # Make sure that fields after $n or $-n are parsed correctly. At one | |
134 | # point while implementing dashes in symbol names, we were dropping | |
135 | # fields after $-n. | |
136 | AT_DATA_GRAMMAR([[input.y]], | |
137 | [[ | |
138 | %{ | |
139 | #include <stdio.h> | |
140 | ]AT_YYERROR_DECLARE[ | |
141 | ]AT_YYLEX_DECLARE[ | |
142 | typedef struct { int val; } stype; | |
143 | # define YYSTYPE stype | |
144 | %} | |
145 | ||
146 | %% | |
147 | start: one two { $$.val = $1.val + $2.val; } sum ; | |
148 | one: { $$.val = 1; } ; | |
149 | two: { $$.val = 2; } ; | |
150 | sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ; | |
151 | ||
152 | %% | |
153 | ||
154 | static int | |
155 | yylex (void) | |
156 | { | |
157 | return 0; | |
158 | } | |
159 | ||
160 | ]AT_YYERROR_DEFINE[ | |
161 | int | |
162 | main (void) | |
163 | { | |
164 | return yyparse (); | |
165 | } | |
166 | ]]) | |
167 | ||
168 | AT_BISON_CHECK([[-o input.c input.y]]) | |
169 | AT_COMPILE([[input]]) | |
170 | AT_PARSER_CHECK([[./input]], [[0]], | |
171 | [[6 | |
172 | ]]) | |
173 | ||
174 | AT_BISON_OPTION_POPDEFS | |
175 | AT_CLEANUP | |
176 | ||
177 | ||
178 | ||
179 | ## -------------------------- ## | |
180 | ## Printers and Destructors. ## | |
181 | ## -------------------------- ## | |
182 | ||
183 | # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG) | |
184 | # ----------------------------------------------------------------------------- | |
185 | m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR], | |
186 | [# Make sure complex $n work. | |
187 | m4_if([$1$2$3], $[1]$[2]$[3], [], | |
188 | [m4_fatal([$0: Invalid arguments: $@])])dnl | |
189 | ||
190 | # Be sure to pass all the %directives to this macro to have correct | |
191 | # helping macros. So don't put any directly in the Bison file. | |
192 | AT_BISON_OPTION_PUSHDEFS([$5]) | |
193 | AT_DATA_GRAMMAR([[input.y]], | |
194 | [[%code requires { | |
195 | #include <stdio.h> | |
196 | #include <stdlib.h> | |
197 | #include <string.h> | |
198 | #include <assert.h> | |
199 | ||
200 | #define YYINITDEPTH 10 | |
201 | #define YYMAXDEPTH 10 | |
202 | ]AT_LALR1_CC_IF( | |
203 | [#define RANGE(Location) (Location).begin.line, (Location).end.line], | |
204 | [#define RANGE(Location) (Location).first_line, (Location).last_line]) | |
205 | [} | |
206 | ||
207 | $5] | |
208 | m4_ifval([$6], [%union | |
209 | { | |
210 | int ival; | |
211 | }]) | |
212 | AT_LALR1_CC_IF([%define global_tokens_and_yystype]) | |
213 | m4_ifval([$6], [[%code provides {]], [[%code {]]) | |
214 | AT_LALR1_CC_IF([typedef yy::location YYLTYPE;]) | |
215 | [static int yylex (]AT_LEX_FORMALS[); | |
216 | ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE]) | |
217 | [} | |
218 | ||
219 | ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[ | |
220 | ||
221 | /* FIXME: This %printer isn't actually tested. */ | |
222 | %printer | |
223 | { | |
224 | ]AT_LALR1_CC_IF([debug_stream () << $$;], | |
225 | [fprintf (yyoutput, "%d", $$)])[; | |
226 | } | |
227 | input line thing 'x' 'y' | |
228 | ||
229 | %destructor | |
230 | { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); } | |
231 | input | |
232 | ||
233 | %destructor | |
234 | { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); } | |
235 | line | |
236 | ||
237 | %destructor | |
238 | { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); } | |
239 | thing | |
240 | ||
241 | %destructor | |
242 | { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); } | |
243 | 'x' | |
244 | ||
245 | %destructor | |
246 | { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); } | |
247 | 'y' | |
248 | ||
249 | %token END 0 | |
250 | %destructor | |
251 | { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); } | |
252 | END | |
253 | ||
254 | %% | |
255 | /* | |
256 | This grammar is made to exercise error recovery. | |
257 | "Lines" starting with `(' support error recovery, with | |
258 | ')' as synchronizing token. Lines starting with 'x' can never | |
259 | be recovered from if in error. | |
260 | */ | |
261 | ||
262 | input: | |
263 | /* Nothing. */ | |
264 | { | |
265 | $$ = 0; | |
266 | printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$)); | |
267 | } | |
268 | | line input /* Right recursive to load the stack so that popping at | |
269 | END can be exercised. */ | |
270 | { | |
271 | $$ = 2; | |
272 | printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n", | |
273 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2)); | |
274 | } | |
275 | ; | |
276 | ||
277 | line: | |
278 | thing thing thing ';' | |
279 | { | |
280 | $$ = $1; | |
281 | printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n", | |
282 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), | |
283 | $3, RANGE (@3), $4, RANGE (@4)); | |
284 | } | |
285 | | '(' thing thing ')' | |
286 | { | |
287 | $$ = $1; | |
288 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", | |
289 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), | |
290 | $3, RANGE (@3), $4, RANGE (@4)); | |
291 | } | |
292 | | '(' thing ')' | |
293 | { | |
294 | $$ = $1; | |
295 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n", | |
296 | $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3)); | |
297 | } | |
298 | | '(' error ')' | |
299 | { | |
300 | $$ = -1; | |
301 | printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n", | |
302 | $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3)); | |
303 | } | |
304 | ; | |
305 | ||
306 | thing: | |
307 | 'x' | |
308 | { | |
309 | $$ = $1; | |
310 | printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n", | |
311 | $$, RANGE (@$), $1, RANGE (@1)); | |
312 | } | |
313 | ; | |
314 | %% | |
315 | /* Alias to ARGV[1]. */ | |
316 | const char *source = YY_NULL; | |
317 | ||
318 | static int | |
319 | yylex (]AT_LEX_FORMALS[) | |
320 | { | |
321 | static unsigned int counter = 0; | |
322 | ||
323 | int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++; | |
324 | /* As in BASIC, line numbers go from 10 to 10. */ | |
325 | ]AT_LALR1_CC_IF( | |
326 | [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c; | |
327 | AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9; | |
328 | ], | |
329 | [ AT_LOC.first_line = AT_LOC.first_column = 10 * c; | |
330 | AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9; | |
331 | ])[ | |
332 | ||
333 | if (! (0 <= c && c <= strlen (source))) | |
334 | abort (); | |
335 | if (source[c]) | |
336 | printf ("sending: '%c'", source[c]); | |
337 | else | |
338 | printf ("sending: END"); | |
339 | printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[)); | |
340 | return source[c]; | |
341 | } | |
342 | ||
343 | ]AT_LALR1_CC_IF( | |
344 | [/* A C++ error reporting function. */ | |
345 | void | |
346 | yy::parser::error (const location& l, const std::string& m) | |
347 | { | |
348 | printf ("%d-%d: %s\n", RANGE (l), m.c_str()); | |
349 | } | |
350 | ||
351 | static bool yydebug; | |
352 | int | |
353 | yyparse () | |
354 | { | |
355 | yy::parser parser; | |
356 | parser.set_debug_level (yydebug); | |
357 | return parser.parse (); | |
358 | } | |
359 | ], | |
360 | [static void | |
361 | yyerror (const char *msg) | |
362 | { | |
363 | printf ("%d-%d: %s\n", RANGE (yylloc), msg); | |
364 | }])[ | |
365 | ||
366 | int | |
367 | main (int argc, const char *argv[]) | |
368 | { | |
369 | int status; | |
370 | yydebug = !!getenv ("YYDEBUG"); | |
371 | assert (argc == 2); | |
372 | source = argv[1]; | |
373 | status = yyparse (); | |
374 | switch (status) | |
375 | { | |
376 | case 0: printf ("Successful parse.\n"); break; | |
377 | case 1: printf ("Parsing FAILED.\n"); break; | |
378 | default: printf ("Parsing FAILED (status %d).\n", status); break; | |
379 | } | |
380 | return status; | |
381 | } | |
382 | ]]) | |
383 | ||
384 | AT_FULL_COMPILE([input]) | |
385 | ||
386 | ||
387 | # Check the location of "empty" | |
388 | # ----------------------------- | |
389 | # I.e., epsilon-reductions, as in "(x)" which ends by reducing | |
390 | # an empty "line" nterm. | |
391 | # FIXME: This location is not satisfying. Depend on the lookahead? | |
392 | AT_PARSER_CHECK([./input '(x)'], 0, | |
393 | [[sending: '(' (0@0-9) | |
394 | sending: 'x' (1@10-19) | |
395 | thing (1@10-19): 'x' (1@10-19) | |
396 | sending: ')' (2@20-29) | |
397 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
398 | sending: END (3@30-39) | |
399 | input (0@29-29): /* Nothing */ | |
400 | input (2@0-29): line (0@0-29) input (0@29-29) | |
401 | Freeing token END (3@30-39) | |
402 | Freeing nterm input (2@0-29) | |
403 | Successful parse. | |
404 | ]]) | |
405 | ||
406 | ||
407 | # Check locations in error recovery | |
408 | # --------------------------------- | |
409 | # '(y)' is an error, but can be recovered from. But what's the location | |
410 | # of the error itself ('y'), and of the resulting reduction ('(error)'). | |
411 | AT_PARSER_CHECK([./input '(y)'], 0, | |
412 | [[sending: '(' (0@0-9) | |
413 | sending: 'y' (1@10-19) | |
414 | 10-19: syntax error, unexpected 'y', expecting 'x' | |
415 | Freeing token 'y' (1@10-19) | |
416 | sending: ')' (2@20-29) | |
417 | line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29) | |
418 | sending: END (3@30-39) | |
419 | input (0@29-29): /* Nothing */ | |
420 | input (2@0-29): line (-1@0-29) input (0@29-29) | |
421 | Freeing token END (3@30-39) | |
422 | Freeing nterm input (2@0-29) | |
423 | Successful parse. | |
424 | ]]) | |
425 | ||
426 | ||
427 | # Syntax errors caught by the parser | |
428 | # ---------------------------------- | |
429 | # Exercise the discarding of stack top and input until `error' | |
430 | # can be reduced. | |
431 | # | |
432 | # '(', 'x', 'x', 'x', 'x', 'x', ')', | |
433 | # | |
434 | # Load the stack and provoke an error that cannot be caught by the | |
435 | # grammar, to check that the stack is cleared. And make sure the | |
436 | # lookahead is freed. | |
437 | # | |
438 | # '(', 'x', ')', | |
439 | # '(', 'x', ')', | |
440 | # 'y' | |
441 | AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, | |
442 | [[sending: '(' (0@0-9) | |
443 | sending: 'x' (1@10-19) | |
444 | thing (1@10-19): 'x' (1@10-19) | |
445 | sending: 'x' (2@20-29) | |
446 | thing (2@20-29): 'x' (2@20-29) | |
447 | sending: 'x' (3@30-39) | |
448 | 30-39: syntax error, unexpected 'x', expecting ')' | |
449 | Freeing nterm thing (2@20-29) | |
450 | Freeing nterm thing (1@10-19) | |
451 | Freeing token 'x' (3@30-39) | |
452 | sending: 'x' (4@40-49) | |
453 | Freeing token 'x' (4@40-49) | |
454 | sending: 'x' (5@50-59) | |
455 | Freeing token 'x' (5@50-59) | |
456 | sending: ')' (6@60-69) | |
457 | line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69) | |
458 | sending: '(' (7@70-79) | |
459 | sending: 'x' (8@80-89) | |
460 | thing (8@80-89): 'x' (8@80-89) | |
461 | sending: ')' (9@90-99) | |
462 | line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99) | |
463 | sending: '(' (10@100-109) | |
464 | sending: 'x' (11@110-119) | |
465 | thing (11@110-119): 'x' (11@110-119) | |
466 | sending: ')' (12@120-129) | |
467 | line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129) | |
468 | sending: 'y' (13@130-139) | |
469 | input (0@129-129): /* Nothing */ | |
470 | input (2@100-129): line (10@100-129) input (0@129-129) | |
471 | input (2@70-129): line (7@70-99) input (2@100-129) | |
472 | input (2@0-129): line (-1@0-69) input (2@70-129) | |
473 | 130-139: syntax error, unexpected 'y', expecting END | |
474 | Freeing nterm input (2@0-129) | |
475 | Freeing token 'y' (13@130-139) | |
476 | Parsing FAILED. | |
477 | ]]) | |
478 | ||
479 | ||
480 | # Syntax error caught by the parser where lookahead = END | |
481 | # -------------------------------------------------------- | |
482 | # Load the stack and provoke an error that cannot be caught by the | |
483 | # grammar, to check that the stack is cleared. And make sure the | |
484 | # lookahead is freed. | |
485 | # | |
486 | # '(', 'x', ')', | |
487 | # '(', 'x', ')', | |
488 | # 'x' | |
489 | AT_PARSER_CHECK([./input '(x)(x)x'], 1, | |
490 | [[sending: '(' (0@0-9) | |
491 | sending: 'x' (1@10-19) | |
492 | thing (1@10-19): 'x' (1@10-19) | |
493 | sending: ')' (2@20-29) | |
494 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
495 | sending: '(' (3@30-39) | |
496 | sending: 'x' (4@40-49) | |
497 | thing (4@40-49): 'x' (4@40-49) | |
498 | sending: ')' (5@50-59) | |
499 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
500 | sending: 'x' (6@60-69) | |
501 | thing (6@60-69): 'x' (6@60-69) | |
502 | sending: END (7@70-79) | |
503 | 70-79: syntax error, unexpected END, expecting 'x' | |
504 | Freeing nterm thing (6@60-69) | |
505 | Freeing nterm line (3@30-59) | |
506 | Freeing nterm line (0@0-29) | |
507 | Freeing token END (7@70-79) | |
508 | Parsing FAILED. | |
509 | ]]) | |
510 | ||
511 | ||
512 | # Check destruction upon stack overflow | |
513 | # ------------------------------------- | |
514 | # Upon stack overflow, all symbols on the stack should be destroyed. | |
515 | # Only check for yacc.c. | |
516 | AT_YACC_IF([ | |
517 | AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, | |
518 | [[sending: '(' (0@0-9) | |
519 | sending: 'x' (1@10-19) | |
520 | thing (1@10-19): 'x' (1@10-19) | |
521 | sending: ')' (2@20-29) | |
522 | line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29) | |
523 | sending: '(' (3@30-39) | |
524 | sending: 'x' (4@40-49) | |
525 | thing (4@40-49): 'x' (4@40-49) | |
526 | sending: ')' (5@50-59) | |
527 | line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59) | |
528 | sending: '(' (6@60-69) | |
529 | sending: 'x' (7@70-79) | |
530 | thing (7@70-79): 'x' (7@70-79) | |
531 | sending: ')' (8@80-89) | |
532 | line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89) | |
533 | sending: '(' (9@90-99) | |
534 | sending: 'x' (10@100-109) | |
535 | thing (10@100-109): 'x' (10@100-109) | |
536 | sending: ')' (11@110-119) | |
537 | line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119) | |
538 | sending: '(' (12@120-129) | |
539 | sending: 'x' (13@130-139) | |
540 | thing (13@130-139): 'x' (13@130-139) | |
541 | sending: ')' (14@140-149) | |
542 | line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149) | |
543 | sending: '(' (15@150-159) | |
544 | sending: 'x' (16@160-169) | |
545 | thing (16@160-169): 'x' (16@160-169) | |
546 | sending: ')' (17@170-179) | |
547 | line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179) | |
548 | sending: '(' (18@180-189) | |
549 | sending: 'x' (19@190-199) | |
550 | thing (19@190-199): 'x' (19@190-199) | |
551 | sending: ')' (20@200-209) | |
552 | 200-209: memory exhausted | |
553 | Freeing nterm thing (19@190-199) | |
554 | Freeing nterm line (15@150-179) | |
555 | Freeing nterm line (12@120-149) | |
556 | Freeing nterm line (9@90-119) | |
557 | Freeing nterm line (6@60-89) | |
558 | Freeing nterm line (3@30-59) | |
559 | Freeing nterm line (0@0-29) | |
560 | Parsing FAILED (status 2). | |
561 | ]]) | |
562 | ]) | |
563 | ||
564 | AT_BISON_OPTION_POPDEFS | |
565 | ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR | |
566 | ||
567 | ||
568 | # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG]) | |
569 | # --------------------------------------------------------------------------- | |
570 | m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR], | |
571 | [AT_SETUP([Printers and Destructors $2: $1]) | |
572 | ||
573 | $3 | |
574 | _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4], | |
575 | [%error-verbose | |
576 | %debug | |
577 | %verbose | |
578 | %locations | |
579 | $1], [$2]) | |
580 | ||
581 | AT_CLEANUP | |
582 | ]) | |
583 | ||
584 | ||
585 | AT_CHECK_PRINTER_AND_DESTRUCTOR([]) | |
586 | AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union]) | |
587 | ||
588 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"]) | |
589 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union]) | |
590 | ||
591 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser]) | |
592 | AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union]) | |
593 | ||
594 | ||
595 | ||
596 | ## ----------------------------------------- ## | |
597 | ## Default tagless %printer and %destructor. ## | |
598 | ## ----------------------------------------- ## | |
599 | ||
600 | # Check that the right %printer and %destructor are called, that they're not | |
601 | # called for $end, and that $$ and @$ work correctly. | |
602 | ||
603 | AT_SETUP([Default tagless %printer and %destructor]) | |
604 | AT_BISON_OPTION_PUSHDEFS([%locations]) | |
605 | AT_DATA_GRAMMAR([[input.y]], | |
606 | [[%error-verbose | |
607 | %debug | |
608 | %locations | |
609 | %initial-action { | |
610 | @$.first_line = @$.last_line = 1; | |
611 | @$.first_column = @$.last_column = 1; | |
612 | } | |
613 | ||
614 | %{ | |
615 | # include <stdio.h> | |
616 | # include <stdlib.h> | |
617 | ]AT_YYLEX_DECLARE[ | |
618 | ]AT_YYERROR_DECLARE[ | |
619 | # define USE(SYM) | |
620 | %} | |
621 | ||
622 | %printer { | |
623 | fprintf (yyoutput, "<*> printer should not be called.\n"); | |
624 | } <*> | |
625 | ||
626 | %printer { | |
627 | fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column); | |
628 | } <> | |
629 | %destructor { | |
630 | fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column); | |
631 | } <> | |
632 | ||
633 | %printer { | |
634 | fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column); | |
635 | } 'b' 'c' | |
636 | %destructor { | |
637 | fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column); | |
638 | } 'b' 'c' | |
639 | ||
640 | %destructor { | |
641 | fprintf (yyoutput, "<*> destructor should not be called.\n"); | |
642 | } <*> | |
643 | ||
644 | %% | |
645 | ||
646 | start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ; | |
647 | ||
648 | %% | |
649 | ]AT_YYERROR_DEFINE[ | |
650 | ]AT_YYLEX_DEFINE([abcd], [[yylval = res]])[ | |
651 | ||
652 | int | |
653 | main (void) | |
654 | { | |
655 | yydebug = 1; | |
656 | return yyparse (); | |
657 | } | |
658 | ]]) | |
659 | ||
660 | AT_BISON_CHECK([-o input.c input.y]) | |
661 | AT_COMPILE([input]) | |
662 | AT_PARSER_CHECK([./input], 1, | |
663 | [[<> destructor for 'd' @ 4. | |
664 | 'b'/'c' destructor for 'c' @ 3. | |
665 | 'b'/'c' destructor for 'b' @ 2. | |
666 | <> destructor for 'a' @ 1. | |
667 | ]], | |
668 | [[Starting parse | |
669 | Entering state 0 | |
670 | Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1) | |
671 | Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1) | |
672 | Entering state 1 | |
673 | Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
674 | Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
675 | Entering state 3 | |
676 | Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
677 | Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
678 | Entering state 5 | |
679 | Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4) | |
680 | Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4) | |
681 | Entering state 6 | |
682 | Reading a token: Now at end of input. | |
683 | syntax error, unexpected $end, expecting 'e' | |
684 | Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4) | |
685 | Stack now 0 1 3 5 | |
686 | Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3) | |
687 | Stack now 0 1 3 | |
688 | Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2) | |
689 | Stack now 0 1 | |
690 | Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1) | |
691 | Stack now 0 | |
692 | Cleanup: discarding lookahead token $end (1.5-1.5: ) | |
693 | Stack now 0 | |
694 | ]]) | |
695 | ||
696 | AT_BISON_OPTION_POPDEFS | |
697 | AT_CLEANUP | |
698 | ||
699 | ||
700 | ||
701 | ## ------------------------------------------------------ ## | |
702 | ## Default tagged and per-type %printer and %destructor. ## | |
703 | ## ------------------------------------------------------ ## | |
704 | ||
705 | AT_SETUP([Default tagged and per-type %printer and %destructor]) | |
706 | AT_BISON_OPTION_PUSHDEFS | |
707 | AT_DATA_GRAMMAR([[input.y]], | |
708 | [[%error-verbose | |
709 | %debug | |
710 | ||
711 | %{ | |
712 | # include <stdio.h> | |
713 | # include <stdlib.h> | |
714 | ]AT_YYERROR_DECLARE[ | |
715 | ]AT_YYLEX_DECLARE[ | |
716 | # define USE(SYM) | |
717 | %} | |
718 | ||
719 | %printer { | |
720 | fprintf (yyoutput, "<> printer should not be called.\n"); | |
721 | } <> | |
722 | ||
723 | %union { int field0; int field1; int field2; } | |
724 | %type <field0> start 'a' 'g' | |
725 | %type <field1> 'e' | |
726 | %type <field2> 'f' | |
727 | %printer { | |
728 | fprintf (yyoutput, "<*>/<field2>/e printer"); | |
729 | } <*> 'e' <field2> | |
730 | %destructor { | |
731 | fprintf (stdout, "<*>/<field2>/e destructor.\n"); | |
732 | } <*> 'e' <field2> | |
733 | ||
734 | %type <field1> 'b' | |
735 | %printer { fprintf (yyoutput, "<field1> printer"); } <field1> | |
736 | %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1> | |
737 | ||
738 | %type <field0> 'c' | |
739 | %printer { fprintf (yyoutput, "'c' printer"); } 'c' | |
740 | %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c' | |
741 | ||
742 | %type <field1> 'd' | |
743 | %printer { fprintf (yyoutput, "'d' printer"); } 'd' | |
744 | %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd' | |
745 | ||
746 | %destructor { | |
747 | fprintf (yyoutput, "<> destructor should not be called.\n"); | |
748 | } <> | |
749 | ||
750 | %% | |
751 | ||
752 | start: | |
753 | 'a' 'b' 'c' 'd' 'e' 'f' 'g' | |
754 | { | |
755 | USE(($1, $2, $3, $4, $5, $6, $7)); | |
756 | $$ = 'S'; | |
757 | } | |
758 | ; | |
759 | ||
760 | %% | |
761 | ]AT_YYERROR_DEFINE[ | |
762 | ]AT_YYLEX_DEFINE([abcdef])[ | |
763 | ||
764 | int | |
765 | main (void) | |
766 | { | |
767 | yydebug = 1; | |
768 | return yyparse (); | |
769 | } | |
770 | ]]) | |
771 | ||
772 | AT_BISON_CHECK([-o input.c input.y]) | |
773 | AT_COMPILE([input]) | |
774 | AT_PARSER_CHECK([./input], 1, | |
775 | [[<*>/<field2>/e destructor. | |
776 | <*>/<field2>/e destructor. | |
777 | 'd' destructor. | |
778 | 'c' destructor. | |
779 | <field1> destructor. | |
780 | <*>/<field2>/e destructor. | |
781 | ]], | |
782 | [[Starting parse | |
783 | Entering state 0 | |
784 | Reading a token: Next token is token 'a' (<*>/<field2>/e printer) | |
785 | Shifting token 'a' (<*>/<field2>/e printer) | |
786 | Entering state 1 | |
787 | Reading a token: Next token is token 'b' (<field1> printer) | |
788 | Shifting token 'b' (<field1> printer) | |
789 | Entering state 3 | |
790 | Reading a token: Next token is token 'c' ('c' printer) | |
791 | Shifting token 'c' ('c' printer) | |
792 | Entering state 5 | |
793 | Reading a token: Next token is token 'd' ('d' printer) | |
794 | Shifting token 'd' ('d' printer) | |
795 | Entering state 6 | |
796 | Reading a token: Next token is token 'e' (<*>/<field2>/e printer) | |
797 | Shifting token 'e' (<*>/<field2>/e printer) | |
798 | Entering state 7 | |
799 | Reading a token: Next token is token 'f' (<*>/<field2>/e printer) | |
800 | Shifting token 'f' (<*>/<field2>/e printer) | |
801 | Entering state 8 | |
802 | Reading a token: Now at end of input. | |
803 | syntax error, unexpected $end, expecting 'g' | |
804 | Error: popping token 'f' (<*>/<field2>/e printer) | |
805 | Stack now 0 1 3 5 6 7 | |
806 | Error: popping token 'e' (<*>/<field2>/e printer) | |
807 | Stack now 0 1 3 5 6 | |
808 | Error: popping token 'd' ('d' printer) | |
809 | Stack now 0 1 3 5 | |
810 | Error: popping token 'c' ('c' printer) | |
811 | Stack now 0 1 3 | |
812 | Error: popping token 'b' (<field1> printer) | |
813 | Stack now 0 1 | |
814 | Error: popping token 'a' (<*>/<field2>/e printer) | |
815 | Stack now 0 | |
816 | Cleanup: discarding lookahead token $end () | |
817 | Stack now 0 | |
818 | ]]) | |
819 | ||
820 | AT_BISON_OPTION_POPDEFS | |
821 | AT_CLEANUP | |
822 | ||
823 | ||
824 | ||
825 | ## ------------------------------------------------------------- ## | |
826 | ## Default %printer and %destructor for user-defined end token. ## | |
827 | ## ------------------------------------------------------------- ## | |
828 | ||
829 | AT_SETUP([Default %printer and %destructor for user-defined end token]) | |
830 | ||
831 | # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED) | |
832 | # ------------------------------------------------------------- | |
833 | m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN], | |
834 | [m4_if($1, 0, | |
835 | [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])], | |
836 | [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])]) | |
837 | ||
838 | AT_BISON_OPTION_PUSHDEFS([%locations]) | |
839 | AT_DATA_GRAMMAR([[input]]$1[[.y]], | |
840 | [[%error-verbose | |
841 | %debug | |
842 | %locations | |
843 | %initial-action { | |
844 | @$.first_line = @$.last_line = 1; | |
845 | @$.first_column = @$.last_column = 1; | |
846 | } | |
847 | ||
848 | %{ | |
849 | # include <stdio.h> | |
850 | # include <stdlib.h> | |
851 | ]AT_YYERROR_DECLARE[ | |
852 | ]AT_YYLEX_DECLARE[ | |
853 | # define USE(SYM) | |
854 | %} | |
855 | ||
856 | %destructor { | |
857 | fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n"); | |
858 | } <]]not_kind[[> | |
859 | ||
860 | %token END 0 | |
861 | %printer { | |
862 | fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column); | |
863 | } <]]kind[[> | |
864 | %destructor { | |
865 | fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column); | |
866 | } <]]kind[[> | |
867 | ||
868 | %printer { | |
869 | fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n"); | |
870 | } <]]not_kind[[> | |
871 | ||
872 | ]]m4_if($1, 0, [[[ | |
873 | ]]], | |
874 | [[[%union { char tag; } | |
875 | %type <tag> start END]]])[[ | |
876 | ||
877 | %% | |
878 | ||
879 | start: { $$ = 'S'; } ; | |
880 | ||
881 | %% | |
882 | ||
883 | static int | |
884 | yylex (void) | |
885 | { | |
886 | static int called; | |
887 | if (called++) | |
888 | abort (); | |
889 | yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E'; | |
890 | yylloc.first_line = yylloc.last_line = 1; | |
891 | yylloc.first_column = yylloc.last_column = 1; | |
892 | return 0; | |
893 | } | |
894 | ]AT_YYERROR_DEFINE[ | |
895 | ||
896 | int | |
897 | main (void) | |
898 | { | |
899 | yydebug = 1; | |
900 | return yyparse (); | |
901 | } | |
902 | ]]) | |
903 | AT_BISON_OPTION_POPDEFS | |
904 | ||
905 | AT_BISON_CHECK([-o input$1.c input$1.y]) | |
906 | AT_COMPILE([input$1]) | |
907 | AT_PARSER_CHECK([./input$1], 0, | |
908 | [[<]]kind[[> for 'E' @ 1. | |
909 | <]]kind[[> for 'S' @ 1. | |
910 | ]], | |
911 | [[Starting parse | |
912 | Entering state 0 | |
913 | Reducing stack by rule 1 (line 46): | |
914 | -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1) | |
915 | Stack now 0 | |
916 | Entering state 1 | |
917 | Reading a token: Now at end of input. | |
918 | Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1) | |
919 | Entering state 2 | |
920 | Stack now 0 1 2 | |
921 | Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1) | |
922 | Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1) | |
923 | ]]) | |
924 | ||
925 | m4_popdef([kind]) | |
926 | m4_popdef([not_kind]) | |
927 | ]) | |
928 | ||
929 | _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0) | |
930 | _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1) | |
931 | ||
932 | AT_CLEANUP | |
933 | ||
934 | ||
935 | ||
936 | ## ------------------------------------------------------------------ ## | |
937 | ## Default %printer and %destructor are not for error or $undefined. ## | |
938 | ## ------------------------------------------------------------------ ## | |
939 | ||
940 | AT_SETUP([Default %printer and %destructor are not for error or $undefined]) | |
941 | ||
942 | # If Bison were to apply the default %printer and %destructor to the error | |
943 | # token or to $undefined: | |
944 | # - For the error token: | |
945 | # - It would generate warnings for unused $n. | |
946 | # - It would invoke the %printer and %destructor on the error token's | |
947 | # semantic value, which would be initialized from the lookahead, which | |
948 | # would be destroyed separately. | |
949 | # - For $undefined, who knows what the semantic value would be. | |
950 | AT_BISON_OPTION_PUSHDEFS | |
951 | AT_DATA_GRAMMAR([[input.y]], | |
952 | [[%debug | |
953 | ||
954 | %{ | |
955 | # include <stdio.h> | |
956 | # include <stdlib.h> | |
957 | ]AT_YYERROR_DECLARE[ | |
958 | ]AT_YYLEX_DECLARE[ | |
959 | # define USE(SYM) | |
960 | %} | |
961 | ||
962 | %printer { | |
963 | fprintf (yyoutput, "'%c'", $$); | |
964 | } <> <*> | |
965 | %destructor { | |
966 | fprintf (stderr, "DESTROY '%c'\n", $$); | |
967 | } <> <*> | |
968 | ||
969 | %% | |
970 | ||
971 | start: | |
972 | { $$ = 'S'; } | |
973 | /* In order to reveal the problems that this bug caused during parsing, add | |
974 | * $2 to USE. */ | |
975 | | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; } | |
976 | ; | |
977 | ||
978 | %% | |
979 | ]AT_YYERROR_DEFINE[ | |
980 | ]AT_YYLEX_DEFINE([abd], [yylval = res])[ | |
981 | int | |
982 | main (void) | |
983 | { | |
984 | yydebug = 1; | |
985 | return yyparse (); | |
986 | } | |
987 | ]]) | |
988 | AT_BISON_OPTION_POPDEFS | |
989 | ||
990 | AT_BISON_CHECK([-o input.c input.y]) | |
991 | AT_COMPILE([input]) | |
992 | AT_PARSER_CHECK([./input], [1], [], | |
993 | [[Starting parse | |
994 | Entering state 0 | |
995 | Reading a token: Next token is token 'a' ('a') | |
996 | Shifting token 'a' ('a') | |
997 | Entering state 1 | |
998 | Reading a token: Next token is token 'b' ('b') | |
999 | syntax error | |
1000 | Shifting token error () | |
1001 | Entering state 3 | |
1002 | Next token is token 'b' ('b') | |
1003 | Shifting token 'b' ('b') | |
1004 | Entering state 5 | |
1005 | Reading a token: Next token is token $undefined () | |
1006 | Error: popping token 'b' ('b') | |
1007 | DESTROY 'b' | |
1008 | Stack now 0 1 3 | |
1009 | Error: popping token error () | |
1010 | Stack now 0 1 | |
1011 | Shifting token error () | |
1012 | Entering state 3 | |
1013 | Next token is token $undefined () | |
1014 | Error: discarding token $undefined () | |
1015 | Error: popping token error () | |
1016 | Stack now 0 1 | |
1017 | Shifting token error () | |
1018 | Entering state 3 | |
1019 | Reading a token: Now at end of input. | |
1020 | Cleanup: discarding lookahead token $end () | |
1021 | Stack now 0 1 3 | |
1022 | Cleanup: popping token error () | |
1023 | Cleanup: popping token 'a' ('a') | |
1024 | DESTROY 'a' | |
1025 | ]]) | |
1026 | ||
1027 | AT_CLEANUP | |
1028 | ||
1029 | ||
1030 | ||
1031 | ## ------------------------------------------------------ ## | |
1032 | ## Default %printer and %destructor are not for $accept. ## | |
1033 | ## ------------------------------------------------------ ## | |
1034 | ||
1035 | AT_SETUP([Default %printer and %destructor are not for $accept]) | |
1036 | ||
1037 | # If YYSTYPE is a union and Bison were to apply the default %printer and | |
1038 | # %destructor to $accept: | |
1039 | # - The %printer and %destructor code generated for $accept would always be | |
1040 | # dead code because $accept is currently never shifted onto the stack. | |
1041 | # - $$ for $accept would always be of type YYSTYPE because it's not possible | |
1042 | # to declare `%type <field> $accept'. (Also true for $undefined.) | |
1043 | # - Thus, the compiler might complain that the user code assumes the wrong | |
1044 | # type for $$ since the code might assume the type associated with a | |
1045 | # specific union field, which is especially reasonable in C++ since that | |
1046 | # type may be a base type. This test case checks for this problem. (Also | |
1047 | # true for $undefined and the error token, so there are three warnings for | |
1048 | # %printer and three for %destructor.) | |
1049 | ||
1050 | AT_BISON_OPTION_PUSHDEFS | |
1051 | AT_DATA_GRAMMAR([[input.y]], | |
1052 | [[%debug /* So that %printer is actually compiled. */ | |
1053 | ||
1054 | %{ | |
1055 | # include <stdio.h> | |
1056 | # include <stdlib.h> | |
1057 | ]AT_YYERROR_DECLARE[ | |
1058 | ]AT_YYLEX_DECLARE[ | |
1059 | # define USE(SYM) | |
1060 | %} | |
1061 | ||
1062 | %printer { | |
1063 | char chr = $$; | |
1064 | fprintf (yyoutput, "'%c'", chr); | |
1065 | } <> <*> | |
1066 | %destructor { | |
1067 | char chr = $$; | |
1068 | fprintf (stderr, "DESTROY '%c'\n", chr); | |
1069 | } <> <*> | |
1070 | ||
1071 | %union { char chr; } | |
1072 | %type <chr> start | |
1073 | ||
1074 | %% | |
1075 | ||
1076 | start: { USE($$); } ; | |
1077 | ||
1078 | %% | |
1079 | ]AT_YYERROR_DEFINE[ | |
1080 | ]AT_YYLEX_DEFINE([])[ | |
1081 | int | |
1082 | main (void) | |
1083 | { | |
1084 | return yyparse (); | |
1085 | } | |
1086 | ]]) | |
1087 | AT_BISON_OPTION_POPDEFS | |
1088 | ||
1089 | AT_BISON_CHECK([-o input.c input.y]) | |
1090 | AT_COMPILE([input]) | |
1091 | ||
1092 | AT_CLEANUP | |
1093 | ||
1094 | ||
1095 | ||
1096 | ## ------------------------------------------------------ ## | |
1097 | ## Default %printer and %destructor for mid-rule values. ## | |
1098 | ## ------------------------------------------------------ ## | |
1099 | ||
1100 | AT_SETUP([Default %printer and %destructor for mid-rule values]) | |
1101 | ||
1102 | AT_BISON_OPTION_PUSHDEFS | |
1103 | AT_DATA_GRAMMAR([[input.y]], | |
1104 | [[%debug /* So that %printer is actually compiled. */ | |
1105 | ||
1106 | %{ | |
1107 | # include <stdio.h> | |
1108 | # include <stdlib.h> | |
1109 | ]AT_YYERROR_DECLARE[ | |
1110 | ]AT_YYLEX_DECLARE[ | |
1111 | # define USE(SYM) | |
1112 | # define YYLTYPE int | |
1113 | # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs) | |
1114 | # define YY_LOCATION_PRINT(File, Loc) | |
1115 | %} | |
1116 | ||
1117 | %printer { fprintf (yyoutput, "%d", @$); } <> | |
1118 | %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <> | |
1119 | %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*> | |
1120 | %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*> | |
1121 | ||
1122 | %% | |
1123 | ||
1124 | start: | |
1125 | { @$ = 1; } // Not set or used. | |
1126 | { USE ($$); @$ = 2; } // Both set and used. | |
1127 | { USE ($$); @$ = 3; } // Only set. | |
1128 | { @$ = 4; } // Only used. | |
1129 | 'c' | |
1130 | { USE (($$, $2, $4, $5)); @$ = 0; } | |
1131 | ; | |
1132 | ||
1133 | %% | |
1134 | ]AT_YYERROR_DEFINE[ | |
1135 | ]AT_YYLEX_DEFINE([])[ | |
1136 | int | |
1137 | main (void) | |
1138 | { | |
1139 | yydebug = 1; | |
1140 | return yyparse (); | |
1141 | } | |
1142 | ]]) | |
1143 | AT_BISON_OPTION_POPDEFS | |
1144 | ||
1145 | AT_BISON_CHECK([-o input.c input.y], 0,, | |
1146 | [[input.y:33.3-23: warning: unset value: $$ | |
1147 | input.y:30.3-35.37: warning: unused value: $3 | |
1148 | ]]) | |
1149 | ||
1150 | AT_COMPILE([input]) | |
1151 | AT_PARSER_CHECK([./input], 1,, | |
1152 | [[Starting parse | |
1153 | Entering state 0 | |
1154 | Reducing stack by rule 1 (line 30): | |
1155 | -> $$ = nterm $@1 (: ) | |
1156 | Stack now 0 | |
1157 | Entering state 2 | |
1158 | Reducing stack by rule 2 (line 31): | |
1159 | -> $$ = nterm @2 (: 2) | |
1160 | Stack now 0 2 | |
1161 | Entering state 4 | |
1162 | Reducing stack by rule 3 (line 32): | |
1163 | -> $$ = nterm @3 (: 3) | |
1164 | Stack now 0 2 4 | |
1165 | Entering state 5 | |
1166 | Reducing stack by rule 4 (line 33): | |
1167 | -> $$ = nterm @4 (: 4) | |
1168 | Stack now 0 2 4 5 | |
1169 | Entering state 6 | |
1170 | Reading a token: Now at end of input. | |
1171 | syntax error | |
1172 | Error: popping nterm @4 (: 4) | |
1173 | DESTROY 4 | |
1174 | Stack now 0 2 4 5 | |
1175 | Error: popping nterm @3 (: 3) | |
1176 | DESTROY 3 | |
1177 | Stack now 0 2 4 | |
1178 | Error: popping nterm @2 (: 2) | |
1179 | DESTROY 2 | |
1180 | Stack now 0 2 | |
1181 | Error: popping nterm $@1 (: ) | |
1182 | Stack now 0 | |
1183 | Cleanup: discarding lookahead token $end (: ) | |
1184 | Stack now 0 | |
1185 | ]]) | |
1186 | ||
1187 | AT_CLEANUP | |
1188 | ||
1189 | ||
1190 | ## ----------------------- ## | |
1191 | ## @$ implies %locations. ## | |
1192 | ## ----------------------- ## | |
1193 | ||
1194 | # Bison once forgot to check for @$ in actions other than semantic actions. | |
1195 | ||
1196 | # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE) | |
1197 | # ------------------------------------------- | |
1198 | m4_define([AT_CHECK_ACTION_LOCATIONS], | |
1199 | [AT_SETUP([[@$ in ]$1[ implies %locations]]) | |
1200 | AT_BISON_OPTION_PUSHDEFS | |
1201 | AT_DATA_GRAMMAR([[input.y]], | |
1202 | [[%code { | |
1203 | #include <stdio.h> | |
1204 | ]AT_YYERROR_DECLARE[ | |
1205 | ]AT_YYLEX_DECLARE[ | |
1206 | } | |
1207 | ||
1208 | %debug | |
1209 | ||
1210 | ]$1[ { | |
1211 | printf ("%d\n", @$.first_line); | |
1212 | } ]m4_if($1, [%initial-action], [], [[start]])[ | |
1213 | ||
1214 | %% | |
1215 | ||
1216 | start: ; | |
1217 | ||
1218 | %% | |
1219 | ||
1220 | static int | |
1221 | yylex (void) | |
1222 | { | |
1223 | return 0; | |
1224 | } | |
1225 | ||
1226 | ]AT_YYERROR_DEFINE[ | |
1227 | int | |
1228 | main (void) | |
1229 | { | |
1230 | return yyparse (); | |
1231 | } | |
1232 | ]]) | |
1233 | ||
1234 | AT_BISON_CHECK([[-o input.c input.y]]) | |
1235 | AT_COMPILE([[input]]) | |
1236 | AT_BISON_OPTION_POPDEFS | |
1237 | AT_CLEANUP]) | |
1238 | ||
1239 | AT_CHECK_ACTION_LOCATIONS([[%initial-action]]) | |
1240 | AT_CHECK_ACTION_LOCATIONS([[%destructor]]) | |
1241 | AT_CHECK_ACTION_LOCATIONS([[%printer]]) | |
1242 | ||
1243 | ||
1244 | ## ----------------------------------------------- ## | |
1245 | ## Fix user actions without a trailing semicolon. ## | |
1246 | ## ----------------------------------------------- ## | |
1247 | ||
1248 | AT_SETUP([[Fix user actions without a trailing semicolon]]) | |
1249 | ||
1250 | # This feature is undocumented, but we accidentally broke it in 2.3a, | |
1251 | # and there was a complaint at: | |
1252 | # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>. | |
1253 | AT_BISON_OPTION_PUSHDEFS | |
1254 | AT_DATA([input.y], | |
1255 | [[%% | |
1256 | start: test2 test1 test0 testc; | |
1257 | ||
1258 | test2 | |
1259 | : 'a' { semi; /* TEST:N:2 */ } | |
1260 | | 'b' { if (0) {no_semi} /* TEST:N:2 */ } | |
1261 | | 'c' { if (0) {semi;} /* TEST:N:2 */ } | |
1262 | | 'd' { semi; no_semi /* TEST:Y:2 */ } | |
1263 | | 'e' { semi(); no_semi() /* TEST:Y:2 */ } | |
1264 | | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ } | |
1265 | | 'g' { semi++; no_semi++ /* TEST:Y:2 */ } | |
1266 | | 'h' { {no_semi} no_semi /* TEST:Y:2 */ } | |
1267 | | 'i' { {semi;} no_semi /* TEST:Y:2 */ } | |
1268 | ; | |
1269 | test1 | |
1270 | : 'a' { semi; // TEST:N:1 ; | |
1271 | } | 'b' { if (0) {no_semi} // TEST:N:1 ; | |
1272 | } | 'c' { if (0) {semi;} // TEST:N:1 ; | |
1273 | } | 'd' { semi; no_semi // TEST:Y:1 ; | |
1274 | } | 'e' { semi(); no_semi() // TEST:Y:1 ; | |
1275 | } | 'f' { semi[]; no_semi[] // TEST:Y:1 ; | |
1276 | } | 'g' { semi++; no_semi++ // TEST:Y:1 ; | |
1277 | } | 'h' { {no_semi} no_semi // TEST:Y:1 ; | |
1278 | } | 'i' { {semi;} no_semi // TEST:Y:1 ; | |
1279 | } ; | |
1280 | test0 | |
1281 | : 'a' { semi; // TEST:N:1 {} | |
1282 | } | 'b' { if (0) {no_semi} // TEST:N:1 {} | |
1283 | } | 'c' { if (0) {semi;} // TEST:N:1 {} | |
1284 | } | 'd' { semi; no_semi // TEST:Y:1 {} | |
1285 | } | 'e' { semi(); no_semi() // TEST:Y:1 {} | |
1286 | } | 'f' { semi[]; no_semi[] // TEST:Y:1 {} | |
1287 | } | 'g' { semi++; no_semi++ // TEST:Y:1 {} | |
1288 | } | 'h' { {no_semi} no_semi // TEST:Y:1 {} | |
1289 | } | 'i' { {semi;} no_semi // TEST:Y:1 {} | |
1290 | } ; | |
1291 | ||
1292 | testc | |
1293 | : 'a' { | |
1294 | #define TEST_MACRO_N \ | |
1295 | []"broken\" $ @ $$ @$ [];\ | |
1296 | string;"} | |
1297 | | 'b' { | |
1298 | no_semi | |
1299 | #define TEST_MACRO_N \ | |
1300 | []"broken\" $ @ $$ @$ [];\ | |
1301 | string;"} | |
1302 | ]]) | |
1303 | AT_BISON_OPTION_POPDEFS | |
1304 | ||
1305 | AT_BISON_CHECK([[-o input.c input.y]], [0], [], | |
1306 | [[input.y:8.48: warning: a ';' might be needed at the end of action code | |
1307 | input.y:8.48: warning: future versions of Bison will not add the ';' | |
1308 | input.y:9.48: warning: a ';' might be needed at the end of action code | |
1309 | input.y:9.48: warning: future versions of Bison will not add the ';' | |
1310 | input.y:10.48: warning: a ';' might be needed at the end of action code | |
1311 | input.y:10.48: warning: future versions of Bison will not add the ';' | |
1312 | input.y:11.48: warning: a ';' might be needed at the end of action code | |
1313 | input.y:11.48: warning: future versions of Bison will not add the ';' | |
1314 | input.y:12.48: warning: a ';' might be needed at the end of action code | |
1315 | input.y:12.48: warning: future versions of Bison will not add the ';' | |
1316 | input.y:13.48: warning: a ';' might be needed at the end of action code | |
1317 | input.y:13.48: warning: future versions of Bison will not add the ';' | |
1318 | input.y:20.1: warning: a ';' might be needed at the end of action code | |
1319 | input.y:20.1: warning: future versions of Bison will not add the ';' | |
1320 | input.y:21.1: warning: a ';' might be needed at the end of action code | |
1321 | input.y:21.1: warning: future versions of Bison will not add the ';' | |
1322 | input.y:22.1: warning: a ';' might be needed at the end of action code | |
1323 | input.y:22.1: warning: future versions of Bison will not add the ';' | |
1324 | input.y:23.1: warning: a ';' might be needed at the end of action code | |
1325 | input.y:23.1: warning: future versions of Bison will not add the ';' | |
1326 | input.y:24.1: warning: a ';' might be needed at the end of action code | |
1327 | input.y:24.1: warning: future versions of Bison will not add the ';' | |
1328 | input.y:25.1: warning: a ';' might be needed at the end of action code | |
1329 | input.y:25.1: warning: future versions of Bison will not add the ';' | |
1330 | input.y:31.1: warning: a ';' might be needed at the end of action code | |
1331 | input.y:31.1: warning: future versions of Bison will not add the ';' | |
1332 | input.y:32.1: warning: a ';' might be needed at the end of action code | |
1333 | input.y:32.1: warning: future versions of Bison will not add the ';' | |
1334 | input.y:33.1: warning: a ';' might be needed at the end of action code | |
1335 | input.y:33.1: warning: future versions of Bison will not add the ';' | |
1336 | input.y:34.1: warning: a ';' might be needed at the end of action code | |
1337 | input.y:34.1: warning: future versions of Bison will not add the ';' | |
1338 | input.y:35.1: warning: a ';' might be needed at the end of action code | |
1339 | input.y:35.1: warning: future versions of Bison will not add the ';' | |
1340 | input.y:36.1: warning: a ';' might be needed at the end of action code | |
1341 | input.y:36.1: warning: future versions of Bison will not add the ';' | |
1342 | ]]) | |
1343 | ||
1344 | AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]]) | |
1345 | AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]]) | |
1346 | AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]]) | |
1347 | AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]]) | |
1348 | AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]]) | |
1349 | ||
1350 | AT_CLEANUP | |
1351 | ||
1352 | ||
1353 | ## -------------------------------------------------- ## | |
1354 | ## Destroying lookahead assigned by semantic action. ## | |
1355 | ## -------------------------------------------------- ## | |
1356 | ||
1357 | AT_SETUP([[Destroying lookahead assigned by semantic action]]) | |
1358 | ||
1359 | AT_BISON_OPTION_PUSHDEFS | |
1360 | AT_DATA_GRAMMAR([input.y], | |
1361 | [[ | |
1362 | %code { | |
1363 | #include <assert.h> | |
1364 | #include <stdio.h> | |
1365 | ]AT_YYERROR_DECLARE[ | |
1366 | ]AT_YYLEX_DECLARE[ | |
1367 | #define USE(Var) | |
1368 | } | |
1369 | ||
1370 | %destructor { fprintf (stderr, "'a' destructor\n"); } 'a' | |
1371 | %destructor { fprintf (stderr, "'b' destructor\n"); } 'b' | |
1372 | ||
1373 | %% | |
1374 | ||
1375 | // In a previous version of Bison, yychar assigned by the semantic | |
1376 | // action below was not translated into yytoken before the lookahead was | |
1377 | // discarded and thus before its destructor (selected according to | |
1378 | // yytoken) was called in order to return from yyparse. This would | |
1379 | // happen even if YYACCEPT was performed in a later semantic action as | |
1380 | // long as only consistent states with default reductions were visited | |
1381 | // in between. However, we leave YYACCEPT in the same semantic action | |
1382 | // for this test in order to show that skeletons cannot simply translate | |
1383 | // immediately after every semantic action because a semantic action | |
1384 | // that has set yychar might not always return normally. Instead, | |
1385 | // skeletons must translate before every use of yytoken. | |
1386 | start: 'a' accept { USE($1); } ; | |
1387 | accept: /*empty*/ { | |
1388 | assert (yychar == YYEMPTY); | |
1389 | yychar = 'b'; | |
1390 | YYACCEPT; | |
1391 | } ; | |
1392 | ||
1393 | %% | |
1394 | ]AT_YYERROR_DEFINE[ | |
1395 | ]AT_YYLEX_DEFINE([a])[ | |
1396 | int | |
1397 | main (void) | |
1398 | { | |
1399 | return yyparse (); | |
1400 | } | |
1401 | ]]) | |
1402 | AT_BISON_OPTION_POPDEFS | |
1403 | AT_BISON_CHECK([[-o input.c input.y]]) | |
1404 | AT_COMPILE([[input]]) | |
1405 | AT_PARSER_CHECK([[./input]], [[0]], [], | |
1406 | [['b' destructor | |
1407 | 'a' destructor | |
1408 | ]]) | |
1409 | ||
1410 | AT_CLEANUP | |
1411 | ||
1412 | ## ---------- ## | |
1413 | ## YYBACKUP. ## | |
1414 | ## ---------- ## | |
1415 | ||
1416 | AT_SETUP([[YYBACKUP]]) | |
1417 | ||
1418 | AT_BISON_OPTION_PUSHDEFS([%pure-parser]) | |
1419 | ||
1420 | AT_DATA_GRAMMAR([input.y], | |
1421 | [[ | |
1422 | %error-verbose | |
1423 | %debug | |
1424 | %pure-parser | |
1425 | %code { | |
1426 | # include <stdio.h> | |
1427 | # include <stdlib.h> | |
1428 | # include <assert.h> | |
1429 | ||
1430 | ]AT_YYERROR_DECLARE[ | |
1431 | static int yylex (YYSTYPE *yylval); | |
1432 | } | |
1433 | %% | |
1434 | input: | |
1435 | exp exp {} | |
1436 | ; | |
1437 | ||
1438 | exp: | |
1439 | 'a' { printf ("a: %d\n", $1); } | |
1440 | | 'b' { YYBACKUP('a', 123); } | |
1441 | | 'c' 'd' { YYBACKUP('a', 456); } | |
1442 | ; | |
1443 | ||
1444 | %% | |
1445 | ]AT_YYERROR_DEFINE[ | |
1446 | static int | |
1447 | yylex (YYSTYPE *yylval) | |
1448 | { | |
1449 | static char const input[] = "bcd"; | |
1450 | static size_t toknum; | |
1451 | assert (toknum < sizeof input); | |
1452 | *yylval = (toknum + 1) * 10; | |
1453 | return input[toknum++]; | |
1454 | } | |
1455 | ||
1456 | int | |
1457 | main (void) | |
1458 | { | |
1459 | yydebug = !!getenv("YYDEBUG"); | |
1460 | return yyparse (); | |
1461 | } | |
1462 | ]]) | |
1463 | AT_BISON_OPTION_POPDEFS | |
1464 | ||
1465 | AT_BISON_CHECK([[-o input.c input.y]]) | |
1466 | AT_COMPILE([[input]]) | |
1467 | AT_PARSER_CHECK([[./input]], [[0]], | |
1468 | [[a: 123 | |
1469 | a: 456 | |
1470 | ]]) | |
1471 | ||
1472 | AT_CLEANUP |