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_DATA_GRAMMAR([[input.y]],
37 static void yyerror (const char *msg);
38 static int yylex (void);
41 exp: { putchar ('0'); }
42 '1' { putchar ('1'); }
43 '2' { putchar ('2'); }
44 '3' { putchar ('3'); }
45 '4' { putchar ('4'); }
46 '5' { putchar ('5'); }
47 '6' { putchar ('6'); }
48 '7' { putchar ('7'); }
49 '8' { putchar ('8'); }
50 '9' { putchar ('9'); }
57 static char const input[] = "123456789";
59 if (! (toknum < sizeof input))
61 return input[toknum++];
65 yyerror (const char *msg)
67 fprintf (stderr, "%s\n", msg);
77 AT_BISON_CHECK([-d -v -o input.c input.y])
79 AT_PARSER_CHECK([./input], 0,
89 ## ---------------- ##
91 ## ---------------- ##
93 AT_SETUP([Exotic Dollars])
95 AT_DATA_GRAMMAR([[input.y]],
101 static void yyerror (const char *msg);
102 static int yylex (void);
111 %type <val> a_1 a_2 a_5
112 sum_of_the_five_previous_values
115 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
116 sum_of_the_five_previous_values
118 USE (($1, $2, $<foo>3, $<foo>4, $5));
126 sum_of_the_five_previous_values:
128 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
143 yyerror (const char *msg)
145 fprintf (stderr, "%s\n", msg);
155 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
157 AT_PARSER_CHECK([./input], 0,
161 # Make sure that fields after $n or $-n are parsed correctly. At one
162 # point while implementing dashes in symbol names, we were dropping
164 AT_DATA_GRAMMAR([[input.y]],
168 static int yylex (void);
169 static void yyerror (char const *msg);
170 typedef struct { int val; } stype;
171 # define YYSTYPE stype
175 start: one two { $$.val = $1.val + $2.val; } sum ;
176 one: { $$.val = 1; } ;
177 two: { $$.val = 2; } ;
178 sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ;
189 yyerror (char const *msg)
191 fprintf (stderr, "%s\n", msg);
201 AT_BISON_CHECK([[-o input.c input.y]])
202 AT_COMPILE([[input]])
203 AT_PARSER_CHECK([[./input]], [[0]],
211 ## -------------------------- ##
212 ## Printers and Destructors. ##
213 ## -------------------------- ##
215 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
216 # -----------------------------------------------------------------------------
217 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
218 [# Make sure complex $n work.
219 m4_if([$1$2$3], $[1]$[2]$[3], [],
220 [m4_fatal([$0: Invalid arguments: $@])])dnl
222 # Be sure to pass all the %directives to this macro to have correct
223 # helping macros. So don't put any directly in the Bison file.
224 AT_BISON_OPTION_PUSHDEFS([$5])
225 AT_DATA_GRAMMAR([[input.y]],
232 #define YYINITDEPTH 10
233 #define YYMAXDEPTH 10
235 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
236 [#define RANGE(Location) (Location).first_line, (Location).last_line])
240 m4_ifval([$6], [%union
244 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
245 m4_ifval([$6], [[%code provides {]], [[%code {]])
246 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
247 [static int yylex (]AT_LEX_FORMALS[);
248 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
251 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
253 /* FIXME: This %printer isn't actually tested. */
256 ]AT_LALR1_CC_IF([debug_stream () << $$;],
257 [fprintf (yyoutput, "%d", $$)])[;
259 input line thing 'x' 'y'
262 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
266 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
270 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
274 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
278 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
283 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
288 This grammar is made to exercise error recovery.
289 "Lines" starting with `(' support error recovery, with
290 ')' as synchronizing token. Lines starting with 'x' can never
291 be recovered from if in error.
298 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
300 | line input /* Right recursive to load the stack so that popping at
301 END can be exercised. */
304 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
305 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
310 thing thing thing ';'
313 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
314 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
315 $3, RANGE (@3), $4, RANGE (@4));
317 | '(' thing thing ')'
320 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
321 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
322 $3, RANGE (@3), $4, RANGE (@4));
327 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
328 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
333 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
334 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
342 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
343 $$, RANGE (@$), $1, RANGE (@1));
347 /* Alias to ARGV[1]. */
348 const char *source = 0;
351 yylex (]AT_LEX_FORMALS[)
353 static unsigned int counter = 0;
355 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
356 /* As in BASIC, line numbers go from 10 to 10. */
358 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
359 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
361 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
362 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
365 if (! (0 <= c && c <= strlen (source)))
368 printf ("sending: '%c'", source[c]);
370 printf ("sending: END");
371 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
376 [/* A C++ error reporting function. */
378 yy::parser::error (const location& l, const std::string& m)
380 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
388 parser.set_debug_level (yydebug);
389 return parser.parse ();
393 yyerror (const char *msg)
395 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
399 main (int argc, const char *argv[])
402 yydebug = !!getenv ("YYDEBUG");
408 case 0: printf ("Successful parse.\n"); break;
409 case 1: printf ("Parsing FAILED.\n"); break;
410 default: printf ("Parsing FAILED (status %d).\n", status); break;
416 AT_FULL_COMPILE([input])
419 # Check the location of "empty"
420 # -----------------------------
421 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
422 # an empty "line" nterm.
423 # FIXME: This location is not satisfying. Depend on the lookahead?
424 AT_PARSER_CHECK([./input '(x)'], 0,
425 [[sending: '(' (0@0-9)
426 sending: 'x' (1@10-19)
427 thing (1@10-19): 'x' (1@10-19)
428 sending: ')' (2@20-29)
429 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
430 sending: END (3@30-39)
431 input (0@29-29): /* Nothing */
432 input (2@0-29): line (0@0-29) input (0@29-29)
433 Freeing token END (3@30-39)
434 Freeing nterm input (2@0-29)
439 # Check locations in error recovery
440 # ---------------------------------
441 # '(y)' is an error, but can be recovered from. But what's the location
442 # of the error itself ('y'), and of the resulting reduction ('(error)').
443 AT_PARSER_CHECK([./input '(y)'], 0,
444 [[sending: '(' (0@0-9)
445 sending: 'y' (1@10-19)
446 10-19: syntax error, unexpected 'y', expecting 'x'
447 Freeing token 'y' (1@10-19)
448 sending: ')' (2@20-29)
449 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
450 sending: END (3@30-39)
451 input (0@29-29): /* Nothing */
452 input (2@0-29): line (-1@0-29) input (0@29-29)
453 Freeing token END (3@30-39)
454 Freeing nterm input (2@0-29)
459 # Syntax errors caught by the parser
460 # ----------------------------------
461 # Exercise the discarding of stack top and input until `error'
464 # '(', 'x', 'x', 'x', 'x', 'x', ')',
466 # Load the stack and provoke an error that cannot be caught by the
467 # grammar, to check that the stack is cleared. And make sure the
468 # lookahead is freed.
473 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
474 [[sending: '(' (0@0-9)
475 sending: 'x' (1@10-19)
476 thing (1@10-19): 'x' (1@10-19)
477 sending: 'x' (2@20-29)
478 thing (2@20-29): 'x' (2@20-29)
479 sending: 'x' (3@30-39)
480 30-39: syntax error, unexpected 'x', expecting ')'
481 Freeing nterm thing (2@20-29)
482 Freeing nterm thing (1@10-19)
483 Freeing token 'x' (3@30-39)
484 sending: 'x' (4@40-49)
485 Freeing token 'x' (4@40-49)
486 sending: 'x' (5@50-59)
487 Freeing token 'x' (5@50-59)
488 sending: ')' (6@60-69)
489 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
490 sending: '(' (7@70-79)
491 sending: 'x' (8@80-89)
492 thing (8@80-89): 'x' (8@80-89)
493 sending: ')' (9@90-99)
494 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
495 sending: '(' (10@100-109)
496 sending: 'x' (11@110-119)
497 thing (11@110-119): 'x' (11@110-119)
498 sending: ')' (12@120-129)
499 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
500 sending: 'y' (13@130-139)
501 input (0@129-129): /* Nothing */
502 input (2@100-129): line (10@100-129) input (0@129-129)
503 input (2@70-129): line (7@70-99) input (2@100-129)
504 input (2@0-129): line (-1@0-69) input (2@70-129)
505 130-139: syntax error, unexpected 'y', expecting END
506 Freeing nterm input (2@0-129)
507 Freeing token 'y' (13@130-139)
512 # Syntax error caught by the parser where lookahead = END
513 # --------------------------------------------------------
514 # Load the stack and provoke an error that cannot be caught by the
515 # grammar, to check that the stack is cleared. And make sure the
516 # lookahead is freed.
521 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
522 [[sending: '(' (0@0-9)
523 sending: 'x' (1@10-19)
524 thing (1@10-19): 'x' (1@10-19)
525 sending: ')' (2@20-29)
526 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
527 sending: '(' (3@30-39)
528 sending: 'x' (4@40-49)
529 thing (4@40-49): 'x' (4@40-49)
530 sending: ')' (5@50-59)
531 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
532 sending: 'x' (6@60-69)
533 thing (6@60-69): 'x' (6@60-69)
534 sending: END (7@70-79)
535 70-79: syntax error, unexpected END, expecting 'x'
536 Freeing nterm thing (6@60-69)
537 Freeing nterm line (3@30-59)
538 Freeing nterm line (0@0-29)
539 Freeing token END (7@70-79)
544 # Check destruction upon stack overflow
545 # -------------------------------------
546 # Upon stack overflow, all symbols on the stack should be destroyed.
547 # Only check for yacc.c.
549 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
550 [[sending: '(' (0@0-9)
551 sending: 'x' (1@10-19)
552 thing (1@10-19): 'x' (1@10-19)
553 sending: ')' (2@20-29)
554 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
555 sending: '(' (3@30-39)
556 sending: 'x' (4@40-49)
557 thing (4@40-49): 'x' (4@40-49)
558 sending: ')' (5@50-59)
559 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
560 sending: '(' (6@60-69)
561 sending: 'x' (7@70-79)
562 thing (7@70-79): 'x' (7@70-79)
563 sending: ')' (8@80-89)
564 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
565 sending: '(' (9@90-99)
566 sending: 'x' (10@100-109)
567 thing (10@100-109): 'x' (10@100-109)
568 sending: ')' (11@110-119)
569 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
570 sending: '(' (12@120-129)
571 sending: 'x' (13@130-139)
572 thing (13@130-139): 'x' (13@130-139)
573 sending: ')' (14@140-149)
574 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
575 sending: '(' (15@150-159)
576 sending: 'x' (16@160-169)
577 thing (16@160-169): 'x' (16@160-169)
578 sending: ')' (17@170-179)
579 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
580 sending: '(' (18@180-189)
581 sending: 'x' (19@190-199)
582 thing (19@190-199): 'x' (19@190-199)
583 sending: ')' (20@200-209)
584 200-209: memory exhausted
585 Freeing nterm thing (19@190-199)
586 Freeing nterm line (15@150-179)
587 Freeing nterm line (12@120-149)
588 Freeing nterm line (9@90-119)
589 Freeing nterm line (6@60-89)
590 Freeing nterm line (3@30-59)
591 Freeing nterm line (0@0-29)
592 Parsing FAILED (status 2).
599 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
600 # ---------------------------------------------------------------------------
601 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
602 [AT_SETUP([Printers and Destructors $2: $1])
605 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
616 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
617 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
619 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
620 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
622 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
623 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
627 ## ----------------------------------------- ##
628 ## Default tagless %printer and %destructor. ##
629 ## ----------------------------------------- ##
631 # Check that the right %printer and %destructor are called, that they're not
632 # called for $end, and that $$ and @$ work correctly.
634 AT_SETUP([Default tagless %printer and %destructor])
636 AT_DATA_GRAMMAR([[input.y]],
641 @$.first_line = @$.last_line = 1;
642 @$.first_column = @$.last_column = 1;
648 static void yyerror (const char *msg);
649 static int yylex (void);
654 fprintf (yyoutput, "<*> printer should not be called.\n");
658 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
661 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
665 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
668 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
672 fprintf (yyoutput, "<*> destructor should not be called.\n");
677 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
684 static char const input[] = "abcd";
685 static size_t toknum;
686 if (! (toknum < sizeof input))
688 yylval = input[toknum++];
689 yylloc.first_line = yylloc.last_line = 1;
690 yylloc.first_column = yylloc.last_column = toknum;
695 yyerror (const char *msg)
697 fprintf (stderr, "%s\n", msg);
708 AT_BISON_CHECK([-o input.c input.y])
710 AT_PARSER_CHECK([./input], 1,
711 [[<> destructor for 'd' @ 4.
712 'b'/'c' destructor for 'c' @ 3.
713 'b'/'c' destructor for 'b' @ 2.
714 <> destructor for 'a' @ 1.
718 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
719 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
721 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
722 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
724 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
725 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
727 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
728 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
730 Reading a token: Now at end of input.
731 syntax error, unexpected $end, expecting 'e'
732 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
734 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
736 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
738 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
740 Cleanup: discarding lookahead token $end (1.5-1.5: )
748 ## ------------------------------------------------------ ##
749 ## Default tagged and per-type %printer and %destructor. ##
750 ## ------------------------------------------------------ ##
752 AT_SETUP([Default tagged and per-type %printer and %destructor])
754 AT_DATA_GRAMMAR([[input.y]],
761 static void yyerror (const char *msg);
762 static int yylex (void);
767 fprintf (yyoutput, "<> printer should not be called.\n");
770 %union { int field0; int field1; int field2; }
771 %type <field0> start 'a' 'g'
775 fprintf (yyoutput, "<*>/<field2>/e printer");
778 fprintf (stdout, "<*>/<field2>/e destructor.\n");
782 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
783 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
786 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
787 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
790 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
791 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
794 fprintf (yyoutput, "<> destructor should not be called.\n");
800 'a' 'b' 'c' 'd' 'e' 'f' 'g'
802 USE(($1, $2, $3, $4, $5, $6, $7));
812 static char const input[] = "abcdef";
813 static size_t toknum;
814 if (! (toknum < sizeof input))
816 return input[toknum++];
820 yyerror (const char *msg)
822 fprintf (stderr, "%s\n", msg);
833 AT_BISON_CHECK([-o input.c input.y])
835 AT_PARSER_CHECK([./input], 1,
836 [[<*>/<field2>/e destructor.
837 <*>/<field2>/e destructor.
841 <*>/<field2>/e destructor.
845 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
846 Shifting token 'a' (<*>/<field2>/e printer)
848 Reading a token: Next token is token 'b' (<field1> printer)
849 Shifting token 'b' (<field1> printer)
851 Reading a token: Next token is token 'c' ('c' printer)
852 Shifting token 'c' ('c' printer)
854 Reading a token: Next token is token 'd' ('d' printer)
855 Shifting token 'd' ('d' printer)
857 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
858 Shifting token 'e' (<*>/<field2>/e printer)
860 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
861 Shifting token 'f' (<*>/<field2>/e printer)
863 Reading a token: Now at end of input.
864 syntax error, unexpected $end, expecting 'g'
865 Error: popping token 'f' (<*>/<field2>/e printer)
866 Stack now 0 1 3 5 6 7
867 Error: popping token 'e' (<*>/<field2>/e printer)
869 Error: popping token 'd' ('d' printer)
871 Error: popping token 'c' ('c' printer)
873 Error: popping token 'b' (<field1> printer)
875 Error: popping token 'a' (<*>/<field2>/e printer)
877 Cleanup: discarding lookahead token $end ()
885 ## ------------------------------------------------------------- ##
886 ## Default %printer and %destructor for user-defined end token. ##
887 ## ------------------------------------------------------------- ##
889 AT_SETUP([Default %printer and %destructor for user-defined end token])
891 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
892 # -----------------------------------------------------------------------------
893 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
895 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
896 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
898 AT_DATA_GRAMMAR([[input]]$1[[.y]],
903 @$.first_line = @$.last_line = 1;
904 @$.first_column = @$.last_column = 1;
910 static void yyerror (const char *msg);
911 static int yylex (void);
916 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
921 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
924 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
928 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
933 [[[%union { char tag; }
934 %type <tag> start END]]])[[
938 start: { $$ = 'S'; } ;
948 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
949 yylloc.first_line = yylloc.last_line = 1;
950 yylloc.first_column = yylloc.last_column = 1;
955 yyerror (const char *msg)
957 fprintf (stderr, "%s\n", msg);
968 AT_BISON_CHECK([-o input$1.c input$1.y])
969 AT_COMPILE([input$1])
970 AT_PARSER_CHECK([./input$1], 0,
971 [[<]]kind[[> for 'E' @ 1.
972 <]]kind[[> for 'S' @ 1.
976 Reducing stack by rule 1 (line 46):
977 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
980 Reading a token: Now at end of input.
981 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
984 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
985 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
989 m4_popdef([not_kind])
992 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
993 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
999 ## ------------------------------------------------------------------ ##
1000 ## Default %printer and %destructor are not for error or $undefined. ##
1001 ## ------------------------------------------------------------------ ##
1003 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
1005 # If Bison were to apply the default %printer and %destructor to the error
1006 # token or to $undefined:
1007 # - For the error token:
1008 # - It would generate warnings for unused $n.
1009 # - It would invoke the %printer and %destructor on the error token's
1010 # semantic value, which would be initialized from the lookahead, which
1011 # would be destroyed separately.
1012 # - For $undefined, who knows what the semantic value would be.
1014 AT_DATA_GRAMMAR([[input.y]],
1019 # include <stdlib.h>
1020 static void yyerror (const char *msg);
1021 static int yylex (void);
1026 fprintf (yyoutput, "'%c'", $$);
1029 fprintf (stderr, "DESTROY '%c'\n", $$);
1036 /* In order to reveal the problems that this bug caused during parsing, add
1038 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1046 static char const input[] = "abd";
1047 static size_t toknum;
1048 if (! (toknum < sizeof input))
1050 yylval = input[toknum++];
1055 yyerror (const char *msg)
1057 fprintf (stderr, "%s\n", msg);
1068 AT_BISON_CHECK([-o input.c input.y])
1070 AT_PARSER_CHECK([./input], [1], [],
1073 Reading a token: Next token is token 'a' ('a')
1074 Shifting token 'a' ('a')
1076 Reading a token: Next token is token 'b' ('b')
1078 Shifting token error ()
1080 Next token is token 'b' ('b')
1081 Shifting token 'b' ('b')
1083 Reading a token: Next token is token $undefined ()
1084 Error: popping token 'b' ('b')
1087 Error: popping token error ()
1089 Shifting token error ()
1091 Next token is token $undefined ()
1092 Error: discarding token $undefined ()
1093 Error: popping token error ()
1095 Shifting token error ()
1097 Reading a token: Now at end of input.
1098 Cleanup: discarding lookahead token $end ()
1100 Cleanup: popping token error ()
1101 Cleanup: popping token 'a' ('a')
1109 ## ------------------------------------------------------ ##
1110 ## Default %printer and %destructor are not for $accept. ##
1111 ## ------------------------------------------------------ ##
1113 AT_SETUP([Default %printer and %destructor are not for $accept])
1115 # If YYSTYPE is a union and Bison were to apply the default %printer and
1116 # %destructor to $accept:
1117 # - The %printer and %destructor code generated for $accept would always be
1118 # dead code because $accept is currently never shifted onto the stack.
1119 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1120 # to declare `%type <field> $accept'. (Also true for $undefined.)
1121 # - Thus, the compiler might complain that the user code assumes the wrong
1122 # type for $$ since the code might assume the type associated with a
1123 # specific union field, which is especially reasonable in C++ since that
1124 # type may be a base type. This test case checks for this problem. (Also
1125 # true for $undefined and the error token, so there are three warnings for
1126 # %printer and three for %destructor.)
1128 AT_DATA_GRAMMAR([[input.y]],
1129 [[%debug /* So that %printer is actually compiled. */
1133 # include <stdlib.h>
1134 static void yyerror (const char *msg);
1135 static int yylex (void);
1141 fprintf (yyoutput, "'%c'", chr);
1145 fprintf (stderr, "DESTROY '%c'\n", chr);
1148 %union { char chr; }
1153 start: { USE($$); } ;
1167 yyerror (const char *msg)
1169 fprintf (stderr, "%s\n", msg);
1179 AT_BISON_CHECK([-o input.c input.y])
1186 ## ------------------------------------------------------ ##
1187 ## Default %printer and %destructor for mid-rule values. ##
1188 ## ------------------------------------------------------ ##
1190 AT_SETUP([Default %printer and %destructor for mid-rule values])
1192 AT_DATA_GRAMMAR([[input.y]],
1193 [[%debug /* So that %printer is actually compiled. */
1197 # include <stdlib.h>
1198 static void yyerror (const char *msg);
1199 static int yylex (void);
1201 # define YYLTYPE int
1202 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1203 # define YY_LOCATION_PRINT(File, Loc)
1206 %printer { fprintf (yyoutput, "%d", @$); } <>
1207 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1208 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1209 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1214 { @$ = 1; } // Not set or used.
1215 { USE ($$); @$ = 2; } // Both set and used.
1216 { USE ($$); @$ = 3; } // Only set.
1217 { @$ = 4; } // Only used.
1219 { USE (($$, $2, $4, $5)); @$ = 0; }
1234 yyerror (const char *msg)
1236 fprintf (stderr, "%s\n", msg);
1247 AT_BISON_CHECK([-o input.c input.y], 0,,
1248 [[input.y:33.3-23: warning: unset value: $$
1249 input.y:30.3-35.37: warning: unused value: $3
1253 AT_PARSER_CHECK([./input], 1,,
1256 Reducing stack by rule 1 (line 30):
1257 -> $$ = nterm $@1 (: )
1260 Reducing stack by rule 2 (line 31):
1261 -> $$ = nterm @2 (: 2)
1264 Reducing stack by rule 3 (line 32):
1265 -> $$ = nterm @3 (: 3)
1268 Reducing stack by rule 4 (line 33):
1269 -> $$ = nterm @4 (: 4)
1272 Reading a token: Now at end of input.
1274 Error: popping nterm @4 (: 4)
1277 Error: popping nterm @3 (: 3)
1280 Error: popping nterm @2 (: 2)
1283 Error: popping nterm $@1 (: )
1285 Cleanup: discarding lookahead token $end (: )
1292 ## ----------------------- ##
1293 ## @$ implies %locations. ##
1294 ## ----------------------- ##
1296 # Bison once forgot to check for @$ in actions other than semantic actions.
1298 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1299 # -------------------------------------------------------
1300 m4_define([AT_CHECK_ACTION_LOCATIONS],
1301 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1303 AT_DATA_GRAMMAR([[input.y]],
1306 static int yylex (void);
1307 static void yyerror (char const *msg);
1313 printf ("%d\n", @$.first_line);
1314 } ]m4_if($1, [%initial-action], [], [[start]])[
1329 yyerror (char const *msg)
1331 fprintf (stderr, "%s\n", msg);
1341 AT_BISON_CHECK([[-o input.c input.y]])
1342 AT_COMPILE([[input]])
1346 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1347 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1348 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1351 ## ----------------------------------------------- ##
1352 ## Fix user actions without a trailing semicolon. ##
1353 ## ----------------------------------------------- ##
1355 AT_SETUP([[Fix user actions without a trailing semicolon]])
1357 # This feature is undocumented, but we accidentally broke it in 2.3a,
1358 # and there was a complaint at:
1359 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1363 start: test2 test1 test0 testc;
1366 : 'a' { semi; /* TEST:N:2 */ }
1367 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1368 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1369 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1370 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1371 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1372 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1373 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1374 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1377 : 'a' { semi; // TEST:N:1 ;
1378 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1379 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1380 } | 'd' { semi; no_semi // TEST:Y:1 ;
1381 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1382 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1383 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1384 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1385 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1388 : 'a' { semi; // TEST:N:1 {}
1389 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1390 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1391 } | 'd' { semi; no_semi // TEST:Y:1 {}
1392 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1393 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1394 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1395 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1396 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1401 #define TEST_MACRO_N \
1402 []"broken\" $ @ $$ @$ [];\
1406 #define TEST_MACRO_N \
1407 []"broken\" $ @ $$ @$ [];\
1411 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1412 [[input.y:8.48: warning: a ';' might be needed at the end of action code
1413 input.y:8.48: warning: future versions of Bison will not add the ';'
1414 input.y:9.48: warning: a ';' might be needed at the end of action code
1415 input.y:9.48: warning: future versions of Bison will not add the ';'
1416 input.y:10.48: warning: a ';' might be needed at the end of action code
1417 input.y:10.48: warning: future versions of Bison will not add the ';'
1418 input.y:11.48: warning: a ';' might be needed at the end of action code
1419 input.y:11.48: warning: future versions of Bison will not add the ';'
1420 input.y:12.48: warning: a ';' might be needed at the end of action code
1421 input.y:12.48: warning: future versions of Bison will not add the ';'
1422 input.y:13.48: warning: a ';' might be needed at the end of action code
1423 input.y:13.48: warning: future versions of Bison will not add the ';'
1424 input.y:20.1: warning: a ';' might be needed at the end of action code
1425 input.y:20.1: warning: future versions of Bison will not add the ';'
1426 input.y:21.1: warning: a ';' might be needed at the end of action code
1427 input.y:21.1: warning: future versions of Bison will not add the ';'
1428 input.y:22.1: warning: a ';' might be needed at the end of action code
1429 input.y:22.1: warning: future versions of Bison will not add the ';'
1430 input.y:23.1: warning: a ';' might be needed at the end of action code
1431 input.y:23.1: warning: future versions of Bison will not add the ';'
1432 input.y:24.1: warning: a ';' might be needed at the end of action code
1433 input.y:24.1: warning: future versions of Bison will not add the ';'
1434 input.y:25.1: warning: a ';' might be needed at the end of action code
1435 input.y:25.1: warning: future versions of Bison will not add the ';'
1436 input.y:31.1: warning: a ';' might be needed at the end of action code
1437 input.y:31.1: warning: future versions of Bison will not add the ';'
1438 input.y:32.1: warning: a ';' might be needed at the end of action code
1439 input.y:32.1: warning: future versions of Bison will not add the ';'
1440 input.y:33.1: warning: a ';' might be needed at the end of action code
1441 input.y:33.1: warning: future versions of Bison will not add the ';'
1442 input.y:34.1: warning: a ';' might be needed at the end of action code
1443 input.y:34.1: warning: future versions of Bison will not add the ';'
1444 input.y:35.1: warning: a ';' might be needed at the end of action code
1445 input.y:35.1: warning: future versions of Bison will not add the ';'
1446 input.y:36.1: warning: a ';' might be needed at the end of action code
1447 input.y:36.1: warning: future versions of Bison will not add the ';'
1450 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1451 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1452 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1453 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1454 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1459 ## -------------------------------------------------- ##
1460 ## Destroying lookahead assigned by semantic action. ##
1461 ## -------------------------------------------------- ##
1463 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1465 AT_DATA_GRAMMAR([input.y],
1470 static void yyerror (char const *);
1471 static int yylex (void);
1475 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1476 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1480 // In a previous version of Bison, yychar assigned by the semantic
1481 // action below was not translated into yytoken before the lookahead was
1482 // discarded and thus before its destructor (selected according to
1483 // yytoken) was called in order to return from yyparse. This would
1484 // happen even if YYACCEPT was performed in a later semantic action as
1485 // long as only consistent states with default reductions were visited
1486 // in between. However, we leave YYACCEPT in the same semantic action
1487 // for this test in order to show that skeletons cannot simply translate
1488 // immediately after every semantic action because a semantic action
1489 // that has set yychar might not always return normally. Instead,
1490 // skeletons must translate before every use of yytoken.
1491 start: 'a' accept { USE($1); } ;
1493 assert (yychar == YYEMPTY);
1501 yyerror (char const *msg)
1503 fprintf (stderr, "%s\n", msg);
1509 static char const *input = "a";
1520 AT_BISON_CHECK([[-o input.c input.y]])
1521 AT_COMPILE([[input]])
1522 AT_PARSER_CHECK([[./input]], [[0]], [],