1 e# 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]],
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"])[
61 AT_BISON_OPTION_POPDEFS
63 AT_BISON_CHECK([-d -v -o input.c input.y])
65 AT_PARSER_CHECK([./input], 0,
72 ## ------------------ ##
73 ## Initial location. ##
74 ## ------------------ ##
76 # AT_TEST(SKELETON-NAME, DIRECTIVES, [MORE-DIRECTIVES], [LOCATION = 1.1])
77 # -----------------------------------------------------------------------
78 # Check that the initial location is correct.
80 [AT_SETUP([Initial location: $1 $2])
82 AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "$1" $2 %parse-param { int x }])
83 AT_DATA_GRAMMAR([[input.y]],
84 [[%defines /* FIXME: Required by lalr1.cc in Bison 2.6. */
90 %parse-param { int x } // Useless, but used to force yyerror purity.
94 # include <stdlib.h> // getenv
99 exp: { ]AT_SKEL_CC_IF([[std::cerr << @$ << std::endl]],
100 [[YY_LOCATION_PRINT(stderr, @$); fputc ('\n', stderr)]])[; }
107 YYUSE(llocp);], [AT_SKEL_CC_IF([
117 p.set_debug_level (!!getenv("YYDEBUG"));
118 return p.parse ();]], [[
119 yydebug = !!getenv("YYDEBUG");
120 return !!yyparse (0);]])[
124 AT_FULL_COMPILE([input])
125 AT_PARSER_CHECK([./input], 1, [],
126 [m4_default([$4], [1.1])
127 m4_default([$4], [1.1])[: syntax error
129 AT_BISON_OPTION_POPDEFS
133 ## FIXME: test Java, and iterate over skeletons.
135 AT_TEST([yacc.c], [%define api.pure])
136 AT_TEST([yacc.c], [%define api.push-pull both])
137 AT_TEST([yacc.c], [%define api.push-pull both %define api.pure])
142 ## A very different test, based on PostgreSQL's implementation of the
144 ## http://lists.gnu.org/archive/html/bug-bison/2012-11/msg00023.html
146 ## Weirdly enough, to trigger the warning with GCC 4.7, we must not
147 ## use fprintf, so run the test twice: once to check the warning
148 ## (absence thereof), and another time to check the value.
149 AT_TEST([yacc.c], [%define api.pure],
152 # define YY_LOCATION_PRINT(Stream, Loc) \
154 # define YYLLOC_DEFAULT(Current, Rhs, N) \
155 (Current) = ((Rhs)[N ? 1 : 0])
160 AT_TEST([yacc.c], [%define api.pure],
163 # define YY_LOCATION_PRINT(Stream, Loc) \
164 fprintf ((Stream), "%d", (Loc))
165 # define YYLLOC_DEFAULT(Current, Rhs, N) \
166 (Current) = ((Rhs)[N ? 1 : 0])
176 ## ---------------- ##
177 ## Exotic Dollars. ##
178 ## ---------------- ##
180 AT_SETUP([Exotic Dollars])
182 AT_BISON_OPTION_PUSHDEFS
183 AT_DATA_GRAMMAR([[input.y]],
184 [[%define parse.error verbose
197 %type <val> a_1 a_2 a_5
198 sum_of_the_five_previous_values
201 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
202 sum_of_the_five_previous_values
204 USE (($1, $2, $<foo>3, $<foo>4, $5));
212 sum_of_the_five_previous_values:
214 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
228 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
230 AT_PARSER_CHECK([./input], 0,
234 # Make sure that fields after $n or $-n are parsed correctly. At one
235 # point while implementing dashes in symbol names, we were dropping
237 AT_DATA_GRAMMAR([[input.y]],
243 typedef struct { int val; } stype;
244 # define YYSTYPE stype
248 start: one two { $$.val = $1.val + $2.val; } sum ;
249 one: { $$.val = 1; } ;
250 two: { $$.val = 2; } ;
251 sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ;
263 AT_FULL_COMPILE([input])
264 AT_PARSER_CHECK([[./input]], [[0]],
268 AT_BISON_OPTION_POPDEFS
273 ## -------------------------- ##
274 ## Printers and Destructors. ##
275 ## -------------------------- ##
277 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4,
278 # BISON-DIRECTIVE, UNION-FLAG)
279 # -------------------------------------------------------------
280 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
281 [# Make sure complex $n work.
282 m4_if([$1$2$3$4], $[1]$[2]$[3]$[4], [],
283 [m4_fatal([$0: Invalid arguments: $@])])dnl
285 # Be sure to pass all the %directives to this macro to have correct
286 # helping macros. So don't put any directly in the Bison file.
287 AT_BISON_OPTION_PUSHDEFS([$5])
288 AT_DATA_GRAMMAR([[input.y]],
295 #define YYINITDEPTH 10
296 #define YYMAXDEPTH 10
297 #define RANGE(Location) ]AT_LALR1_CC_IF([(Location).begin.line, (Location).end.line],
298 [(Location).first_line, (Location).last_line])[
300 /* Display the symbol type Symbol. */
301 #define V(Symbol, Value, Location, Sep) \
302 fprintf (stderr, #Symbol " (%d@%d-%d)" Sep, Value, RANGE(Location))
306 ]m4_ifval([$6], [%union
310 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
311 m4_ifval([$6], [[%code provides {]], [[%code {]])
312 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])[
314 ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE])
317 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
319 /* FIXME: This %printer isn't actually tested. */
322 ]AT_LALR1_CC_IF([debug_stream () << $$;],
323 [fprintf (yyoutput, "%d", $$)])[;
325 input line thing 'x' 'y'
328 { fprintf (stderr, "Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
332 { fprintf (stderr, "Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
336 { fprintf (stderr, "Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
340 { fprintf (stderr, "Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
344 { fprintf (stderr, "Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
349 { fprintf (stderr, "Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
354 This grammar is made to exercise error recovery.
355 "Lines" starting with `(' support error recovery, with
356 ')' as synchronizing token. Lines starting with 'x' can never
357 be recovered from if in error.
364 V(input, $$, @$, ": /* Nothing */\n");
366 | line input /* Right recursive to load the stack so that popping at
367 END can be exercised. */
370 V(input, $$, @$, ": ");
371 V(line, $1, @1, " ");
372 V(input, $2, @2, "\n");
377 thing thing thing ';'
380 V(line, $$, @$, ": ");
381 V(thing, $1, @1, " ");
382 V(thing, $2, @2, " ");
383 V(thing, $3, @3, " ");
386 | '(' thing thing ')'
389 V(line, $$, @$, ": ");
391 V(thing, $2, @2, " ");
392 V(thing, $3, @3, " ");
393 V(')', $4, @4, "\n");
398 V(line, $$, @$, ": ");
400 V(thing, $2, @2, " ");
401 V(')', $3, @3, "\n");
406 V(line, $$, @$, ": ");
408 fprintf (stderr, "error (@%d-%d) ", RANGE(@2));
409 V(')', $3, @3, "\n");
417 V(thing, $$, @$, ": ");
418 V('x', $1, @1, "\n");
422 /* Alias to ARGV[1]. */
423 const char *source = YY_NULL;
430 static unsigned int counter = 0;
432 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
433 /* As in BASIC, line numbers go from 10 to 10. */
434 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_FIRST_COLUMN[ = 10 * c;
435 ]AT_LOC_LAST_LINE[ = ]AT_LOC_LAST_COLUMN[ = ]AT_LOC_FIRST_LINE[ + 9;
436 assert (0 <= c && c <= strlen (source));
438 fprintf (stderr, "sending: '%c'", source[c]);
440 fprintf (stderr, "sending: END");
441 fprintf (stderr, " (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
445 [static bool yydebug;
450 parser.set_debug_level (yydebug);
451 return parser.parse ();
456 main (int argc, const char *argv[])
459 yydebug = !!getenv ("YYDEBUG");
465 case 0: fprintf (stderr, "Successful parse.\n"); break;
466 case 1: fprintf (stderr, "Parsing FAILED.\n"); break;
467 default: fprintf (stderr, "Parsing FAILED (status %d).\n", status); break;
473 AT_FULL_COMPILE([input])
476 # Check the location of "empty"
477 # -----------------------------
478 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
479 # an empty "line" nterm.
480 # FIXME: This location is not satisfying. Depend on the lookahead?
481 AT_PARSER_CHECK([./input '(x)'], 0, [],
482 [[sending: '(' (0@0-9)
483 sending: 'x' (1@10-19)
484 thing (1@10-19): 'x' (1@10-19)
485 sending: ')' (2@20-29)
486 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
487 sending: END (3@30-39)
488 input (0@29-29): /* Nothing */
489 input (2@0-29): line (0@0-29) input (0@29-29)
490 Freeing token END (3@30-39)
491 Freeing nterm input (2@0-29)
496 # Check locations in error recovery
497 # ---------------------------------
498 # '(y)' is an error, but can be recovered from. But what's the location
499 # of the error itself ('y'), and of the resulting reduction ('(error)').
500 AT_PARSER_CHECK([./input '(y)'], 0, [],
501 [[sending: '(' (0@0-9)
502 sending: 'y' (1@10-19)
503 10.10-19.18: syntax error, unexpected 'y', expecting 'x'
504 Freeing token 'y' (1@10-19)
505 sending: ')' (2@20-29)
506 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
507 sending: END (3@30-39)
508 input (0@29-29): /* Nothing */
509 input (2@0-29): line (-1@0-29) input (0@29-29)
510 Freeing token END (3@30-39)
511 Freeing nterm input (2@0-29)
516 # Syntax errors caught by the parser
517 # ----------------------------------
518 # Exercise the discarding of stack top and input until `error'
521 # '(', 'x', 'x', 'x', 'x', 'x', ')',
523 # Load the stack and provoke an error that cannot be caught by the
524 # grammar, to check that the stack is cleared. And make sure the
525 # lookahead is freed.
530 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, [],
531 [[sending: '(' (0@0-9)
532 sending: 'x' (1@10-19)
533 thing (1@10-19): 'x' (1@10-19)
534 sending: 'x' (2@20-29)
535 thing (2@20-29): 'x' (2@20-29)
536 sending: 'x' (3@30-39)
537 30.30-39.38: syntax error, unexpected 'x', expecting ')'
538 Freeing nterm thing (2@20-29)
539 Freeing nterm thing (1@10-19)
540 Freeing token 'x' (3@30-39)
541 sending: 'x' (4@40-49)
542 Freeing token 'x' (4@40-49)
543 sending: 'x' (5@50-59)
544 Freeing token 'x' (5@50-59)
545 sending: ')' (6@60-69)
546 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
547 sending: '(' (7@70-79)
548 sending: 'x' (8@80-89)
549 thing (8@80-89): 'x' (8@80-89)
550 sending: ')' (9@90-99)
551 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
552 sending: '(' (10@100-109)
553 sending: 'x' (11@110-119)
554 thing (11@110-119): 'x' (11@110-119)
555 sending: ')' (12@120-129)
556 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
557 sending: 'y' (13@130-139)
558 input (0@129-129): /* Nothing */
559 input (2@100-129): line (10@100-129) input (0@129-129)
560 input (2@70-129): line (7@70-99) input (2@100-129)
561 input (2@0-129): line (-1@0-69) input (2@70-129)
562 130.130-139.138: syntax error, unexpected 'y', expecting END
563 Freeing nterm input (2@0-129)
564 Freeing token 'y' (13@130-139)
569 # Syntax error caught by the parser where lookahead = END
570 # --------------------------------------------------------
571 # Load the stack and provoke an error that cannot be caught by the
572 # grammar, to check that the stack is cleared. And make sure the
573 # lookahead is freed.
578 AT_PARSER_CHECK([./input '(x)(x)x'], 1, [],
579 [[sending: '(' (0@0-9)
580 sending: 'x' (1@10-19)
581 thing (1@10-19): 'x' (1@10-19)
582 sending: ')' (2@20-29)
583 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
584 sending: '(' (3@30-39)
585 sending: 'x' (4@40-49)
586 thing (4@40-49): 'x' (4@40-49)
587 sending: ')' (5@50-59)
588 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
589 sending: 'x' (6@60-69)
590 thing (6@60-69): 'x' (6@60-69)
591 sending: END (7@70-79)
592 70.70-79.78: syntax error, unexpected END, expecting 'x'
593 Freeing nterm thing (6@60-69)
594 Freeing nterm line (3@30-59)
595 Freeing nterm line (0@0-29)
596 Freeing token END (7@70-79)
601 # Check destruction upon stack overflow
602 # -------------------------------------
603 # Upon stack overflow, all symbols on the stack should be destroyed.
604 # Only check for yacc.c.
606 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, [],
607 [[sending: '(' (0@0-9)
608 sending: 'x' (1@10-19)
609 thing (1@10-19): 'x' (1@10-19)
610 sending: ')' (2@20-29)
611 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
612 sending: '(' (3@30-39)
613 sending: 'x' (4@40-49)
614 thing (4@40-49): 'x' (4@40-49)
615 sending: ')' (5@50-59)
616 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
617 sending: '(' (6@60-69)
618 sending: 'x' (7@70-79)
619 thing (7@70-79): 'x' (7@70-79)
620 sending: ')' (8@80-89)
621 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
622 sending: '(' (9@90-99)
623 sending: 'x' (10@100-109)
624 thing (10@100-109): 'x' (10@100-109)
625 sending: ')' (11@110-119)
626 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
627 sending: '(' (12@120-129)
628 sending: 'x' (13@130-139)
629 thing (13@130-139): 'x' (13@130-139)
630 sending: ')' (14@140-149)
631 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
632 sending: '(' (15@150-159)
633 sending: 'x' (16@160-169)
634 thing (16@160-169): 'x' (16@160-169)
635 sending: ')' (17@170-179)
636 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
637 sending: '(' (18@180-189)
638 sending: 'x' (19@190-199)
639 thing (19@190-199): 'x' (19@190-199)
640 sending: ')' (20@200-209)
641 200.200-209.208: memory exhausted
642 Freeing nterm thing (19@190-199)
643 Freeing nterm line (15@150-179)
644 Freeing nterm line (12@120-149)
645 Freeing nterm line (9@90-119)
646 Freeing nterm line (6@60-89)
647 Freeing nterm line (3@30-59)
648 Freeing nterm line (0@0-29)
649 Parsing FAILED (status 2).
653 AT_BISON_OPTION_POPDEFS
654 ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR
657 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
658 # ---------------------------------------------------------------------------
659 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
660 [AT_SETUP([Printers and Destructors$2]m4_ifval([$1], [[: $1]]))
663 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
664 [%define parse.error verbose
674 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
675 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
677 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
678 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
680 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
681 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
685 ## ----------------------------------------- ##
686 ## Default tagless %printer and %destructor. ##
687 ## ----------------------------------------- ##
689 # Check that the right %printer and %destructor are called, that they're not
690 # called for $end, and that $$ and @$ work correctly.
692 AT_SETUP([Default tagless %printer and %destructor])
693 AT_BISON_OPTION_PUSHDEFS([%locations])
694 AT_DATA_GRAMMAR([[input.y]],
695 [[%define parse.error verbose
708 fprintf (yyoutput, "<*> printer should not be called.\n");
712 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
715 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
719 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
722 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
726 fprintf (yyoutput, "<*> destructor should not be called.\n");
731 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
735 ]AT_YYLEX_DEFINE(["abcd"], [[yylval = res]])[
745 AT_BISON_CHECK([-o input.c input.y], [], [],
746 [[input.y:23.3-5: warning: useless %destructor for type <*> [-Wother]
747 input.y:23.3-5: warning: useless %printer for type <*> [-Wother]
750 AT_PARSER_CHECK([./input], 1,
751 [[<> destructor for 'd' @ 4.
752 'b'/'c' destructor for 'c' @ 3.
753 'b'/'c' destructor for 'b' @ 2.
754 <> destructor for 'a' @ 1.
758 Reading a token: Next token is token 'a' (1.1: <> printer for 'a' @ 1)
759 Shifting token 'a' (1.1: <> printer for 'a' @ 1)
761 Reading a token: Next token is token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
762 Shifting token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
764 Reading a token: Next token is token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
765 Shifting token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
767 Reading a token: Next token is token 'd' (1.4: <> printer for 'd' @ 4)
768 Shifting token 'd' (1.4: <> printer for 'd' @ 4)
770 Reading a token: Now at end of input.
771 1.5: syntax error, unexpected $end, expecting 'e'
772 Error: popping token 'd' (1.4: <> printer for 'd' @ 4)
774 Error: popping token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
776 Error: popping token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
778 Error: popping token 'a' (1.1: <> printer for 'a' @ 1)
780 Cleanup: discarding lookahead token $end (1.5: )
784 AT_BISON_OPTION_POPDEFS
789 ## ------------------------------------------------------ ##
790 ## Default tagged and per-type %printer and %destructor. ##
791 ## ------------------------------------------------------ ##
793 AT_SETUP([Default tagged and per-type %printer and %destructor])
794 AT_BISON_OPTION_PUSHDEFS
795 AT_DATA_GRAMMAR([[input.y]],
796 [[%define parse.error verbose
808 fprintf (yyoutput, "<> printer should not be called.\n");
811 %union { int field0; int field1; int field2; }
812 %type <field0> start 'a' 'g'
816 fprintf (yyoutput, "<*>/<field2>/e printer");
819 fprintf (stdout, "<*>/<field2>/e destructor.\n");
823 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
824 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
827 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
828 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
831 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
832 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
835 fprintf (yyoutput, "<> destructor should not be called.\n");
841 'a' 'b' 'c' 'd' 'e' 'f' 'g'
843 USE(($1, $2, $3, $4, $5, $6, $7));
850 ]AT_YYLEX_DEFINE(["abcdef"])[
860 AT_BISON_CHECK([-o input.c input.y], [], [],
861 [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother]
862 input.y:22.3-4: warning: useless %printer for type <> [-Wother]
865 AT_PARSER_CHECK([./input], 1,
866 [[<*>/<field2>/e destructor.
867 <*>/<field2>/e destructor.
871 <*>/<field2>/e destructor.
875 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
876 Shifting token 'a' (<*>/<field2>/e printer)
878 Reading a token: Next token is token 'b' (<field1> printer)
879 Shifting token 'b' (<field1> printer)
881 Reading a token: Next token is token 'c' ('c' printer)
882 Shifting token 'c' ('c' printer)
884 Reading a token: Next token is token 'd' ('d' printer)
885 Shifting token 'd' ('d' printer)
887 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
888 Shifting token 'e' (<*>/<field2>/e printer)
890 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
891 Shifting token 'f' (<*>/<field2>/e printer)
893 Reading a token: Now at end of input.
894 syntax error, unexpected $end, expecting 'g'
895 Error: popping token 'f' (<*>/<field2>/e printer)
896 Stack now 0 1 3 5 6 7
897 Error: popping token 'e' (<*>/<field2>/e printer)
899 Error: popping token 'd' ('d' printer)
901 Error: popping token 'c' ('c' printer)
903 Error: popping token 'b' (<field1> printer)
905 Error: popping token 'a' (<*>/<field2>/e printer)
907 Cleanup: discarding lookahead token $end ()
911 AT_BISON_OPTION_POPDEFS
916 ## ------------------------------------------------------------- ##
917 ## Default %printer and %destructor for user-defined end token. ##
918 ## ------------------------------------------------------------- ##
920 AT_SETUP([Default %printer and %destructor for user-defined end token])
924 m4_pushdef([AT_TEST],
926 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
927 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
929 AT_BISON_OPTION_PUSHDEFS([%locations])
930 AT_DATA_GRAMMAR([[input]]$1[[.y]],
931 [[%define parse.error verbose
944 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
949 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
952 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
956 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
961 [[[%union { char tag; }
962 %type <tag> start END]]])[[
966 start: { $$ = 'S'; } ;
976 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
977 yylloc.first_line = yylloc.last_line = 1;
978 yylloc.first_column = yylloc.last_column = 1;
990 AT_BISON_OPTION_POPDEFS
992 AT_BISON_CHECK([-o input$1.c input$1.y], [], [],
994 [[input0.y:23.3-5: warning: useless %destructor for type <*> [-Wother]
995 input0.y:23.3-5: warning: useless %printer for type <*> [-Wother]
997 [[input1.y:23.3-4: warning: useless %destructor for type <> [-Wother]
998 input1.y:23.3-4: warning: useless %printer for type <> [-Wother]
1001 AT_COMPILE([input$1])
1003 AT_PARSER_CHECK([./input$1], 0,
1004 [[<]]kind[[> for 'E' @ 1.
1005 <]]kind[[> for 'S' @ 1.
1009 Reducing stack by rule 1 (line 42):
1010 -> $$ = nterm start (1.1: <]]kind[[> for 'S' @ 1)
1013 Reading a token: Now at end of input.
1014 Shifting token END (1.1: <]]kind[[> for 'E' @ 1)
1017 Cleanup: popping token END (1.1: <]]kind[[> for 'E' @ 1)
1018 Cleanup: popping nterm start (1.1: <]]kind[[> for 'S' @ 1)
1022 m4_popdef([not_kind])
1028 m4_popdef([AT_TEST])
1034 ## ------------------------------------------------------------------ ##
1035 ## Default %printer and %destructor are not for error or $undefined. ##
1036 ## ------------------------------------------------------------------ ##
1038 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
1040 # If Bison were to apply the default %printer and %destructor to the error
1041 # token or to $undefined:
1042 # - For the error token:
1043 # - It would generate warnings for unused $n.
1044 # - It would invoke the %printer and %destructor on the error token's
1045 # semantic value, which would be initialized from the lookahead, which
1046 # would be destroyed separately.
1047 # - For $undefined, who knows what the semantic value would be.
1048 AT_BISON_OPTION_PUSHDEFS
1049 AT_DATA_GRAMMAR([[input.y]],
1054 # include <stdlib.h>
1055 ]AT_YYERROR_DECLARE[
1061 fprintf (yyoutput, "'%c'", $$);
1064 fprintf (stderr, "DESTROY '%c'\n", $$);
1071 /* In order to reveal the problems that this bug caused during parsing, add
1073 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1078 ]AT_YYLEX_DEFINE(["abd"], [yylval = res])[
1086 AT_BISON_OPTION_POPDEFS
1088 AT_BISON_CHECK([-o input.c input.y], [], [],
1089 [[input.y:21.6-8: warning: useless %destructor for type <*> [-Wother]
1090 input.y:21.6-8: warning: useless %printer for type <*> [-Wother]
1093 AT_PARSER_CHECK([./input], [1], [],
1096 Reading a token: Next token is token 'a' ('a')
1097 Shifting token 'a' ('a')
1099 Reading a token: Next token is token 'b' ('b')
1101 Shifting token error ()
1103 Next token is token 'b' ('b')
1104 Shifting token 'b' ('b')
1106 Reading a token: Next token is token $undefined ()
1107 Error: popping token 'b' ('b')
1110 Error: popping token error ()
1112 Shifting token error ()
1114 Next token is token $undefined ()
1115 Error: discarding token $undefined ()
1116 Error: popping token error ()
1118 Shifting token error ()
1120 Reading a token: Now at end of input.
1121 Cleanup: discarding lookahead token $end ()
1123 Cleanup: popping token error ()
1124 Cleanup: popping token 'a' ('a')
1132 ## ------------------------------------------------------ ##
1133 ## Default %printer and %destructor are not for $accept. ##
1134 ## ------------------------------------------------------ ##
1136 AT_SETUP([Default %printer and %destructor are not for $accept])
1138 # If YYSTYPE is a union and Bison were to apply the default %printer and
1139 # %destructor to $accept:
1140 # - The %printer and %destructor code generated for $accept would always be
1141 # dead code because $accept is currently never shifted onto the stack.
1142 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1143 # to declare `%type <field> $accept'. (Also true for $undefined.)
1144 # - Thus, the compiler might complain that the user code assumes the wrong
1145 # type for $$ since the code might assume the type associated with a
1146 # specific union field, which is especially reasonable in C++ since that
1147 # type may be a base type. This test case checks for this problem. (Also
1148 # true for $undefined and the error token, so there are three warnings for
1149 # %printer and three for %destructor.)
1151 AT_BISON_OPTION_PUSHDEFS
1152 AT_DATA_GRAMMAR([[input.y]],
1153 [[%debug /* So that %printer is actually compiled. */
1157 # include <stdlib.h>
1158 ]AT_YYERROR_DECLARE[
1165 fprintf (yyoutput, "'%c'", chr);
1169 fprintf (stderr, "DESTROY '%c'\n", chr);
1172 %union { char chr; }
1177 start: { USE($$); } ;
1188 AT_BISON_OPTION_POPDEFS
1190 AT_BISON_CHECK([-o input.c input.y], [], [],
1191 [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother]
1192 input.y:22.3-4: warning: useless %printer for type <> [-Wother]
1200 ## ------------------------------------------------------ ##
1201 ## Default %printer and %destructor for mid-rule values. ##
1202 ## ------------------------------------------------------ ##
1204 AT_SETUP([Default %printer and %destructor for mid-rule values])
1206 AT_BISON_OPTION_PUSHDEFS
1207 AT_DATA_GRAMMAR([[input.y]],
1208 [[%debug /* So that %printer is actually compiled. */
1212 # include <stdlib.h>
1213 ]AT_YYERROR_DECLARE[
1216 # define YYLTYPE int
1217 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1218 # define YY_LOCATION_PRINT(File, Loc)
1221 %printer { fprintf (yyoutput, "%d", @$); } <>
1222 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1223 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1224 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1229 { @$ = 1; } // Not set or used.
1230 { USE ($$); @$ = 2; } // Both set and used.
1231 { USE ($$); @$ = 3; } // Only set.
1232 { @$ = 4; } // Only used.
1234 { USE (($$, $2, $4, $5)); @$ = 0; }
1247 AT_BISON_OPTION_POPDEFS
1249 AT_BISON_CHECK([-o input.c input.y], 0,,
1250 [[input.y:24.70-72: warning: useless %destructor for type <*> [-Wother]
1251 input.y:24.70-72: warning: useless %printer for type <*> [-Wother]
1252 input.y:33.3-23: warning: unset value: $$ [-Wother]
1253 input.y:30.3-35.37: warning: unused value: $3 [-Wother]
1257 AT_PARSER_CHECK([./input], 1,,
1260 Reducing stack by rule 1 (line 30):
1261 -> $$ = nterm $@1 (: )
1264 Reducing stack by rule 2 (line 31):
1265 -> $$ = nterm @2 (: 2)
1268 Reducing stack by rule 3 (line 32):
1269 -> $$ = nterm @3 (: 3)
1272 Reducing stack by rule 4 (line 33):
1273 -> $$ = nterm @4 (: 4)
1276 Reading a token: Now at end of input.
1278 Error: popping nterm @4 (: 4)
1281 Error: popping nterm @3 (: 3)
1284 Error: popping nterm @2 (: 2)
1287 Error: popping nterm $@1 (: )
1289 Cleanup: discarding lookahead token $end (: )
1296 ## ----------------------- ##
1297 ## @$ implies %locations. ##
1298 ## ----------------------- ##
1300 # Bison once forgot to check for @$ in actions other than semantic actions.
1302 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1303 # -------------------------------------------
1304 m4_define([AT_CHECK_ACTION_LOCATIONS],
1305 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1306 AT_BISON_OPTION_PUSHDEFS
1307 AT_DATA_GRAMMAR([[input.y]],
1310 ]AT_YYERROR_DECLARE[
1317 fprintf (stderr, "%d\n", @$.first_line);
1318 } ]m4_if($1, [%initial-action], [], [[start]])[
1340 AT_BISON_CHECK([[-o input.c input.y]])
1341 AT_COMPILE([[input]])
1342 AT_BISON_OPTION_POPDEFS
1345 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1346 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1347 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1350 ## ------------------------- ##
1351 ## Qualified $$ in actions. ##
1352 ## ------------------------- ##
1354 # Check that we can use qualified $$ (v.g., $<type>$) not only in rule
1355 # actions, but also where $$ is valid: %destructor/%printer and
1358 # FIXME: Not actually checking %destructor, but it's the same code as
1361 # To do that, use a semantic value that has two fields (sem_type),
1362 # declare symbols to have only one of these types (INT, float), and
1363 # use $<type>$ to get the other one. Including for symbols that are
1364 # not typed (UNTYPED).
1366 m4_pushdef([AT_TEST],
1367 [AT_SETUP([[Qualified $$ in actions: $1]])
1369 AT_BISON_OPTION_PUSHDEFS([%skeleton "$1"])
1371 AT_DATA_GRAMMAR([[input.y]],
1376 typedef struct sem_type
1382 # define YYSTYPE sem_type
1385 # include <iostream>
1387 report (std::ostream& yyo, int ival, float fval)
1389 yyo << "ival: " << ival << ", fval: " << fval;
1394 report (FILE* yyo, int ival, float fval)
1396 fprintf (yyo, "ival: %d, fval: %1.1f", ival, fval);
1403 ]AT_YYERROR_DECLARE[
1410 %printer { report (yyo, $$, $<fval>$); } <ival>;
1411 %printer { report (yyo, $<ival>$, $$ ); } <fval>;
1412 %printer { report (yyo, $<ival>$, $<fval>$); } <>;
1423 $$ = $<fval>1 + $<fval>2;
1424 $<ival>$ = $<ival>1 + $][2;
1428 ]AT_YYLEX_DEFINE(AT_SKEL_CC_IF([[{yy::parser::token::UNTYPED,
1429 yy::parser::token::INT,
1431 [[{UNTYPED, INT, EOF}]]),
1432 [AT_VAL.ival = toknum * 10; AT_VAL.fval = toknum / 10.0;])[
1437 p.set_debug_level(1);
1438 return p.parse ();]], [[
1440 return yyparse ();]])[
1444 AT_FULL_COMPILE([[input]])
1445 AT_PARSER_CHECK([./input], 0, [], [stderr])
1446 # Don't be too picky on the traces, GLR is not exactly the same. Keep
1447 # only the lines from the printer.
1448 AT_CHECK([[sed -ne '/ival:/p' stderr]], 0,
1449 [[Reading a token: Next token is token UNTYPED (ival: 10, fval: 0.1)
1450 Shifting token UNTYPED (ival: 10, fval: 0.1)
1451 Reading a token: Next token is token INT (ival: 20, fval: 0.2)
1452 Shifting token INT (ival: 20, fval: 0.2)
1453 $][1 = token UNTYPED (ival: 10, fval: 0.1)
1454 $][2 = token INT (ival: 20, fval: 0.2)
1455 -> $$ = nterm float (ival: 30, fval: 0.3)
1456 Cleanup: popping nterm float (ival: 30, fval: 0.3)
1459 AT_BISON_OPTION_POPDEFS
1469 m4_popdef([AT_TEST])
1471 ## ----------------------------------------------- ##
1472 ## Fix user actions without a trailing semicolon. ##
1473 ## ----------------------------------------------- ##
1475 AT_SETUP([[Fix user actions without a trailing semicolon]])
1477 # This feature is undocumented, but we accidentally broke it in 2.3a,
1478 # and there was a complaint at:
1479 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1480 AT_BISON_OPTION_PUSHDEFS
1483 start: test2 test1 test0 testc;
1486 : 'a' { semi; /* TEST:N:2 */ }
1487 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1488 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1489 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1490 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1491 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1492 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1493 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1494 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1497 : 'a' { semi; // TEST:N:1 ;
1498 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1499 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1500 } | 'd' { semi; no_semi // TEST:Y:1 ;
1501 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1502 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1503 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1504 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1505 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1508 : 'a' { semi; // TEST:N:1 {}
1509 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1510 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1511 } | 'd' { semi; no_semi // TEST:Y:1 {}
1512 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1513 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1514 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1515 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1516 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1521 #define TEST_MACRO_N \
1522 []"broken\" $ @ $$ @$ [];\
1526 #define TEST_MACRO_N \
1527 []"broken\" $ @ $$ @$ [];\
1530 AT_BISON_OPTION_POPDEFS
1532 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1533 [[input.y:8.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1534 input.y:8.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1535 input.y:9.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1536 input.y:9.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1537 input.y:10.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1538 input.y:10.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1539 input.y:11.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1540 input.y:11.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1541 input.y:12.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1542 input.y:12.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1543 input.y:13.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1544 input.y:13.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1545 input.y:20.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1546 input.y:20.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1547 input.y:21.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1548 input.y:21.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1549 input.y:22.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1550 input.y:22.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1551 input.y:23.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1552 input.y:23.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1553 input.y:24.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1554 input.y:24.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1555 input.y:25.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1556 input.y:25.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1557 input.y:31.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1558 input.y:31.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1559 input.y:32.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1560 input.y:32.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1561 input.y:33.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1562 input.y:33.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1563 input.y:34.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1564 input.y:34.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1565 input.y:35.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1566 input.y:35.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1567 input.y:36.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1568 input.y:36.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1571 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1572 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1573 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1574 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1575 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1580 ## -------------------------------------------------- ##
1581 ## Destroying lookahead assigned by semantic action. ##
1582 ## -------------------------------------------------- ##
1584 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1586 AT_BISON_OPTION_PUSHDEFS
1587 AT_DATA_GRAMMAR([input.y],
1592 ]AT_YYERROR_DECLARE[
1597 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1598 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1602 // In a previous version of Bison, yychar assigned by the semantic
1603 // action below was not translated into yytoken before the lookahead was
1604 // discarded and thus before its destructor (selected according to
1605 // yytoken) was called in order to return from yyparse. This would
1606 // happen even if YYACCEPT was performed in a later semantic action as
1607 // long as only consistent states with default reductions were visited
1608 // in between. However, we leave YYACCEPT in the same semantic action
1609 // for this test in order to show that skeletons cannot simply translate
1610 // immediately after every semantic action because a semantic action
1611 // that has set yychar might not always return normally. Instead,
1612 // skeletons must translate before every use of yytoken.
1613 start: 'a' accept { USE($1); } ;
1615 assert (yychar == YYEMPTY);
1622 ]AT_YYLEX_DEFINE(["a"])[
1629 AT_BISON_OPTION_POPDEFS
1630 AT_BISON_CHECK([[-o input.c input.y]])
1631 AT_COMPILE([[input]])
1632 AT_PARSER_CHECK([[./input]], [[0]], [],
1643 AT_SETUP([[YYBACKUP]])
1645 AT_BISON_OPTION_PUSHDEFS([%pure-parser])
1647 AT_DATA_GRAMMAR([input.y],
1654 # include <stdlib.h>
1655 # include <assert.h>
1657 ]AT_YYERROR_DECLARE[
1666 'a' { printf ("a: %d\n", $1); }
1667 | 'b' { YYBACKUP('a', 123); }
1668 | 'c' 'd' { YYBACKUP('a', 456); }
1673 ]AT_YYLEX_DEFINE(["bcd"], [*lvalp = (toknum + 1) * 10])[
1678 yydebug = !!getenv("YYDEBUG");
1682 AT_BISON_OPTION_POPDEFS
1684 AT_BISON_CHECK([[-o input.c input.y]])
1685 AT_COMPILE([[input]])
1686 AT_PARSER_CHECK([[./input]], [[0]],