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,
75 ## ---------------- ##
77 ## ---------------- ##
79 AT_SETUP([Exotic Dollars])
81 AT_BISON_OPTION_PUSHDEFS
82 AT_DATA_GRAMMAR([[input.y]],
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;
119 ]AT_YYLEX_DEFINE([])[
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); } ;
168 AT_BISON_CHECK([[-o input.c input.y]])
169 AT_COMPILE([[input]])
170 AT_PARSER_CHECK([[./input]], [[0]],
174 AT_BISON_OPTION_POPDEFS
179 ## -------------------------- ##
180 ## Printers and Destructors. ##
181 ## -------------------------- ##
183 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
184 # -----------------------------------------------------------------------------
185 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
186 [# Make sure complex $n work.
187 m4_if([$1$2$3], $[1]$[2]$[3], [],
188 [m4_fatal([$0: Invalid arguments: $@])])dnl
190 # Be sure to pass all the %directives to this macro to have correct
191 # helping macros. So don't put any directly in the Bison file.
192 AT_BISON_OPTION_PUSHDEFS([$5])
193 AT_DATA_GRAMMAR([[input.y]],
200 #define YYINITDEPTH 10
201 #define YYMAXDEPTH 10
203 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
204 [#define RANGE(Location) (Location).first_line, (Location).last_line])
208 m4_ifval([$6], [%union
212 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
213 m4_ifval([$6], [[%code provides {]], [[%code {]])
214 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
215 [static int yylex (]AT_LEX_FORMALS[);
216 ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE])
219 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
221 /* FIXME: This %printer isn't actually tested. */
224 ]AT_LALR1_CC_IF([debug_stream () << $$;],
225 [fprintf (yyoutput, "%d", $$)])[;
227 input line thing 'x' 'y'
230 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
234 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
238 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
242 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
246 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
251 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
256 This grammar is made to exercise error recovery.
257 "Lines" starting with `(' support error recovery, with
258 ')' as synchronizing token. Lines starting with 'x' can never
259 be recovered from if in error.
266 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
268 | line input /* Right recursive to load the stack so that popping at
269 END can be exercised. */
272 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
273 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
278 thing thing thing ';'
281 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
282 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
283 $3, RANGE (@3), $4, RANGE (@4));
285 | '(' thing thing ')'
288 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
289 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
290 $3, RANGE (@3), $4, RANGE (@4));
295 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
296 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
301 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
302 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
310 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
311 $$, RANGE (@$), $1, RANGE (@1));
315 /* Alias to ARGV[1]. */
316 const char *source = YY_NULL;
319 yylex (]AT_LEX_FORMALS[)
321 static unsigned int counter = 0;
323 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
324 /* As in BASIC, line numbers go from 10 to 10. */
326 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
327 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
329 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
330 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
333 if (! (0 <= c && c <= strlen (source)))
336 printf ("sending: '%c'", source[c]);
338 printf ("sending: END");
339 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
344 [/* A C++ error reporting function. */
346 yy::parser::error (const location& l, const std::string& m)
348 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
356 parser.set_debug_level (yydebug);
357 return parser.parse ();
361 yyerror (const char *msg)
363 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
367 main (int argc, const char *argv[])
370 yydebug = !!getenv ("YYDEBUG");
376 case 0: printf ("Successful parse.\n"); break;
377 case 1: printf ("Parsing FAILED.\n"); break;
378 default: printf ("Parsing FAILED (status %d).\n", status); break;
384 AT_FULL_COMPILE([input])
387 # Check the location of "empty"
388 # -----------------------------
389 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
390 # an empty "line" nterm.
391 # FIXME: This location is not satisfying. Depend on the lookahead?
392 AT_PARSER_CHECK([./input '(x)'], 0,
393 [[sending: '(' (0@0-9)
394 sending: 'x' (1@10-19)
395 thing (1@10-19): 'x' (1@10-19)
396 sending: ')' (2@20-29)
397 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
398 sending: END (3@30-39)
399 input (0@29-29): /* Nothing */
400 input (2@0-29): line (0@0-29) input (0@29-29)
401 Freeing token END (3@30-39)
402 Freeing nterm input (2@0-29)
407 # Check locations in error recovery
408 # ---------------------------------
409 # '(y)' is an error, but can be recovered from. But what's the location
410 # of the error itself ('y'), and of the resulting reduction ('(error)').
411 AT_PARSER_CHECK([./input '(y)'], 0,
412 [[sending: '(' (0@0-9)
413 sending: 'y' (1@10-19)
414 10-19: syntax error, unexpected 'y', expecting 'x'
415 Freeing token 'y' (1@10-19)
416 sending: ')' (2@20-29)
417 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
418 sending: END (3@30-39)
419 input (0@29-29): /* Nothing */
420 input (2@0-29): line (-1@0-29) input (0@29-29)
421 Freeing token END (3@30-39)
422 Freeing nterm input (2@0-29)
427 # Syntax errors caught by the parser
428 # ----------------------------------
429 # Exercise the discarding of stack top and input until `error'
432 # '(', 'x', 'x', 'x', 'x', 'x', ')',
434 # Load the stack and provoke an error that cannot be caught by the
435 # grammar, to check that the stack is cleared. And make sure the
436 # lookahead is freed.
441 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
442 [[sending: '(' (0@0-9)
443 sending: 'x' (1@10-19)
444 thing (1@10-19): 'x' (1@10-19)
445 sending: 'x' (2@20-29)
446 thing (2@20-29): 'x' (2@20-29)
447 sending: 'x' (3@30-39)
448 30-39: syntax error, unexpected 'x', expecting ')'
449 Freeing nterm thing (2@20-29)
450 Freeing nterm thing (1@10-19)
451 Freeing token 'x' (3@30-39)
452 sending: 'x' (4@40-49)
453 Freeing token 'x' (4@40-49)
454 sending: 'x' (5@50-59)
455 Freeing token 'x' (5@50-59)
456 sending: ')' (6@60-69)
457 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
458 sending: '(' (7@70-79)
459 sending: 'x' (8@80-89)
460 thing (8@80-89): 'x' (8@80-89)
461 sending: ')' (9@90-99)
462 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
463 sending: '(' (10@100-109)
464 sending: 'x' (11@110-119)
465 thing (11@110-119): 'x' (11@110-119)
466 sending: ')' (12@120-129)
467 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
468 sending: 'y' (13@130-139)
469 input (0@129-129): /* Nothing */
470 input (2@100-129): line (10@100-129) input (0@129-129)
471 input (2@70-129): line (7@70-99) input (2@100-129)
472 input (2@0-129): line (-1@0-69) input (2@70-129)
473 130-139: syntax error, unexpected 'y', expecting END
474 Freeing nterm input (2@0-129)
475 Freeing token 'y' (13@130-139)
480 # Syntax error caught by the parser where lookahead = END
481 # --------------------------------------------------------
482 # Load the stack and provoke an error that cannot be caught by the
483 # grammar, to check that the stack is cleared. And make sure the
484 # lookahead is freed.
489 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
490 [[sending: '(' (0@0-9)
491 sending: 'x' (1@10-19)
492 thing (1@10-19): 'x' (1@10-19)
493 sending: ')' (2@20-29)
494 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
495 sending: '(' (3@30-39)
496 sending: 'x' (4@40-49)
497 thing (4@40-49): 'x' (4@40-49)
498 sending: ')' (5@50-59)
499 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
500 sending: 'x' (6@60-69)
501 thing (6@60-69): 'x' (6@60-69)
502 sending: END (7@70-79)
503 70-79: syntax error, unexpected END, expecting 'x'
504 Freeing nterm thing (6@60-69)
505 Freeing nterm line (3@30-59)
506 Freeing nterm line (0@0-29)
507 Freeing token END (7@70-79)
512 # Check destruction upon stack overflow
513 # -------------------------------------
514 # Upon stack overflow, all symbols on the stack should be destroyed.
515 # Only check for yacc.c.
517 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
518 [[sending: '(' (0@0-9)
519 sending: 'x' (1@10-19)
520 thing (1@10-19): 'x' (1@10-19)
521 sending: ')' (2@20-29)
522 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
523 sending: '(' (3@30-39)
524 sending: 'x' (4@40-49)
525 thing (4@40-49): 'x' (4@40-49)
526 sending: ')' (5@50-59)
527 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
528 sending: '(' (6@60-69)
529 sending: 'x' (7@70-79)
530 thing (7@70-79): 'x' (7@70-79)
531 sending: ')' (8@80-89)
532 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
533 sending: '(' (9@90-99)
534 sending: 'x' (10@100-109)
535 thing (10@100-109): 'x' (10@100-109)
536 sending: ')' (11@110-119)
537 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
538 sending: '(' (12@120-129)
539 sending: 'x' (13@130-139)
540 thing (13@130-139): 'x' (13@130-139)
541 sending: ')' (14@140-149)
542 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
543 sending: '(' (15@150-159)
544 sending: 'x' (16@160-169)
545 thing (16@160-169): 'x' (16@160-169)
546 sending: ')' (17@170-179)
547 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
548 sending: '(' (18@180-189)
549 sending: 'x' (19@190-199)
550 thing (19@190-199): 'x' (19@190-199)
551 sending: ')' (20@200-209)
552 200-209: memory exhausted
553 Freeing nterm thing (19@190-199)
554 Freeing nterm line (15@150-179)
555 Freeing nterm line (12@120-149)
556 Freeing nterm line (9@90-119)
557 Freeing nterm line (6@60-89)
558 Freeing nterm line (3@30-59)
559 Freeing nterm line (0@0-29)
560 Parsing FAILED (status 2).
564 AT_BISON_OPTION_POPDEFS
565 ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR
568 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
569 # ---------------------------------------------------------------------------
570 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
571 [AT_SETUP([Printers and Destructors $2: $1])
574 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
585 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
586 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
588 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
589 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
591 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
592 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
596 ## ----------------------------------------- ##
597 ## Default tagless %printer and %destructor. ##
598 ## ----------------------------------------- ##
600 # Check that the right %printer and %destructor are called, that they're not
601 # called for $end, and that $$ and @$ work correctly.
603 AT_SETUP([Default tagless %printer and %destructor])
604 AT_BISON_OPTION_PUSHDEFS([%locations])
605 AT_DATA_GRAMMAR([[input.y]],
610 @$.first_line = @$.last_line = 1;
611 @$.first_column = @$.last_column = 1;
623 fprintf (yyoutput, "<*> printer should not be called.\n");
627 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
630 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
634 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
637 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
641 fprintf (yyoutput, "<*> destructor should not be called.\n");
646 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
650 ]AT_YYLEX_DEFINE([abcd], [[yylval = res]])[
660 AT_BISON_CHECK([-o input.c input.y])
662 AT_PARSER_CHECK([./input], 1,
663 [[<> destructor for 'd' @ 4.
664 'b'/'c' destructor for 'c' @ 3.
665 'b'/'c' destructor for 'b' @ 2.
666 <> destructor for 'a' @ 1.
670 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
671 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
673 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
674 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
676 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
677 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
679 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
680 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
682 Reading a token: Now at end of input.
683 syntax error, unexpected $end, expecting 'e'
684 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
686 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
688 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
690 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
692 Cleanup: discarding lookahead token $end (1.5-1.5: )
696 AT_BISON_OPTION_POPDEFS
701 ## ------------------------------------------------------ ##
702 ## Default tagged and per-type %printer and %destructor. ##
703 ## ------------------------------------------------------ ##
705 AT_SETUP([Default tagged and per-type %printer and %destructor])
706 AT_BISON_OPTION_PUSHDEFS
707 AT_DATA_GRAMMAR([[input.y]],
720 fprintf (yyoutput, "<> printer should not be called.\n");
723 %union { int field0; int field1; int field2; }
724 %type <field0> start 'a' 'g'
728 fprintf (yyoutput, "<*>/<field2>/e printer");
731 fprintf (stdout, "<*>/<field2>/e destructor.\n");
735 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
736 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
739 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
740 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
743 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
744 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
747 fprintf (yyoutput, "<> destructor should not be called.\n");
753 'a' 'b' 'c' 'd' 'e' 'f' 'g'
755 USE(($1, $2, $3, $4, $5, $6, $7));
762 ]AT_YYLEX_DEFINE([abcdef])[
772 AT_BISON_CHECK([-o input.c input.y])
774 AT_PARSER_CHECK([./input], 1,
775 [[<*>/<field2>/e destructor.
776 <*>/<field2>/e destructor.
780 <*>/<field2>/e destructor.
784 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
785 Shifting token 'a' (<*>/<field2>/e printer)
787 Reading a token: Next token is token 'b' (<field1> printer)
788 Shifting token 'b' (<field1> printer)
790 Reading a token: Next token is token 'c' ('c' printer)
791 Shifting token 'c' ('c' printer)
793 Reading a token: Next token is token 'd' ('d' printer)
794 Shifting token 'd' ('d' printer)
796 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
797 Shifting token 'e' (<*>/<field2>/e printer)
799 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
800 Shifting token 'f' (<*>/<field2>/e printer)
802 Reading a token: Now at end of input.
803 syntax error, unexpected $end, expecting 'g'
804 Error: popping token 'f' (<*>/<field2>/e printer)
805 Stack now 0 1 3 5 6 7
806 Error: popping token 'e' (<*>/<field2>/e printer)
808 Error: popping token 'd' ('d' printer)
810 Error: popping token 'c' ('c' printer)
812 Error: popping token 'b' (<field1> printer)
814 Error: popping token 'a' (<*>/<field2>/e printer)
816 Cleanup: discarding lookahead token $end ()
820 AT_BISON_OPTION_POPDEFS
825 ## ------------------------------------------------------------- ##
826 ## Default %printer and %destructor for user-defined end token. ##
827 ## ------------------------------------------------------------- ##
829 AT_SETUP([Default %printer and %destructor for user-defined end token])
831 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
832 # -------------------------------------------------------------
833 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
835 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
836 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
838 AT_BISON_OPTION_PUSHDEFS([%locations])
839 AT_DATA_GRAMMAR([[input]]$1[[.y]],
844 @$.first_line = @$.last_line = 1;
845 @$.first_column = @$.last_column = 1;
857 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
862 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
865 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
869 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
874 [[[%union { char tag; }
875 %type <tag> start END]]])[[
879 start: { $$ = 'S'; } ;
889 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
890 yylloc.first_line = yylloc.last_line = 1;
891 yylloc.first_column = yylloc.last_column = 1;
903 AT_BISON_OPTION_POPDEFS
905 AT_BISON_CHECK([-o input$1.c input$1.y])
906 AT_COMPILE([input$1])
907 AT_PARSER_CHECK([./input$1], 0,
908 [[<]]kind[[> for 'E' @ 1.
909 <]]kind[[> for 'S' @ 1.
913 Reducing stack by rule 1 (line 46):
914 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
917 Reading a token: Now at end of input.
918 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
921 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
922 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
926 m4_popdef([not_kind])
929 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
930 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
936 ## ------------------------------------------------------------------ ##
937 ## Default %printer and %destructor are not for error or $undefined. ##
938 ## ------------------------------------------------------------------ ##
940 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
942 # If Bison were to apply the default %printer and %destructor to the error
943 # token or to $undefined:
944 # - For the error token:
945 # - It would generate warnings for unused $n.
946 # - It would invoke the %printer and %destructor on the error token's
947 # semantic value, which would be initialized from the lookahead, which
948 # would be destroyed separately.
949 # - For $undefined, who knows what the semantic value would be.
950 AT_BISON_OPTION_PUSHDEFS
951 AT_DATA_GRAMMAR([[input.y]],
963 fprintf (yyoutput, "'%c'", $$);
966 fprintf (stderr, "DESTROY '%c'\n", $$);
973 /* In order to reveal the problems that this bug caused during parsing, add
975 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
980 ]AT_YYLEX_DEFINE([abd], [yylval = res])[
988 AT_BISON_OPTION_POPDEFS
990 AT_BISON_CHECK([-o input.c input.y])
992 AT_PARSER_CHECK([./input], [1], [],
995 Reading a token: Next token is token 'a' ('a')
996 Shifting token 'a' ('a')
998 Reading a token: Next token is token 'b' ('b')
1000 Shifting token error ()
1002 Next token is token 'b' ('b')
1003 Shifting token 'b' ('b')
1005 Reading a token: Next token is token $undefined ()
1006 Error: popping token 'b' ('b')
1009 Error: popping token error ()
1011 Shifting token error ()
1013 Next token is token $undefined ()
1014 Error: discarding token $undefined ()
1015 Error: popping token error ()
1017 Shifting token error ()
1019 Reading a token: Now at end of input.
1020 Cleanup: discarding lookahead token $end ()
1022 Cleanup: popping token error ()
1023 Cleanup: popping token 'a' ('a')
1031 ## ------------------------------------------------------ ##
1032 ## Default %printer and %destructor are not for $accept. ##
1033 ## ------------------------------------------------------ ##
1035 AT_SETUP([Default %printer and %destructor are not for $accept])
1037 # If YYSTYPE is a union and Bison were to apply the default %printer and
1038 # %destructor to $accept:
1039 # - The %printer and %destructor code generated for $accept would always be
1040 # dead code because $accept is currently never shifted onto the stack.
1041 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1042 # to declare `%type <field> $accept'. (Also true for $undefined.)
1043 # - Thus, the compiler might complain that the user code assumes the wrong
1044 # type for $$ since the code might assume the type associated with a
1045 # specific union field, which is especially reasonable in C++ since that
1046 # type may be a base type. This test case checks for this problem. (Also
1047 # true for $undefined and the error token, so there are three warnings for
1048 # %printer and three for %destructor.)
1050 AT_BISON_OPTION_PUSHDEFS
1051 AT_DATA_GRAMMAR([[input.y]],
1052 [[%debug /* So that %printer is actually compiled. */
1056 # include <stdlib.h>
1057 ]AT_YYERROR_DECLARE[
1064 fprintf (yyoutput, "'%c'", chr);
1068 fprintf (stderr, "DESTROY '%c'\n", chr);
1071 %union { char chr; }
1076 start: { USE($$); } ;
1080 ]AT_YYLEX_DEFINE([])[
1087 AT_BISON_OPTION_POPDEFS
1089 AT_BISON_CHECK([-o input.c input.y])
1096 ## ------------------------------------------------------ ##
1097 ## Default %printer and %destructor for mid-rule values. ##
1098 ## ------------------------------------------------------ ##
1100 AT_SETUP([Default %printer and %destructor for mid-rule values])
1102 AT_BISON_OPTION_PUSHDEFS
1103 AT_DATA_GRAMMAR([[input.y]],
1104 [[%debug /* So that %printer is actually compiled. */
1108 # include <stdlib.h>
1109 ]AT_YYERROR_DECLARE[
1112 # define YYLTYPE int
1113 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1114 # define YY_LOCATION_PRINT(File, Loc)
1117 %printer { fprintf (yyoutput, "%d", @$); } <>
1118 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1119 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1120 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1125 { @$ = 1; } // Not set or used.
1126 { USE ($$); @$ = 2; } // Both set and used.
1127 { USE ($$); @$ = 3; } // Only set.
1128 { @$ = 4; } // Only used.
1130 { USE (($$, $2, $4, $5)); @$ = 0; }
1135 ]AT_YYLEX_DEFINE([])[
1143 AT_BISON_OPTION_POPDEFS
1145 AT_BISON_CHECK([-o input.c input.y], 0,,
1146 [[input.y:33.3-23: warning: unset value: $$
1147 input.y:30.3-35.37: warning: unused value: $3
1151 AT_PARSER_CHECK([./input], 1,,
1154 Reducing stack by rule 1 (line 30):
1155 -> $$ = nterm $@1 (: )
1158 Reducing stack by rule 2 (line 31):
1159 -> $$ = nterm @2 (: 2)
1162 Reducing stack by rule 3 (line 32):
1163 -> $$ = nterm @3 (: 3)
1166 Reducing stack by rule 4 (line 33):
1167 -> $$ = nterm @4 (: 4)
1170 Reading a token: Now at end of input.
1172 Error: popping nterm @4 (: 4)
1175 Error: popping nterm @3 (: 3)
1178 Error: popping nterm @2 (: 2)
1181 Error: popping nterm $@1 (: )
1183 Cleanup: discarding lookahead token $end (: )
1190 ## ----------------------- ##
1191 ## @$ implies %locations. ##
1192 ## ----------------------- ##
1194 # Bison once forgot to check for @$ in actions other than semantic actions.
1196 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1197 # -------------------------------------------
1198 m4_define([AT_CHECK_ACTION_LOCATIONS],
1199 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1200 AT_BISON_OPTION_PUSHDEFS
1201 AT_DATA_GRAMMAR([[input.y]],
1204 ]AT_YYERROR_DECLARE[
1211 printf ("%d\n", @$.first_line);
1212 } ]m4_if($1, [%initial-action], [], [[start]])[
1234 AT_BISON_CHECK([[-o input.c input.y]])
1235 AT_COMPILE([[input]])
1236 AT_BISON_OPTION_POPDEFS
1239 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1240 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1241 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1244 ## ----------------------------------------------- ##
1245 ## Fix user actions without a trailing semicolon. ##
1246 ## ----------------------------------------------- ##
1248 AT_SETUP([[Fix user actions without a trailing semicolon]])
1250 # This feature is undocumented, but we accidentally broke it in 2.3a,
1251 # and there was a complaint at:
1252 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1253 AT_BISON_OPTION_PUSHDEFS
1256 start: test2 test1 test0 testc;
1259 : 'a' { semi; /* TEST:N:2 */ }
1260 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1261 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1262 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1263 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1264 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1265 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1266 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1267 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1270 : 'a' { semi; // TEST:N:1 ;
1271 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1272 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1273 } | 'd' { semi; no_semi // TEST:Y:1 ;
1274 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1275 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1276 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1277 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1278 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1281 : 'a' { semi; // TEST:N:1 {}
1282 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1283 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1284 } | 'd' { semi; no_semi // TEST:Y:1 {}
1285 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1286 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1287 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1288 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1289 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1294 #define TEST_MACRO_N \
1295 []"broken\" $ @ $$ @$ [];\
1299 #define TEST_MACRO_N \
1300 []"broken\" $ @ $$ @$ [];\
1303 AT_BISON_OPTION_POPDEFS
1305 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1306 [[input.y:8.48: warning: a ';' might be needed at the end of action code
1307 input.y:8.48: warning: future versions of Bison will not add the ';'
1308 input.y:9.48: warning: a ';' might be needed at the end of action code
1309 input.y:9.48: warning: future versions of Bison will not add the ';'
1310 input.y:10.48: warning: a ';' might be needed at the end of action code
1311 input.y:10.48: warning: future versions of Bison will not add the ';'
1312 input.y:11.48: warning: a ';' might be needed at the end of action code
1313 input.y:11.48: warning: future versions of Bison will not add the ';'
1314 input.y:12.48: warning: a ';' might be needed at the end of action code
1315 input.y:12.48: warning: future versions of Bison will not add the ';'
1316 input.y:13.48: warning: a ';' might be needed at the end of action code
1317 input.y:13.48: warning: future versions of Bison will not add the ';'
1318 input.y:20.1: warning: a ';' might be needed at the end of action code
1319 input.y:20.1: warning: future versions of Bison will not add the ';'
1320 input.y:21.1: warning: a ';' might be needed at the end of action code
1321 input.y:21.1: warning: future versions of Bison will not add the ';'
1322 input.y:22.1: warning: a ';' might be needed at the end of action code
1323 input.y:22.1: warning: future versions of Bison will not add the ';'
1324 input.y:23.1: warning: a ';' might be needed at the end of action code
1325 input.y:23.1: warning: future versions of Bison will not add the ';'
1326 input.y:24.1: warning: a ';' might be needed at the end of action code
1327 input.y:24.1: warning: future versions of Bison will not add the ';'
1328 input.y:25.1: warning: a ';' might be needed at the end of action code
1329 input.y:25.1: warning: future versions of Bison will not add the ';'
1330 input.y:31.1: warning: a ';' might be needed at the end of action code
1331 input.y:31.1: warning: future versions of Bison will not add the ';'
1332 input.y:32.1: warning: a ';' might be needed at the end of action code
1333 input.y:32.1: warning: future versions of Bison will not add the ';'
1334 input.y:33.1: warning: a ';' might be needed at the end of action code
1335 input.y:33.1: warning: future versions of Bison will not add the ';'
1336 input.y:34.1: warning: a ';' might be needed at the end of action code
1337 input.y:34.1: warning: future versions of Bison will not add the ';'
1338 input.y:35.1: warning: a ';' might be needed at the end of action code
1339 input.y:35.1: warning: future versions of Bison will not add the ';'
1340 input.y:36.1: warning: a ';' might be needed at the end of action code
1341 input.y:36.1: warning: future versions of Bison will not add the ';'
1344 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1345 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1346 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1347 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1348 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1353 ## -------------------------------------------------- ##
1354 ## Destroying lookahead assigned by semantic action. ##
1355 ## -------------------------------------------------- ##
1357 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1359 AT_BISON_OPTION_PUSHDEFS
1360 AT_DATA_GRAMMAR([input.y],
1365 ]AT_YYERROR_DECLARE[
1370 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1371 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1375 // In a previous version of Bison, yychar assigned by the semantic
1376 // action below was not translated into yytoken before the lookahead was
1377 // discarded and thus before its destructor (selected according to
1378 // yytoken) was called in order to return from yyparse. This would
1379 // happen even if YYACCEPT was performed in a later semantic action as
1380 // long as only consistent states with default reductions were visited
1381 // in between. However, we leave YYACCEPT in the same semantic action
1382 // for this test in order to show that skeletons cannot simply translate
1383 // immediately after every semantic action because a semantic action
1384 // that has set yychar might not always return normally. Instead,
1385 // skeletons must translate before every use of yytoken.
1386 start: 'a' accept { USE($1); } ;
1388 assert (yychar == YYEMPTY);
1395 ]AT_YYLEX_DEFINE([a])[
1402 AT_BISON_OPTION_POPDEFS
1403 AT_BISON_CHECK([[-o input.c input.y]])
1404 AT_COMPILE([[input]])
1405 AT_PARSER_CHECK([[./input]], [[0]], [],
1416 AT_SETUP([[YYBACKUP]])
1418 AT_BISON_OPTION_PUSHDEFS([%pure-parser])
1420 AT_DATA_GRAMMAR([input.y],
1427 # include <stdlib.h>
1428 # include <assert.h>
1430 ]AT_YYERROR_DECLARE[
1431 static int yylex (YYSTYPE *yylval);
1439 'a' { printf ("a: %d\n", $1); }
1440 | 'b' { YYBACKUP('a', 123); }
1441 | 'c' 'd' { YYBACKUP('a', 456); }
1447 yylex (YYSTYPE *yylval)
1449 static char const input[] = "bcd";
1450 static size_t toknum;
1451 assert (toknum < sizeof input);
1452 *yylval = (toknum + 1) * 10;
1453 return input[toknum++];
1459 yydebug = !!getenv("YYDEBUG");
1463 AT_BISON_OPTION_POPDEFS
1465 AT_BISON_CHECK([[-o input.c input.y]])
1466 AT_COMPILE([[input]])
1467 AT_PARSER_CHECK([[./input]], [[0]],