1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001-2010 Free Software Foundation, Inc.
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 AT_BANNER([[User Actions.]])
19 ## ------------------ ##
20 ## Mid-rule actions. ##
21 ## ------------------ ##
23 AT_SETUP([Mid-rule actions])
25 # Bison once forgot the mid-rule actions. It was because the action
26 # was attached to the host rule (the one with the mid-rule action),
27 # instead of being attached to the empty rule dedicated to this
30 AT_DATA_GRAMMAR([[input.y]],
36 static void yyerror (const char *msg);
37 static int yylex (void);
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'); }
56 static char const input[] = "123456789";
58 if (! (toknum < sizeof input))
60 return input[toknum++];
64 yyerror (const char *msg)
66 fprintf (stderr, "%s\n", msg);
76 AT_BISON_CHECK([-d -v -o input.c input.y])
78 AT_PARSER_CHECK([./input], 0,
88 ## ---------------- ##
90 ## ---------------- ##
92 AT_SETUP([Exotic Dollars])
94 AT_DATA_GRAMMAR([[input.y]],
100 static void yyerror (const char *msg);
101 static int yylex (void);
110 %type <val> a_1 a_2 a_5
111 sum_of_the_five_previous_values
114 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
115 sum_of_the_five_previous_values
117 USE (($1, $2, $<foo>3, $<foo>4, $5));
125 sum_of_the_five_previous_values:
127 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
142 yyerror (const char *msg)
144 fprintf (stderr, "%s\n", msg);
154 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
156 AT_PARSER_CHECK([./input], 0,
164 ## -------------------------- ##
165 ## Printers and Destructors. ##
166 ## -------------------------- ##
168 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
169 # -----------------------------------------------------------------------------
170 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
171 [# Make sure complex $n work.
172 m4_if([$1$2$3], $[1]$[2]$[3], [],
173 [m4_fatal([$0: Invalid arguments: $@])])dnl
175 # Be sure to pass all the %directives to this macro to have correct
176 # helping macros. So don't put any directly in the Bison file.
177 AT_BISON_OPTION_PUSHDEFS([$5])
178 AT_DATA_GRAMMAR([[input.y]],
185 #define YYINITDEPTH 10
186 #define YYMAXDEPTH 10
188 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
189 [#define RANGE(Location) (Location).first_line, (Location).last_line])
193 m4_ifval([$6], [%union
197 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
198 m4_ifval([$6], [[%code provides {]], [[%code {]])
199 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
200 [static int yylex (]AT_LEX_FORMALS[);
201 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
204 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
206 /* FIXME: This %printer isn't actually tested. */
209 ]AT_LALR1_CC_IF([debug_stream () << $$;],
210 [fprintf (yyoutput, "%d", $$)])[;
212 input line thing 'x' 'y'
215 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
219 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
223 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
227 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
231 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
236 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
241 This grammar is made to exercise error recovery.
242 "Lines" starting with `(' support error recovery, with
243 ')' as synchronizing token. Lines starting with 'x' can never
244 be recovered from if in error.
251 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
253 | line input /* Right recursive to load the stack so that popping at
254 END can be exercised. */
257 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
258 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
263 thing thing thing ';'
266 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
267 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
268 $3, RANGE (@3), $4, RANGE (@4));
270 | '(' thing thing ')'
273 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
274 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
275 $3, RANGE (@3), $4, RANGE (@4));
280 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
281 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
286 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
287 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
295 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
296 $$, RANGE (@$), $1, RANGE (@1));
300 /* Alias to ARGV[1]. */
301 const char *source = 0;
304 yylex (]AT_LEX_FORMALS[)
306 static unsigned int counter = 0;
308 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
309 /* As in BASIC, line numbers go from 10 to 10. */
311 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
312 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
314 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
315 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
318 if (! (0 <= c && c <= strlen (source)))
321 printf ("sending: '%c'", source[c]);
323 printf ("sending: END");
324 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
329 [/* A C++ error reporting function. */
331 yy::parser::error (const location& l, const std::string& m)
333 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
341 parser.set_debug_level (yydebug);
342 return parser.parse ();
346 yyerror (const char *msg)
348 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
352 main (int argc, const char *argv[])
355 yydebug = !!getenv ("YYDEBUG");
361 case 0: printf ("Successful parse.\n"); break;
362 case 1: printf ("Parsing FAILED.\n"); break;
363 default: printf ("Parsing FAILED (status %d).\n", status); break;
369 AT_FULL_COMPILE([input])
372 # Check the location of "empty"
373 # -----------------------------
374 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
375 # an empty "line" nterm.
376 # FIXME: This location is not satisfying. Depend on the lookahead?
377 AT_PARSER_CHECK([./input '(x)'], 0,
378 [[sending: '(' (0@0-9)
379 sending: 'x' (1@10-19)
380 thing (1@10-19): 'x' (1@10-19)
381 sending: ')' (2@20-29)
382 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
383 sending: END (3@30-39)
384 input (0@29-29): /* Nothing */
385 input (2@0-29): line (0@0-29) input (0@29-29)
386 Freeing token END (3@30-39)
387 Freeing nterm input (2@0-29)
392 # Check locations in error recovery
393 # ---------------------------------
394 # '(y)' is an error, but can be recovered from. But what's the location
395 # of the error itself ('y'), and of the resulting reduction ('(error)').
396 AT_PARSER_CHECK([./input '(y)'], 0,
397 [[sending: '(' (0@0-9)
398 sending: 'y' (1@10-19)
399 10-19: syntax error, unexpected 'y', expecting 'x'
400 Freeing token 'y' (1@10-19)
401 sending: ')' (2@20-29)
402 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
403 sending: END (3@30-39)
404 input (0@29-29): /* Nothing */
405 input (2@0-29): line (-1@0-29) input (0@29-29)
406 Freeing token END (3@30-39)
407 Freeing nterm input (2@0-29)
412 # Syntax errors caught by the parser
413 # ----------------------------------
414 # Exercise the discarding of stack top and input until `error'
417 # '(', 'x', 'x', 'x', 'x', 'x', ')',
419 # Load the stack and provoke an error that cannot be caught by the
420 # grammar, to check that the stack is cleared. And make sure the
421 # lookahead is freed.
426 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
427 [[sending: '(' (0@0-9)
428 sending: 'x' (1@10-19)
429 thing (1@10-19): 'x' (1@10-19)
430 sending: 'x' (2@20-29)
431 thing (2@20-29): 'x' (2@20-29)
432 sending: 'x' (3@30-39)
433 30-39: syntax error, unexpected 'x', expecting ')'
434 Freeing nterm thing (2@20-29)
435 Freeing nterm thing (1@10-19)
436 Freeing token 'x' (3@30-39)
437 sending: 'x' (4@40-49)
438 Freeing token 'x' (4@40-49)
439 sending: 'x' (5@50-59)
440 Freeing token 'x' (5@50-59)
441 sending: ')' (6@60-69)
442 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
443 sending: '(' (7@70-79)
444 sending: 'x' (8@80-89)
445 thing (8@80-89): 'x' (8@80-89)
446 sending: ')' (9@90-99)
447 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
448 sending: '(' (10@100-109)
449 sending: 'x' (11@110-119)
450 thing (11@110-119): 'x' (11@110-119)
451 sending: ')' (12@120-129)
452 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
453 sending: 'y' (13@130-139)
454 input (0@129-129): /* Nothing */
455 input (2@100-129): line (10@100-129) input (0@129-129)
456 input (2@70-129): line (7@70-99) input (2@100-129)
457 input (2@0-129): line (-1@0-69) input (2@70-129)
458 130-139: syntax error, unexpected 'y', expecting END
459 Freeing nterm input (2@0-129)
460 Freeing token 'y' (13@130-139)
465 # Syntax error caught by the parser where lookahead = END
466 # --------------------------------------------------------
467 # Load the stack and provoke an error that cannot be caught by the
468 # grammar, to check that the stack is cleared. And make sure the
469 # lookahead is freed.
474 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
475 [[sending: '(' (0@0-9)
476 sending: 'x' (1@10-19)
477 thing (1@10-19): 'x' (1@10-19)
478 sending: ')' (2@20-29)
479 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
480 sending: '(' (3@30-39)
481 sending: 'x' (4@40-49)
482 thing (4@40-49): 'x' (4@40-49)
483 sending: ')' (5@50-59)
484 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
485 sending: 'x' (6@60-69)
486 thing (6@60-69): 'x' (6@60-69)
487 sending: END (7@70-79)
488 70-79: syntax error, unexpected END, expecting 'x'
489 Freeing nterm thing (6@60-69)
490 Freeing nterm line (3@30-59)
491 Freeing nterm line (0@0-29)
492 Freeing token END (7@70-79)
497 # Check destruction upon stack overflow
498 # -------------------------------------
499 # Upon stack overflow, all symbols on the stack should be destroyed.
500 # Only check for yacc.c.
502 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
503 [[sending: '(' (0@0-9)
504 sending: 'x' (1@10-19)
505 thing (1@10-19): 'x' (1@10-19)
506 sending: ')' (2@20-29)
507 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
508 sending: '(' (3@30-39)
509 sending: 'x' (4@40-49)
510 thing (4@40-49): 'x' (4@40-49)
511 sending: ')' (5@50-59)
512 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
513 sending: '(' (6@60-69)
514 sending: 'x' (7@70-79)
515 thing (7@70-79): 'x' (7@70-79)
516 sending: ')' (8@80-89)
517 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
518 sending: '(' (9@90-99)
519 sending: 'x' (10@100-109)
520 thing (10@100-109): 'x' (10@100-109)
521 sending: ')' (11@110-119)
522 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
523 sending: '(' (12@120-129)
524 sending: 'x' (13@130-139)
525 thing (13@130-139): 'x' (13@130-139)
526 sending: ')' (14@140-149)
527 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
528 sending: '(' (15@150-159)
529 sending: 'x' (16@160-169)
530 thing (16@160-169): 'x' (16@160-169)
531 sending: ')' (17@170-179)
532 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
533 sending: '(' (18@180-189)
534 sending: 'x' (19@190-199)
535 thing (19@190-199): 'x' (19@190-199)
536 sending: ')' (20@200-209)
537 200-209: memory exhausted
538 Freeing nterm thing (19@190-199)
539 Freeing nterm line (15@150-179)
540 Freeing nterm line (12@120-149)
541 Freeing nterm line (9@90-119)
542 Freeing nterm line (6@60-89)
543 Freeing nterm line (3@30-59)
544 Freeing nterm line (0@0-29)
545 Parsing FAILED (status 2).
552 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
553 # ---------------------------------------------------------------------------
554 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
555 [AT_SETUP([Printers and Destructors $2: $1])
558 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
569 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
570 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
572 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
573 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
575 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
576 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
580 ## ----------------------------------------- ##
581 ## Default tagless %printer and %destructor. ##
582 ## ----------------------------------------- ##
584 # Check that the right %printer and %destructor are called, that they're not
585 # called for $end, and that $$ and @$ work correctly.
587 AT_SETUP([Default tagless %printer and %destructor])
589 AT_DATA_GRAMMAR([[input.y]],
594 @$.first_line = @$.last_line = 1;
595 @$.first_column = @$.last_column = 1;
601 static void yyerror (const char *msg);
602 static int yylex (void);
607 fprintf (yyoutput, "<*> printer should not be called.\n");
611 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
614 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
618 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
621 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
625 fprintf (yyoutput, "<*> destructor should not be called.\n");
630 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
637 static char const input[] = "abcd";
638 static size_t toknum;
639 if (! (toknum < sizeof input))
641 yylval = input[toknum++];
642 yylloc.first_line = yylloc.last_line = 1;
643 yylloc.first_column = yylloc.last_column = toknum;
648 yyerror (const char *msg)
650 fprintf (stderr, "%s\n", msg);
661 AT_BISON_CHECK([-o input.c input.y])
663 AT_PARSER_CHECK([./input], 1,
664 [[<> destructor for 'd' @ 4.
665 'b'/'c' destructor for 'c' @ 3.
666 'b'/'c' destructor for 'b' @ 2.
667 <> destructor for 'a' @ 1.
671 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
672 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
674 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
675 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
677 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
678 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
680 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
681 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
683 Reading a token: Now at end of input.
684 syntax error, unexpected $end, expecting 'e'
685 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
687 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
689 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
691 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
693 Cleanup: discarding lookahead token $end (1.5-1.5: )
701 ## ------------------------------------------------------ ##
702 ## Default tagged and per-type %printer and %destructor. ##
703 ## ------------------------------------------------------ ##
705 AT_SETUP([Default tagged and per-type %printer and %destructor])
707 AT_DATA_GRAMMAR([[input.y]],
714 static void yyerror (const char *msg);
715 static int yylex (void);
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));
765 static char const input[] = "abcdef";
766 static size_t toknum;
767 if (! (toknum < sizeof input))
769 return input[toknum++];
773 yyerror (const char *msg)
775 fprintf (stderr, "%s\n", msg);
786 AT_BISON_CHECK([-o input.c input.y])
788 AT_PARSER_CHECK([./input], 1,
789 [[<*>/<field2>/e destructor.
790 <*>/<field2>/e destructor.
794 <*>/<field2>/e destructor.
798 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
799 Shifting token 'a' (<*>/<field2>/e printer)
801 Reading a token: Next token is token 'b' (<field1> printer)
802 Shifting token 'b' (<field1> printer)
804 Reading a token: Next token is token 'c' ('c' printer)
805 Shifting token 'c' ('c' printer)
807 Reading a token: Next token is token 'd' ('d' printer)
808 Shifting token 'd' ('d' printer)
810 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
811 Shifting token 'e' (<*>/<field2>/e printer)
813 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
814 Shifting token 'f' (<*>/<field2>/e printer)
816 Reading a token: Now at end of input.
817 syntax error, unexpected $end, expecting 'g'
818 Error: popping token 'f' (<*>/<field2>/e printer)
819 Stack now 0 1 3 5 6 7
820 Error: popping token 'e' (<*>/<field2>/e printer)
822 Error: popping token 'd' ('d' printer)
824 Error: popping token 'c' ('c' printer)
826 Error: popping token 'b' (<field1> printer)
828 Error: popping token 'a' (<*>/<field2>/e printer)
830 Cleanup: discarding lookahead token $end ()
838 ## ------------------------------------------------------------- ##
839 ## Default %printer and %destructor for user-defined end token. ##
840 ## ------------------------------------------------------------- ##
842 AT_SETUP([Default %printer and %destructor for user-defined end token])
844 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
845 # -----------------------------------------------------------------------------
846 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
848 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
849 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
851 AT_DATA_GRAMMAR([[input]]$1[[.y]],
856 @$.first_line = @$.last_line = 1;
857 @$.first_column = @$.last_column = 1;
863 static void yyerror (const char *msg);
864 static int yylex (void);
869 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
874 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
877 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
881 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
886 [[[%union { char tag; }
887 %type <tag> start END]]])[[
891 start: { $$ = 'S'; } ;
901 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
902 yylloc.first_line = yylloc.last_line = 1;
903 yylloc.first_column = yylloc.last_column = 1;
908 yyerror (const char *msg)
910 fprintf (stderr, "%s\n", msg);
921 AT_BISON_CHECK([-o input$1.c input$1.y])
922 AT_COMPILE([input$1])
923 AT_PARSER_CHECK([./input$1], 0,
924 [[<]]kind[[> for 'E' @ 1.
925 <]]kind[[> for 'S' @ 1.
929 Reducing stack by rule 1 (line 46):
930 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
933 Reading a token: Now at end of input.
934 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
937 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
938 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
942 m4_popdef([not_kind])
945 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
946 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
952 ## ------------------------------------------------------------------ ##
953 ## Default %printer and %destructor are not for error or $undefined. ##
954 ## ------------------------------------------------------------------ ##
956 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
958 # If Bison were to apply the default %printer and %destructor to the error
959 # token or to $undefined:
960 # - For the error token:
961 # - It would generate warnings for unused $n.
962 # - It would invoke the %printer and %destructor on the error token's
963 # semantic value, which would be initialized from the lookahead, which
964 # would be destroyed separately.
965 # - For $undefined, who knows what the semantic value would be.
967 AT_DATA_GRAMMAR([[input.y]],
973 static void yyerror (const char *msg);
974 static int yylex (void);
979 fprintf (yyoutput, "'%c'", $$);
982 fprintf (stderr, "DESTROY '%c'\n", $$);
989 /* In order to reveal the problems that this bug caused during parsing, add
991 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
999 static char const input[] = "abd";
1000 static size_t toknum;
1001 if (! (toknum < sizeof input))
1003 yylval = input[toknum++];
1008 yyerror (const char *msg)
1010 fprintf (stderr, "%s\n", msg);
1021 AT_BISON_CHECK([-o input.c input.y])
1023 AT_PARSER_CHECK([./input], [1], [],
1026 Reading a token: Next token is token 'a' ('a')
1027 Shifting token 'a' ('a')
1029 Reading a token: Next token is token 'b' ('b')
1031 Shifting token error ()
1033 Next token is token 'b' ('b')
1034 Shifting token 'b' ('b')
1036 Reading a token: Next token is token $undefined ()
1037 Error: popping token 'b' ('b')
1040 Error: popping token error ()
1042 Shifting token error ()
1044 Next token is token $undefined ()
1045 Error: discarding token $undefined ()
1046 Error: popping token error ()
1048 Shifting token error ()
1050 Reading a token: Now at end of input.
1051 Cleanup: discarding lookahead token $end ()
1053 Cleanup: popping token error ()
1054 Cleanup: popping token 'a' ('a')
1062 ## ------------------------------------------------------ ##
1063 ## Default %printer and %destructor are not for $accept. ##
1064 ## ------------------------------------------------------ ##
1066 AT_SETUP([Default %printer and %destructor are not for $accept])
1068 # If YYSTYPE is a union and Bison were to apply the default %printer and
1069 # %destructor to $accept:
1070 # - The %printer and %destructor code generated for $accept would always be
1071 # dead code because $accept is currently never shifted onto the stack.
1072 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1073 # to declare `%type <field> $accept'. (Also true for $undefined.)
1074 # - Thus, the compiler might complain that the user code assumes the wrong
1075 # type for $$ since the code might assume the type associated with a
1076 # specific union field, which is especially reasonable in C++ since that
1077 # type may be a base type. This test case checks for this problem. (Also
1078 # true for $undefined and the error token, so there are three warnings for
1079 # %printer and three for %destructor.)
1081 AT_DATA_GRAMMAR([[input.y]],
1082 [[%debug /* So that %printer is actually compiled. */
1086 # include <stdlib.h>
1087 static void yyerror (const char *msg);
1088 static int yylex (void);
1094 fprintf (yyoutput, "'%c'", chr);
1098 fprintf (stderr, "DESTROY '%c'\n", chr);
1101 %union { char chr; }
1106 start: { USE($$); } ;
1120 yyerror (const char *msg)
1122 fprintf (stderr, "%s\n", msg);
1132 AT_BISON_CHECK([-o input.c input.y])
1139 ## ------------------------------------------------------ ##
1140 ## Default %printer and %destructor for mid-rule values. ##
1141 ## ------------------------------------------------------ ##
1143 AT_SETUP([Default %printer and %destructor for mid-rule values])
1145 AT_DATA_GRAMMAR([[input.y]],
1146 [[%debug /* So that %printer is actually compiled. */
1150 # include <stdlib.h>
1151 static void yyerror (const char *msg);
1152 static int yylex (void);
1154 # define YYLTYPE int
1155 # define YYLLOC_DEFAULT(Current, Rhs, N)
1156 # define YY_LOCATION_PRINT(File, Loc)
1159 %printer { fprintf (yyoutput, "%d", @$); } <>
1160 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1161 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1162 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1167 { @$ = 1; } // Not set or used.
1168 { USE ($$); @$ = 2; } // Both set and used.
1169 { USE ($$); @$ = 3; } // Only set.
1170 { @$ = 4; } // Only used.
1172 { USE (($$, $2, $4, $5)); @$ = 0; }
1187 yyerror (const char *msg)
1189 fprintf (stderr, "%s\n", msg);
1200 AT_BISON_CHECK([-o input.c input.y], 0,,
1201 [[input.y:33.3-23: warning: unset value: $$
1202 input.y:30.3-35.37: warning: unused value: $3
1206 AT_PARSER_CHECK([./input], 1,,
1209 Reducing stack by rule 1 (line 30):
1210 -> $$ = nterm $@1 (: )
1213 Reducing stack by rule 2 (line 31):
1214 -> $$ = nterm @2 (: 2)
1217 Reducing stack by rule 3 (line 32):
1218 -> $$ = nterm @3 (: 3)
1221 Reducing stack by rule 4 (line 33):
1222 -> $$ = nterm @4 (: 4)
1225 Reading a token: Now at end of input.
1227 Error: popping nterm @4 (: 4)
1230 Error: popping nterm @3 (: 3)
1233 Error: popping nterm @2 (: 2)
1236 Error: popping nterm $@1 (: )
1238 Cleanup: discarding lookahead token $end (: )
1245 ## ----------------------- ##
1246 ## @$ implies %locations. ##
1247 ## ----------------------- ##
1249 # Bison once forgot to check for @$ in actions other than semantic actions.
1251 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1252 # -------------------------------------------------------
1253 m4_define([AT_CHECK_ACTION_LOCATIONS],
1254 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1256 AT_DATA_GRAMMAR([[input.y]],
1259 static int yylex (void);
1260 static void yyerror (char const *msg);
1266 printf ("%d\n", @$.first_line);
1267 } ]m4_if($1, [%initial-action], [], [[start]])[
1282 yyerror (char const *msg)
1284 fprintf (stderr, "%s\n", msg);
1294 AT_BISON_CHECK([[-o input.c input.y]])
1295 AT_COMPILE([[input]])
1299 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1300 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1301 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1304 ## ----------------------------------------------- ##
1305 ## Fix user actions without a trailing semicolon. ##
1306 ## ----------------------------------------------- ##
1308 AT_SETUP([[Fix user actions without a trailing semicolon]])
1310 # This feature is undocumented, but we accidentally broke it in 2.3a,
1311 # and there was a complaint at:
1312 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1316 start: test2 test1 test0 testc;
1319 : 'a' { semi; /* TEST:N:2 */ }
1320 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1321 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1322 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1323 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1324 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1325 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1326 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1327 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1330 : 'a' { semi; // TEST:N:1 ;
1331 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1332 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1333 } | 'd' { semi; no_semi // TEST:Y:1 ;
1334 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1335 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1336 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1337 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1338 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1341 : 'a' { semi; // TEST:N:1 {}
1342 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1343 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1344 } | 'd' { semi; no_semi // TEST:Y:1 {}
1345 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1346 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1347 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1348 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1349 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1354 #define TEST_MACRO_N \
1355 []"broken\" $ @ $$ @$ [];\
1359 #define TEST_MACRO_N \
1360 []"broken\" $ @ $$ @$ [];\
1364 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1365 [[input.y:8.48: warning: a `;' might be needed at the end of action code
1366 input.y:8.48: warning: future versions of Bison will not add the `;'
1367 input.y:9.48: warning: a `;' might be needed at the end of action code
1368 input.y:9.48: warning: future versions of Bison will not add the `;'
1369 input.y:10.48: warning: a `;' might be needed at the end of action code
1370 input.y:10.48: warning: future versions of Bison will not add the `;'
1371 input.y:11.48: warning: a `;' might be needed at the end of action code
1372 input.y:11.48: warning: future versions of Bison will not add the `;'
1373 input.y:12.48: warning: a `;' might be needed at the end of action code
1374 input.y:12.48: warning: future versions of Bison will not add the `;'
1375 input.y:13.48: warning: a `;' might be needed at the end of action code
1376 input.y:13.48: warning: future versions of Bison will not add the `;'
1377 input.y:20.1: warning: a `;' might be needed at the end of action code
1378 input.y:20.1: warning: future versions of Bison will not add the `;'
1379 input.y:21.1: warning: a `;' might be needed at the end of action code
1380 input.y:21.1: warning: future versions of Bison will not add the `;'
1381 input.y:22.1: warning: a `;' might be needed at the end of action code
1382 input.y:22.1: warning: future versions of Bison will not add the `;'
1383 input.y:23.1: warning: a `;' might be needed at the end of action code
1384 input.y:23.1: warning: future versions of Bison will not add the `;'
1385 input.y:24.1: warning: a `;' might be needed at the end of action code
1386 input.y:24.1: warning: future versions of Bison will not add the `;'
1387 input.y:25.1: warning: a `;' might be needed at the end of action code
1388 input.y:25.1: warning: future versions of Bison will not add the `;'
1389 input.y:31.1: warning: a `;' might be needed at the end of action code
1390 input.y:31.1: warning: future versions of Bison will not add the `;'
1391 input.y:32.1: warning: a `;' might be needed at the end of action code
1392 input.y:32.1: warning: future versions of Bison will not add the `;'
1393 input.y:33.1: warning: a `;' might be needed at the end of action code
1394 input.y:33.1: warning: future versions of Bison will not add the `;'
1395 input.y:34.1: warning: a `;' might be needed at the end of action code
1396 input.y:34.1: warning: future versions of Bison will not add the `;'
1397 input.y:35.1: warning: a `;' might be needed at the end of action code
1398 input.y:35.1: warning: future versions of Bison will not add the `;'
1399 input.y:36.1: warning: a `;' might be needed at the end of action code
1400 input.y:36.1: warning: future versions of Bison will not add the `;'
1403 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1404 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1405 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1406 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1407 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1412 ## -------------------------------------------------- ##
1413 ## Destroying lookahead assigned by semantic action. ##
1414 ## -------------------------------------------------- ##
1416 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1418 AT_DATA_GRAMMAR([input.y],
1423 static void yyerror (char const *);
1424 static int yylex (void);
1428 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1429 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1433 // In a previous version of Bison, yychar assigned by the semantic
1434 // action below was not translated into yytoken before the lookahead was
1435 // discarded and thus before its destructor (selected according to
1436 // yytoken) was called in order to return from yyparse. This would
1437 // happen even if YYACCEPT was performed in a later semantic action as
1438 // long as only consistent states with default reductions were visited
1439 // in between. However, we leave YYACCEPT in the same semantic action
1440 // for this test in order to show that skeletons cannot simply translate
1441 // immediately after every semantic action because a semantic action
1442 // that has set yychar might not always return normally. Instead,
1443 // skeletons must translate before every use of yytoken.
1444 start: 'a' accept { USE($1); } ;
1446 assert (yychar == YYEMPTY);
1454 yyerror (char const *msg)
1456 fprintf (stderr, "%s\n", msg);
1462 static char const *input = "a";
1473 AT_BISON_CHECK([[-o input.c input.y]])
1474 AT_COMPILE([[input]])
1475 AT_PARSER_CHECK([[./input]], [[0]], [],