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,
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])
83 AT_DATA_GRAMMAR([[input.y]],
84 [[%defines /* FIXME: Required by lalr1.cc in Bison 2.6. */
93 # include <stdlib.h> // getenv
98 exp: { ]AT_SKEL_CC_IF([[std::cerr << @$ << std::endl]],
99 [[YY_LOCATION_PRINT(stderr, @$); fputc ('\n', stderr)]])[; }
106 YYUSE(llocp);], [AT_SKEL_CC_IF([
116 p.set_debug_level (!!getenv("YYDEBUG"));
117 return p.parse ();]], [[
118 yydebug = !!getenv("YYDEBUG");
119 return !!yyparse (]AT_PARAM_IF([0])[);]])[
123 AT_FULL_COMPILE([input])
124 AT_PARSER_CHECK([./input], 1, [],
125 [m4_default([$4], [1.1])
126 m4_default([$4], [1.1])[: syntax error
128 AT_BISON_OPTION_POPDEFS
132 ## FIXME: test Java, and iterate over skeletons.
134 AT_TEST([yacc.c], [%define api.pure full])
135 AT_TEST([yacc.c], [%define api.pure %parse-param { int x }])
136 AT_TEST([yacc.c], [%define api.push-pull both])
137 AT_TEST([yacc.c], [%define api.push-pull both %define api.pure full])
139 AT_TEST([glr.c], [%define api.pure])
143 ## A very different test, based on PostgreSQL's implementation of the
145 ## http://lists.gnu.org/archive/html/bug-bison/2012-11/msg00023.html
147 ## Weirdly enough, to trigger the warning with GCC 4.7, we must not
148 ## use fprintf, so run the test twice: once to check the warning
149 ## (absence thereof), and another time to check the value.
150 AT_TEST([yacc.c], [%define api.pure full],
153 # define YY_LOCATION_PRINT(Stream, Loc) \
155 # define YYLLOC_DEFAULT(Current, Rhs, N) \
156 (Current) = ((Rhs)[N ? 1 : 0])
161 AT_TEST([yacc.c], [%define api.pure full],
164 # define YY_LOCATION_PRINT(Stream, Loc) \
165 fprintf ((Stream), "%d", (Loc))
166 # define YYLLOC_DEFAULT(Current, Rhs, N) \
167 (Current) = ((Rhs)[N ? 1 : 0])
177 ## ---------------- ##
178 ## Exotic Dollars. ##
179 ## ---------------- ##
181 AT_SETUP([Exotic Dollars])
183 AT_BISON_OPTION_PUSHDEFS
184 AT_DATA_GRAMMAR([[input.y]],
198 %type <val> a_1 a_2 a_5
199 sum_of_the_five_previous_values
202 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
203 sum_of_the_five_previous_values
205 USE (($1, $2, $<foo>3, $<foo>4, $5));
213 sum_of_the_five_previous_values:
215 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
229 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
231 AT_PARSER_CHECK([./input], 0,
235 # Make sure that fields after $n or $-n are parsed correctly. At one
236 # point while implementing dashes in symbol names, we were dropping
238 AT_DATA_GRAMMAR([[input.y]],
244 typedef struct { int val; } stype;
245 # define YYSTYPE stype
249 start: one two { $$.val = $1.val + $2.val; } sum ;
250 one: { $$.val = 1; } ;
251 two: { $$.val = 2; } ;
252 sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ;
264 AT_FULL_COMPILE([input])
265 AT_PARSER_CHECK([[./input]], [[0]],
269 AT_BISON_OPTION_POPDEFS
274 ## -------------------------- ##
275 ## Printers and Destructors. ##
276 ## -------------------------- ##
278 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4,
279 # BISON-DIRECTIVE, UNION-FLAG)
280 # -------------------------------------------------------------
281 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
282 [# Make sure complex $n work.
283 m4_if([$1$2$3$4], $[1]$[2]$[3]$[4], [],
284 [m4_fatal([$0: Invalid arguments: $@])])dnl
286 # Be sure to pass all the %directives to this macro to have correct
287 # helping macros. So don't put any directly in the Bison file.
288 AT_BISON_OPTION_PUSHDEFS([$5])
289 AT_DATA_GRAMMAR([[input.y]],
296 #define YYINITDEPTH 10
297 #define YYMAXDEPTH 10
298 #define RANGE(Location) ]AT_LALR1_CC_IF([(Location).begin.line, (Location).end.line],
299 [(Location).first_line, (Location).last_line])[
301 /* Display the symbol type Symbol. */
302 #define V(Symbol, Value, Location, Sep) \
303 fprintf (stderr, #Symbol " (%d@%d-%d)" Sep, Value, RANGE(Location))
307 ]m4_ifval([$6], [%union
311 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
312 m4_ifval([$6], [[%code provides {]], [[%code {]])
313 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])[
315 ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE])
318 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
320 /* FIXME: This %printer isn't actually tested. */
323 ]AT_LALR1_CC_IF([debug_stream () << $$;],
324 [fprintf (yyoutput, "%d", $$)])[;
326 input line thing 'x' 'y'
329 { fprintf (stderr, "Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
333 { fprintf (stderr, "Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
337 { fprintf (stderr, "Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
341 { fprintf (stderr, "Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
345 { fprintf (stderr, "Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
350 { fprintf (stderr, "Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
355 This grammar is made to exercise error recovery.
356 "Lines" starting with `(' support error recovery, with
357 ')' as synchronizing token. Lines starting with 'x' can never
358 be recovered from if in error.
365 V(input, $$, @$, ": /* Nothing */\n");
367 | line input /* Right recursive to load the stack so that popping at
368 END can be exercised. */
371 V(input, $$, @$, ": ");
372 V(line, $1, @1, " ");
373 V(input, $2, @2, "\n");
378 thing thing thing ';'
381 V(line, $$, @$, ": ");
382 V(thing, $1, @1, " ");
383 V(thing, $2, @2, " ");
384 V(thing, $3, @3, " ");
387 | '(' thing thing ')'
390 V(line, $$, @$, ": ");
392 V(thing, $2, @2, " ");
393 V(thing, $3, @3, " ");
394 V(')', $4, @4, "\n");
399 V(line, $$, @$, ": ");
401 V(thing, $2, @2, " ");
402 V(')', $3, @3, "\n");
407 V(line, $$, @$, ": ");
409 fprintf (stderr, "error (@%d-%d) ", RANGE (@2));
410 V(')', $3, @3, "\n");
418 V(thing, $$, @$, ": ");
419 V('x', $1, @1, "\n");
423 /* Alias to ARGV[1]. */
424 const char *source = YY_NULL;
431 static unsigned int counter = 0;
433 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
434 /* As in BASIC, line numbers go from 10 to 10. */
435 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_FIRST_COLUMN[ = 10 * c;
436 ]AT_LOC_LAST_LINE[ = ]AT_LOC_LAST_COLUMN[ = ]AT_LOC_FIRST_LINE[ + 9;
437 assert (0 <= c && c <= strlen (source));
439 fprintf (stderr, "sending: '%c'", source[c]);
441 fprintf (stderr, "sending: END");
442 fprintf (stderr, " (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
446 [static bool yydebug;
451 parser.set_debug_level (yydebug);
452 return parser.parse ();
457 main (int argc, const char *argv[])
460 yydebug = !!getenv ("YYDEBUG");
466 case 0: fprintf (stderr, "Successful parse.\n"); break;
467 case 1: fprintf (stderr, "Parsing FAILED.\n"); break;
468 default: fprintf (stderr, "Parsing FAILED (status %d).\n", status); break;
474 AT_FULL_COMPILE([input])
477 # Check the location of "empty"
478 # -----------------------------
479 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
480 # an empty "line" nterm.
481 # FIXME: This location is not satisfying. Depend on the lookahead?
482 AT_PARSER_CHECK([./input '(x)'], 0, [],
483 [[sending: '(' (0@0-9)
484 sending: 'x' (1@10-19)
485 thing (1@10-19): 'x' (1@10-19)
486 sending: ')' (2@20-29)
487 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
488 sending: END (3@30-39)
489 input (0@29-29): /* Nothing */
490 input (2@0-29): line (0@0-29) input (0@29-29)
491 Freeing token END (3@30-39)
492 Freeing nterm input (2@0-29)
497 # Check locations in error recovery
498 # ---------------------------------
499 # '(y)' is an error, but can be recovered from. But what's the location
500 # of the error itself ('y'), and of the resulting reduction ('(error)').
501 AT_PARSER_CHECK([./input '(y)'], 0, [],
502 [[sending: '(' (0@0-9)
503 sending: 'y' (1@10-19)
504 10.10-19.18: syntax error, unexpected 'y', expecting 'x'
505 Freeing token 'y' (1@10-19)
506 sending: ')' (2@20-29)
507 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
508 sending: END (3@30-39)
509 input (0@29-29): /* Nothing */
510 input (2@0-29): line (-1@0-29) input (0@29-29)
511 Freeing token END (3@30-39)
512 Freeing nterm input (2@0-29)
517 # Syntax errors caught by the parser
518 # ----------------------------------
519 # Exercise the discarding of stack top and input until `error'
522 # '(', 'x', 'x', 'x', 'x', 'x', ')',
524 # Load the stack and provoke an error that cannot be caught by the
525 # grammar, to check that the stack is cleared. And make sure the
526 # lookahead is freed.
531 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, [],
532 [[sending: '(' (0@0-9)
533 sending: 'x' (1@10-19)
534 thing (1@10-19): 'x' (1@10-19)
535 sending: 'x' (2@20-29)
536 thing (2@20-29): 'x' (2@20-29)
537 sending: 'x' (3@30-39)
538 30.30-39.38: syntax error, unexpected 'x', expecting ')'
539 Freeing nterm thing (2@20-29)
540 Freeing nterm thing (1@10-19)
541 Freeing token 'x' (3@30-39)
542 sending: 'x' (4@40-49)
543 Freeing token 'x' (4@40-49)
544 sending: 'x' (5@50-59)
545 Freeing token 'x' (5@50-59)
546 sending: ')' (6@60-69)
547 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
548 sending: '(' (7@70-79)
549 sending: 'x' (8@80-89)
550 thing (8@80-89): 'x' (8@80-89)
551 sending: ')' (9@90-99)
552 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
553 sending: '(' (10@100-109)
554 sending: 'x' (11@110-119)
555 thing (11@110-119): 'x' (11@110-119)
556 sending: ')' (12@120-129)
557 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
558 sending: 'y' (13@130-139)
559 input (0@129-129): /* Nothing */
560 input (2@100-129): line (10@100-129) input (0@129-129)
561 input (2@70-129): line (7@70-99) input (2@100-129)
562 input (2@0-129): line (-1@0-69) input (2@70-129)
563 130.130-139.138: syntax error, unexpected 'y', expecting END
564 Freeing nterm input (2@0-129)
565 Freeing token 'y' (13@130-139)
570 # Syntax error caught by the parser where lookahead = END
571 # --------------------------------------------------------
572 # Load the stack and provoke an error that cannot be caught by the
573 # grammar, to check that the stack is cleared. And make sure the
574 # lookahead is freed.
579 AT_PARSER_CHECK([./input '(x)(x)x'], 1, [],
580 [[sending: '(' (0@0-9)
581 sending: 'x' (1@10-19)
582 thing (1@10-19): 'x' (1@10-19)
583 sending: ')' (2@20-29)
584 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
585 sending: '(' (3@30-39)
586 sending: 'x' (4@40-49)
587 thing (4@40-49): 'x' (4@40-49)
588 sending: ')' (5@50-59)
589 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
590 sending: 'x' (6@60-69)
591 thing (6@60-69): 'x' (6@60-69)
592 sending: END (7@70-79)
593 70.70-79.78: syntax error, unexpected END, expecting 'x'
594 Freeing nterm thing (6@60-69)
595 Freeing nterm line (3@30-59)
596 Freeing nterm line (0@0-29)
597 Freeing token END (7@70-79)
602 # Check destruction upon stack overflow
603 # -------------------------------------
604 # Upon stack overflow, all symbols on the stack should be destroyed.
605 # Only check for yacc.c.
607 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, [],
608 [[sending: '(' (0@0-9)
609 sending: 'x' (1@10-19)
610 thing (1@10-19): 'x' (1@10-19)
611 sending: ')' (2@20-29)
612 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
613 sending: '(' (3@30-39)
614 sending: 'x' (4@40-49)
615 thing (4@40-49): 'x' (4@40-49)
616 sending: ')' (5@50-59)
617 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
618 sending: '(' (6@60-69)
619 sending: 'x' (7@70-79)
620 thing (7@70-79): 'x' (7@70-79)
621 sending: ')' (8@80-89)
622 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
623 sending: '(' (9@90-99)
624 sending: 'x' (10@100-109)
625 thing (10@100-109): 'x' (10@100-109)
626 sending: ')' (11@110-119)
627 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
628 sending: '(' (12@120-129)
629 sending: 'x' (13@130-139)
630 thing (13@130-139): 'x' (13@130-139)
631 sending: ')' (14@140-149)
632 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
633 sending: '(' (15@150-159)
634 sending: 'x' (16@160-169)
635 thing (16@160-169): 'x' (16@160-169)
636 sending: ')' (17@170-179)
637 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
638 sending: '(' (18@180-189)
639 sending: 'x' (19@190-199)
640 thing (19@190-199): 'x' (19@190-199)
641 sending: ')' (20@200-209)
642 200.200-209.208: memory exhausted
643 Freeing nterm thing (19@190-199)
644 Freeing nterm line (15@150-179)
645 Freeing nterm line (12@120-149)
646 Freeing nterm line (9@90-119)
647 Freeing nterm line (6@60-89)
648 Freeing nterm line (3@30-59)
649 Freeing nterm line (0@0-29)
650 Parsing FAILED (status 2).
654 AT_BISON_OPTION_POPDEFS
655 ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR
658 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
659 # ---------------------------------------------------------------------------
660 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
661 [AT_SETUP([Printers and Destructors$2]m4_ifval([$1], [[: $1]]))
664 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
675 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
676 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [ with union])
678 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
679 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [ with union])
681 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
682 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [ with union])
686 ## ----------------------------------------- ##
687 ## Default tagless %printer and %destructor. ##
688 ## ----------------------------------------- ##
690 # Check that the right %printer and %destructor are called, that they're not
691 # called for $end, and that $$ and @$ work correctly.
693 AT_SETUP([Default tagless %printer and %destructor])
694 AT_BISON_OPTION_PUSHDEFS([%locations])
695 AT_DATA_GRAMMAR([[input.y]],
709 fprintf (yyoutput, "<*> printer should not be called.\n");
713 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
716 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
720 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
723 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
727 fprintf (yyoutput, "<*> destructor should not be called.\n");
732 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
736 ]AT_YYLEX_DEFINE(["abcd"], [[yylval = res]])[
746 AT_BISON_CHECK([-o input.c input.y])
748 AT_PARSER_CHECK([./input], 1,
749 [[<> destructor for 'd' @ 4.
750 'b'/'c' destructor for 'c' @ 3.
751 'b'/'c' destructor for 'b' @ 2.
752 <> destructor for 'a' @ 1.
756 Reading a token: Next token is token 'a' (1.1: <> printer for 'a' @ 1)
757 Shifting token 'a' (1.1: <> printer for 'a' @ 1)
759 Reading a token: Next token is token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
760 Shifting token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
762 Reading a token: Next token is token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
763 Shifting token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
765 Reading a token: Next token is token 'd' (1.4: <> printer for 'd' @ 4)
766 Shifting token 'd' (1.4: <> printer for 'd' @ 4)
768 Reading a token: Now at end of input.
769 1.5: syntax error, unexpected $end, expecting 'e'
770 Error: popping token 'd' (1.4: <> printer for 'd' @ 4)
772 Error: popping token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
774 Error: popping token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
776 Error: popping token 'a' (1.1: <> printer for 'a' @ 1)
778 Cleanup: discarding lookahead token $end (1.5: )
782 AT_BISON_OPTION_POPDEFS
787 ## ------------------------------------------------------ ##
788 ## Default tagged and per-type %printer and %destructor. ##
789 ## ------------------------------------------------------ ##
791 AT_SETUP([Default tagged and per-type %printer and %destructor])
792 AT_BISON_OPTION_PUSHDEFS
793 AT_DATA_GRAMMAR([[input.y]],
806 fprintf (yyoutput, "<> printer should not be called.\n");
809 %union { int field0; int field1; int field2; }
810 %type <field0> start 'a' 'g'
814 fprintf (yyoutput, "<*>/<field2>/e printer");
817 fprintf (stdout, "<*>/<field2>/e destructor.\n");
821 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
822 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
825 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
826 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
829 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
830 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
833 fprintf (yyoutput, "<> destructor should not be called.\n");
839 'a' 'b' 'c' 'd' 'e' 'f' 'g'
841 USE(($1, $2, $3, $4, $5, $6, $7));
848 ]AT_YYLEX_DEFINE(["abcdef"])[
858 AT_BISON_CHECK([-o input.c input.y])
860 AT_PARSER_CHECK([./input], 1,
861 [[<*>/<field2>/e destructor.
862 <*>/<field2>/e destructor.
866 <*>/<field2>/e destructor.
870 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
871 Shifting token 'a' (<*>/<field2>/e printer)
873 Reading a token: Next token is token 'b' (<field1> printer)
874 Shifting token 'b' (<field1> printer)
876 Reading a token: Next token is token 'c' ('c' printer)
877 Shifting token 'c' ('c' printer)
879 Reading a token: Next token is token 'd' ('d' printer)
880 Shifting token 'd' ('d' printer)
882 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
883 Shifting token 'e' (<*>/<field2>/e printer)
885 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
886 Shifting token 'f' (<*>/<field2>/e printer)
888 Reading a token: Now at end of input.
889 syntax error, unexpected $end, expecting 'g'
890 Error: popping token 'f' (<*>/<field2>/e printer)
891 Stack now 0 1 3 5 6 7
892 Error: popping token 'e' (<*>/<field2>/e printer)
894 Error: popping token 'd' ('d' printer)
896 Error: popping token 'c' ('c' printer)
898 Error: popping token 'b' (<field1> printer)
900 Error: popping token 'a' (<*>/<field2>/e printer)
902 Cleanup: discarding lookahead token $end ()
906 AT_BISON_OPTION_POPDEFS
911 ## ------------------------------------------------------------- ##
912 ## Default %printer and %destructor for user-defined end token. ##
913 ## ------------------------------------------------------------- ##
915 AT_SETUP([Default %printer and %destructor for user-defined end token])
917 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
918 # -------------------------------------------------------------
919 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
921 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
922 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
924 AT_BISON_OPTION_PUSHDEFS([%locations])
925 AT_DATA_GRAMMAR([[input]]$1[[.y]],
939 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
944 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
947 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
951 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
956 [[[%union { char tag; }
957 %type <tag> start END]]])[[
961 start: { $$ = 'S'; } ;
971 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
972 yylloc.first_line = yylloc.last_line = 1;
973 yylloc.first_column = yylloc.last_column = 1;
985 AT_BISON_OPTION_POPDEFS
987 AT_BISON_CHECK([-o input$1.c input$1.y])
988 AT_COMPILE([input$1])
989 AT_PARSER_CHECK([./input$1], 0,
990 [[<]]kind[[> for 'E' @ 1.
991 <]]kind[[> for 'S' @ 1.
995 Reducing stack by rule 1 (line 42):
996 -> $$ = nterm start (1.1: <]]kind[[> for 'S' @ 1)
999 Reading a token: Now at end of input.
1000 Shifting token END (1.1: <]]kind[[> for 'E' @ 1)
1003 Cleanup: popping token END (1.1: <]]kind[[> for 'E' @ 1)
1004 Cleanup: popping nterm start (1.1: <]]kind[[> for 'S' @ 1)
1008 m4_popdef([not_kind])
1011 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
1012 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
1018 ## ------------------------------------------------------------------ ##
1019 ## Default %printer and %destructor are not for error or $undefined. ##
1020 ## ------------------------------------------------------------------ ##
1022 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
1024 # If Bison were to apply the default %printer and %destructor to the error
1025 # token or to $undefined:
1026 # - For the error token:
1027 # - It would generate warnings for unused $n.
1028 # - It would invoke the %printer and %destructor on the error token's
1029 # semantic value, which would be initialized from the lookahead, which
1030 # would be destroyed separately.
1031 # - For $undefined, who knows what the semantic value would be.
1032 AT_BISON_OPTION_PUSHDEFS
1033 AT_DATA_GRAMMAR([[input.y]],
1038 # include <stdlib.h>
1039 ]AT_YYERROR_DECLARE[
1045 fprintf (yyoutput, "'%c'", $$);
1048 fprintf (stderr, "DESTROY '%c'\n", $$);
1055 /* In order to reveal the problems that this bug caused during parsing, add
1057 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1062 ]AT_YYLEX_DEFINE(["abd"], [yylval = res])[
1070 AT_BISON_OPTION_POPDEFS
1072 AT_BISON_CHECK([-o input.c input.y])
1074 AT_PARSER_CHECK([./input], [1], [],
1077 Reading a token: Next token is token 'a' ('a')
1078 Shifting token 'a' ('a')
1080 Reading a token: Next token is token 'b' ('b')
1082 Shifting token error ()
1084 Next token is token 'b' ('b')
1085 Shifting token 'b' ('b')
1087 Reading a token: Next token is token $undefined ()
1088 Error: popping token 'b' ('b')
1091 Error: popping token error ()
1093 Shifting token error ()
1095 Next token is token $undefined ()
1096 Error: discarding token $undefined ()
1097 Error: popping token error ()
1099 Shifting token error ()
1101 Reading a token: Now at end of input.
1102 Cleanup: discarding lookahead token $end ()
1104 Cleanup: popping token error ()
1105 Cleanup: popping token 'a' ('a')
1113 ## ------------------------------------------------------ ##
1114 ## Default %printer and %destructor are not for $accept. ##
1115 ## ------------------------------------------------------ ##
1117 AT_SETUP([Default %printer and %destructor are not for $accept])
1119 # If YYSTYPE is a union and Bison were to apply the default %printer and
1120 # %destructor to $accept:
1121 # - The %printer and %destructor code generated for $accept would always be
1122 # dead code because $accept is currently never shifted onto the stack.
1123 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1124 # to declare `%type <field> $accept'. (Also true for $undefined.)
1125 # - Thus, the compiler might complain that the user code assumes the wrong
1126 # type for $$ since the code might assume the type associated with a
1127 # specific union field, which is especially reasonable in C++ since that
1128 # type may be a base type. This test case checks for this problem. (Also
1129 # true for $undefined and the error token, so there are three warnings for
1130 # %printer and three for %destructor.)
1132 AT_BISON_OPTION_PUSHDEFS
1133 AT_DATA_GRAMMAR([[input.y]],
1134 [[%debug /* So that %printer is actually compiled. */
1138 # include <stdlib.h>
1139 ]AT_YYERROR_DECLARE[
1146 fprintf (yyoutput, "'%c'", chr);
1150 fprintf (stderr, "DESTROY '%c'\n", chr);
1153 %union { char chr; }
1158 start: { USE($$); } ;
1169 AT_BISON_OPTION_POPDEFS
1171 AT_BISON_CHECK([-o input.c input.y])
1178 ## ------------------------------------------------------ ##
1179 ## Default %printer and %destructor for mid-rule values. ##
1180 ## ------------------------------------------------------ ##
1182 AT_SETUP([Default %printer and %destructor for mid-rule values])
1184 AT_BISON_OPTION_PUSHDEFS
1185 AT_DATA_GRAMMAR([[input.y]],
1186 [[%debug /* So that %printer is actually compiled. */
1190 # include <stdlib.h>
1191 ]AT_YYERROR_DECLARE[
1194 # define YYLTYPE int
1195 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1196 # define YY_LOCATION_PRINT(File, Loc)
1199 %printer { fprintf (yyoutput, "%d", @$); } <>
1200 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1201 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1202 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1207 { @$ = 1; } // Not set or used.
1208 { USE ($$); @$ = 2; } // Both set and used.
1209 { USE ($$); @$ = 3; } // Only set.
1210 { @$ = 4; } // Only used.
1212 { USE (($$, $2, $4, $5)); @$ = 0; }
1225 AT_BISON_OPTION_POPDEFS
1227 AT_BISON_CHECK([-o input.c input.y], 0,,
1228 [[input.y:33.3-23: warning: unset value: $$
1229 input.y:30.3-35.37: warning: unused value: $3
1233 AT_PARSER_CHECK([./input], 1,,
1236 Reducing stack by rule 1 (line 30):
1237 -> $$ = nterm $@1 (: )
1240 Reducing stack by rule 2 (line 31):
1241 -> $$ = nterm @2 (: 2)
1244 Reducing stack by rule 3 (line 32):
1245 -> $$ = nterm @3 (: 3)
1248 Reducing stack by rule 4 (line 33):
1249 -> $$ = nterm @4 (: 4)
1252 Reading a token: Now at end of input.
1254 Error: popping nterm @4 (: 4)
1257 Error: popping nterm @3 (: 3)
1260 Error: popping nterm @2 (: 2)
1263 Error: popping nterm $@1 (: )
1265 Cleanup: discarding lookahead token $end (: )
1272 ## ----------------------- ##
1273 ## @$ implies %locations. ##
1274 ## ----------------------- ##
1276 # Bison once forgot to check for @$ in actions other than semantic actions.
1278 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1279 # -------------------------------------------
1280 m4_define([AT_CHECK_ACTION_LOCATIONS],
1281 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1282 AT_BISON_OPTION_PUSHDEFS
1283 AT_DATA_GRAMMAR([[input.y]],
1286 ]AT_YYERROR_DECLARE[
1293 fprintf (stderr, "%d\n", @$.first_line);
1294 } ]m4_if($1, [%initial-action], [], [[start]])[
1316 AT_BISON_CHECK([[-o input.c input.y]])
1317 AT_COMPILE([[input]])
1318 AT_BISON_OPTION_POPDEFS
1321 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1322 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1323 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1326 ## ------------------------- ##
1327 ## Qualified $$ in actions. ##
1328 ## ------------------------- ##
1330 # Check that we can used qualified $$ (v.g., $<type>$) not only in
1331 # rule actions, but also where $$ is valid: %printer and %destructor.
1333 # FIXME: Not actually checking %destructor, but it's the same code as
1336 # To do that, use a semantic value that has two fields (sem_type),
1337 # declare symbols to have only one of these types (INT, float), and
1338 # use $<type>$ to get the other one. Including for symbols that are
1339 # not typed (UNTYPED).
1341 m4_pushdef([AT_TEST],
1342 [AT_SETUP([[Qualified $$ in actions: $1]])
1344 AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "$1"])
1346 AT_DATA_GRAMMAR([[input.y]],
1348 %defines // FIXME: Mandated by lalr1.cc in Bison 2.6.
1349 %locations // FIXME: Mandated by lalr1.cc in Bison 2.6.
1353 typedef struct sem_type
1359 # define YYSTYPE sem_type
1362 # include <iostream>
1364 report (std::ostream& yyo, int ival, float fval)
1366 yyo << "ival: " << ival << ", fval: " << fval;
1371 report (FILE* yyo, int ival, float fval)
1373 fprintf (yyo, "ival: %d, fval: %1.1f", ival, fval);
1380 ]AT_YYERROR_DECLARE[
1387 %printer { report (yyo, $$, $<fval>$); } <ival>;
1388 %printer { report (yyo, $<ival>$, $$ ); } <fval>;
1389 %printer { report (yyo, $<ival>$, $<fval>$); } <>;
1400 $$ = $<fval>1 + $<fval>2;
1401 $<ival>$ = $<ival>1 + $][2;
1405 ]AT_YYLEX_DEFINE(AT_SKEL_CC_IF([[{yy::parser::token::UNTYPED,
1406 yy::parser::token::INT,
1408 [[{UNTYPED, INT, EOF}]]),
1409 [AT_VAL.ival = toknum * 10; AT_VAL.fval = toknum / 10.0;])[
1414 p.set_debug_level(1);
1415 return p.parse ();]], [[
1417 return yyparse ();]])[
1421 AT_FULL_COMPILE([[input]])
1422 AT_PARSER_CHECK([./input], 0, [], [stderr])
1423 # Don't be too picky on the traces, GLR is not exactly the same. Keep
1424 # only the lines from the printer.
1426 # Don't care about locations. FIXME: remove their removal when Bison
1427 # supports C++ without locations.
1428 AT_CHECK([[sed -ne 's/([-0-9.]*: /(/;/ival:/p' stderr]], 0,
1429 [[Reading a token: Next token is token UNTYPED (ival: 10, fval: 0.1)
1430 Shifting token UNTYPED (ival: 10, fval: 0.1)
1431 Reading a token: Next token is token INT (ival: 20, fval: 0.2)
1432 Shifting token INT (ival: 20, fval: 0.2)
1433 $][1 = token UNTYPED (ival: 10, fval: 0.1)
1434 $][2 = token INT (ival: 20, fval: 0.2)
1435 -> $$ = nterm float (ival: 30, fval: 0.3)
1436 Cleanup: popping nterm float (ival: 30, fval: 0.3)
1439 AT_BISON_OPTION_POPDEFS
1449 m4_popdef([AT_TEST])
1451 ## ----------------------------------------------- ##
1452 ## Fix user actions without a trailing semicolon. ##
1453 ## ----------------------------------------------- ##
1455 AT_SETUP([[Fix user actions without a trailing semicolon]])
1457 # This feature is undocumented, but we accidentally broke it in 2.3a,
1458 # and there was a complaint at:
1459 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1460 AT_BISON_OPTION_PUSHDEFS
1463 start: test2 test1 test0 testc;
1466 : 'a' { semi; /* TEST:N:2 */ }
1467 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1468 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1469 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1470 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1471 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1472 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1473 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1474 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1477 : 'a' { semi; // TEST:N:1 ;
1478 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1479 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1480 } | 'd' { semi; no_semi // TEST:Y:1 ;
1481 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1482 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1483 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1484 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1485 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1488 : 'a' { semi; // TEST:N:1 {}
1489 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1490 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1491 } | 'd' { semi; no_semi // TEST:Y:1 {}
1492 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1493 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1494 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1495 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1496 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1501 #define TEST_MACRO_N \
1502 []"broken\" $ @ $$ @$ [];\
1506 #define TEST_MACRO_N \
1507 []"broken\" $ @ $$ @$ [];\
1510 AT_BISON_OPTION_POPDEFS
1512 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1513 [[input.y:8.48: warning: a ';' might be needed at the end of action code
1514 input.y:8.48: warning: future versions of Bison will not add the ';'
1515 input.y:9.48: warning: a ';' might be needed at the end of action code
1516 input.y:9.48: warning: future versions of Bison will not add the ';'
1517 input.y:10.48: warning: a ';' might be needed at the end of action code
1518 input.y:10.48: warning: future versions of Bison will not add the ';'
1519 input.y:11.48: warning: a ';' might be needed at the end of action code
1520 input.y:11.48: warning: future versions of Bison will not add the ';'
1521 input.y:12.48: warning: a ';' might be needed at the end of action code
1522 input.y:12.48: warning: future versions of Bison will not add the ';'
1523 input.y:13.48: warning: a ';' might be needed at the end of action code
1524 input.y:13.48: warning: future versions of Bison will not add the ';'
1525 input.y:20.1: warning: a ';' might be needed at the end of action code
1526 input.y:20.1: warning: future versions of Bison will not add the ';'
1527 input.y:21.1: warning: a ';' might be needed at the end of action code
1528 input.y:21.1: warning: future versions of Bison will not add the ';'
1529 input.y:22.1: warning: a ';' might be needed at the end of action code
1530 input.y:22.1: warning: future versions of Bison will not add the ';'
1531 input.y:23.1: warning: a ';' might be needed at the end of action code
1532 input.y:23.1: warning: future versions of Bison will not add the ';'
1533 input.y:24.1: warning: a ';' might be needed at the end of action code
1534 input.y:24.1: warning: future versions of Bison will not add the ';'
1535 input.y:25.1: warning: a ';' might be needed at the end of action code
1536 input.y:25.1: warning: future versions of Bison will not add the ';'
1537 input.y:31.1: warning: a ';' might be needed at the end of action code
1538 input.y:31.1: warning: future versions of Bison will not add the ';'
1539 input.y:32.1: warning: a ';' might be needed at the end of action code
1540 input.y:32.1: warning: future versions of Bison will not add the ';'
1541 input.y:33.1: warning: a ';' might be needed at the end of action code
1542 input.y:33.1: warning: future versions of Bison will not add the ';'
1543 input.y:34.1: warning: a ';' might be needed at the end of action code
1544 input.y:34.1: warning: future versions of Bison will not add the ';'
1545 input.y:35.1: warning: a ';' might be needed at the end of action code
1546 input.y:35.1: warning: future versions of Bison will not add the ';'
1547 input.y:36.1: warning: a ';' might be needed at the end of action code
1548 input.y:36.1: warning: future versions of Bison will not add the ';'
1551 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1552 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1553 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1554 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1555 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1560 ## -------------------------------------------------- ##
1561 ## Destroying lookahead assigned by semantic action. ##
1562 ## -------------------------------------------------- ##
1564 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1566 AT_BISON_OPTION_PUSHDEFS
1567 AT_DATA_GRAMMAR([input.y],
1572 ]AT_YYERROR_DECLARE[
1577 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1578 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1582 // In a previous version of Bison, yychar assigned by the semantic
1583 // action below was not translated into yytoken before the lookahead was
1584 // discarded and thus before its destructor (selected according to
1585 // yytoken) was called in order to return from yyparse. This would
1586 // happen even if YYACCEPT was performed in a later semantic action as
1587 // long as only consistent states with default reductions were visited
1588 // in between. However, we leave YYACCEPT in the same semantic action
1589 // for this test in order to show that skeletons cannot simply translate
1590 // immediately after every semantic action because a semantic action
1591 // that has set yychar might not always return normally. Instead,
1592 // skeletons must translate before every use of yytoken.
1593 start: 'a' accept { USE($1); } ;
1595 assert (yychar == YYEMPTY);
1602 ]AT_YYLEX_DEFINE(["a"])[
1609 AT_BISON_OPTION_POPDEFS
1610 AT_BISON_CHECK([[-o input.c input.y]])
1611 AT_COMPILE([[input]])
1612 AT_PARSER_CHECK([[./input]], [[0]], [],
1623 AT_SETUP([[YYBACKUP]])
1625 AT_BISON_OPTION_PUSHDEFS([%pure-parser])
1627 AT_DATA_GRAMMAR([input.y],
1634 # include <stdlib.h>
1635 # include <assert.h>
1637 ]AT_YYERROR_DECLARE[
1646 'a' { printf ("a: %d\n", $1); }
1647 | 'b' { YYBACKUP('a', 123); }
1648 | 'c' 'd' { YYBACKUP('a', 456); }
1653 ]AT_YYLEX_DEFINE(["bcd"], [*lvalp = (toknum + 1) * 10])[
1658 yydebug = !!getenv("YYDEBUG");
1662 AT_BISON_OPTION_POPDEFS
1664 AT_BISON_CHECK([[-o input.c input.y]])
1665 AT_COMPILE([[input]])
1666 AT_PARSER_CHECK([[./input]], [[0]],