1 # Executing Actions. -*- Autotest -*-
3 # Copyright (C) 2001-2012 Free Software Foundation, Inc.
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.
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.
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/>.
18 AT_BANNER([[User Actions.]])
20 ## ------------------ ##
21 ## Mid-rule actions. ##
22 ## ------------------ ##
24 AT_SETUP([Mid-rule actions])
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
31 AT_BISON_OPTION_PUSHDEFS
32 AT_DATA_GRAMMAR([[input.y]],
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'); }
54 ]AT_YYLEX_DEFINE(123456789)[
61 AT_BISON_OPTION_POPDEFS
63 AT_BISON_CHECK([-d -v -o input.c input.y])
65 AT_PARSER_CHECK([./input], 0,
75 ## ---------------- ##
77 ## ---------------- ##
79 AT_SETUP([Exotic Dollars])
81 AT_BISON_OPTION_PUSHDEFS
82 AT_DATA_GRAMMAR([[input.y]],
96 %type <val> a_1 a_2 a_5
97 sum_of_the_five_previous_values
100 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
101 sum_of_the_five_previous_values
103 USE (($1, $2, $<foo>3, $<foo>4, $5));
111 sum_of_the_five_previous_values:
113 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
119 ]AT_YYLEX_DEFINE([])[
127 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
129 AT_PARSER_CHECK([./input], 0,
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
136 AT_DATA_GRAMMAR([[input.y]],
142 typedef struct { int val; } stype;
143 # define YYSTYPE stype
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); } ;
162 AT_BISON_CHECK([[-o input.c input.y]])
163 AT_COMPILE([[input]])
164 AT_PARSER_CHECK([[./input]], [[0]],
168 AT_BISON_OPTION_POPDEFS
173 ## -------------------------- ##
174 ## Printers and Destructors. ##
175 ## -------------------------- ##
177 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
178 # -----------------------------------------------------------------------------
179 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
180 [# Make sure complex $n work.
181 m4_if([$1$2$3], $[1]$[2]$[3], [],
182 [m4_fatal([$0: Invalid arguments: $@])])dnl
184 # Be sure to pass all the %directives to this macro to have correct
185 # helping macros. So don't put any directly in the Bison file.
186 AT_BISON_OPTION_PUSHDEFS([$5])
187 AT_DATA_GRAMMAR([[input.y]],
194 #define YYINITDEPTH 10
195 #define YYMAXDEPTH 10
197 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
198 [#define RANGE(Location) (Location).first_line, (Location).last_line])
202 m4_ifval([$6], [%union
206 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
207 m4_ifval([$6], [[%code provides {]], [[%code {]])
208 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
209 [static int yylex (]AT_LEX_FORMALS[);
210 ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE])
213 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
215 /* FIXME: This %printer isn't actually tested. */
218 ]AT_LALR1_CC_IF([debug_stream () << $$;],
219 [fprintf (yyoutput, "%d", $$)])[;
221 input line thing 'x' 'y'
224 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
228 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
232 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
236 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
240 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
245 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
250 This grammar is made to exercise error recovery.
251 "Lines" starting with `(' support error recovery, with
252 ')' as synchronizing token. Lines starting with 'x' can never
253 be recovered from if in error.
260 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
262 | line input /* Right recursive to load the stack so that popping at
263 END can be exercised. */
266 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
267 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
272 thing thing thing ';'
275 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
276 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
277 $3, RANGE (@3), $4, RANGE (@4));
279 | '(' thing thing ')'
282 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
283 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
284 $3, RANGE (@3), $4, RANGE (@4));
289 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
290 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
295 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
296 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
304 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
305 $$, RANGE (@$), $1, RANGE (@1));
309 /* Alias to ARGV[1]. */
310 const char *source = YY_NULL;
313 yylex (]AT_LEX_FORMALS[)
315 static unsigned int counter = 0;
317 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
318 /* As in BASIC, line numbers go from 10 to 10. */
320 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
321 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
323 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
324 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
327 if (! (0 <= c && c <= strlen (source)))
330 printf ("sending: '%c'", source[c]);
332 printf ("sending: END");
333 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
338 [/* A C++ error reporting function. */
340 yy::parser::error (const location& l, const std::string& m)
342 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
350 parser.set_debug_level (yydebug);
351 return parser.parse ();
355 yyerror (const char *msg)
357 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
361 main (int argc, const char *argv[])
364 yydebug = !!getenv ("YYDEBUG");
370 case 0: printf ("Successful parse.\n"); break;
371 case 1: printf ("Parsing FAILED.\n"); break;
372 default: printf ("Parsing FAILED (status %d).\n", status); break;
378 AT_FULL_COMPILE([input])
381 # Check the location of "empty"
382 # -----------------------------
383 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
384 # an empty "line" nterm.
385 # FIXME: This location is not satisfying. Depend on the lookahead?
386 AT_PARSER_CHECK([./input '(x)'], 0,
387 [[sending: '(' (0@0-9)
388 sending: 'x' (1@10-19)
389 thing (1@10-19): 'x' (1@10-19)
390 sending: ')' (2@20-29)
391 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
392 sending: END (3@30-39)
393 input (0@29-29): /* Nothing */
394 input (2@0-29): line (0@0-29) input (0@29-29)
395 Freeing token END (3@30-39)
396 Freeing nterm input (2@0-29)
401 # Check locations in error recovery
402 # ---------------------------------
403 # '(y)' is an error, but can be recovered from. But what's the location
404 # of the error itself ('y'), and of the resulting reduction ('(error)').
405 AT_PARSER_CHECK([./input '(y)'], 0,
406 [[sending: '(' (0@0-9)
407 sending: 'y' (1@10-19)
408 10-19: syntax error, unexpected 'y', expecting 'x'
409 Freeing token 'y' (1@10-19)
410 sending: ')' (2@20-29)
411 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
412 sending: END (3@30-39)
413 input (0@29-29): /* Nothing */
414 input (2@0-29): line (-1@0-29) input (0@29-29)
415 Freeing token END (3@30-39)
416 Freeing nterm input (2@0-29)
421 # Syntax errors caught by the parser
422 # ----------------------------------
423 # Exercise the discarding of stack top and input until `error'
426 # '(', 'x', 'x', 'x', 'x', 'x', ')',
428 # Load the stack and provoke an error that cannot be caught by the
429 # grammar, to check that the stack is cleared. And make sure the
430 # lookahead is freed.
435 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
436 [[sending: '(' (0@0-9)
437 sending: 'x' (1@10-19)
438 thing (1@10-19): 'x' (1@10-19)
439 sending: 'x' (2@20-29)
440 thing (2@20-29): 'x' (2@20-29)
441 sending: 'x' (3@30-39)
442 30-39: syntax error, unexpected 'x', expecting ')'
443 Freeing nterm thing (2@20-29)
444 Freeing nterm thing (1@10-19)
445 Freeing token 'x' (3@30-39)
446 sending: 'x' (4@40-49)
447 Freeing token 'x' (4@40-49)
448 sending: 'x' (5@50-59)
449 Freeing token 'x' (5@50-59)
450 sending: ')' (6@60-69)
451 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
452 sending: '(' (7@70-79)
453 sending: 'x' (8@80-89)
454 thing (8@80-89): 'x' (8@80-89)
455 sending: ')' (9@90-99)
456 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
457 sending: '(' (10@100-109)
458 sending: 'x' (11@110-119)
459 thing (11@110-119): 'x' (11@110-119)
460 sending: ')' (12@120-129)
461 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
462 sending: 'y' (13@130-139)
463 input (0@129-129): /* Nothing */
464 input (2@100-129): line (10@100-129) input (0@129-129)
465 input (2@70-129): line (7@70-99) input (2@100-129)
466 input (2@0-129): line (-1@0-69) input (2@70-129)
467 130-139: syntax error, unexpected 'y', expecting END
468 Freeing nterm input (2@0-129)
469 Freeing token 'y' (13@130-139)
474 # Syntax error caught by the parser where lookahead = END
475 # --------------------------------------------------------
476 # Load the stack and provoke an error that cannot be caught by the
477 # grammar, to check that the stack is cleared. And make sure the
478 # lookahead is freed.
483 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
484 [[sending: '(' (0@0-9)
485 sending: 'x' (1@10-19)
486 thing (1@10-19): 'x' (1@10-19)
487 sending: ')' (2@20-29)
488 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
489 sending: '(' (3@30-39)
490 sending: 'x' (4@40-49)
491 thing (4@40-49): 'x' (4@40-49)
492 sending: ')' (5@50-59)
493 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
494 sending: 'x' (6@60-69)
495 thing (6@60-69): 'x' (6@60-69)
496 sending: END (7@70-79)
497 70-79: syntax error, unexpected END, expecting 'x'
498 Freeing nterm thing (6@60-69)
499 Freeing nterm line (3@30-59)
500 Freeing nterm line (0@0-29)
501 Freeing token END (7@70-79)
506 # Check destruction upon stack overflow
507 # -------------------------------------
508 # Upon stack overflow, all symbols on the stack should be destroyed.
509 # Only check for yacc.c.
511 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
512 [[sending: '(' (0@0-9)
513 sending: 'x' (1@10-19)
514 thing (1@10-19): 'x' (1@10-19)
515 sending: ')' (2@20-29)
516 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
517 sending: '(' (3@30-39)
518 sending: 'x' (4@40-49)
519 thing (4@40-49): 'x' (4@40-49)
520 sending: ')' (5@50-59)
521 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
522 sending: '(' (6@60-69)
523 sending: 'x' (7@70-79)
524 thing (7@70-79): 'x' (7@70-79)
525 sending: ')' (8@80-89)
526 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
527 sending: '(' (9@90-99)
528 sending: 'x' (10@100-109)
529 thing (10@100-109): 'x' (10@100-109)
530 sending: ')' (11@110-119)
531 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
532 sending: '(' (12@120-129)
533 sending: 'x' (13@130-139)
534 thing (13@130-139): 'x' (13@130-139)
535 sending: ')' (14@140-149)
536 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
537 sending: '(' (15@150-159)
538 sending: 'x' (16@160-169)
539 thing (16@160-169): 'x' (16@160-169)
540 sending: ')' (17@170-179)
541 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
542 sending: '(' (18@180-189)
543 sending: 'x' (19@190-199)
544 thing (19@190-199): 'x' (19@190-199)
545 sending: ')' (20@200-209)
546 200-209: memory exhausted
547 Freeing nterm thing (19@190-199)
548 Freeing nterm line (15@150-179)
549 Freeing nterm line (12@120-149)
550 Freeing nterm line (9@90-119)
551 Freeing nterm line (6@60-89)
552 Freeing nterm line (3@30-59)
553 Freeing nterm line (0@0-29)
554 Parsing FAILED (status 2).
558 AT_BISON_OPTION_POPDEFS
559 ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR
562 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
563 # ---------------------------------------------------------------------------
564 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
565 [AT_SETUP([Printers and Destructors $2: $1])
568 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
579 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
580 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
582 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
583 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
585 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
586 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
590 ## ----------------------------------------- ##
591 ## Default tagless %printer and %destructor. ##
592 ## ----------------------------------------- ##
594 # Check that the right %printer and %destructor are called, that they're not
595 # called for $end, and that $$ and @$ work correctly.
597 AT_SETUP([Default tagless %printer and %destructor])
598 AT_BISON_OPTION_PUSHDEFS([%locations])
599 AT_DATA_GRAMMAR([[input.y]],
604 @$.first_line = @$.last_line = 1;
605 @$.first_column = @$.last_column = 1;
617 fprintf (yyoutput, "<*> printer should not be called.\n");
621 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
624 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
628 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
631 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
635 fprintf (yyoutput, "<*> destructor should not be called.\n");
640 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
644 ]AT_YYLEX_DEFINE([abcd], [[yylval = res]])[
654 AT_BISON_CHECK([-o input.c input.y])
656 AT_PARSER_CHECK([./input], 1,
657 [[<> destructor for 'd' @ 4.
658 'b'/'c' destructor for 'c' @ 3.
659 'b'/'c' destructor for 'b' @ 2.
660 <> destructor for 'a' @ 1.
664 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
665 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
667 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
668 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
670 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
671 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
673 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
674 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
676 Reading a token: Now at end of input.
677 syntax error, unexpected $end, expecting 'e'
678 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
680 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
682 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
684 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
686 Cleanup: discarding lookahead token $end (1.5-1.5: )
690 AT_BISON_OPTION_POPDEFS
695 ## ------------------------------------------------------ ##
696 ## Default tagged and per-type %printer and %destructor. ##
697 ## ------------------------------------------------------ ##
699 AT_SETUP([Default tagged and per-type %printer and %destructor])
700 AT_BISON_OPTION_PUSHDEFS
701 AT_DATA_GRAMMAR([[input.y]],
714 fprintf (yyoutput, "<> printer should not be called.\n");
717 %union { int field0; int field1; int field2; }
718 %type <field0> start 'a' 'g'
722 fprintf (yyoutput, "<*>/<field2>/e printer");
725 fprintf (stdout, "<*>/<field2>/e destructor.\n");
729 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
730 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
733 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
734 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
737 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
738 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
741 fprintf (yyoutput, "<> destructor should not be called.\n");
747 'a' 'b' 'c' 'd' 'e' 'f' 'g'
749 USE(($1, $2, $3, $4, $5, $6, $7));
756 ]AT_YYLEX_DEFINE([abcdef])[
766 AT_BISON_CHECK([-o input.c input.y])
768 AT_PARSER_CHECK([./input], 1,
769 [[<*>/<field2>/e destructor.
770 <*>/<field2>/e destructor.
774 <*>/<field2>/e destructor.
778 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
779 Shifting token 'a' (<*>/<field2>/e printer)
781 Reading a token: Next token is token 'b' (<field1> printer)
782 Shifting token 'b' (<field1> printer)
784 Reading a token: Next token is token 'c' ('c' printer)
785 Shifting token 'c' ('c' printer)
787 Reading a token: Next token is token 'd' ('d' printer)
788 Shifting token 'd' ('d' printer)
790 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
791 Shifting token 'e' (<*>/<field2>/e printer)
793 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
794 Shifting token 'f' (<*>/<field2>/e printer)
796 Reading a token: Now at end of input.
797 syntax error, unexpected $end, expecting 'g'
798 Error: popping token 'f' (<*>/<field2>/e printer)
799 Stack now 0 1 3 5 6 7
800 Error: popping token 'e' (<*>/<field2>/e printer)
802 Error: popping token 'd' ('d' printer)
804 Error: popping token 'c' ('c' printer)
806 Error: popping token 'b' (<field1> printer)
808 Error: popping token 'a' (<*>/<field2>/e printer)
810 Cleanup: discarding lookahead token $end ()
814 AT_BISON_OPTION_POPDEFS
819 ## ------------------------------------------------------------- ##
820 ## Default %printer and %destructor for user-defined end token. ##
821 ## ------------------------------------------------------------- ##
823 AT_SETUP([Default %printer and %destructor for user-defined end token])
825 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
826 # -------------------------------------------------------------
827 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
829 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
830 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
832 AT_BISON_OPTION_PUSHDEFS([%locations])
833 AT_DATA_GRAMMAR([[input]]$1[[.y]],
838 @$.first_line = @$.last_line = 1;
839 @$.first_column = @$.last_column = 1;
851 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
856 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
859 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
863 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
868 [[[%union { char tag; }
869 %type <tag> start END]]])[[
873 start: { $$ = 'S'; } ;
883 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
884 yylloc.first_line = yylloc.last_line = 1;
885 yylloc.first_column = yylloc.last_column = 1;
897 AT_BISON_OPTION_POPDEFS
899 AT_BISON_CHECK([-o input$1.c input$1.y])
900 AT_COMPILE([input$1])
901 AT_PARSER_CHECK([./input$1], 0,
902 [[<]]kind[[> for 'E' @ 1.
903 <]]kind[[> for 'S' @ 1.
907 Reducing stack by rule 1 (line 46):
908 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
911 Reading a token: Now at end of input.
912 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
915 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
916 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
920 m4_popdef([not_kind])
923 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
924 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
930 ## ------------------------------------------------------------------ ##
931 ## Default %printer and %destructor are not for error or $undefined. ##
932 ## ------------------------------------------------------------------ ##
934 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
936 # If Bison were to apply the default %printer and %destructor to the error
937 # token or to $undefined:
938 # - For the error token:
939 # - It would generate warnings for unused $n.
940 # - It would invoke the %printer and %destructor on the error token's
941 # semantic value, which would be initialized from the lookahead, which
942 # would be destroyed separately.
943 # - For $undefined, who knows what the semantic value would be.
944 AT_BISON_OPTION_PUSHDEFS
945 AT_DATA_GRAMMAR([[input.y]],
957 fprintf (yyoutput, "'%c'", $$);
960 fprintf (stderr, "DESTROY '%c'\n", $$);
967 /* In order to reveal the problems that this bug caused during parsing, add
969 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
974 ]AT_YYLEX_DEFINE([abd], [yylval = res])[
982 AT_BISON_OPTION_POPDEFS
984 AT_BISON_CHECK([-o input.c input.y])
986 AT_PARSER_CHECK([./input], [1], [],
989 Reading a token: Next token is token 'a' ('a')
990 Shifting token 'a' ('a')
992 Reading a token: Next token is token 'b' ('b')
994 Shifting token error ()
996 Next token is token 'b' ('b')
997 Shifting token 'b' ('b')
999 Reading a token: Next token is token $undefined ()
1000 Error: popping token 'b' ('b')
1003 Error: popping token error ()
1005 Shifting token error ()
1007 Next token is token $undefined ()
1008 Error: discarding token $undefined ()
1009 Error: popping token error ()
1011 Shifting token error ()
1013 Reading a token: Now at end of input.
1014 Cleanup: discarding lookahead token $end ()
1016 Cleanup: popping token error ()
1017 Cleanup: popping token 'a' ('a')
1025 ## ------------------------------------------------------ ##
1026 ## Default %printer and %destructor are not for $accept. ##
1027 ## ------------------------------------------------------ ##
1029 AT_SETUP([Default %printer and %destructor are not for $accept])
1031 # If YYSTYPE is a union and Bison were to apply the default %printer and
1032 # %destructor to $accept:
1033 # - The %printer and %destructor code generated for $accept would always be
1034 # dead code because $accept is currently never shifted onto the stack.
1035 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1036 # to declare `%type <field> $accept'. (Also true for $undefined.)
1037 # - Thus, the compiler might complain that the user code assumes the wrong
1038 # type for $$ since the code might assume the type associated with a
1039 # specific union field, which is especially reasonable in C++ since that
1040 # type may be a base type. This test case checks for this problem. (Also
1041 # true for $undefined and the error token, so there are three warnings for
1042 # %printer and three for %destructor.)
1044 AT_BISON_OPTION_PUSHDEFS
1045 AT_DATA_GRAMMAR([[input.y]],
1046 [[%debug /* So that %printer is actually compiled. */
1050 # include <stdlib.h>
1051 ]AT_YYERROR_DECLARE[
1058 fprintf (yyoutput, "'%c'", chr);
1062 fprintf (stderr, "DESTROY '%c'\n", chr);
1065 %union { char chr; }
1070 start: { USE($$); } ;
1074 ]AT_YYLEX_DEFINE([])[
1081 AT_BISON_OPTION_POPDEFS
1083 AT_BISON_CHECK([-o input.c input.y])
1090 ## ------------------------------------------------------ ##
1091 ## Default %printer and %destructor for mid-rule values. ##
1092 ## ------------------------------------------------------ ##
1094 AT_SETUP([Default %printer and %destructor for mid-rule values])
1096 AT_BISON_OPTION_PUSHDEFS
1097 AT_DATA_GRAMMAR([[input.y]],
1098 [[%debug /* So that %printer is actually compiled. */
1102 # include <stdlib.h>
1103 ]AT_YYERROR_DECLARE[
1106 # define YYLTYPE int
1107 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1108 # define YY_LOCATION_PRINT(File, Loc)
1111 %printer { fprintf (yyoutput, "%d", @$); } <>
1112 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1113 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1114 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1119 { @$ = 1; } // Not set or used.
1120 { USE ($$); @$ = 2; } // Both set and used.
1121 { USE ($$); @$ = 3; } // Only set.
1122 { @$ = 4; } // Only used.
1124 { USE (($$, $2, $4, $5)); @$ = 0; }
1129 ]AT_YYLEX_DEFINE([])[
1137 AT_BISON_OPTION_POPDEFS
1139 AT_BISON_CHECK([-o input.c input.y], 0,,
1140 [[input.y:33.3-23: warning: unset value: $$
1141 input.y:30.3-35.37: warning: unused value: $3
1145 AT_PARSER_CHECK([./input], 1,,
1148 Reducing stack by rule 1 (line 30):
1149 -> $$ = nterm $@1 (: )
1152 Reducing stack by rule 2 (line 31):
1153 -> $$ = nterm @2 (: 2)
1156 Reducing stack by rule 3 (line 32):
1157 -> $$ = nterm @3 (: 3)
1160 Reducing stack by rule 4 (line 33):
1161 -> $$ = nterm @4 (: 4)
1164 Reading a token: Now at end of input.
1166 Error: popping nterm @4 (: 4)
1169 Error: popping nterm @3 (: 3)
1172 Error: popping nterm @2 (: 2)
1175 Error: popping nterm $@1 (: )
1177 Cleanup: discarding lookahead token $end (: )
1184 ## ----------------------- ##
1185 ## @$ implies %locations. ##
1186 ## ----------------------- ##
1188 # Bison once forgot to check for @$ in actions other than semantic actions.
1190 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1191 # -------------------------------------------
1192 m4_define([AT_CHECK_ACTION_LOCATIONS],
1193 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1194 AT_BISON_OPTION_PUSHDEFS
1195 AT_DATA_GRAMMAR([[input.y]],
1198 ]AT_YYERROR_DECLARE[
1205 printf ("%d\n", @$.first_line);
1206 } ]m4_if($1, [%initial-action], [], [[start]])[
1228 AT_BISON_CHECK([[-o input.c input.y]])
1229 AT_COMPILE([[input]])
1230 AT_BISON_OPTION_POPDEFS
1233 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1234 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1235 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1238 ## ----------------------------------------------- ##
1239 ## Fix user actions without a trailing semicolon. ##
1240 ## ----------------------------------------------- ##
1242 AT_SETUP([[Fix user actions without a trailing semicolon]])
1244 # This feature is undocumented, but we accidentally broke it in 2.3a,
1245 # and there was a complaint at:
1246 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1247 AT_BISON_OPTION_PUSHDEFS
1250 start: test2 test1 test0 testc;
1253 : 'a' { semi; /* TEST:N:2 */ }
1254 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1255 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1256 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1257 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1258 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1259 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1260 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1261 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1264 : 'a' { semi; // TEST:N:1 ;
1265 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1266 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1267 } | 'd' { semi; no_semi // TEST:Y:1 ;
1268 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1269 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1270 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1271 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1272 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1275 : 'a' { semi; // TEST:N:1 {}
1276 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1277 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1278 } | 'd' { semi; no_semi // TEST:Y:1 {}
1279 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1280 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1281 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1282 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1283 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1288 #define TEST_MACRO_N \
1289 []"broken\" $ @ $$ @$ [];\
1293 #define TEST_MACRO_N \
1294 []"broken\" $ @ $$ @$ [];\
1297 AT_BISON_OPTION_POPDEFS
1299 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1300 [[input.y:8.48: warning: a ';' might be needed at the end of action code
1301 input.y:8.48: warning: future versions of Bison will not add the ';'
1302 input.y:9.48: warning: a ';' might be needed at the end of action code
1303 input.y:9.48: warning: future versions of Bison will not add the ';'
1304 input.y:10.48: warning: a ';' might be needed at the end of action code
1305 input.y:10.48: warning: future versions of Bison will not add the ';'
1306 input.y:11.48: warning: a ';' might be needed at the end of action code
1307 input.y:11.48: warning: future versions of Bison will not add the ';'
1308 input.y:12.48: warning: a ';' might be needed at the end of action code
1309 input.y:12.48: warning: future versions of Bison will not add the ';'
1310 input.y:13.48: warning: a ';' might be needed at the end of action code
1311 input.y:13.48: warning: future versions of Bison will not add the ';'
1312 input.y:20.1: warning: a ';' might be needed at the end of action code
1313 input.y:20.1: warning: future versions of Bison will not add the ';'
1314 input.y:21.1: warning: a ';' might be needed at the end of action code
1315 input.y:21.1: warning: future versions of Bison will not add the ';'
1316 input.y:22.1: warning: a ';' might be needed at the end of action code
1317 input.y:22.1: warning: future versions of Bison will not add the ';'
1318 input.y:23.1: warning: a ';' might be needed at the end of action code
1319 input.y:23.1: warning: future versions of Bison will not add the ';'
1320 input.y:24.1: warning: a ';' might be needed at the end of action code
1321 input.y:24.1: warning: future versions of Bison will not add the ';'
1322 input.y:25.1: warning: a ';' might be needed at the end of action code
1323 input.y:25.1: warning: future versions of Bison will not add the ';'
1324 input.y:31.1: warning: a ';' might be needed at the end of action code
1325 input.y:31.1: warning: future versions of Bison will not add the ';'
1326 input.y:32.1: warning: a ';' might be needed at the end of action code
1327 input.y:32.1: warning: future versions of Bison will not add the ';'
1328 input.y:33.1: warning: a ';' might be needed at the end of action code
1329 input.y:33.1: warning: future versions of Bison will not add the ';'
1330 input.y:34.1: warning: a ';' might be needed at the end of action code
1331 input.y:34.1: warning: future versions of Bison will not add the ';'
1332 input.y:35.1: warning: a ';' might be needed at the end of action code
1333 input.y:35.1: warning: future versions of Bison will not add the ';'
1334 input.y:36.1: warning: a ';' might be needed at the end of action code
1335 input.y:36.1: warning: future versions of Bison will not add the ';'
1338 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1339 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1340 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1341 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1342 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1347 ## -------------------------------------------------- ##
1348 ## Destroying lookahead assigned by semantic action. ##
1349 ## -------------------------------------------------- ##
1351 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1353 AT_BISON_OPTION_PUSHDEFS
1354 AT_DATA_GRAMMAR([input.y],
1359 ]AT_YYERROR_DECLARE[
1364 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1365 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1369 // In a previous version of Bison, yychar assigned by the semantic
1370 // action below was not translated into yytoken before the lookahead was
1371 // discarded and thus before its destructor (selected according to
1372 // yytoken) was called in order to return from yyparse. This would
1373 // happen even if YYACCEPT was performed in a later semantic action as
1374 // long as only consistent states with default reductions were visited
1375 // in between. However, we leave YYACCEPT in the same semantic action
1376 // for this test in order to show that skeletons cannot simply translate
1377 // immediately after every semantic action because a semantic action
1378 // that has set yychar might not always return normally. Instead,
1379 // skeletons must translate before every use of yytoken.
1380 start: 'a' accept { USE($1); } ;
1382 assert (yychar == YYEMPTY);
1389 ]AT_YYLEX_DEFINE([a])[
1396 AT_BISON_OPTION_POPDEFS
1397 AT_BISON_CHECK([[-o input.c input.y]])
1398 AT_COMPILE([[input]])
1399 AT_PARSER_CHECK([[./input]], [[0]], [],
1410 AT_SETUP([[YYBACKUP]])
1412 AT_BISON_OPTION_PUSHDEFS([%pure-parser])
1414 AT_DATA_GRAMMAR([input.y],
1421 # include <stdlib.h>
1422 # include <assert.h>
1424 ]AT_YYERROR_DECLARE[
1425 static int yylex (YYSTYPE *yylval);
1433 'a' { printf ("a: %d\n", $1); }
1434 | 'b' { YYBACKUP('a', 123); }
1435 | 'c' 'd' { YYBACKUP('a', 456); }
1441 yylex (YYSTYPE *yylval)
1443 static char const input[] = "bcd";
1444 static size_t toknum;
1445 assert (toknum < sizeof input);
1446 *yylval = (toknum + 1) * 10;
1447 return input[toknum++];
1453 yydebug = !!getenv("YYDEBUG");
1457 AT_BISON_OPTION_POPDEFS
1459 AT_BISON_CHECK([[-o input.c input.y]])
1460 AT_COMPILE([[input]])
1461 AT_PARSER_CHECK([[./input]], [[0]],