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,
75 ## ---------------- ##
77 ## ---------------- ##
79 AT_SETUP([Exotic Dollars])
81 AT_BISON_OPTION_PUSHDEFS
82 AT_DATA_GRAMMAR([[input.y]],
83 [[%define parse.error verbose
96 %type <val> a_1 a_2 a_5
97 sum_of_the_five_previous_values
100 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
101 sum_of_the_five_previous_values
103 USE (($1, $2, $<foo>3, $<foo>4, $5));
111 sum_of_the_five_previous_values:
113 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
127 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
129 AT_PARSER_CHECK([./input], 0,
133 # Make sure that fields after $n or $-n are parsed correctly. At one
134 # point while implementing dashes in symbol names, we were dropping
136 AT_DATA_GRAMMAR([[input.y]],
142 typedef struct { int val; } stype;
143 # define YYSTYPE stype
147 start: one two { $$.val = $1.val + $2.val; } sum ;
148 one: { $$.val = 1; } ;
149 two: { $$.val = 2; } ;
150 sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ;
162 AT_BISON_CHECK([[-o input.c input.y]])
163 AT_COMPILE([[input]])
164 AT_PARSER_CHECK([[./input]], [[0]],
168 AT_BISON_OPTION_POPDEFS
173 ## -------------------------- ##
174 ## Printers and Destructors. ##
175 ## -------------------------- ##
177 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4,
178 # BISON-DIRECTIVE, UNION-FLAG)
179 # -------------------------------------------------------------
180 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
181 [# Make sure complex $n work.
182 m4_if([$1$2$3$4], $[1]$[2]$[3]$[4], [],
183 [m4_fatal([$0: Invalid arguments: $@])])dnl
185 # Be sure to pass all the %directives to this macro to have correct
186 # helping macros. So don't put any directly in the Bison file.
187 AT_BISON_OPTION_PUSHDEFS([$5])
188 AT_DATA_GRAMMAR([[input.y]],
195 #define YYINITDEPTH 10
196 #define YYMAXDEPTH 10
197 #define RANGE(Location) ]AT_LALR1_CC_IF([(Location).begin.line, (Location).end.line],
198 [(Location).first_line, (Location).last_line])[
200 /* Display the symbol type Symbol. */
201 #define V(Symbol, Value, Location, Sep) \
202 fprintf (stderr, #Symbol " (%d@%d-%d)" Sep, Value, RANGE(Location))
206 ]m4_ifval([$6], [%union
210 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
211 m4_ifval([$6], [[%code provides {]], [[%code {]])
212 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])[
214 ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE])
217 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
219 /* FIXME: This %printer isn't actually tested. */
222 ]AT_LALR1_CC_IF([debug_stream () << $$;],
223 [fprintf (yyoutput, "%d", $$)])[;
225 input line thing 'x' 'y'
228 { fprintf (stderr, "Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
232 { fprintf (stderr, "Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
236 { fprintf (stderr, "Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
240 { fprintf (stderr, "Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
244 { fprintf (stderr, "Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
249 { fprintf (stderr, "Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
254 This grammar is made to exercise error recovery.
255 "Lines" starting with `(' support error recovery, with
256 ')' as synchronizing token. Lines starting with 'x' can never
257 be recovered from if in error.
264 V(input, $$, @$, ": /* Nothing */\n");
266 | line input /* Right recursive to load the stack so that popping at
267 END can be exercised. */
270 V(input, $$, @$, ": ");
271 V(line, $1, @1, " ");
272 V(input, $2, @2, "\n");
277 thing thing thing ';'
280 V(line, $$, @$, ": ");
281 V(thing, $1, @1, " ");
282 V(thing, $2, @2, " ");
283 V(thing, $3, @3, " ");
286 | '(' thing thing ')'
289 V(line, $$, @$, ": ");
291 V(thing, $2, @2, " ");
292 V(thing, $3, @3, " ");
293 V(')', $4, @4, "\n");
298 V(line, $$, @$, ": ");
300 V(thing, $2, @2, " ");
301 V(')', $3, @3, "\n");
306 V(line, $$, @$, ": ");
308 fprintf (stderr, "error (@%d-%d) ", RANGE(@2));
309 V(')', $3, @3, "\n");
317 V(thing, $$, @$, ": ");
318 V('x', $1, @1, "\n");
322 /* Alias to ARGV[1]. */
323 const char *source = YY_NULL;
330 static unsigned int counter = 0;
332 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
333 /* As in BASIC, line numbers go from 10 to 10. */
334 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_FIRST_COLUMN[ = 10 * c;
335 ]AT_LOC_LAST_LINE[ = ]AT_LOC_LAST_COLUMN[ = ]AT_LOC_FIRST_LINE[ + 9;
336 assert (0 <= c && c <= strlen (source));
338 fprintf (stderr, "sending: '%c'", source[c]);
340 fprintf (stderr, "sending: END");
341 fprintf (stderr, " (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
345 [static bool yydebug;
350 parser.set_debug_level (yydebug);
351 return parser.parse ();
356 main (int argc, const char *argv[])
359 yydebug = !!getenv ("YYDEBUG");
365 case 0: fprintf (stderr, "Successful parse.\n"); break;
366 case 1: fprintf (stderr, "Parsing FAILED.\n"); break;
367 default: fprintf (stderr, "Parsing FAILED (status %d).\n", status); break;
373 AT_FULL_COMPILE([input])
376 # Check the location of "empty"
377 # -----------------------------
378 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
379 # an empty "line" nterm.
380 # FIXME: This location is not satisfying. Depend on the lookahead?
381 AT_PARSER_CHECK([./input '(x)'], 0, [],
382 [[sending: '(' (0@0-9)
383 sending: 'x' (1@10-19)
384 thing (1@10-19): 'x' (1@10-19)
385 sending: ')' (2@20-29)
386 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
387 sending: END (3@30-39)
388 input (0@29-29): /* Nothing */
389 input (2@0-29): line (0@0-29) input (0@29-29)
390 Freeing token END (3@30-39)
391 Freeing nterm input (2@0-29)
396 # Check locations in error recovery
397 # ---------------------------------
398 # '(y)' is an error, but can be recovered from. But what's the location
399 # of the error itself ('y'), and of the resulting reduction ('(error)').
400 AT_PARSER_CHECK([./input '(y)'], 0, [],
401 [[sending: '(' (0@0-9)
402 sending: 'y' (1@10-19)
403 10.10-19.18: syntax error, unexpected 'y', expecting 'x'
404 Freeing token 'y' (1@10-19)
405 sending: ')' (2@20-29)
406 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
407 sending: END (3@30-39)
408 input (0@29-29): /* Nothing */
409 input (2@0-29): line (-1@0-29) input (0@29-29)
410 Freeing token END (3@30-39)
411 Freeing nterm input (2@0-29)
416 # Syntax errors caught by the parser
417 # ----------------------------------
418 # Exercise the discarding of stack top and input until `error'
421 # '(', 'x', 'x', 'x', 'x', 'x', ')',
423 # Load the stack and provoke an error that cannot be caught by the
424 # grammar, to check that the stack is cleared. And make sure the
425 # lookahead is freed.
430 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, [],
431 [[sending: '(' (0@0-9)
432 sending: 'x' (1@10-19)
433 thing (1@10-19): 'x' (1@10-19)
434 sending: 'x' (2@20-29)
435 thing (2@20-29): 'x' (2@20-29)
436 sending: 'x' (3@30-39)
437 30.30-39.38: syntax error, unexpected 'x', expecting ')'
438 Freeing nterm thing (2@20-29)
439 Freeing nterm thing (1@10-19)
440 Freeing token 'x' (3@30-39)
441 sending: 'x' (4@40-49)
442 Freeing token 'x' (4@40-49)
443 sending: 'x' (5@50-59)
444 Freeing token 'x' (5@50-59)
445 sending: ')' (6@60-69)
446 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
447 sending: '(' (7@70-79)
448 sending: 'x' (8@80-89)
449 thing (8@80-89): 'x' (8@80-89)
450 sending: ')' (9@90-99)
451 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
452 sending: '(' (10@100-109)
453 sending: 'x' (11@110-119)
454 thing (11@110-119): 'x' (11@110-119)
455 sending: ')' (12@120-129)
456 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
457 sending: 'y' (13@130-139)
458 input (0@129-129): /* Nothing */
459 input (2@100-129): line (10@100-129) input (0@129-129)
460 input (2@70-129): line (7@70-99) input (2@100-129)
461 input (2@0-129): line (-1@0-69) input (2@70-129)
462 130.130-139.138: syntax error, unexpected 'y', expecting END
463 Freeing nterm input (2@0-129)
464 Freeing token 'y' (13@130-139)
469 # Syntax error caught by the parser where lookahead = END
470 # --------------------------------------------------------
471 # Load the stack and provoke an error that cannot be caught by the
472 # grammar, to check that the stack is cleared. And make sure the
473 # lookahead is freed.
478 AT_PARSER_CHECK([./input '(x)(x)x'], 1, [],
479 [[sending: '(' (0@0-9)
480 sending: 'x' (1@10-19)
481 thing (1@10-19): 'x' (1@10-19)
482 sending: ')' (2@20-29)
483 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
484 sending: '(' (3@30-39)
485 sending: 'x' (4@40-49)
486 thing (4@40-49): 'x' (4@40-49)
487 sending: ')' (5@50-59)
488 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
489 sending: 'x' (6@60-69)
490 thing (6@60-69): 'x' (6@60-69)
491 sending: END (7@70-79)
492 70.70-79.78: syntax error, unexpected END, expecting 'x'
493 Freeing nterm thing (6@60-69)
494 Freeing nterm line (3@30-59)
495 Freeing nterm line (0@0-29)
496 Freeing token END (7@70-79)
501 # Check destruction upon stack overflow
502 # -------------------------------------
503 # Upon stack overflow, all symbols on the stack should be destroyed.
504 # Only check for yacc.c.
506 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, [],
507 [[sending: '(' (0@0-9)
508 sending: 'x' (1@10-19)
509 thing (1@10-19): 'x' (1@10-19)
510 sending: ')' (2@20-29)
511 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
512 sending: '(' (3@30-39)
513 sending: 'x' (4@40-49)
514 thing (4@40-49): 'x' (4@40-49)
515 sending: ')' (5@50-59)
516 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
517 sending: '(' (6@60-69)
518 sending: 'x' (7@70-79)
519 thing (7@70-79): 'x' (7@70-79)
520 sending: ')' (8@80-89)
521 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
522 sending: '(' (9@90-99)
523 sending: 'x' (10@100-109)
524 thing (10@100-109): 'x' (10@100-109)
525 sending: ')' (11@110-119)
526 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
527 sending: '(' (12@120-129)
528 sending: 'x' (13@130-139)
529 thing (13@130-139): 'x' (13@130-139)
530 sending: ')' (14@140-149)
531 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
532 sending: '(' (15@150-159)
533 sending: 'x' (16@160-169)
534 thing (16@160-169): 'x' (16@160-169)
535 sending: ')' (17@170-179)
536 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
537 sending: '(' (18@180-189)
538 sending: 'x' (19@190-199)
539 thing (19@190-199): 'x' (19@190-199)
540 sending: ')' (20@200-209)
541 200.200-209.208: memory exhausted
542 Freeing nterm thing (19@190-199)
543 Freeing nterm line (15@150-179)
544 Freeing nterm line (12@120-149)
545 Freeing nterm line (9@90-119)
546 Freeing nterm line (6@60-89)
547 Freeing nterm line (3@30-59)
548 Freeing nterm line (0@0-29)
549 Parsing FAILED (status 2).
553 AT_BISON_OPTION_POPDEFS
554 ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR
557 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
558 # ---------------------------------------------------------------------------
559 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
560 [AT_SETUP([Printers and Destructors $2: $1])
563 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
564 [%define parse.error verbose
574 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
575 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
577 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
578 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
580 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
581 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
585 ## ----------------------------------------- ##
586 ## Default tagless %printer and %destructor. ##
587 ## ----------------------------------------- ##
589 # Check that the right %printer and %destructor are called, that they're not
590 # called for $end, and that $$ and @$ work correctly.
592 AT_SETUP([Default tagless %printer and %destructor])
593 AT_BISON_OPTION_PUSHDEFS([%locations])
594 AT_DATA_GRAMMAR([[input.y]],
595 [[%define parse.error verbose
599 @$.first_line = @$.last_line = 1;
600 @$.first_column = @$.last_column = 1;
612 fprintf (yyoutput, "<*> printer should not be called.\n");
616 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
619 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
623 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
626 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
630 fprintf (yyoutput, "<*> destructor should not be called.\n");
635 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
639 ]AT_YYLEX_DEFINE(["abcd"], [[yylval = res]])[
649 AT_BISON_CHECK([-o input.c input.y], [], [],
650 [[input.y:27.3-5: warning: useless %destructor for type <*> [-Wother]
651 input.y:27.3-5: warning: useless %printer for type <*> [-Wother]
654 AT_PARSER_CHECK([./input], 1,
655 [[<> destructor for 'd' @ 4.
656 'b'/'c' destructor for 'c' @ 3.
657 'b'/'c' destructor for 'b' @ 2.
658 <> destructor for 'a' @ 1.
662 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
663 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
665 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
666 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
668 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
669 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
671 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
672 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
674 Reading a token: Now at end of input.
675 1.5-4: syntax error, unexpected $end, expecting 'e'
676 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
678 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
680 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
682 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
684 Cleanup: discarding lookahead token $end (1.5-1.5: )
688 AT_BISON_OPTION_POPDEFS
693 ## ------------------------------------------------------ ##
694 ## Default tagged and per-type %printer and %destructor. ##
695 ## ------------------------------------------------------ ##
697 AT_SETUP([Default tagged and per-type %printer and %destructor])
698 AT_BISON_OPTION_PUSHDEFS
699 AT_DATA_GRAMMAR([[input.y]],
700 [[%define parse.error verbose
712 fprintf (yyoutput, "<> printer should not be called.\n");
715 %union { int field0; int field1; int field2; }
716 %type <field0> start 'a' 'g'
720 fprintf (yyoutput, "<*>/<field2>/e printer");
723 fprintf (stdout, "<*>/<field2>/e destructor.\n");
727 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
728 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
731 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
732 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
735 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
736 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
739 fprintf (yyoutput, "<> destructor should not be called.\n");
745 'a' 'b' 'c' 'd' 'e' 'f' 'g'
747 USE(($1, $2, $3, $4, $5, $6, $7));
754 ]AT_YYLEX_DEFINE(["abcdef"])[
764 AT_BISON_CHECK([-o input.c input.y], [], [],
765 [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother]
766 input.y:22.3-4: warning: useless %printer for type <> [-Wother]
769 AT_PARSER_CHECK([./input], 1,
770 [[<*>/<field2>/e destructor.
771 <*>/<field2>/e destructor.
775 <*>/<field2>/e destructor.
779 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
780 Shifting token 'a' (<*>/<field2>/e printer)
782 Reading a token: Next token is token 'b' (<field1> printer)
783 Shifting token 'b' (<field1> printer)
785 Reading a token: Next token is token 'c' ('c' printer)
786 Shifting token 'c' ('c' printer)
788 Reading a token: Next token is token 'd' ('d' printer)
789 Shifting token 'd' ('d' printer)
791 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
792 Shifting token 'e' (<*>/<field2>/e printer)
794 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
795 Shifting token 'f' (<*>/<field2>/e printer)
797 Reading a token: Now at end of input.
798 syntax error, unexpected $end, expecting 'g'
799 Error: popping token 'f' (<*>/<field2>/e printer)
800 Stack now 0 1 3 5 6 7
801 Error: popping token 'e' (<*>/<field2>/e printer)
803 Error: popping token 'd' ('d' printer)
805 Error: popping token 'c' ('c' printer)
807 Error: popping token 'b' (<field1> printer)
809 Error: popping token 'a' (<*>/<field2>/e printer)
811 Cleanup: discarding lookahead token $end ()
815 AT_BISON_OPTION_POPDEFS
820 ## ------------------------------------------------------------- ##
821 ## Default %printer and %destructor for user-defined end token. ##
822 ## ------------------------------------------------------------- ##
824 AT_SETUP([Default %printer and %destructor for user-defined end token])
828 m4_pushdef([AT_TEST],
830 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
831 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
833 AT_BISON_OPTION_PUSHDEFS([%locations])
834 AT_DATA_GRAMMAR([[input]]$1[[.y]],
835 [[%define parse.error verbose
839 @$.first_line = @$.last_line = 1;
840 @$.first_column = @$.last_column = 1;
852 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
857 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
860 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
864 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
869 [[[%union { char tag; }
870 %type <tag> start END]]])[[
874 start: { $$ = 'S'; } ;
884 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
885 yylloc.first_line = yylloc.last_line = 1;
886 yylloc.first_column = yylloc.last_column = 1;
898 AT_BISON_OPTION_POPDEFS
900 AT_BISON_CHECK([-o input$1.c input$1.y], [], [],
902 [[input0.y:27.3-5: warning: useless %destructor for type <*> [-Wother]
903 input0.y:27.3-5: warning: useless %printer for type <*> [-Wother]
905 [[input1.y:27.3-4: warning: useless %destructor for type <> [-Wother]
906 input1.y:27.3-4: warning: useless %printer for type <> [-Wother]
909 AT_COMPILE([input$1])
911 AT_PARSER_CHECK([./input$1], 0,
912 [[<]]kind[[> for 'E' @ 1.
913 <]]kind[[> for 'S' @ 1.
917 Reducing stack by rule 1 (line 46):
918 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
921 Reading a token: Now at end of input.
922 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
925 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
926 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
930 m4_popdef([not_kind])
942 ## ------------------------------------------------------------------ ##
943 ## Default %printer and %destructor are not for error or $undefined. ##
944 ## ------------------------------------------------------------------ ##
946 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
948 # If Bison were to apply the default %printer and %destructor to the error
949 # token or to $undefined:
950 # - For the error token:
951 # - It would generate warnings for unused $n.
952 # - It would invoke the %printer and %destructor on the error token's
953 # semantic value, which would be initialized from the lookahead, which
954 # would be destroyed separately.
955 # - For $undefined, who knows what the semantic value would be.
956 AT_BISON_OPTION_PUSHDEFS
957 AT_DATA_GRAMMAR([[input.y]],
969 fprintf (yyoutput, "'%c'", $$);
972 fprintf (stderr, "DESTROY '%c'\n", $$);
979 /* In order to reveal the problems that this bug caused during parsing, add
981 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
986 ]AT_YYLEX_DEFINE(["abd"], [yylval = res])[
994 AT_BISON_OPTION_POPDEFS
996 AT_BISON_CHECK([-o input.c input.y], [], [],
997 [[input.y:21.6-8: warning: useless %destructor for type <*> [-Wother]
998 input.y:21.6-8: warning: useless %printer for type <*> [-Wother]
1001 AT_PARSER_CHECK([./input], [1], [],
1004 Reading a token: Next token is token 'a' ('a')
1005 Shifting token 'a' ('a')
1007 Reading a token: Next token is token 'b' ('b')
1009 Shifting token error ()
1011 Next token is token 'b' ('b')
1012 Shifting token 'b' ('b')
1014 Reading a token: Next token is token $undefined ()
1015 Error: popping token 'b' ('b')
1018 Error: popping token error ()
1020 Shifting token error ()
1022 Next token is token $undefined ()
1023 Error: discarding token $undefined ()
1024 Error: popping token error ()
1026 Shifting token error ()
1028 Reading a token: Now at end of input.
1029 Cleanup: discarding lookahead token $end ()
1031 Cleanup: popping token error ()
1032 Cleanup: popping token 'a' ('a')
1040 ## ------------------------------------------------------ ##
1041 ## Default %printer and %destructor are not for $accept. ##
1042 ## ------------------------------------------------------ ##
1044 AT_SETUP([Default %printer and %destructor are not for $accept])
1046 # If YYSTYPE is a union and Bison were to apply the default %printer and
1047 # %destructor to $accept:
1048 # - The %printer and %destructor code generated for $accept would always be
1049 # dead code because $accept is currently never shifted onto the stack.
1050 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1051 # to declare `%type <field> $accept'. (Also true for $undefined.)
1052 # - Thus, the compiler might complain that the user code assumes the wrong
1053 # type for $$ since the code might assume the type associated with a
1054 # specific union field, which is especially reasonable in C++ since that
1055 # type may be a base type. This test case checks for this problem. (Also
1056 # true for $undefined and the error token, so there are three warnings for
1057 # %printer and three for %destructor.)
1059 AT_BISON_OPTION_PUSHDEFS
1060 AT_DATA_GRAMMAR([[input.y]],
1061 [[%debug /* So that %printer is actually compiled. */
1065 # include <stdlib.h>
1066 ]AT_YYERROR_DECLARE[
1073 fprintf (yyoutput, "'%c'", chr);
1077 fprintf (stderr, "DESTROY '%c'\n", chr);
1080 %union { char chr; }
1085 start: { USE($$); } ;
1096 AT_BISON_OPTION_POPDEFS
1098 AT_BISON_CHECK([-o input.c input.y], [], [],
1099 [[input.y:22.3-4: warning: useless %destructor for type <> [-Wother]
1100 input.y:22.3-4: warning: useless %printer for type <> [-Wother]
1108 ## ------------------------------------------------------ ##
1109 ## Default %printer and %destructor for mid-rule values. ##
1110 ## ------------------------------------------------------ ##
1112 AT_SETUP([Default %printer and %destructor for mid-rule values])
1114 AT_BISON_OPTION_PUSHDEFS
1115 AT_DATA_GRAMMAR([[input.y]],
1116 [[%debug /* So that %printer is actually compiled. */
1120 # include <stdlib.h>
1121 ]AT_YYERROR_DECLARE[
1124 # define YYLTYPE int
1125 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1126 # define YY_LOCATION_PRINT(File, Loc)
1129 %printer { fprintf (yyoutput, "%d", @$); } <>
1130 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1131 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1132 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1137 { @$ = 1; } // Not set or used.
1138 { USE ($$); @$ = 2; } // Both set and used.
1139 { USE ($$); @$ = 3; } // Only set.
1140 { @$ = 4; } // Only used.
1142 { USE (($$, $2, $4, $5)); @$ = 0; }
1155 AT_BISON_OPTION_POPDEFS
1157 AT_BISON_CHECK([-o input.c input.y], 0,,
1158 [[input.y:24.70-72: warning: useless %destructor for type <*> [-Wother]
1159 input.y:24.70-72: warning: useless %printer for type <*> [-Wother]
1160 input.y:33.3-23: warning: unset value: $$ [-Wother]
1161 input.y:30.3-35.37: warning: unused value: $3 [-Wother]
1165 AT_PARSER_CHECK([./input], 1,,
1168 Reducing stack by rule 1 (line 30):
1169 -> $$ = nterm $@1 (: )
1172 Reducing stack by rule 2 (line 31):
1173 -> $$ = nterm @2 (: 2)
1176 Reducing stack by rule 3 (line 32):
1177 -> $$ = nterm @3 (: 3)
1180 Reducing stack by rule 4 (line 33):
1181 -> $$ = nterm @4 (: 4)
1184 Reading a token: Now at end of input.
1186 Error: popping nterm @4 (: 4)
1189 Error: popping nterm @3 (: 3)
1192 Error: popping nterm @2 (: 2)
1195 Error: popping nterm $@1 (: )
1197 Cleanup: discarding lookahead token $end (: )
1204 ## ----------------------- ##
1205 ## @$ implies %locations. ##
1206 ## ----------------------- ##
1208 # Bison once forgot to check for @$ in actions other than semantic actions.
1210 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1211 # -------------------------------------------
1212 m4_define([AT_CHECK_ACTION_LOCATIONS],
1213 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1214 AT_BISON_OPTION_PUSHDEFS
1215 AT_DATA_GRAMMAR([[input.y]],
1218 ]AT_YYERROR_DECLARE[
1225 fprintf (stderr, "%d\n", @$.first_line);
1226 } ]m4_if($1, [%initial-action], [], [[start]])[
1248 AT_BISON_CHECK([[-o input.c input.y]])
1249 AT_COMPILE([[input]])
1250 AT_BISON_OPTION_POPDEFS
1253 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1254 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1255 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1258 ## ----------------------------------------------- ##
1259 ## Fix user actions without a trailing semicolon. ##
1260 ## ----------------------------------------------- ##
1262 AT_SETUP([[Fix user actions without a trailing semicolon]])
1264 # This feature is undocumented, but we accidentally broke it in 2.3a,
1265 # and there was a complaint at:
1266 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1267 AT_BISON_OPTION_PUSHDEFS
1270 start: test2 test1 test0 testc;
1273 : 'a' { semi; /* TEST:N:2 */ }
1274 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1275 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1276 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1277 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1278 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1279 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1280 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1281 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1284 : 'a' { semi; // TEST:N:1 ;
1285 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1286 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1287 } | 'd' { semi; no_semi // TEST:Y:1 ;
1288 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1289 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1290 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1291 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1292 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1295 : 'a' { semi; // TEST:N:1 {}
1296 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1297 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1298 } | 'd' { semi; no_semi // TEST:Y:1 {}
1299 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1300 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1301 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1302 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1303 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1308 #define TEST_MACRO_N \
1309 []"broken\" $ @ $$ @$ [];\
1313 #define TEST_MACRO_N \
1314 []"broken\" $ @ $$ @$ [];\
1317 AT_BISON_OPTION_POPDEFS
1319 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1320 [[input.y:8.48: warning: a ';' might be needed at the end of action code [-Wother]
1321 input.y:8.48: warning: future versions of Bison will not add the ';' [-Wother]
1322 input.y:9.48: warning: a ';' might be needed at the end of action code [-Wother]
1323 input.y:9.48: warning: future versions of Bison will not add the ';' [-Wother]
1324 input.y:10.48: warning: a ';' might be needed at the end of action code [-Wother]
1325 input.y:10.48: warning: future versions of Bison will not add the ';' [-Wother]
1326 input.y:11.48: warning: a ';' might be needed at the end of action code [-Wother]
1327 input.y:11.48: warning: future versions of Bison will not add the ';' [-Wother]
1328 input.y:12.48: warning: a ';' might be needed at the end of action code [-Wother]
1329 input.y:12.48: warning: future versions of Bison will not add the ';' [-Wother]
1330 input.y:13.48: warning: a ';' might be needed at the end of action code [-Wother]
1331 input.y:13.48: warning: future versions of Bison will not add the ';' [-Wother]
1332 input.y:20.1: warning: a ';' might be needed at the end of action code [-Wother]
1333 input.y:20.1: warning: future versions of Bison will not add the ';' [-Wother]
1334 input.y:21.1: warning: a ';' might be needed at the end of action code [-Wother]
1335 input.y:21.1: warning: future versions of Bison will not add the ';' [-Wother]
1336 input.y:22.1: warning: a ';' might be needed at the end of action code [-Wother]
1337 input.y:22.1: warning: future versions of Bison will not add the ';' [-Wother]
1338 input.y:23.1: warning: a ';' might be needed at the end of action code [-Wother]
1339 input.y:23.1: warning: future versions of Bison will not add the ';' [-Wother]
1340 input.y:24.1: warning: a ';' might be needed at the end of action code [-Wother]
1341 input.y:24.1: warning: future versions of Bison will not add the ';' [-Wother]
1342 input.y:25.1: warning: a ';' might be needed at the end of action code [-Wother]
1343 input.y:25.1: warning: future versions of Bison will not add the ';' [-Wother]
1344 input.y:31.1: warning: a ';' might be needed at the end of action code [-Wother]
1345 input.y:31.1: warning: future versions of Bison will not add the ';' [-Wother]
1346 input.y:32.1: warning: a ';' might be needed at the end of action code [-Wother]
1347 input.y:32.1: warning: future versions of Bison will not add the ';' [-Wother]
1348 input.y:33.1: warning: a ';' might be needed at the end of action code [-Wother]
1349 input.y:33.1: warning: future versions of Bison will not add the ';' [-Wother]
1350 input.y:34.1: warning: a ';' might be needed at the end of action code [-Wother]
1351 input.y:34.1: warning: future versions of Bison will not add the ';' [-Wother]
1352 input.y:35.1: warning: a ';' might be needed at the end of action code [-Wother]
1353 input.y:35.1: warning: future versions of Bison will not add the ';' [-Wother]
1354 input.y:36.1: warning: a ';' might be needed at the end of action code [-Wother]
1355 input.y:36.1: warning: future versions of Bison will not add the ';' [-Wother]
1358 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1359 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1360 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1361 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1362 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1367 ## -------------------------------------------------- ##
1368 ## Destroying lookahead assigned by semantic action. ##
1369 ## -------------------------------------------------- ##
1371 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1373 AT_BISON_OPTION_PUSHDEFS
1374 AT_DATA_GRAMMAR([input.y],
1379 ]AT_YYERROR_DECLARE[
1384 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1385 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1389 // In a previous version of Bison, yychar assigned by the semantic
1390 // action below was not translated into yytoken before the lookahead was
1391 // discarded and thus before its destructor (selected according to
1392 // yytoken) was called in order to return from yyparse. This would
1393 // happen even if YYACCEPT was performed in a later semantic action as
1394 // long as only consistent states with default reductions were visited
1395 // in between. However, we leave YYACCEPT in the same semantic action
1396 // for this test in order to show that skeletons cannot simply translate
1397 // immediately after every semantic action because a semantic action
1398 // that has set yychar might not always return normally. Instead,
1399 // skeletons must translate before every use of yytoken.
1400 start: 'a' accept { USE($1); } ;
1402 assert (yychar == YYEMPTY);
1409 ]AT_YYLEX_DEFINE(["a"])[
1416 AT_BISON_OPTION_POPDEFS
1417 AT_BISON_CHECK([[-o input.c input.y]])
1418 AT_COMPILE([[input]])
1419 AT_PARSER_CHECK([[./input]], [[0]], [],
1430 AT_SETUP([[YYBACKUP]])
1432 AT_BISON_OPTION_PUSHDEFS([%pure-parser])
1434 AT_DATA_GRAMMAR([input.y],
1441 # include <stdlib.h>
1442 # include <assert.h>
1444 ]AT_YYERROR_DECLARE[
1453 'a' { printf ("a: %d\n", $1); }
1454 | 'b' { YYBACKUP('a', 123); }
1455 | 'c' 'd' { YYBACKUP('a', 456); }
1460 ]AT_YYLEX_DEFINE(["bcd"], [*lvalp = (toknum + 1) * 10])[
1465 yydebug = !!getenv("YYDEBUG");
1469 AT_BISON_OPTION_POPDEFS
1471 AT_BISON_CHECK([[-o input.c input.y]])
1472 AT_COMPILE([[input]])
1473 AT_PARSER_CHECK([[./input]], [[0]],