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)
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. */
89 %parse-param { int x } // Useless, but used to force yyerror purity.
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 (0);]])[
123 AT_FULL_COMPILE([input])
124 AT_PARSER_CHECK([./input], 1, [],
128 AT_BISON_OPTION_POPDEFS
132 ## FIXME: test Java, and iterate over skeletons.
134 AT_TEST([yacc.c], [%define api.pure])
135 AT_TEST([yacc.c], [%define api.push-pull both])
136 AT_TEST([yacc.c], [%define api.push-pull both %define api.pure])
145 ## ---------------- ##
146 ## Exotic Dollars. ##
147 ## ---------------- ##
149 AT_SETUP([Exotic Dollars])
151 AT_BISON_OPTION_PUSHDEFS
152 AT_DATA_GRAMMAR([[input.y]],
153 [[%define parse.error verbose
166 %type <val> a_1 a_2 a_5
167 sum_of_the_five_previous_values
170 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
171 sum_of_the_five_previous_values
173 USE (($1, $2, $<foo>3, $<foo>4, $5));
181 sum_of_the_five_previous_values:
183 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
197 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
199 AT_PARSER_CHECK([./input], 0,
203 # Make sure that fields after $n or $-n are parsed correctly. At one
204 # point while implementing dashes in symbol names, we were dropping
206 AT_DATA_GRAMMAR([[input.y]],
212 typedef struct { int val; } stype;
213 # define YYSTYPE stype
217 start: one two { $$.val = $1.val + $2.val; } sum ;
218 one: { $$.val = 1; } ;
219 two: { $$.val = 2; } ;
220 sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ;
232 AT_FULL_COMPILE([input])
233 AT_PARSER_CHECK([[./input]], [[0]],
237 AT_BISON_OPTION_POPDEFS
242 ## -------------------------- ##
243 ## Printers and Destructors. ##
244 ## -------------------------- ##
246 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4,
247 # BISON-DIRECTIVE, UNION-FLAG)
248 # -------------------------------------------------------------
249 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
250 [# Make sure complex $n work.
251 m4_if([$1$2$3$4], $[1]$[2]$[3]$[4], [],
252 [m4_fatal([$0: Invalid arguments: $@])])dnl
254 # Be sure to pass all the %directives to this macro to have correct
255 # helping macros. So don't put any directly in the Bison file.
256 AT_BISON_OPTION_PUSHDEFS([$5])
257 AT_DATA_GRAMMAR([[input.y]],
264 #define YYINITDEPTH 10
265 #define YYMAXDEPTH 10
266 #define RANGE(Location) ]AT_LALR1_CC_IF([(Location).begin.line, (Location).end.line],
267 [(Location).first_line, (Location).last_line])[
269 /* Display the symbol type Symbol. */
270 #define V(Symbol, Value, Location, Sep) \
271 fprintf (stderr, #Symbol " (%d@%d-%d)" Sep, Value, RANGE(Location))
275 ]m4_ifval([$6], [%union
279 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
280 m4_ifval([$6], [[%code provides {]], [[%code {]])
281 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])[
283 ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE])
286 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
288 /* FIXME: This %printer isn't actually tested. */
291 ]AT_LALR1_CC_IF([debug_stream () << $$;],
292 [fprintf (yyoutput, "%d", $$)])[;
294 input line thing 'x' 'y'
297 { fprintf (stderr, "Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
301 { fprintf (stderr, "Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
305 { fprintf (stderr, "Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
309 { fprintf (stderr, "Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
313 { fprintf (stderr, "Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
318 { fprintf (stderr, "Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
323 This grammar is made to exercise error recovery.
324 "Lines" starting with `(' support error recovery, with
325 ')' as synchronizing token. Lines starting with 'x' can never
326 be recovered from if in error.
333 V(input, $$, @$, ": /* Nothing */\n");
335 | line input /* Right recursive to load the stack so that popping at
336 END can be exercised. */
339 V(input, $$, @$, ": ");
340 V(line, $1, @1, " ");
341 V(input, $2, @2, "\n");
346 thing thing thing ';'
349 V(line, $$, @$, ": ");
350 V(thing, $1, @1, " ");
351 V(thing, $2, @2, " ");
352 V(thing, $3, @3, " ");
355 | '(' thing thing ')'
358 V(line, $$, @$, ": ");
360 V(thing, $2, @2, " ");
361 V(thing, $3, @3, " ");
362 V(')', $4, @4, "\n");
367 V(line, $$, @$, ": ");
369 V(thing, $2, @2, " ");
370 V(')', $3, @3, "\n");
375 V(line, $$, @$, ": ");
377 fprintf (stderr, "error (@%d-%d) ", RANGE(@2));
378 V(')', $3, @3, "\n");
386 V(thing, $$, @$, ": ");
387 V('x', $1, @1, "\n");
391 /* Alias to ARGV[1]. */
392 const char *source = YY_NULL;
399 static unsigned int counter = 0;
401 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
402 /* As in BASIC, line numbers go from 10 to 10. */
403 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_FIRST_COLUMN[ = 10 * c;
404 ]AT_LOC_LAST_LINE[ = ]AT_LOC_LAST_COLUMN[ = ]AT_LOC_FIRST_LINE[ + 9;
405 assert (0 <= c && c <= strlen (source));
407 fprintf (stderr, "sending: '%c'", source[c]);
409 fprintf (stderr, "sending: END");
410 fprintf (stderr, " (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
414 [static bool yydebug;
419 parser.set_debug_level (yydebug);
420 return parser.parse ();
425 main (int argc, const char *argv[])
428 yydebug = !!getenv ("YYDEBUG");
434 case 0: fprintf (stderr, "Successful parse.\n"); break;
435 case 1: fprintf (stderr, "Parsing FAILED.\n"); break;
436 default: fprintf (stderr, "Parsing FAILED (status %d).\n", status); break;
442 AT_FULL_COMPILE([input])
445 # Check the location of "empty"
446 # -----------------------------
447 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
448 # an empty "line" nterm.
449 # FIXME: This location is not satisfying. Depend on the lookahead?
450 AT_PARSER_CHECK([./input '(x)'], 0, [],
451 [[sending: '(' (0@0-9)
452 sending: 'x' (1@10-19)
453 thing (1@10-19): 'x' (1@10-19)
454 sending: ')' (2@20-29)
455 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
456 sending: END (3@30-39)
457 input (0@29-29): /* Nothing */
458 input (2@0-29): line (0@0-29) input (0@29-29)
459 Freeing token END (3@30-39)
460 Freeing nterm input (2@0-29)
465 # Check locations in error recovery
466 # ---------------------------------
467 # '(y)' is an error, but can be recovered from. But what's the location
468 # of the error itself ('y'), and of the resulting reduction ('(error)').
469 AT_PARSER_CHECK([./input '(y)'], 0, [],
470 [[sending: '(' (0@0-9)
471 sending: 'y' (1@10-19)
472 10.10-19.18: syntax error, unexpected 'y', expecting 'x'
473 Freeing token 'y' (1@10-19)
474 sending: ')' (2@20-29)
475 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
476 sending: END (3@30-39)
477 input (0@29-29): /* Nothing */
478 input (2@0-29): line (-1@0-29) input (0@29-29)
479 Freeing token END (3@30-39)
480 Freeing nterm input (2@0-29)
485 # Syntax errors caught by the parser
486 # ----------------------------------
487 # Exercise the discarding of stack top and input until `error'
490 # '(', 'x', 'x', 'x', 'x', 'x', ')',
492 # Load the stack and provoke an error that cannot be caught by the
493 # grammar, to check that the stack is cleared. And make sure the
494 # lookahead is freed.
499 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, [],
500 [[sending: '(' (0@0-9)
501 sending: 'x' (1@10-19)
502 thing (1@10-19): 'x' (1@10-19)
503 sending: 'x' (2@20-29)
504 thing (2@20-29): 'x' (2@20-29)
505 sending: 'x' (3@30-39)
506 30.30-39.38: syntax error, unexpected 'x', expecting ')'
507 Freeing nterm thing (2@20-29)
508 Freeing nterm thing (1@10-19)
509 Freeing token 'x' (3@30-39)
510 sending: 'x' (4@40-49)
511 Freeing token 'x' (4@40-49)
512 sending: 'x' (5@50-59)
513 Freeing token 'x' (5@50-59)
514 sending: ')' (6@60-69)
515 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
516 sending: '(' (7@70-79)
517 sending: 'x' (8@80-89)
518 thing (8@80-89): 'x' (8@80-89)
519 sending: ')' (9@90-99)
520 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
521 sending: '(' (10@100-109)
522 sending: 'x' (11@110-119)
523 thing (11@110-119): 'x' (11@110-119)
524 sending: ')' (12@120-129)
525 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
526 sending: 'y' (13@130-139)
527 input (0@129-129): /* Nothing */
528 input (2@100-129): line (10@100-129) input (0@129-129)
529 input (2@70-129): line (7@70-99) input (2@100-129)
530 input (2@0-129): line (-1@0-69) input (2@70-129)
531 130.130-139.138: syntax error, unexpected 'y', expecting END
532 Freeing nterm input (2@0-129)
533 Freeing token 'y' (13@130-139)
538 # Syntax error caught by the parser where lookahead = END
539 # --------------------------------------------------------
540 # Load the stack and provoke an error that cannot be caught by the
541 # grammar, to check that the stack is cleared. And make sure the
542 # lookahead is freed.
547 AT_PARSER_CHECK([./input '(x)(x)x'], 1, [],
548 [[sending: '(' (0@0-9)
549 sending: 'x' (1@10-19)
550 thing (1@10-19): 'x' (1@10-19)
551 sending: ')' (2@20-29)
552 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
553 sending: '(' (3@30-39)
554 sending: 'x' (4@40-49)
555 thing (4@40-49): 'x' (4@40-49)
556 sending: ')' (5@50-59)
557 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
558 sending: 'x' (6@60-69)
559 thing (6@60-69): 'x' (6@60-69)
560 sending: END (7@70-79)
561 70.70-79.78: syntax error, unexpected END, expecting 'x'
562 Freeing nterm thing (6@60-69)
563 Freeing nterm line (3@30-59)
564 Freeing nterm line (0@0-29)
565 Freeing token END (7@70-79)
570 # Check destruction upon stack overflow
571 # -------------------------------------
572 # Upon stack overflow, all symbols on the stack should be destroyed.
573 # Only check for yacc.c.
575 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, [],
576 [[sending: '(' (0@0-9)
577 sending: 'x' (1@10-19)
578 thing (1@10-19): 'x' (1@10-19)
579 sending: ')' (2@20-29)
580 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
581 sending: '(' (3@30-39)
582 sending: 'x' (4@40-49)
583 thing (4@40-49): 'x' (4@40-49)
584 sending: ')' (5@50-59)
585 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
586 sending: '(' (6@60-69)
587 sending: 'x' (7@70-79)
588 thing (7@70-79): 'x' (7@70-79)
589 sending: ')' (8@80-89)
590 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
591 sending: '(' (9@90-99)
592 sending: 'x' (10@100-109)
593 thing (10@100-109): 'x' (10@100-109)
594 sending: ')' (11@110-119)
595 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
596 sending: '(' (12@120-129)
597 sending: 'x' (13@130-139)
598 thing (13@130-139): 'x' (13@130-139)
599 sending: ')' (14@140-149)
600 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
601 sending: '(' (15@150-159)
602 sending: 'x' (16@160-169)
603 thing (16@160-169): 'x' (16@160-169)
604 sending: ')' (17@170-179)
605 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
606 sending: '(' (18@180-189)
607 sending: 'x' (19@190-199)
608 thing (19@190-199): 'x' (19@190-199)
609 sending: ')' (20@200-209)
610 200.200-209.208: memory exhausted
611 Freeing nterm thing (19@190-199)
612 Freeing nterm line (15@150-179)
613 Freeing nterm line (12@120-149)
614 Freeing nterm line (9@90-119)
615 Freeing nterm line (6@60-89)
616 Freeing nterm line (3@30-59)
617 Freeing nterm line (0@0-29)
618 Parsing FAILED (status 2).
622 AT_BISON_OPTION_POPDEFS
623 ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR
626 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
627 # ---------------------------------------------------------------------------
628 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
629 [AT_SETUP([Printers and Destructors$2]m4_ifval([$1], [[: $1]]))
632 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
633 [%define parse.error verbose
643 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
644 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
646 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
647 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
649 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
650 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
654 ## ----------------------------------------- ##
655 ## Default tagless %printer and %destructor. ##
656 ## ----------------------------------------- ##
658 # Check that the right %printer and %destructor are called, that they're not
659 # called for $end, and that $$ and @$ work correctly.
661 AT_SETUP([Default tagless %printer and %destructor])
662 AT_BISON_OPTION_PUSHDEFS([%locations])
663 AT_DATA_GRAMMAR([[input.y]],
664 [[%define parse.error verbose
677 fprintf (yyoutput, "<*> printer should not be called.\n");
681 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
684 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
688 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
691 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
695 fprintf (yyoutput, "<*> destructor should not be called.\n");
700 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
704 ]AT_YYLEX_DEFINE(["abcd"], [[yylval = res]])[
714 AT_BISON_CHECK([-o input.c input.y], [], [],
715 [[input.y:23.3-5: warning: useless %destructor for type <*> [-Wother]
716 input.y:23.3-5: warning: useless %printer for type <*> [-Wother]
719 AT_PARSER_CHECK([./input], 1,
720 [[<> destructor for 'd' @ 4.
721 'b'/'c' destructor for 'c' @ 3.
722 'b'/'c' destructor for 'b' @ 2.
723 <> destructor for 'a' @ 1.
727 Reading a token: Next token is token 'a' (1.1: <> printer for 'a' @ 1)
728 Shifting token 'a' (1.1: <> printer for 'a' @ 1)
730 Reading a token: Next token is token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
731 Shifting token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
733 Reading a token: Next token is token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
734 Shifting token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
736 Reading a token: Next token is token 'd' (1.4: <> printer for 'd' @ 4)
737 Shifting token 'd' (1.4: <> printer for 'd' @ 4)
739 Reading a token: Now at end of input.
740 1.5: syntax error, unexpected $end, expecting 'e'
741 Error: popping token 'd' (1.4: <> printer for 'd' @ 4)
743 Error: popping token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
745 Error: popping token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
747 Error: popping token 'a' (1.1: <> printer for 'a' @ 1)
749 Cleanup: discarding lookahead token $end (1.5: )
753 AT_BISON_OPTION_POPDEFS
758 ## ------------------------------------------------------ ##
759 ## Default tagged and per-type %printer and %destructor. ##
760 ## ------------------------------------------------------ ##
762 AT_SETUP([Default tagged and per-type %printer and %destructor])
763 AT_BISON_OPTION_PUSHDEFS
764 AT_DATA_GRAMMAR([[input.y]],
765 [[%define parse.error verbose
777 fprintf (yyoutput, "<> printer should not be called.\n");
780 %union { int field0; int field1; int field2; }
781 %type <field0> start 'a' 'g'
785 fprintf (yyoutput, "<*>/<field2>/e printer");
788 fprintf (stdout, "<*>/<field2>/e destructor.\n");
792 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
793 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
796 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
797 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
800 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
801 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
804 fprintf (yyoutput, "<> destructor should not be called.\n");
810 'a' 'b' 'c' 'd' 'e' 'f' 'g'
812 USE(($1, $2, $3, $4, $5, $6, $7));
819 ]AT_YYLEX_DEFINE(["abcdef"])[
829 AT_BISON_CHECK([-o input.c input.y], [], [],
830 [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother]
831 input.y:22.3-4: warning: useless %printer for type <> [-Wother]
834 AT_PARSER_CHECK([./input], 1,
835 [[<*>/<field2>/e destructor.
836 <*>/<field2>/e destructor.
840 <*>/<field2>/e destructor.
844 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
845 Shifting token 'a' (<*>/<field2>/e printer)
847 Reading a token: Next token is token 'b' (<field1> printer)
848 Shifting token 'b' (<field1> printer)
850 Reading a token: Next token is token 'c' ('c' printer)
851 Shifting token 'c' ('c' printer)
853 Reading a token: Next token is token 'd' ('d' printer)
854 Shifting token 'd' ('d' printer)
856 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
857 Shifting token 'e' (<*>/<field2>/e printer)
859 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
860 Shifting token 'f' (<*>/<field2>/e printer)
862 Reading a token: Now at end of input.
863 syntax error, unexpected $end, expecting 'g'
864 Error: popping token 'f' (<*>/<field2>/e printer)
865 Stack now 0 1 3 5 6 7
866 Error: popping token 'e' (<*>/<field2>/e printer)
868 Error: popping token 'd' ('d' printer)
870 Error: popping token 'c' ('c' printer)
872 Error: popping token 'b' (<field1> printer)
874 Error: popping token 'a' (<*>/<field2>/e printer)
876 Cleanup: discarding lookahead token $end ()
880 AT_BISON_OPTION_POPDEFS
885 ## ------------------------------------------------------------- ##
886 ## Default %printer and %destructor for user-defined end token. ##
887 ## ------------------------------------------------------------- ##
889 AT_SETUP([Default %printer and %destructor for user-defined end token])
893 m4_pushdef([AT_TEST],
895 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
896 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
898 AT_BISON_OPTION_PUSHDEFS([%locations])
899 AT_DATA_GRAMMAR([[input]]$1[[.y]],
900 [[%define parse.error verbose
913 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
918 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
921 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
925 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
930 [[[%union { char tag; }
931 %type <tag> start END]]])[[
935 start: { $$ = 'S'; } ;
945 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
946 yylloc.first_line = yylloc.last_line = 1;
947 yylloc.first_column = yylloc.last_column = 1;
959 AT_BISON_OPTION_POPDEFS
961 AT_BISON_CHECK([-o input$1.c input$1.y], [], [],
963 [[input0.y:23.3-5: warning: useless %destructor for type <*> [-Wother]
964 input0.y:23.3-5: warning: useless %printer for type <*> [-Wother]
966 [[input1.y:23.3-4: warning: useless %destructor for type <> [-Wother]
967 input1.y:23.3-4: warning: useless %printer for type <> [-Wother]
970 AT_COMPILE([input$1])
972 AT_PARSER_CHECK([./input$1], 0,
973 [[<]]kind[[> for 'E' @ 1.
974 <]]kind[[> for 'S' @ 1.
978 Reducing stack by rule 1 (line 42):
979 -> $$ = nterm start (1.1: <]]kind[[> for 'S' @ 1)
982 Reading a token: Now at end of input.
983 Shifting token END (1.1: <]]kind[[> for 'E' @ 1)
986 Cleanup: popping token END (1.1: <]]kind[[> for 'E' @ 1)
987 Cleanup: popping nterm start (1.1: <]]kind[[> for 'S' @ 1)
991 m4_popdef([not_kind])
1003 ## ------------------------------------------------------------------ ##
1004 ## Default %printer and %destructor are not for error or $undefined. ##
1005 ## ------------------------------------------------------------------ ##
1007 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
1009 # If Bison were to apply the default %printer and %destructor to the error
1010 # token or to $undefined:
1011 # - For the error token:
1012 # - It would generate warnings for unused $n.
1013 # - It would invoke the %printer and %destructor on the error token's
1014 # semantic value, which would be initialized from the lookahead, which
1015 # would be destroyed separately.
1016 # - For $undefined, who knows what the semantic value would be.
1017 AT_BISON_OPTION_PUSHDEFS
1018 AT_DATA_GRAMMAR([[input.y]],
1023 # include <stdlib.h>
1024 ]AT_YYERROR_DECLARE[
1030 fprintf (yyoutput, "'%c'", $$);
1033 fprintf (stderr, "DESTROY '%c'\n", $$);
1040 /* In order to reveal the problems that this bug caused during parsing, add
1042 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1047 ]AT_YYLEX_DEFINE(["abd"], [yylval = res])[
1055 AT_BISON_OPTION_POPDEFS
1057 AT_BISON_CHECK([-o input.c input.y], [], [],
1058 [[input.y:21.6-8: warning: useless %destructor for type <*> [-Wother]
1059 input.y:21.6-8: warning: useless %printer for type <*> [-Wother]
1062 AT_PARSER_CHECK([./input], [1], [],
1065 Reading a token: Next token is token 'a' ('a')
1066 Shifting token 'a' ('a')
1068 Reading a token: Next token is token 'b' ('b')
1070 Shifting token error ()
1072 Next token is token 'b' ('b')
1073 Shifting token 'b' ('b')
1075 Reading a token: Next token is token $undefined ()
1076 Error: popping token 'b' ('b')
1079 Error: popping token error ()
1081 Shifting token error ()
1083 Next token is token $undefined ()
1084 Error: discarding token $undefined ()
1085 Error: popping token error ()
1087 Shifting token error ()
1089 Reading a token: Now at end of input.
1090 Cleanup: discarding lookahead token $end ()
1092 Cleanup: popping token error ()
1093 Cleanup: popping token 'a' ('a')
1101 ## ------------------------------------------------------ ##
1102 ## Default %printer and %destructor are not for $accept. ##
1103 ## ------------------------------------------------------ ##
1105 AT_SETUP([Default %printer and %destructor are not for $accept])
1107 # If YYSTYPE is a union and Bison were to apply the default %printer and
1108 # %destructor to $accept:
1109 # - The %printer and %destructor code generated for $accept would always be
1110 # dead code because $accept is currently never shifted onto the stack.
1111 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1112 # to declare `%type <field> $accept'. (Also true for $undefined.)
1113 # - Thus, the compiler might complain that the user code assumes the wrong
1114 # type for $$ since the code might assume the type associated with a
1115 # specific union field, which is especially reasonable in C++ since that
1116 # type may be a base type. This test case checks for this problem. (Also
1117 # true for $undefined and the error token, so there are three warnings for
1118 # %printer and three for %destructor.)
1120 AT_BISON_OPTION_PUSHDEFS
1121 AT_DATA_GRAMMAR([[input.y]],
1122 [[%debug /* So that %printer is actually compiled. */
1126 # include <stdlib.h>
1127 ]AT_YYERROR_DECLARE[
1134 fprintf (yyoutput, "'%c'", chr);
1138 fprintf (stderr, "DESTROY '%c'\n", chr);
1141 %union { char chr; }
1146 start: { USE($$); } ;
1157 AT_BISON_OPTION_POPDEFS
1159 AT_BISON_CHECK([-o input.c input.y], [], [],
1160 [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother]
1161 input.y:22.3-4: warning: useless %printer for type <> [-Wother]
1169 ## ------------------------------------------------------ ##
1170 ## Default %printer and %destructor for mid-rule values. ##
1171 ## ------------------------------------------------------ ##
1173 AT_SETUP([Default %printer and %destructor for mid-rule values])
1175 AT_BISON_OPTION_PUSHDEFS
1176 AT_DATA_GRAMMAR([[input.y]],
1177 [[%debug /* So that %printer is actually compiled. */
1181 # include <stdlib.h>
1182 ]AT_YYERROR_DECLARE[
1185 # define YYLTYPE int
1186 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1187 # define YY_LOCATION_PRINT(File, Loc)
1190 %printer { fprintf (yyoutput, "%d", @$); } <>
1191 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1192 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1193 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1198 { @$ = 1; } // Not set or used.
1199 { USE ($$); @$ = 2; } // Both set and used.
1200 { USE ($$); @$ = 3; } // Only set.
1201 { @$ = 4; } // Only used.
1203 { USE (($$, $2, $4, $5)); @$ = 0; }
1216 AT_BISON_OPTION_POPDEFS
1218 AT_BISON_CHECK([-o input.c input.y], 0,,
1219 [[input.y:24.70-72: warning: useless %destructor for type <*> [-Wother]
1220 input.y:24.70-72: warning: useless %printer for type <*> [-Wother]
1221 input.y:33.3-23: warning: unset value: $$ [-Wother]
1222 input.y:30.3-35.37: warning: unused value: $3 [-Wother]
1226 AT_PARSER_CHECK([./input], 1,,
1229 Reducing stack by rule 1 (line 30):
1230 -> $$ = nterm $@1 (: )
1233 Reducing stack by rule 2 (line 31):
1234 -> $$ = nterm @2 (: 2)
1237 Reducing stack by rule 3 (line 32):
1238 -> $$ = nterm @3 (: 3)
1241 Reducing stack by rule 4 (line 33):
1242 -> $$ = nterm @4 (: 4)
1245 Reading a token: Now at end of input.
1247 Error: popping nterm @4 (: 4)
1250 Error: popping nterm @3 (: 3)
1253 Error: popping nterm @2 (: 2)
1256 Error: popping nterm $@1 (: )
1258 Cleanup: discarding lookahead token $end (: )
1265 ## ----------------------- ##
1266 ## @$ implies %locations. ##
1267 ## ----------------------- ##
1269 # Bison once forgot to check for @$ in actions other than semantic actions.
1271 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1272 # -------------------------------------------
1273 m4_define([AT_CHECK_ACTION_LOCATIONS],
1274 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1275 AT_BISON_OPTION_PUSHDEFS
1276 AT_DATA_GRAMMAR([[input.y]],
1279 ]AT_YYERROR_DECLARE[
1286 fprintf (stderr, "%d\n", @$.first_line);
1287 } ]m4_if($1, [%initial-action], [], [[start]])[
1309 AT_BISON_CHECK([[-o input.c input.y]])
1310 AT_COMPILE([[input]])
1311 AT_BISON_OPTION_POPDEFS
1314 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1315 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1316 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1319 ## ------------------------- ##
1320 ## Qualified $$ in actions. ##
1321 ## ------------------------- ##
1323 # Check that we can use qualified $$ (v.g., $<type>$) not only in rule
1324 # actions, but also where $$ is valid: %destructor/%printer and
1327 # FIXME: Not actually checking %destructor, but it's the same code as
1330 # To do that, use a semantic value that has two fields (sem_type),
1331 # declare symbols to have only one of these types (INT, float), and
1332 # use $<type>$ to get the other one. Including for symbols that are
1333 # not typed (UNTYPED).
1335 m4_pushdef([AT_TEST],
1336 [AT_SETUP([[Qualified $$ in actions: $1]])
1338 AT_BISON_OPTION_PUSHDEFS([%skeleton "$1"])
1340 AT_DATA_GRAMMAR([[input.y]],
1345 typedef struct sem_type
1351 # define YYSTYPE sem_type
1354 # include <iostream>
1356 report (std::ostream& yyo, int ival, float fval)
1358 yyo << "ival: " << ival << ", fval: " << fval;
1363 report (FILE* yyo, int ival, float fval)
1365 fprintf (yyo, "ival: %d, fval: %1.1f", ival, fval);
1372 ]AT_YYERROR_DECLARE[
1379 %printer { report (yyo, $$, $<fval>$); } <ival>;
1380 %printer { report (yyo, $<ival>$, $$ ); } <fval>;
1381 %printer { report (yyo, $<ival>$, $<fval>$); } <>;
1392 $$ = $<fval>1 + $<fval>2;
1393 $<ival>$ = $<ival>1 + $][2;
1397 ]AT_YYLEX_DEFINE(AT_SKEL_CC_IF([[{yy::parser::token::UNTYPED,
1398 yy::parser::token::INT,
1400 [[{UNTYPED, INT, EOF}]]),
1401 [AT_VAL.ival = toknum * 10; AT_VAL.fval = toknum / 10.0;])[
1406 p.set_debug_level(1);
1407 return p.parse ();]], [[
1409 return yyparse ();]])[
1413 AT_FULL_COMPILE([[input]])
1414 AT_PARSER_CHECK([./input], 0, [], [stderr])
1415 # Don't be too picky on the traces, GLR is not exactly the same. Keep
1416 # only the lines from the printer.
1417 AT_CHECK([[sed -ne '/ival:/p' stderr]], 0,
1418 [[Reading a token: Next token is token UNTYPED (ival: 10, fval: 0.1)
1419 Shifting token UNTYPED (ival: 10, fval: 0.1)
1420 Reading a token: Next token is token INT (ival: 20, fval: 0.2)
1421 Shifting token INT (ival: 20, fval: 0.2)
1422 $][1 = token UNTYPED (ival: 10, fval: 0.1)
1423 $][2 = token INT (ival: 20, fval: 0.2)
1424 -> $$ = nterm float (ival: 30, fval: 0.3)
1425 Cleanup: popping nterm float (ival: 30, fval: 0.3)
1428 AT_BISON_OPTION_POPDEFS
1438 m4_popdef([AT_TEST])
1440 ## ----------------------------------------------- ##
1441 ## Fix user actions without a trailing semicolon. ##
1442 ## ----------------------------------------------- ##
1444 AT_SETUP([[Fix user actions without a trailing semicolon]])
1446 # This feature is undocumented, but we accidentally broke it in 2.3a,
1447 # and there was a complaint at:
1448 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1449 AT_BISON_OPTION_PUSHDEFS
1452 start: test2 test1 test0 testc;
1455 : 'a' { semi; /* TEST:N:2 */ }
1456 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1457 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1458 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1459 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1460 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1461 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1462 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1463 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1466 : 'a' { semi; // TEST:N:1 ;
1467 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1468 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1469 } | 'd' { semi; no_semi // TEST:Y:1 ;
1470 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1471 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1472 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1473 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1474 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
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 {}
1490 #define TEST_MACRO_N \
1491 []"broken\" $ @ $$ @$ [];\
1495 #define TEST_MACRO_N \
1496 []"broken\" $ @ $$ @$ [];\
1499 AT_BISON_OPTION_POPDEFS
1501 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1502 [[input.y:8.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1503 input.y:8.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1504 input.y:9.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1505 input.y:9.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1506 input.y:10.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1507 input.y:10.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1508 input.y:11.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1509 input.y:11.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1510 input.y:12.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1511 input.y:12.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1512 input.y:13.48: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1513 input.y:13.48: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1514 input.y:20.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1515 input.y:20.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1516 input.y:21.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1517 input.y:21.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1518 input.y:22.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1519 input.y:22.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1520 input.y:23.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1521 input.y:23.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1522 input.y:24.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1523 input.y:24.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1524 input.y:25.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1525 input.y:25.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1526 input.y:31.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1527 input.y:31.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1528 input.y:32.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1529 input.y:32.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1530 input.y:33.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1531 input.y:33.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1532 input.y:34.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1533 input.y:34.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1534 input.y:35.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1535 input.y:35.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1536 input.y:36.1: warning: a ';' might be needed at the end of action code [-Wdeprecated]
1537 input.y:36.1: warning: future versions of Bison will not add the ';' [-Wdeprecated]
1540 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1541 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1542 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1543 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1544 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1549 ## -------------------------------------------------- ##
1550 ## Destroying lookahead assigned by semantic action. ##
1551 ## -------------------------------------------------- ##
1553 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1555 AT_BISON_OPTION_PUSHDEFS
1556 AT_DATA_GRAMMAR([input.y],
1561 ]AT_YYERROR_DECLARE[
1566 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1567 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1571 // In a previous version of Bison, yychar assigned by the semantic
1572 // action below was not translated into yytoken before the lookahead was
1573 // discarded and thus before its destructor (selected according to
1574 // yytoken) was called in order to return from yyparse. This would
1575 // happen even if YYACCEPT was performed in a later semantic action as
1576 // long as only consistent states with default reductions were visited
1577 // in between. However, we leave YYACCEPT in the same semantic action
1578 // for this test in order to show that skeletons cannot simply translate
1579 // immediately after every semantic action because a semantic action
1580 // that has set yychar might not always return normally. Instead,
1581 // skeletons must translate before every use of yytoken.
1582 start: 'a' accept { USE($1); } ;
1584 assert (yychar == YYEMPTY);
1591 ]AT_YYLEX_DEFINE(["a"])[
1598 AT_BISON_OPTION_POPDEFS
1599 AT_BISON_CHECK([[-o input.c input.y]])
1600 AT_COMPILE([[input]])
1601 AT_PARSER_CHECK([[./input]], [[0]], [],
1612 AT_SETUP([[YYBACKUP]])
1614 AT_BISON_OPTION_PUSHDEFS([%pure-parser])
1616 AT_DATA_GRAMMAR([input.y],
1623 # include <stdlib.h>
1624 # include <assert.h>
1626 ]AT_YYERROR_DECLARE[
1635 'a' { printf ("a: %d\n", $1); }
1636 | 'b' { YYBACKUP('a', 123); }
1637 | 'c' 'd' { YYBACKUP('a', 456); }
1642 ]AT_YYLEX_DEFINE(["bcd"], [*lvalp = (toknum + 1) * 10])[
1647 yydebug = !!getenv("YYDEBUG");
1651 AT_BISON_OPTION_POPDEFS
1653 AT_BISON_CHECK([[-o input.c input.y]])
1654 AT_COMPILE([[input]])
1655 AT_PARSER_CHECK([[./input]], [[0]],