1 e# Executing Actions. -*- Autotest -*-
3 # Copyright (C) 2001-2015 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]],
33 [[%define parse.error verbose
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"])[
57 AT_BISON_OPTION_POPDEFS
59 AT_BISON_CHECK([-d -v -o input.c input.y])
61 AT_PARSER_CHECK([./input], 0,
67 ## ----------------------- ##
68 ## Implicitly empty rule. ##
69 ## ----------------------- ##
71 AT_SETUP([Implicitly empty rule])
73 AT_DATA_GRAMMAR([[1.y]],
77 // A mid-rule action does not count as an empty rule.
81 AT_BISON_CHECK([-fcaret -Wempty-rule 1.y], [0], [],
82 [[1.y:11.17-18: warning: empty rule without %empty [-Wempty-rule]
87 AT_DATA_GRAMMAR([[2.y]],
95 AT_BISON_CHECK([-fcaret 2.y], [0], [],
96 [[2.y:11.17-18: warning: empty rule without %empty [-Wempty-rule]
99 2.y:13.17-18: warning: empty rule without %empty [-Wempty-rule]
104 AT_BISON_CHECK([-fcaret -Wno-empty-rule 2.y], [0])
108 ## ------------------------ ##
109 ## Invalid uses of %empty. ##
110 ## ------------------------ ##
112 AT_SETUP([Invalid uses of %empty])
114 AT_DATA_GRAMMAR([[one.y]],
121 AT_BISON_CHECK([-fcaret one.y], [1], [],
122 [[one.y:11.13-18: error: only one %empty allowed per rule
125 one.y:11.3-8: previous declaration
130 AT_DATA_GRAMMAR([[two.y]],
139 AT_BISON_CHECK([-fcaret two.y], [1], [],
140 [[two.y:11.7-12: error: %empty on non-empty rule
143 two.y:12.3-8: error: %empty on non-empty rule
146 two.y:13.3-8: error: %empty on non-empty rule
153 ## ---------------------- ##
154 ## Valid uses of %empty. ##
155 ## ---------------------- ##
157 AT_SETUP([Valid uses of %empty])
159 AT_BISON_OPTION_PUSHDEFS
160 AT_DATA_GRAMMAR([[input.y]],
176 AT_FULL_COMPILE([input])
177 AT_PARSER_CHECK([./input])
178 AT_BISON_OPTION_POPDEFS
181 ## ------------------ ##
182 ## Initial location. ##
183 ## ------------------ ##
185 # AT_TEST(SKELETON-NAME, DIRECTIVES, [MORE-DIRECTIVES], [LOCATION = 1.1])
186 # -----------------------------------------------------------------------
187 # Check that the initial location is correct.
188 m4_pushdef([AT_TEST],
189 [AT_SETUP([Initial location: $1 $2])
191 AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "$1" $2])
192 AT_DATA_GRAMMAR([[input.y]],
201 # include <stdlib.h> /* getenv */
206 exp: { ]AT_SKEL_CC_IF([[std::cerr << @$ << std::endl]],
207 [[LOCATION_PRINT(stderr, @$); fputc ('\n', stderr)]])[; }
214 YYUSE(llocp);], [AT_SKEL_CC_IF([
224 p.set_debug_level (!!getenv("YYDEBUG"));
225 return p.parse ();]], [[
226 yydebug = !!getenv("YYDEBUG");
227 return !!yyparse (]AT_PARAM_IF([0])[);]])[
231 AT_FULL_COMPILE([input])
232 AT_PARSER_CHECK([./input], 1, [],
233 [m4_default([$4], [1.1])
234 m4_default([$4], [1.1])[: syntax error
236 AT_BISON_OPTION_POPDEFS
240 ## FIXME: test Java, and iterate over skeletons.
242 AT_TEST([yacc.c], [%define api.pure full])
243 AT_TEST([yacc.c], [%define api.pure %parse-param { int x }])
244 AT_TEST([yacc.c], [%define api.push-pull both])
245 AT_TEST([yacc.c], [%define api.push-pull both %define api.pure full])
247 AT_TEST([glr.c], [%define api.pure])
251 ## A very different test, based on PostgreSQL's implementation of the
253 ## http://lists.gnu.org/archive/html/bug-bison/2012-11/msg00023.html
255 ## Weirdly enough, to trigger the warning with GCC 4.7, we must not
256 ## use fprintf, so run the test twice: once to check the warning
257 ## (absence thereof), and another time to check the value.
258 AT_TEST([yacc.c], [%define api.pure full],
261 # define LOCATION_PRINT(Stream, Loc) \
263 # define YYLLOC_DEFAULT(Current, Rhs, N) \
264 (Current) = ((Rhs)[N ? 1 : 0])
269 AT_TEST([yacc.c], [%define api.pure full],
272 # define LOCATION_PRINT(Stream, Loc) \
273 fprintf ((Stream), "%d", (Loc))
274 # define YYLLOC_DEFAULT(Current, Rhs, N) \
275 (Current) = ((Rhs)[N ? 1 : 0])
285 ## ---------------- ##
286 ## Location Print. ##
287 ## ---------------- ##
289 # AT_TEST(SKELETON-NAME, DIRECTIVES, [MORE-DIRECTIVES])
290 # -----------------------------------------------------
291 # Check that the initial location is correct.
292 m4_pushdef([AT_TEST],
293 [AT_SETUP([Location print: $1 $2])
295 AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "$1" $2])
296 AT_DATA_GRAMMAR([[input.y]],
317 ]AT_GLR_CC_IF([loc.initialize();])[
318 #define TEST(L1, C1, L2, C2) \
319 loc.]AT_FIRST_LINE[ = L1; \
320 loc.]AT_FIRST_COLUMN[ = C1; \
321 loc.]AT_LAST_LINE[ = L2; \
322 loc.]AT_LAST_COLUMN[ = C2; \
323 ]AT_SKEL_CC_IF([std::cout << loc],
324 [LOCATION_PRINT(stdout, loc)])[;\
338 AT_FULL_COMPILE([input])
339 AT_PARSER_CHECK([./input], 0,
347 AT_BISON_OPTION_POPDEFS
351 ## FIXME: test Java, and iterate over skeletons.
361 ## ---------------- ##
362 ## Exotic Dollars. ##
363 ## ---------------- ##
365 AT_SETUP([Exotic Dollars])
367 AT_BISON_OPTION_PUSHDEFS
368 AT_DATA_GRAMMAR([[input.y]],
369 [[%define parse.error verbose
382 %type <val> a_1 a_2 a_5
383 sum_of_the_five_previous_values
386 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
387 sum_of_the_five_previous_values
389 USE (($1, $2, $<foo>3, $<foo>4, $5));
397 sum_of_the_five_previous_values:
399 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
409 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
411 AT_PARSER_CHECK([./input], 0,
415 # Make sure that fields after $n or $-n are parsed correctly. At one
416 # point while implementing dashes in symbol names, we were dropping
418 AT_DATA_GRAMMAR([[input.y]],
423 typedef struct { int val; } stype;
424 # define YYSTYPE stype
428 start: one two { $$.val = $1.val + $2.val; } sum ;
429 one: { $$.val = 1; } ;
430 two: { $$.val = 2; } ;
431 sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ;
439 AT_FULL_COMPILE([input])
440 AT_PARSER_CHECK([[./input]], [[0]],
444 AT_BISON_OPTION_POPDEFS
449 ## -------------------------- ##
450 ## Printers and Destructors. ##
451 ## -------------------------- ##
453 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4,
454 # BISON-DIRECTIVE, UNION-FLAG)
455 # -------------------------------------------------------------
456 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
457 [# Make sure complex $n work.
458 m4_if([$1$2$3$4], $[1]$[2]$[3]$[4], [],
459 [m4_fatal([$0: Invalid arguments: $@])])dnl
461 # Be sure to pass all the %directives to this macro to have correct
462 # helping macros. So don't put any directly in the Bison file.
463 AT_BISON_OPTION_PUSHDEFS([$5])
464 AT_DATA_GRAMMAR([[input.y]],
471 #define YYINITDEPTH 10
472 #define YYMAXDEPTH 10
473 #define RANGE(Location) ]AT_LALR1_CC_IF([(Location).begin.line, (Location).end.line],
474 [(Location).first_line, (Location).last_line])[
476 /* Display the symbol type Symbol. */
477 #define V(Symbol, Value, Location, Sep) \
478 fprintf (stderr, #Symbol " (%d@%d-%d)" Sep, Value, RANGE(Location))
482 ]m4_ifval([$6], [%union
486 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
487 m4_ifval([$6], [[%code provides {]], [[%code {]])
488 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])[
490 ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE])
493 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
495 /* FIXME: This %printer isn't actually tested. */
498 ]AT_LALR1_CC_IF([debug_stream () << $$;],
499 [fprintf (yyoutput, "%d", $$)])[;
501 input line thing 'x' 'y'
504 { fprintf (stderr, "Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
508 { fprintf (stderr, "Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
512 { fprintf (stderr, "Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
516 { fprintf (stderr, "Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
520 { fprintf (stderr, "Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
525 { fprintf (stderr, "Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
530 This grammar is made to exercise error recovery.
531 "Lines" starting with '(' support error recovery, with
532 ')' as synchronizing token. Lines starting with 'x' can never
533 be recovered from if in error.
540 V(input, $$, @$, ": /* Nothing */\n");
542 | line input /* Right recursive to load the stack so that popping at
543 END can be exercised. */
546 V(input, $$, @$, ": ");
547 V(line, $1, @1, " ");
548 V(input, $2, @2, "\n");
553 thing thing thing ';'
556 V(line, $$, @$, ": ");
557 V(thing, $1, @1, " ");
558 V(thing, $2, @2, " ");
559 V(thing, $3, @3, " ");
562 | '(' thing thing ')'
565 V(line, $$, @$, ": ");
567 V(thing, $2, @2, " ");
568 V(thing, $3, @3, " ");
569 V(')', $4, @4, "\n");
574 V(line, $$, @$, ": ");
576 V(thing, $2, @2, " ");
577 V(')', $3, @3, "\n");
582 V(line, $$, @$, ": ");
584 fprintf (stderr, "error (@%d-%d) ", RANGE(@2));
585 V(')', $3, @3, "\n");
593 V(thing, $$, @$, ": ");
594 V('x', $1, @1, "\n");
598 /* Alias to ARGV[1]. */
599 const char *source = YY_NULLPTR;
606 static unsigned int counter = 0;
608 unsigned int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
609 /* As in BASIC, line numbers go from 10 to 10. */
610 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_FIRST_COLUMN[ = 10 * c;
611 ]AT_LOC_LAST_LINE[ = ]AT_LOC_LAST_COLUMN[ = ]AT_LOC_FIRST_LINE[ + 9;
612 assert (c <= strlen (source));
614 fprintf (stderr, "sending: '%c'", source[c]);
616 fprintf (stderr, "sending: END");
617 fprintf (stderr, " (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
621 [static bool yydebug;
626 parser.set_debug_level (yydebug);
627 return parser.parse ();
632 main (int argc, const char *argv[])
635 yydebug = !!getenv ("YYDEBUG");
641 case 0: fprintf (stderr, "Successful parse.\n"); break;
642 case 1: fprintf (stderr, "Parsing FAILED.\n"); break;
643 default: fprintf (stderr, "Parsing FAILED (status %d).\n", status); break;
649 AT_FULL_COMPILE([input])
652 # Check the location of "empty"
653 # -----------------------------
654 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
655 # an empty "line" nterm.
656 # FIXME: This location is not satisfying. Depend on the lookahead?
657 AT_PARSER_CHECK([./input '(x)'], 0, [],
658 [[sending: '(' (0@0-9)
659 sending: 'x' (1@10-19)
660 thing (1@10-19): 'x' (1@10-19)
661 sending: ')' (2@20-29)
662 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
663 sending: END (3@30-39)
664 input (0@29-29): /* Nothing */
665 input (2@0-29): line (0@0-29) input (0@29-29)
666 Freeing token END (3@30-39)
667 Freeing nterm input (2@0-29)
672 # Check locations in error recovery
673 # ---------------------------------
674 # '(y)' is an error, but can be recovered from. But what's the location
675 # of the error itself ('y'), and of the resulting reduction ('(error)').
676 AT_PARSER_CHECK([./input '(y)'], 0, [],
677 [[sending: '(' (0@0-9)
678 sending: 'y' (1@10-19)
679 10.10-19.18: syntax error, unexpected 'y', expecting 'x'
680 Freeing token 'y' (1@10-19)
681 sending: ')' (2@20-29)
682 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
683 sending: END (3@30-39)
684 input (0@29-29): /* Nothing */
685 input (2@0-29): line (-1@0-29) input (0@29-29)
686 Freeing token END (3@30-39)
687 Freeing nterm input (2@0-29)
692 # Syntax errors caught by the parser
693 # ----------------------------------
694 # Exercise the discarding of stack top and input until 'error'
697 # '(', 'x', 'x', 'x', 'x', 'x', ')',
699 # Load the stack and provoke an error that cannot be caught by the
700 # grammar, to check that the stack is cleared. And make sure the
701 # lookahead is freed.
706 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, [],
707 [[sending: '(' (0@0-9)
708 sending: 'x' (1@10-19)
709 thing (1@10-19): 'x' (1@10-19)
710 sending: 'x' (2@20-29)
711 thing (2@20-29): 'x' (2@20-29)
712 sending: 'x' (3@30-39)
713 30.30-39.38: syntax error, unexpected 'x', expecting ')'
714 Freeing nterm thing (2@20-29)
715 Freeing nterm thing (1@10-19)
716 Freeing token 'x' (3@30-39)
717 sending: 'x' (4@40-49)
718 Freeing token 'x' (4@40-49)
719 sending: 'x' (5@50-59)
720 Freeing token 'x' (5@50-59)
721 sending: ')' (6@60-69)
722 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
723 sending: '(' (7@70-79)
724 sending: 'x' (8@80-89)
725 thing (8@80-89): 'x' (8@80-89)
726 sending: ')' (9@90-99)
727 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
728 sending: '(' (10@100-109)
729 sending: 'x' (11@110-119)
730 thing (11@110-119): 'x' (11@110-119)
731 sending: ')' (12@120-129)
732 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
733 sending: 'y' (13@130-139)
734 input (0@129-129): /* Nothing */
735 input (2@100-129): line (10@100-129) input (0@129-129)
736 input (2@70-129): line (7@70-99) input (2@100-129)
737 input (2@0-129): line (-1@0-69) input (2@70-129)
738 130.130-139.138: syntax error, unexpected 'y', expecting END
739 Freeing nterm input (2@0-129)
740 Freeing token 'y' (13@130-139)
745 # Syntax error caught by the parser where lookahead = END
746 # --------------------------------------------------------
747 # Load the stack and provoke an error that cannot be caught by the
748 # grammar, to check that the stack is cleared. And make sure the
749 # lookahead is freed.
754 AT_PARSER_CHECK([./input '(x)(x)x'], 1, [],
755 [[sending: '(' (0@0-9)
756 sending: 'x' (1@10-19)
757 thing (1@10-19): 'x' (1@10-19)
758 sending: ')' (2@20-29)
759 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
760 sending: '(' (3@30-39)
761 sending: 'x' (4@40-49)
762 thing (4@40-49): 'x' (4@40-49)
763 sending: ')' (5@50-59)
764 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
765 sending: 'x' (6@60-69)
766 thing (6@60-69): 'x' (6@60-69)
767 sending: END (7@70-79)
768 70.70-79.78: syntax error, unexpected END, expecting 'x'
769 Freeing nterm thing (6@60-69)
770 Freeing nterm line (3@30-59)
771 Freeing nterm line (0@0-29)
772 Freeing token END (7@70-79)
777 # Check destruction upon stack overflow
778 # -------------------------------------
779 # Upon stack overflow, all symbols on the stack should be destroyed.
780 # Only check for yacc.c.
782 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, [],
783 [[sending: '(' (0@0-9)
784 sending: 'x' (1@10-19)
785 thing (1@10-19): 'x' (1@10-19)
786 sending: ')' (2@20-29)
787 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
788 sending: '(' (3@30-39)
789 sending: 'x' (4@40-49)
790 thing (4@40-49): 'x' (4@40-49)
791 sending: ')' (5@50-59)
792 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
793 sending: '(' (6@60-69)
794 sending: 'x' (7@70-79)
795 thing (7@70-79): 'x' (7@70-79)
796 sending: ')' (8@80-89)
797 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
798 sending: '(' (9@90-99)
799 sending: 'x' (10@100-109)
800 thing (10@100-109): 'x' (10@100-109)
801 sending: ')' (11@110-119)
802 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
803 sending: '(' (12@120-129)
804 sending: 'x' (13@130-139)
805 thing (13@130-139): 'x' (13@130-139)
806 sending: ')' (14@140-149)
807 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
808 sending: '(' (15@150-159)
809 sending: 'x' (16@160-169)
810 thing (16@160-169): 'x' (16@160-169)
811 sending: ')' (17@170-179)
812 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
813 sending: '(' (18@180-189)
814 sending: 'x' (19@190-199)
815 thing (19@190-199): 'x' (19@190-199)
816 sending: ')' (20@200-209)
817 200.200-209.208: memory exhausted
818 Freeing nterm thing (19@190-199)
819 Freeing nterm line (15@150-179)
820 Freeing nterm line (12@120-149)
821 Freeing nterm line (9@90-119)
822 Freeing nterm line (6@60-89)
823 Freeing nterm line (3@30-59)
824 Freeing nterm line (0@0-29)
825 Parsing FAILED (status 2).
829 AT_BISON_OPTION_POPDEFS
830 ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR
833 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
834 # ---------------------------------------------------------------------------
835 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
836 [AT_SETUP([Printers and Destructors$2]m4_ifval([$1], [[: $1]]))
839 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
840 [%define parse.error verbose
850 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
851 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [ with union])
853 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
854 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [ with union])
856 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
857 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [ with union])
861 ## ----------------------------------------- ##
862 ## Default tagless %printer and %destructor. ##
863 ## ----------------------------------------- ##
865 # Check that the right %printer and %destructor are called, that they're not
866 # called for $end, and that $$ and @$ work correctly.
868 AT_SETUP([Default tagless %printer and %destructor])
869 AT_BISON_OPTION_PUSHDEFS([%locations %debug])
870 AT_DATA_GRAMMAR([[input.y]],
871 [[%define parse.error verbose
882 fprintf (yyoutput, "<*> printer should not be called.\n");
886 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
889 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
893 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
896 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
900 fprintf (yyoutput, "<*> destructor should not be called.\n");
905 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
909 ]AT_YYLEX_DEFINE(["abcd"], [[yylval = res]])[
913 AT_BISON_CHECK([-o input.c input.y], [], [],
914 [[input.y:30.3-5: warning: useless %destructor for type <*> [-Wother]
915 input.y:30.3-5: warning: useless %printer for type <*> [-Wother]
918 AT_PARSER_CHECK([./input --debug], 1,
919 [[<> destructor for 'd' @ 4.
920 'b'/'c' destructor for 'c' @ 3.
921 'b'/'c' destructor for 'b' @ 2.
922 <> destructor for 'a' @ 1.
926 Reading a token: Next token is token 'a' (1.1: <> printer for 'a' @ 1)
927 Shifting token 'a' (1.1: <> printer for 'a' @ 1)
929 Reading a token: Next token is token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
930 Shifting token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
932 Reading a token: Next token is token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
933 Shifting token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
935 Reading a token: Next token is token 'd' (1.4: <> printer for 'd' @ 4)
936 Shifting token 'd' (1.4: <> printer for 'd' @ 4)
938 Reading a token: Now at end of input.
939 1.5: syntax error, unexpected $end, expecting 'e'
940 Error: popping token 'd' (1.4: <> printer for 'd' @ 4)
942 Error: popping token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
944 Error: popping token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
946 Error: popping token 'a' (1.1: <> printer for 'a' @ 1)
948 Cleanup: discarding lookahead token $end (1.5: )
952 AT_BISON_OPTION_POPDEFS
957 ## ------------------------------------------------------ ##
958 ## Default tagged and per-type %printer and %destructor. ##
959 ## ------------------------------------------------------ ##
961 AT_SETUP([Default tagged and per-type %printer and %destructor])
962 AT_BISON_OPTION_PUSHDEFS([%debug])
963 AT_DATA_GRAMMAR([[input.y]],
964 [[%define parse.error verbose
974 fprintf (yyoutput, "<> printer should not be called.\n");
977 %union { int field0; int field1; int field2; }
978 %type <field0> start 'a' 'g'
982 fprintf (yyoutput, "<*>/<field2>/e printer");
985 fprintf (stdout, "<*>/<field2>/e destructor.\n");
989 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
990 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
993 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
994 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
997 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
998 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
1001 fprintf (yyoutput, "<> destructor should not be called.\n");
1007 'a' 'b' 'c' 'd' 'e' 'f' 'g'
1009 USE(($1, $2, $3, $4, $5, $6, $7));
1016 ]AT_YYLEX_DEFINE(["abcdef"])[
1020 AT_BISON_CHECK([-o input.c input.y], [], [],
1021 [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother]
1022 input.y:22.3-4: warning: useless %printer for type <> [-Wother]
1025 AT_PARSER_CHECK([./input --debug], 1,
1026 [[<*>/<field2>/e destructor.
1027 <*>/<field2>/e destructor.
1030 <field1> destructor.
1031 <*>/<field2>/e destructor.
1035 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
1036 Shifting token 'a' (<*>/<field2>/e printer)
1038 Reading a token: Next token is token 'b' (<field1> printer)
1039 Shifting token 'b' (<field1> printer)
1041 Reading a token: Next token is token 'c' ('c' printer)
1042 Shifting token 'c' ('c' printer)
1044 Reading a token: Next token is token 'd' ('d' printer)
1045 Shifting token 'd' ('d' printer)
1047 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
1048 Shifting token 'e' (<*>/<field2>/e printer)
1050 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
1051 Shifting token 'f' (<*>/<field2>/e printer)
1053 Reading a token: Now at end of input.
1054 syntax error, unexpected $end, expecting 'g'
1055 Error: popping token 'f' (<*>/<field2>/e printer)
1056 Stack now 0 1 3 5 6 7
1057 Error: popping token 'e' (<*>/<field2>/e printer)
1059 Error: popping token 'd' ('d' printer)
1061 Error: popping token 'c' ('c' printer)
1063 Error: popping token 'b' (<field1> printer)
1065 Error: popping token 'a' (<*>/<field2>/e printer)
1067 Cleanup: discarding lookahead token $end ()
1071 AT_BISON_OPTION_POPDEFS
1076 ## ------------------------------------------------------------- ##
1077 ## Default %printer and %destructor for user-defined end token. ##
1078 ## ------------------------------------------------------------- ##
1080 AT_SETUP([Default %printer and %destructor for user-defined end token])
1084 m4_pushdef([AT_TEST],
1086 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
1087 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
1089 AT_BISON_OPTION_PUSHDEFS([%locations %debug])
1090 AT_DATA_GRAMMAR([[input]]$1[[.y]],
1091 [[%define parse.error verbose
1096 ]AT_YYERROR_DECLARE[
1102 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
1107 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
1110 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
1114 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
1119 [[[%union { char tag; }
1120 %type <tag> start END]]])[[
1124 start: { $$ = 'S'; } ;
1127 #include <stdlib.h> /* abort */
1134 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
1135 yylloc.first_line = yylloc.last_line = 1;
1136 yylloc.first_column = yylloc.last_column = 1;
1142 AT_BISON_OPTION_POPDEFS
1144 AT_BISON_CHECK([-o input$1.c input$1.y], [], [],
1146 [[input0.y:30.3-5: warning: useless %destructor for type <*> [-Wother]
1147 input0.y:30.3-5: warning: useless %printer for type <*> [-Wother]
1149 [[input1.y:30.3-4: warning: useless %destructor for type <> [-Wother]
1150 input1.y:30.3-4: warning: useless %printer for type <> [-Wother]
1153 AT_COMPILE([input$1])
1155 AT_PARSER_CHECK([./input$1 --debug], 0,
1156 [[<]]kind[[> for 'E' @ 1.
1157 <]]kind[[> for 'S' @ 1.
1161 Reducing stack by rule 1 (line 49):
1162 -> $$ = nterm start (1.1: <]]kind[[> for 'S' @ 1)
1165 Reading a token: Now at end of input.
1166 Shifting token END (1.1: <]]kind[[> for 'E' @ 1)
1169 Cleanup: popping token END (1.1: <]]kind[[> for 'E' @ 1)
1170 Cleanup: popping nterm start (1.1: <]]kind[[> for 'S' @ 1)
1174 m4_popdef([not_kind])
1180 m4_popdef([AT_TEST])
1186 ## ------------------------------------------------------------------ ##
1187 ## Default %printer and %destructor are not for error or $undefined. ##
1188 ## ------------------------------------------------------------------ ##
1190 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
1192 # If Bison were to apply the default %printer and %destructor to the error
1193 # token or to $undefined:
1194 # - For the error token:
1195 # - It would generate warnings for unused $n.
1196 # - It would invoke the %printer and %destructor on the error token's
1197 # semantic value, which would be initialized from the lookahead, which
1198 # would be destroyed separately.
1199 # - For $undefined, who knows what the semantic value would be.
1200 AT_BISON_OPTION_PUSHDEFS([%debug])
1201 AT_DATA_GRAMMAR([[input.y]],
1206 # include <stdlib.h>
1207 ]AT_YYERROR_DECLARE[
1213 fprintf (yyoutput, "'%c'", $$);
1216 fprintf (stderr, "DESTROY '%c'\n", $$);
1223 /* In order to reveal the problems that this bug caused during parsing, add
1225 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1230 ]AT_YYLEX_DEFINE(["abd"], [yylval = res])[
1233 AT_BISON_OPTION_POPDEFS
1235 AT_BISON_CHECK([-o input.c input.y], [], [],
1236 [[input.y:23.6-8: warning: useless %destructor for type <*> [-Wother]
1237 input.y:23.6-8: warning: useless %printer for type <*> [-Wother]
1240 AT_PARSER_CHECK([./input --debug], [1], [],
1243 Reading a token: Next token is token 'a' ('a')
1244 Shifting token 'a' ('a')
1246 Reading a token: Next token is token 'b' ('b')
1248 Shifting token error ()
1250 Next token is token 'b' ('b')
1251 Shifting token 'b' ('b')
1253 Reading a token: Next token is token $undefined ()
1254 Error: popping token 'b' ('b')
1257 Error: popping token error ()
1259 Shifting token error ()
1261 Next token is token $undefined ()
1262 Error: discarding token $undefined ()
1263 Error: popping token error ()
1265 Shifting token error ()
1267 Reading a token: Now at end of input.
1268 Cleanup: discarding lookahead token $end ()
1270 Cleanup: popping token error ()
1271 Cleanup: popping token 'a' ('a')
1279 ## ------------------------------------------------------ ##
1280 ## Default %printer and %destructor are not for $accept. ##
1281 ## ------------------------------------------------------ ##
1283 AT_SETUP([Default %printer and %destructor are not for $accept])
1285 # If YYSTYPE is a union and Bison were to apply the default %printer and
1286 # %destructor to $accept:
1287 # - The %printer and %destructor code generated for $accept would always be
1288 # dead code because $accept is currently never shifted onto the stack.
1289 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1290 # to declare '%type <field> $accept'. (Also true for $undefined.)
1291 # - Thus, the compiler might complain that the user code assumes the wrong
1292 # type for $$ since the code might assume the type associated with a
1293 # specific union field, which is especially reasonable in C++ since that
1294 # type may be a base type. This test case checks for this problem. (Also
1295 # true for $undefined and the error token, so there are three warnings for
1296 # %printer and three for %destructor.)
1298 AT_BISON_OPTION_PUSHDEFS([%debug])
1299 AT_DATA_GRAMMAR([[input.y]],
1300 [[%debug /* So that %printer is actually compiled. */
1304 # include <stdlib.h>
1305 ]AT_YYERROR_DECLARE[
1312 fprintf (yyoutput, "'%c'", chr);
1316 fprintf (stderr, "DESTROY '%c'\n", chr);
1319 %union { char chr; }
1324 start: { USE($$); } ;
1331 AT_BISON_OPTION_POPDEFS
1333 AT_BISON_CHECK([-o input.c input.y], [], [],
1334 [[input.y:24.3-4: warning: useless %destructor for type <> [-Wother]
1335 input.y:24.3-4: warning: useless %printer for type <> [-Wother]
1343 ## ------------------------------------------------------ ##
1344 ## Default %printer and %destructor for mid-rule values. ##
1345 ## ------------------------------------------------------ ##
1347 AT_SETUP([Default %printer and %destructor for mid-rule values])
1349 AT_BISON_OPTION_PUSHDEFS([%debug])
1350 AT_DATA_GRAMMAR([[input.y]],
1351 [[%debug /* So that %printer is actually compiled. */
1354 ]AT_YYERROR_DECLARE[
1357 # define YYLTYPE int
1358 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1359 # define LOCATION_PRINT(File, Loc)
1362 %printer { fprintf (yyoutput, "%d", @$); } <>
1363 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1364 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1365 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1370 { @$ = 1; } // Not set or used.
1371 { USE ($$); @$ = 2; } // Both set and used.
1372 { USE ($$); @$ = 3; } // Only set.
1373 { @$ = 4; } // Only used.
1375 { USE (($$, $2, $4, $5)); @$ = 0; }
1383 AT_BISON_OPTION_POPDEFS
1385 AT_BISON_CHECK([-o input.c input.y], 0,,
1386 [[input.y:24.70-72: warning: useless %destructor for type <*> [-Wother]
1387 input.y:24.70-72: warning: useless %printer for type <*> [-Wother]
1388 input.y:33.3-23: warning: unset value: $$ [-Wother]
1389 input.y:32.3-23: warning: unused value: $3 [-Wother]
1392 AT_BISON_CHECK([-fcaret -o input.c input.y], 0,,
1393 [[input.y:24.70-72: warning: useless %destructor for type <*> [-Wother]
1394 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1396 input.y:24.70-72: warning: useless %printer for type <*> [-Wother]
1397 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1399 input.y:33.3-23: warning: unset value: $$ [-Wother]
1400 { @$ = 4; } // Only used.
1401 ^^^^^^^^^^^^^^^^^^^^^
1402 input.y:32.3-23: warning: unused value: $3 [-Wother]
1403 { USE ($$); @$ = 3; } // Only set.
1404 ^^^^^^^^^^^^^^^^^^^^^
1408 AT_PARSER_CHECK([./input --debug], 1,,
1411 Reducing stack by rule 1 (line 30):
1412 -> $$ = nterm $@1 (: )
1415 Reducing stack by rule 2 (line 31):
1416 -> $$ = nterm @2 (: 2)
1419 Reducing stack by rule 3 (line 32):
1420 -> $$ = nterm @3 (: 3)
1423 Reducing stack by rule 4 (line 33):
1424 -> $$ = nterm @4 (: 4)
1427 Reading a token: Now at end of input.
1429 Error: popping nterm @4 (: 4)
1432 Error: popping nterm @3 (: 3)
1435 Error: popping nterm @2 (: 2)
1438 Error: popping nterm $@1 (: )
1440 Cleanup: discarding lookahead token $end (: )
1447 ## ----------------------- ##
1448 ## @$ implies %locations. ##
1449 ## ----------------------- ##
1451 # Bison once forgot to check for @$ in actions other than semantic actions.
1453 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1454 # -------------------------------------------
1455 m4_define([AT_CHECK_ACTION_LOCATIONS],
1456 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1457 AT_BISON_OPTION_PUSHDEFS([%debug])
1458 AT_DATA_GRAMMAR([[input.y]],
1461 ]AT_YYERROR_DECLARE[
1468 fprintf (stderr, "%d\n", @$.first_line);
1469 } ]m4_if($1, [%initial-action], [], [[start]])[
1487 AT_BISON_CHECK([[-o input.c input.y]])
1488 AT_COMPILE([[input]])
1489 AT_BISON_OPTION_POPDEFS
1492 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1493 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1494 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1497 ## ------------------------- ##
1498 ## Qualified $$ in actions. ##
1499 ## ------------------------- ##
1501 # Check that we can use qualified $$ (v.g., $<type>$) not only in rule
1502 # actions, but also where $$ is valid: %destructor/%printer and
1505 # FIXME: Not actually checking %destructor, but it's the same code as
1508 # To do that, use a semantic value that has two fields (sem_type),
1509 # declare symbols to have only one of these types (INT, float), and
1510 # use $<type>$ to get the other one. Including for symbols that are
1511 # not typed (UNTYPED).
1513 m4_pushdef([AT_TEST],
1514 [AT_SETUP([[Qualified $$ in actions: $1]])
1516 AT_BISON_OPTION_PUSHDEFS([%skeleton "$1" %debug])
1518 AT_DATA_GRAMMAR([[input.y]],
1523 typedef struct sem_type
1529 # define YYSTYPE sem_type
1532 # include <iostream>
1534 report (std::ostream& yyo, int ival, float fval)
1536 yyo << "ival: " << ival << ", fval: " << fval;
1541 report (FILE* yyo, int ival, float fval)
1543 fprintf (yyo, "ival: %d, fval: %1.1f", ival, fval);
1550 ]AT_YYERROR_DECLARE[
1557 %printer { report (yyo, $$, $<fval>$); } <ival>;
1558 %printer { report (yyo, $<ival>$, $$ ); } <fval>;
1559 %printer { report (yyo, $<ival>$, $<fval>$); } <>;
1570 $$ = $<fval>1 + $<fval>2;
1571 $<ival>$ = $<ival>1 + $][2;
1575 ]AT_YYLEX_DEFINE(AT_SKEL_CC_IF([[{yy::parser::token::UNTYPED,
1576 yy::parser::token::INT,
1578 [[{UNTYPED, INT, EOF}]]),
1579 [AT_VAL.ival = toknum * 10; AT_VAL.fval = toknum / 10.0;])[
1583 AT_FULL_COMPILE([[input]])
1584 AT_PARSER_CHECK([./input --debug], 0, [], [stderr])
1585 # Don't be too picky on the traces, GLR is not exactly the same. Keep
1586 # only the lines from the printer.
1587 AT_CHECK([[sed -ne '/ival:/p' stderr]], 0,
1588 [[Reading a token: Next token is token UNTYPED (ival: 10, fval: 0.1)
1589 Shifting token UNTYPED (ival: 10, fval: 0.1)
1590 Reading a token: Next token is token INT (ival: 20, fval: 0.2)
1591 Shifting token INT (ival: 20, fval: 0.2)
1592 $][1 = token UNTYPED (ival: 10, fval: 0.1)
1593 $][2 = token INT (ival: 20, fval: 0.2)
1594 -> $$ = nterm float (ival: 30, fval: 0.3)
1595 Cleanup: popping nterm float (ival: 30, fval: 0.3)
1598 AT_BISON_OPTION_POPDEFS
1608 m4_popdef([AT_TEST])
1610 ## -------------------------------------------------- ##
1611 ## Destroying lookahead assigned by semantic action. ##
1612 ## -------------------------------------------------- ##
1614 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1616 AT_BISON_OPTION_PUSHDEFS
1617 AT_DATA_GRAMMAR([input.y],
1622 ]AT_YYERROR_DECLARE[
1627 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1628 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1632 // In a previous version of Bison, yychar assigned by the semantic
1633 // action below was not translated into yytoken before the lookahead was
1634 // discarded and thus before its destructor (selected according to
1635 // yytoken) was called in order to return from yyparse. This would
1636 // happen even if YYACCEPT was performed in a later semantic action as
1637 // long as only consistent states with default reductions were visited
1638 // in between. However, we leave YYACCEPT in the same semantic action
1639 // for this test in order to show that skeletons cannot simply translate
1640 // immediately after every semantic action because a semantic action
1641 // that has set yychar might not always return normally. Instead,
1642 // skeletons must translate before every use of yytoken.
1643 start: 'a' accept { USE($1); } ;
1645 assert (yychar == YYEMPTY);
1652 ]AT_YYLEX_DEFINE(["a"])[
1655 AT_BISON_OPTION_POPDEFS
1656 AT_BISON_CHECK([[-o input.c input.y]])
1657 AT_COMPILE([[input]])
1658 AT_PARSER_CHECK([[./input]], [[0]], [],
1669 AT_SETUP([[YYBACKUP]])
1671 AT_BISON_OPTION_PUSHDEFS([%pure-parser %debug])
1673 AT_DATA_GRAMMAR([input.y],
1680 # include <stdlib.h>
1681 # include <assert.h>
1683 ]AT_YYERROR_DECLARE[
1692 'a' { printf ("a: %d\n", $1); }
1693 | 'b' { YYBACKUP('a', 123); }
1694 | 'c' 'd' { YYBACKUP('a', 456); }
1699 ]AT_YYLEX_DEFINE(["bcd"], [*lvalp = (toknum + 1) * 10])[
1702 AT_BISON_OPTION_POPDEFS
1704 AT_BISON_CHECK([[-o input.c input.y]])
1705 AT_COMPILE([[input]])
1706 AT_PARSER_CHECK([[./input]], [[0]],