1 # Executing Actions. -*- Autotest -*-
3 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
4 # 2010 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 AT_BANNER([[User Actions.]])
21 ## ------------------ ##
22 ## Mid-rule actions. ##
23 ## ------------------ ##
25 AT_SETUP([Mid-rule actions])
27 # Bison once forgot the mid-rule actions. It was because the action
28 # was attached to the host rule (the one with the mid-rule action),
29 # instead of being attached to the empty rule dedicated to this
32 AT_DATA_GRAMMAR([[input.y]],
33 [[%define parse.error verbose
38 static void yyerror (const char *msg);
39 static int yylex (void);
42 exp: { putchar ('0'); }
43 '1' { putchar ('1'); }
44 '2' { putchar ('2'); }
45 '3' { putchar ('3'); }
46 '4' { putchar ('4'); }
47 '5' { putchar ('5'); }
48 '6' { putchar ('6'); }
49 '7' { putchar ('7'); }
50 '8' { putchar ('8'); }
51 '9' { putchar ('9'); }
58 static char const input[] = "123456789";
60 if (! (toknum < sizeof input))
62 return input[toknum++];
66 yyerror (const char *msg)
68 fprintf (stderr, "%s\n", msg);
78 AT_BISON_CHECK([-d -v -o input.c input.y])
80 AT_PARSER_CHECK([./input], 0,
90 ## ---------------- ##
92 ## ---------------- ##
94 AT_SETUP([Exotic Dollars])
96 AT_DATA_GRAMMAR([[input.y]],
97 [[%define parse.error verbose
102 static void yyerror (const char *msg);
103 static int yylex (void);
112 %type <val> a_1 a_2 a_5
113 sum_of_the_five_previous_values
116 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
117 sum_of_the_five_previous_values
119 USE (($1, $2, $<foo>3, $<foo>4, $5));
127 sum_of_the_five_previous_values:
129 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
144 yyerror (const char *msg)
146 fprintf (stderr, "%s\n", msg);
156 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
158 AT_PARSER_CHECK([./input], 0,
166 ## -------------------------- ##
167 ## Printers and Destructors. ##
168 ## -------------------------- ##
170 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
171 # -----------------------------------------------------------------------------
172 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
173 [# Make sure complex $n work.
174 m4_if([$1$2$3], $[1]$[2]$[3], [],
175 [m4_fatal([$0: Invalid arguments: $@])])dnl
177 # Be sure to pass all the %directives to this macro to have correct
178 # helping macros. So don't put any directly in the Bison file.
179 AT_BISON_OPTION_PUSHDEFS([$5])
180 AT_DATA_GRAMMAR([[input.y]],
187 #define YYINITDEPTH 10
188 #define YYMAXDEPTH 10
190 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
191 [#define RANGE(Location) (Location).first_line, (Location).last_line])
195 m4_ifval([$6], [%union
199 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
200 m4_ifval([$6], [[%code provides {]], [[%code {]])
201 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
202 [static int yylex (]AT_LEX_FORMALS[);
203 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
206 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
208 /* FIXME: This %printer isn't actually tested. */
211 ]AT_LALR1_CC_IF([debug_stream () << $$;],
212 [fprintf (yyoutput, "%d", $$)])[;
214 input line thing 'x' 'y'
217 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
221 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
225 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
229 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
233 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
238 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
243 This grammar is made to exercise error recovery.
244 "Lines" starting with `(' support error recovery, with
245 ')' as synchronizing token. Lines starting with 'x' can never
246 be recovered from if in error.
253 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
255 | line input /* Right recursive to load the stack so that popping at
256 END can be exercised. */
259 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
260 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
265 thing thing thing ';'
268 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
269 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
270 $3, RANGE (@3), $4, RANGE (@4));
272 | '(' thing thing ')'
275 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
276 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
277 $3, RANGE (@3), $4, RANGE (@4));
282 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
283 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
288 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
289 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
297 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
298 $$, RANGE (@$), $1, RANGE (@1));
302 /* Alias to ARGV[1]. */
303 const char *source = 0;
306 yylex (]AT_LEX_FORMALS[)
308 static unsigned int counter = 0;
310 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
311 /* As in BASIC, line numbers go from 10 to 10. */
313 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
314 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
316 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
317 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
320 if (! (0 <= c && c <= strlen (source)))
323 printf ("sending: '%c'", source[c]);
325 printf ("sending: END");
326 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
331 [/* A C++ error reporting function. */
333 yy::parser::error (const location& l, const std::string& m)
335 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
343 parser.set_debug_level (yydebug);
344 return parser.parse ();
348 yyerror (const char *msg)
350 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
354 main (int argc, const char *argv[])
357 yydebug = !!getenv ("YYDEBUG");
363 case 0: printf ("Successful parse.\n"); break;
364 case 1: printf ("Parsing FAILED.\n"); break;
365 default: printf ("Parsing FAILED (status %d).\n", status); break;
371 AT_FULL_COMPILE([input])
374 # Check the location of "empty"
375 # -----------------------------
376 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
377 # an empty "line" nterm.
378 # FIXME: This location is not satisfying. Depend on the lookahead?
379 AT_PARSER_CHECK([./input '(x)'], 0,
380 [[sending: '(' (0@0-9)
381 sending: 'x' (1@10-19)
382 thing (1@10-19): 'x' (1@10-19)
383 sending: ')' (2@20-29)
384 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
385 sending: END (3@30-39)
386 input (0@29-29): /* Nothing */
387 input (2@0-29): line (0@0-29) input (0@29-29)
388 Freeing token END (3@30-39)
389 Freeing nterm input (2@0-29)
394 # Check locations in error recovery
395 # ---------------------------------
396 # '(y)' is an error, but can be recovered from. But what's the location
397 # of the error itself ('y'), and of the resulting reduction ('(error)').
398 AT_PARSER_CHECK([./input '(y)'], 0,
399 [[sending: '(' (0@0-9)
400 sending: 'y' (1@10-19)
401 10-19: syntax error, unexpected 'y', expecting 'x'
402 Freeing token 'y' (1@10-19)
403 sending: ')' (2@20-29)
404 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
405 sending: END (3@30-39)
406 input (0@29-29): /* Nothing */
407 input (2@0-29): line (-1@0-29) input (0@29-29)
408 Freeing token END (3@30-39)
409 Freeing nterm input (2@0-29)
414 # Syntax errors caught by the parser
415 # ----------------------------------
416 # Exercise the discarding of stack top and input until `error'
419 # '(', 'x', 'x', 'x', 'x', 'x', ')',
421 # Load the stack and provoke an error that cannot be caught by the
422 # grammar, to check that the stack is cleared. And make sure the
423 # lookahead is freed.
428 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
429 [[sending: '(' (0@0-9)
430 sending: 'x' (1@10-19)
431 thing (1@10-19): 'x' (1@10-19)
432 sending: 'x' (2@20-29)
433 thing (2@20-29): 'x' (2@20-29)
434 sending: 'x' (3@30-39)
435 30-39: syntax error, unexpected 'x', expecting ')'
436 Freeing nterm thing (2@20-29)
437 Freeing nterm thing (1@10-19)
438 Freeing token 'x' (3@30-39)
439 sending: 'x' (4@40-49)
440 Freeing token 'x' (4@40-49)
441 sending: 'x' (5@50-59)
442 Freeing token 'x' (5@50-59)
443 sending: ')' (6@60-69)
444 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
445 sending: '(' (7@70-79)
446 sending: 'x' (8@80-89)
447 thing (8@80-89): 'x' (8@80-89)
448 sending: ')' (9@90-99)
449 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
450 sending: '(' (10@100-109)
451 sending: 'x' (11@110-119)
452 thing (11@110-119): 'x' (11@110-119)
453 sending: ')' (12@120-129)
454 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
455 sending: 'y' (13@130-139)
456 input (0@129-129): /* Nothing */
457 input (2@100-129): line (10@100-129) input (0@129-129)
458 input (2@70-129): line (7@70-99) input (2@100-129)
459 input (2@0-129): line (-1@0-69) input (2@70-129)
460 130-139: syntax error, unexpected 'y', expecting END
461 Freeing nterm input (2@0-129)
462 Freeing token 'y' (13@130-139)
467 # Syntax error caught by the parser where lookahead = END
468 # --------------------------------------------------------
469 # Load the stack and provoke an error that cannot be caught by the
470 # grammar, to check that the stack is cleared. And make sure the
471 # lookahead is freed.
476 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
477 [[sending: '(' (0@0-9)
478 sending: 'x' (1@10-19)
479 thing (1@10-19): 'x' (1@10-19)
480 sending: ')' (2@20-29)
481 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
482 sending: '(' (3@30-39)
483 sending: 'x' (4@40-49)
484 thing (4@40-49): 'x' (4@40-49)
485 sending: ')' (5@50-59)
486 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
487 sending: 'x' (6@60-69)
488 thing (6@60-69): 'x' (6@60-69)
489 sending: END (7@70-79)
490 70-79: syntax error, unexpected END, expecting 'x'
491 Freeing nterm thing (6@60-69)
492 Freeing nterm line (3@30-59)
493 Freeing nterm line (0@0-29)
494 Freeing token END (7@70-79)
499 # Check destruction upon stack overflow
500 # -------------------------------------
501 # Upon stack overflow, all symbols on the stack should be destroyed.
502 # Only check for yacc.c.
504 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
505 [[sending: '(' (0@0-9)
506 sending: 'x' (1@10-19)
507 thing (1@10-19): 'x' (1@10-19)
508 sending: ')' (2@20-29)
509 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
510 sending: '(' (3@30-39)
511 sending: 'x' (4@40-49)
512 thing (4@40-49): 'x' (4@40-49)
513 sending: ')' (5@50-59)
514 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
515 sending: '(' (6@60-69)
516 sending: 'x' (7@70-79)
517 thing (7@70-79): 'x' (7@70-79)
518 sending: ')' (8@80-89)
519 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
520 sending: '(' (9@90-99)
521 sending: 'x' (10@100-109)
522 thing (10@100-109): 'x' (10@100-109)
523 sending: ')' (11@110-119)
524 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
525 sending: '(' (12@120-129)
526 sending: 'x' (13@130-139)
527 thing (13@130-139): 'x' (13@130-139)
528 sending: ')' (14@140-149)
529 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
530 sending: '(' (15@150-159)
531 sending: 'x' (16@160-169)
532 thing (16@160-169): 'x' (16@160-169)
533 sending: ')' (17@170-179)
534 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
535 sending: '(' (18@180-189)
536 sending: 'x' (19@190-199)
537 thing (19@190-199): 'x' (19@190-199)
538 sending: ')' (20@200-209)
539 200-209: memory exhausted
540 Freeing nterm thing (19@190-199)
541 Freeing nterm line (15@150-179)
542 Freeing nterm line (12@120-149)
543 Freeing nterm line (9@90-119)
544 Freeing nterm line (6@60-89)
545 Freeing nterm line (3@30-59)
546 Freeing nterm line (0@0-29)
547 Parsing FAILED (status 2).
554 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
555 # ---------------------------------------------------------------------------
556 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
557 [AT_SETUP([Printers and Destructors $2: $1])
560 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
561 [%define parse.error verbose
571 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
572 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
574 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
575 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
577 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
578 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
582 ## ----------------------------------------- ##
583 ## Default tagless %printer and %destructor. ##
584 ## ----------------------------------------- ##
586 # Check that the right %printer and %destructor are called, that they're not
587 # called for $end, and that $$ and @$ work correctly.
589 AT_SETUP([Default tagless %printer and %destructor])
591 AT_DATA_GRAMMAR([[input.y]],
592 [[%define parse.error verbose
596 @$.first_line = @$.last_line = 1;
597 @$.first_column = @$.last_column = 1;
603 static void yyerror (const char *msg);
604 static int yylex (void);
609 fprintf (yyoutput, "<*> printer should not be called.\n");
613 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
616 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
620 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
623 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
627 fprintf (yyoutput, "<*> destructor should not be called.\n");
632 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
639 static char const input[] = "abcd";
640 static size_t toknum;
641 if (! (toknum < sizeof input))
643 yylval = input[toknum++];
644 yylloc.first_line = yylloc.last_line = 1;
645 yylloc.first_column = yylloc.last_column = toknum;
650 yyerror (const char *msg)
652 fprintf (stderr, "%s\n", msg);
663 AT_BISON_CHECK([-o input.c input.y])
665 AT_PARSER_CHECK([./input], 1,
666 [[<> destructor for 'd' @ 4.
667 'b'/'c' destructor for 'c' @ 3.
668 'b'/'c' destructor for 'b' @ 2.
669 <> destructor for 'a' @ 1.
673 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
674 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
676 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
677 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
679 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
680 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
682 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
683 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
685 Reading a token: Now at end of input.
686 syntax error, unexpected $end, expecting 'e'
687 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
689 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
691 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
693 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
695 Cleanup: discarding lookahead token $end (1.5-1.5: )
703 ## ------------------------------------------------------ ##
704 ## Default tagged and per-type %printer and %destructor. ##
705 ## ------------------------------------------------------ ##
707 AT_SETUP([Default tagged and per-type %printer and %destructor])
709 AT_DATA_GRAMMAR([[input.y]],
710 [[%define parse.error verbose
716 static void yyerror (const char *msg);
717 static int yylex (void);
722 fprintf (yyoutput, "<> printer should not be called.\n");
725 %union { int field0; int field1; int field2; }
726 %type <field0> start 'a' 'g'
730 fprintf (yyoutput, "<*>/<field2>/e printer");
733 fprintf (stdout, "<*>/<field2>/e destructor.\n");
737 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
738 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
741 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
742 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
745 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
746 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
749 fprintf (yyoutput, "<> destructor should not be called.\n");
755 'a' 'b' 'c' 'd' 'e' 'f' 'g'
757 USE(($1, $2, $3, $4, $5, $6, $7));
767 static char const input[] = "abcdef";
768 static size_t toknum;
769 if (! (toknum < sizeof input))
771 return input[toknum++];
775 yyerror (const char *msg)
777 fprintf (stderr, "%s\n", msg);
788 AT_BISON_CHECK([-o input.c input.y])
790 AT_PARSER_CHECK([./input], 1,
791 [[<*>/<field2>/e destructor.
792 <*>/<field2>/e destructor.
796 <*>/<field2>/e destructor.
800 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
801 Shifting token 'a' (<*>/<field2>/e printer)
803 Reading a token: Next token is token 'b' (<field1> printer)
804 Shifting token 'b' (<field1> printer)
806 Reading a token: Next token is token 'c' ('c' printer)
807 Shifting token 'c' ('c' printer)
809 Reading a token: Next token is token 'd' ('d' printer)
810 Shifting token 'd' ('d' printer)
812 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
813 Shifting token 'e' (<*>/<field2>/e printer)
815 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
816 Shifting token 'f' (<*>/<field2>/e printer)
818 Reading a token: Now at end of input.
819 syntax error, unexpected $end, expecting 'g'
820 Error: popping token 'f' (<*>/<field2>/e printer)
821 Stack now 0 1 3 5 6 7
822 Error: popping token 'e' (<*>/<field2>/e printer)
824 Error: popping token 'd' ('d' printer)
826 Error: popping token 'c' ('c' printer)
828 Error: popping token 'b' (<field1> printer)
830 Error: popping token 'a' (<*>/<field2>/e printer)
832 Cleanup: discarding lookahead token $end ()
840 ## ------------------------------------------------------------- ##
841 ## Default %printer and %destructor for user-defined end token. ##
842 ## ------------------------------------------------------------- ##
844 AT_SETUP([Default %printer and %destructor for user-defined end token])
846 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
847 # -----------------------------------------------------------------------------
848 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
850 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
851 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
853 AT_DATA_GRAMMAR([[input]]$1[[.y]],
854 [[%define parse.error verbose
858 @$.first_line = @$.last_line = 1;
859 @$.first_column = @$.last_column = 1;
865 static void yyerror (const char *msg);
866 static int yylex (void);
871 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
876 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
879 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
883 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
888 [[[%union { char tag; }
889 %type <tag> start END]]])[[
893 start: { $$ = 'S'; } ;
903 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
904 yylloc.first_line = yylloc.last_line = 1;
905 yylloc.first_column = yylloc.last_column = 1;
910 yyerror (const char *msg)
912 fprintf (stderr, "%s\n", msg);
923 AT_BISON_CHECK([-o input$1.c input$1.y])
924 AT_COMPILE([input$1])
925 AT_PARSER_CHECK([./input$1], 0,
926 [[<]]kind[[> for 'E' @ 1.
927 <]]kind[[> for 'S' @ 1.
931 Reducing stack by rule 1 (line 46):
932 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
935 Reading a token: Now at end of input.
936 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
939 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
940 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
944 m4_popdef([not_kind])
947 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
948 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
954 ## ------------------------------------------------------------------ ##
955 ## Default %printer and %destructor are not for error or $undefined. ##
956 ## ------------------------------------------------------------------ ##
958 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
960 # If Bison were to apply the default %printer and %destructor to the error
961 # token or to $undefined:
962 # - For the error token:
963 # - It would generate warnings for unused $n.
964 # - It would invoke the %printer and %destructor on the error token's
965 # semantic value, which would be initialized from the lookahead, which
966 # would be destroyed separately.
967 # - For $undefined, who knows what the semantic value would be.
969 AT_DATA_GRAMMAR([[input.y]],
975 static void yyerror (const char *msg);
976 static int yylex (void);
981 fprintf (yyoutput, "'%c'", $$);
984 fprintf (stderr, "DESTROY '%c'\n", $$);
991 /* In order to reveal the problems that this bug caused during parsing, add
993 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1001 static char const input[] = "abd";
1002 static size_t toknum;
1003 if (! (toknum < sizeof input))
1005 yylval = input[toknum++];
1010 yyerror (const char *msg)
1012 fprintf (stderr, "%s\n", msg);
1023 AT_BISON_CHECK([-o input.c input.y])
1025 AT_PARSER_CHECK([./input], [1], [],
1028 Reading a token: Next token is token 'a' ('a')
1029 Shifting token 'a' ('a')
1031 Reading a token: Next token is token 'b' ('b')
1033 Shifting token error ()
1035 Next token is token 'b' ('b')
1036 Shifting token 'b' ('b')
1038 Reading a token: Next token is token $undefined ()
1039 Error: popping token 'b' ('b')
1042 Error: popping token error ()
1044 Shifting token error ()
1046 Next token is token $undefined ()
1047 Error: discarding token $undefined ()
1048 Error: popping token error ()
1050 Shifting token error ()
1052 Reading a token: Now at end of input.
1053 Cleanup: discarding lookahead token $end ()
1055 Cleanup: popping token error ()
1056 Cleanup: popping token 'a' ('a')
1064 ## ------------------------------------------------------ ##
1065 ## Default %printer and %destructor are not for $accept. ##
1066 ## ------------------------------------------------------ ##
1068 AT_SETUP([Default %printer and %destructor are not for $accept])
1070 # If YYSTYPE is a union and Bison were to apply the default %printer and
1071 # %destructor to $accept:
1072 # - The %printer and %destructor code generated for $accept would always be
1073 # dead code because $accept is currently never shifted onto the stack.
1074 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1075 # to declare `%type <field> $accept'. (Also true for $undefined.)
1076 # - Thus, the compiler might complain that the user code assumes the wrong
1077 # type for $$ since the code might assume the type associated with a
1078 # specific union field, which is especially reasonable in C++ since that
1079 # type may be a base type. This test case checks for this problem. (Also
1080 # true for $undefined and the error token, so there are three warnings for
1081 # %printer and three for %destructor.)
1083 AT_DATA_GRAMMAR([[input.y]],
1084 [[%debug /* So that %printer is actually compiled. */
1088 # include <stdlib.h>
1089 static void yyerror (const char *msg);
1090 static int yylex (void);
1096 fprintf (yyoutput, "'%c'", chr);
1100 fprintf (stderr, "DESTROY '%c'\n", chr);
1103 %union { char chr; }
1108 start: { USE($$); } ;
1122 yyerror (const char *msg)
1124 fprintf (stderr, "%s\n", msg);
1134 AT_BISON_CHECK([-o input.c input.y])
1141 ## ------------------------------------------------------ ##
1142 ## Default %printer and %destructor for mid-rule values. ##
1143 ## ------------------------------------------------------ ##
1145 AT_SETUP([Default %printer and %destructor for mid-rule values])
1147 AT_DATA_GRAMMAR([[input.y]],
1148 [[%debug /* So that %printer is actually compiled. */
1152 # include <stdlib.h>
1153 static void yyerror (const char *msg);
1154 static int yylex (void);
1156 # define YYLTYPE int
1157 # define YYLLOC_DEFAULT(Current, Rhs, N)
1158 # define YY_LOCATION_PRINT(File, Loc)
1161 %printer { fprintf (yyoutput, "%d", @$); } <>
1162 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1163 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1164 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1169 { @$ = 1; } // Not set or used.
1170 { USE ($$); @$ = 2; } // Both set and used.
1171 { USE ($$); @$ = 3; } // Only set.
1172 { @$ = 4; } // Only used.
1174 { USE (($$, $2, $4, $5)); @$ = 0; }
1189 yyerror (const char *msg)
1191 fprintf (stderr, "%s\n", msg);
1202 AT_BISON_CHECK([-o input.c input.y], 0,,
1203 [[input.y:33.3-23: warning: unset value: $$
1204 input.y:30.3-35.37: warning: unused value: $3
1208 AT_PARSER_CHECK([./input], 1,,
1211 Reducing stack by rule 1 (line 30):
1212 -> $$ = nterm $@1 (: )
1215 Reducing stack by rule 2 (line 31):
1216 -> $$ = nterm @2 (: 2)
1219 Reducing stack by rule 3 (line 32):
1220 -> $$ = nterm @3 (: 3)
1223 Reducing stack by rule 4 (line 33):
1224 -> $$ = nterm @4 (: 4)
1227 Reading a token: Now at end of input.
1229 Error: popping nterm @4 (: 4)
1232 Error: popping nterm @3 (: 3)
1235 Error: popping nterm @2 (: 2)
1238 Error: popping nterm $@1 (: )
1240 Cleanup: discarding lookahead token $end (: )
1247 ## ----------------------- ##
1248 ## @$ implies %locations. ##
1249 ## ----------------------- ##
1251 # Bison once forgot to check for @$ in actions other than semantic actions.
1253 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1254 # -------------------------------------------------------
1255 m4_define([AT_CHECK_ACTION_LOCATIONS],
1256 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1258 AT_DATA_GRAMMAR([[input.y]],
1261 static int yylex (void);
1262 static void yyerror (char const *msg);
1268 printf ("%d\n", @$.first_line);
1269 } ]m4_if($1, [%initial-action], [], [[start]])[
1284 yyerror (char const *msg)
1286 fprintf (stderr, "%s\n", msg);
1296 AT_BISON_CHECK([[-o input.c input.y]])
1297 AT_COMPILE([[input]])
1301 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1302 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1303 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1306 ## ----------------------------------------------- ##
1307 ## Fix user actions without a trailing semicolon. ##
1308 ## ----------------------------------------------- ##
1310 AT_SETUP([[Fix user actions without a trailing semicolon]])
1312 # This feature is undocumented, but we accidentally broke it in 2.3a,
1313 # and there was a complaint at:
1314 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1318 start: test2 test1 test0 testc;
1321 : 'a' { semi; /* TEST:N:2 */ }
1322 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1323 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1324 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1325 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1326 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1327 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1328 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1329 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1332 : 'a' { semi; // TEST:N:1 ;
1333 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1334 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1335 } | 'd' { semi; no_semi // TEST:Y:1 ;
1336 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1337 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1338 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1339 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1340 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1343 : 'a' { semi; // TEST:N:1 {}
1344 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1345 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1346 } | 'd' { semi; no_semi // TEST:Y:1 {}
1347 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1348 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1349 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1350 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1351 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1356 #define TEST_MACRO_N \
1357 []"broken\" $ @ $$ @$ [];\
1361 #define TEST_MACRO_N \
1362 []"broken\" $ @ $$ @$ [];\
1366 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1367 [[input.y:8.48: warning: a `;' might be needed at the end of action code
1368 input.y:8.48: warning: future versions of Bison will not add the `;'
1369 input.y:9.48: warning: a `;' might be needed at the end of action code
1370 input.y:9.48: warning: future versions of Bison will not add the `;'
1371 input.y:10.48: warning: a `;' might be needed at the end of action code
1372 input.y:10.48: warning: future versions of Bison will not add the `;'
1373 input.y:11.48: warning: a `;' might be needed at the end of action code
1374 input.y:11.48: warning: future versions of Bison will not add the `;'
1375 input.y:12.48: warning: a `;' might be needed at the end of action code
1376 input.y:12.48: warning: future versions of Bison will not add the `;'
1377 input.y:13.48: warning: a `;' might be needed at the end of action code
1378 input.y:13.48: warning: future versions of Bison will not add the `;'
1379 input.y:20.1: warning: a `;' might be needed at the end of action code
1380 input.y:20.1: warning: future versions of Bison will not add the `;'
1381 input.y:21.1: warning: a `;' might be needed at the end of action code
1382 input.y:21.1: warning: future versions of Bison will not add the `;'
1383 input.y:22.1: warning: a `;' might be needed at the end of action code
1384 input.y:22.1: warning: future versions of Bison will not add the `;'
1385 input.y:23.1: warning: a `;' might be needed at the end of action code
1386 input.y:23.1: warning: future versions of Bison will not add the `;'
1387 input.y:24.1: warning: a `;' might be needed at the end of action code
1388 input.y:24.1: warning: future versions of Bison will not add the `;'
1389 input.y:25.1: warning: a `;' might be needed at the end of action code
1390 input.y:25.1: warning: future versions of Bison will not add the `;'
1391 input.y:31.1: warning: a `;' might be needed at the end of action code
1392 input.y:31.1: warning: future versions of Bison will not add the `;'
1393 input.y:32.1: warning: a `;' might be needed at the end of action code
1394 input.y:32.1: warning: future versions of Bison will not add the `;'
1395 input.y:33.1: warning: a `;' might be needed at the end of action code
1396 input.y:33.1: warning: future versions of Bison will not add the `;'
1397 input.y:34.1: warning: a `;' might be needed at the end of action code
1398 input.y:34.1: warning: future versions of Bison will not add the `;'
1399 input.y:35.1: warning: a `;' might be needed at the end of action code
1400 input.y:35.1: warning: future versions of Bison will not add the `;'
1401 input.y:36.1: warning: a `;' might be needed at the end of action code
1402 input.y:36.1: warning: future versions of Bison will not add the `;'
1405 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1406 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1407 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1408 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1409 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1414 ## -------------------------------------------------- ##
1415 ## Destroying lookahead assigned by semantic action. ##
1416 ## -------------------------------------------------- ##
1418 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1420 AT_DATA_GRAMMAR([input.y],
1425 static void yyerror (char const *);
1426 static int yylex (void);
1430 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1431 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1435 // In a previous version of Bison, yychar assigned by the semantic
1436 // action below was not translated into yytoken before the lookahead was
1437 // discarded and thus before its destructor (selected according to
1438 // yytoken) was called in order to return from yyparse. This would
1439 // happen even if YYACCEPT was performed in a later semantic action as
1440 // long as only consistent states with default reductions were visited
1441 // in between. However, we leave YYACCEPT in the same semantic action
1442 // for this test in order to show that skeletons cannot simply translate
1443 // immediately after every semantic action because a semantic action
1444 // that has set yychar might not always return normally. Instead,
1445 // skeletons must translate before every use of yytoken.
1446 start: 'a' accept { USE($1); } ;
1448 assert (yychar == YYEMPTY);
1456 yyerror (char const *msg)
1458 fprintf (stderr, "%s\n", msg);
1464 static char const *input = "a";
1475 AT_BISON_CHECK([[-o input.c input.y]])
1476 AT_COMPILE([[input]])
1477 AT_PARSER_CHECK([[./input]], [[0]], [],