1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
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 2, or (at your option)
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, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 AT_BANNER([[User Actions.]])
22 ## ------------------ ##
23 ## Mid-rule actions. ##
24 ## ------------------ ##
26 AT_SETUP([Mid-rule actions])
28 # Bison once forgot the mid-rule actions. It was because the action
29 # was attached to the host rule (the one with the mid-rule action),
30 # instead of being attached to the empty rule dedicated to this
33 AT_DATA_GRAMMAR([[input.y]],
39 static void yyerror (const char *msg);
40 static int yylex (void);
43 exp: { putchar ('0'); }
44 '1' { putchar ('1'); }
45 '2' { putchar ('2'); }
46 '3' { putchar ('3'); }
47 '4' { putchar ('4'); }
48 '5' { putchar ('5'); }
49 '6' { putchar ('6'); }
50 '7' { putchar ('7'); }
51 '8' { putchar ('8'); }
52 '9' { putchar ('9'); }
59 static char const input[] = "123456789";
61 if (! (toknum < sizeof input))
63 return input[toknum++];
67 yyerror (const char *msg)
69 fprintf (stderr, "%s\n", msg);
79 AT_CHECK([bison -d -v -o input.c input.y])
81 AT_PARSER_CHECK([./input], 0,
91 ## ---------------- ##
93 ## ---------------- ##
95 AT_SETUP([Exotic Dollars])
97 AT_DATA_GRAMMAR([[input.y]],
103 static void yyerror (const char *msg);
104 static int yylex (void);
113 %type <val> a_1 a_2 a_5
114 sum_of_the_five_previous_values
117 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
118 sum_of_the_five_previous_values
120 USE (($1, $2, $<foo>3, $<foo>4, $5));
128 sum_of_the_five_previous_values:
130 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
145 yyerror (const char *msg)
147 fprintf (stderr, "%s\n", msg);
157 AT_CHECK([bison -d -v -o input.c input.y], 0)
159 AT_PARSER_CHECK([./input], 0,
167 ## -------------------------- ##
168 ## Printers and Destructors. ##
169 ## -------------------------- ##
171 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
172 # -----------------------------------------------------------------------------
173 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
174 [# Make sure complex $n work.
175 m4_if([$1$2$3], $[1]$[2]$[3], [],
176 [m4_fatal([$0: Invalid arguments: $@])])dnl
178 # Be sure to pass all the %directives to this macro to have correct
179 # helping macros. So don't put any directly in the Bison file.
180 AT_BISON_OPTION_PUSHDEFS([$5])
181 AT_DATA_GRAMMAR([[input.y]],
188 #define YYINITDEPTH 10
189 #define YYMAXDEPTH 10
191 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
192 [#define RANGE(Location) (Location).first_line, (Location).last_line])
196 m4_ifval([$6], [%union
200 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
201 m4_ifval([$6], [[%code provides {]], [[%code {]])
202 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
203 [static int yylex (]AT_LEX_FORMALS[);
204 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
207 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
209 /* FIXME: This %printer isn't actually tested. */
212 ]AT_LALR1_CC_IF([debug_stream () << $$;],
213 [fprintf (yyoutput, "%d", $$)])[;
215 input line thing 'x' 'y'
218 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
222 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
226 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
230 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
234 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
239 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
244 This grammar is made to exercise error recovery.
245 "Lines" starting with `(' support error recovery, with
246 ')' as synchronizing token. Lines starting with 'x' can never
247 be recovered from if in error.
254 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
256 | line input /* Right recursive to load the stack so that popping at
257 END can be exercised. */
260 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
261 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
266 thing thing thing ';'
269 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
270 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
271 $3, RANGE (@3), $4, RANGE (@4));
273 | '(' thing thing ')'
276 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
277 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
278 $3, RANGE (@3), $4, RANGE (@4));
283 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
284 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
289 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
290 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
298 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
299 $$, RANGE (@$), $1, RANGE (@1));
303 /* Alias to ARGV[1]. */
304 const char *source = 0;
307 yylex (]AT_LEX_FORMALS[)
309 static unsigned int counter = 0;
311 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
312 /* As in BASIC, line numbers go from 10 to 10. */
314 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
315 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
317 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
318 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
321 if (! (0 <= c && c <= strlen (source)))
324 printf ("sending: '%c'", source[c]);
326 printf ("sending: END");
327 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
332 [/* A C++ error reporting function. */
334 yy::parser::error (const location& l, const std::string& m)
336 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
344 parser.set_debug_level (yydebug);
345 return parser.parse ();
349 yyerror (const char *msg)
351 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
355 main (int argc, const char *argv[])
358 yydebug = !!getenv ("YYDEBUG");
364 case 0: printf ("Successful parse.\n"); break;
365 case 1: printf ("Parsing FAILED.\n"); break;
366 default: printf ("Parsing FAILED (status %d).\n", status); break;
373 [AT_CHECK([bison -o input.cc input.y])
374 AT_COMPILE_CXX([input])],
375 [AT_CHECK([bison -o input.c input.y])
376 AT_COMPILE([input])])
379 # Check the location of "empty"
380 # -----------------------------
381 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
382 # an empty "line" nterm.
383 # FIXME: This location is not satisfying. Depend on the lookahead?
384 AT_PARSER_CHECK([./input '(x)'], 0,
385 [[sending: '(' (0@0-9)
386 sending: 'x' (1@10-19)
387 thing (1@10-19): 'x' (1@10-19)
388 sending: ')' (2@20-29)
389 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
390 sending: END (3@30-39)
391 input (0@29-29): /* Nothing */
392 input (2@0-29): line (0@0-29) input (0@29-29)
393 Freeing token END (3@30-39)
394 Freeing nterm input (2@0-29)
399 # Check locations in error recovery
400 # ---------------------------------
401 # '(y)' is an error, but can be recovered from. But what's the location
402 # of the error itself ('y'), and of the resulting reduction ('(error)').
403 AT_PARSER_CHECK([./input '(y)'], 0,
404 [[sending: '(' (0@0-9)
405 sending: 'y' (1@10-19)
406 10-19: syntax error, unexpected 'y', expecting 'x'
407 Freeing token 'y' (1@10-19)
408 sending: ')' (2@20-29)
409 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
410 sending: END (3@30-39)
411 input (0@29-29): /* Nothing */
412 input (2@0-29): line (-1@0-29) input (0@29-29)
413 Freeing token END (3@30-39)
414 Freeing nterm input (2@0-29)
419 # Syntax errors caught by the parser
420 # ----------------------------------
421 # Exercise the discarding of stack top and input until `error'
424 # '(', 'x', 'x', 'x', 'x', 'x', ')',
426 # Load the stack and provoke an error that cannot be caught by the
427 # grammar, to check that the stack is cleared. And make sure the
428 # lookahead is freed.
433 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
434 [[sending: '(' (0@0-9)
435 sending: 'x' (1@10-19)
436 thing (1@10-19): 'x' (1@10-19)
437 sending: 'x' (2@20-29)
438 thing (2@20-29): 'x' (2@20-29)
439 sending: 'x' (3@30-39)
440 30-39: syntax error, unexpected 'x', expecting ')'
441 Freeing nterm thing (2@20-29)
442 Freeing nterm thing (1@10-19)
443 Freeing token 'x' (3@30-39)
444 sending: 'x' (4@40-49)
445 Freeing token 'x' (4@40-49)
446 sending: 'x' (5@50-59)
447 Freeing token 'x' (5@50-59)
448 sending: ')' (6@60-69)
449 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
450 sending: '(' (7@70-79)
451 sending: 'x' (8@80-89)
452 thing (8@80-89): 'x' (8@80-89)
453 sending: ')' (9@90-99)
454 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
455 sending: '(' (10@100-109)
456 sending: 'x' (11@110-119)
457 thing (11@110-119): 'x' (11@110-119)
458 sending: ')' (12@120-129)
459 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
460 sending: 'y' (13@130-139)
461 input (0@129-129): /* Nothing */
462 input (2@100-129): line (10@100-129) input (0@129-129)
463 input (2@70-129): line (7@70-99) input (2@100-129)
464 input (2@0-129): line (-1@0-69) input (2@70-129)
465 130-139: syntax error, unexpected 'y', expecting END
466 Freeing nterm input (2@0-129)
467 Freeing token 'y' (13@130-139)
472 # Syntax error caught by the parser where lookahead = END
473 # --------------------------------------------------------
474 # Load the stack and provoke an error that cannot be caught by the
475 # grammar, to check that the stack is cleared. And make sure the
476 # lookahead is freed.
481 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
482 [[sending: '(' (0@0-9)
483 sending: 'x' (1@10-19)
484 thing (1@10-19): 'x' (1@10-19)
485 sending: ')' (2@20-29)
486 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
487 sending: '(' (3@30-39)
488 sending: 'x' (4@40-49)
489 thing (4@40-49): 'x' (4@40-49)
490 sending: ')' (5@50-59)
491 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
492 sending: 'x' (6@60-69)
493 thing (6@60-69): 'x' (6@60-69)
494 sending: END (7@70-79)
495 70-79: syntax error, unexpected END, expecting 'x'
496 Freeing nterm thing (6@60-69)
497 Freeing nterm line (3@30-59)
498 Freeing nterm line (0@0-29)
499 Freeing token END (7@70-79)
504 # Check destruction upon stack overflow
505 # -------------------------------------
506 # Upon stack overflow, all symbols on the stack should be destroyed.
507 # Only check for yacc.c.
509 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
510 [[sending: '(' (0@0-9)
511 sending: 'x' (1@10-19)
512 thing (1@10-19): 'x' (1@10-19)
513 sending: ')' (2@20-29)
514 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
515 sending: '(' (3@30-39)
516 sending: 'x' (4@40-49)
517 thing (4@40-49): 'x' (4@40-49)
518 sending: ')' (5@50-59)
519 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
520 sending: '(' (6@60-69)
521 sending: 'x' (7@70-79)
522 thing (7@70-79): 'x' (7@70-79)
523 sending: ')' (8@80-89)
524 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
525 sending: '(' (9@90-99)
526 sending: 'x' (10@100-109)
527 thing (10@100-109): 'x' (10@100-109)
528 sending: ')' (11@110-119)
529 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
530 sending: '(' (12@120-129)
531 sending: 'x' (13@130-139)
532 thing (13@130-139): 'x' (13@130-139)
533 sending: ')' (14@140-149)
534 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
535 sending: '(' (15@150-159)
536 sending: 'x' (16@160-169)
537 thing (16@160-169): 'x' (16@160-169)
538 sending: ')' (17@170-179)
539 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
540 sending: '(' (18@180-189)
541 sending: 'x' (19@190-199)
542 thing (19@190-199): 'x' (19@190-199)
543 sending: ')' (20@200-209)
544 200-209: memory exhausted
545 Freeing nterm thing (19@190-199)
546 Freeing nterm line (15@150-179)
547 Freeing nterm line (12@120-149)
548 Freeing nterm line (9@90-119)
549 Freeing nterm line (6@60-89)
550 Freeing nterm line (3@30-59)
551 Freeing nterm line (0@0-29)
552 Parsing FAILED (status 2).
559 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
560 # ---------------------------------------------------------------------------
561 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
562 [AT_SETUP([Printers and Destructors $2: $1])
565 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
576 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
577 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
579 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
580 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
582 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
583 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
587 ## ----------------------------------------- ##
588 ## Default tagless %printer and %destructor. ##
589 ## ----------------------------------------- ##
591 # Check that the right %printer and %destructor are called, that they're not
592 # called for $end, and that $$ and @$ work correctly.
594 AT_SETUP([Default tagless %printer and %destructor])
596 AT_DATA_GRAMMAR([[input.y]],
601 @$.first_line = @$.last_line = 1;
602 @$.first_column = @$.last_column = 1;
608 static void yyerror (const char *msg);
609 static int yylex (void);
614 fprintf (yyoutput, "<*> printer should not be called.\n");
618 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
621 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
625 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
628 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
632 fprintf (yyoutput, "<*> destructor should not be called.\n");
637 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
644 static char const input[] = "abcd";
645 static size_t toknum;
646 if (! (toknum < sizeof input))
648 yylval = input[toknum++];
649 yylloc.first_line = yylloc.last_line = 1;
650 yylloc.first_column = yylloc.last_column = toknum;
655 yyerror (const char *msg)
657 fprintf (stderr, "%s\n", msg);
668 AT_CHECK([bison -o input.c input.y])
670 AT_PARSER_CHECK([./input], 1,
671 [[<> destructor for 'd' @ 4.
672 'b'/'c' destructor for 'c' @ 3.
673 'b'/'c' destructor for 'b' @ 2.
674 <> destructor for 'a' @ 1.
678 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
679 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
681 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
682 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
684 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
685 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
687 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
688 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
690 Reading a token: Now at end of input.
691 syntax error, unexpected $end, expecting 'e'
692 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
694 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
696 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
698 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
700 Cleanup: discarding lookahead token $end (1.5-1.5: )
708 ## ------------------------------------------------------ ##
709 ## Default tagged and per-type %printer and %destructor. ##
710 ## ------------------------------------------------------ ##
712 AT_SETUP([Default tagged and per-type %printer and %destructor])
714 AT_DATA_GRAMMAR([[input.y]],
721 static void yyerror (const char *msg);
722 static int yylex (void);
727 fprintf (yyoutput, "<> printer should not be called.\n");
730 %union { int field0; int field1; int field2; }
731 %type <field0> start 'a' 'g'
735 fprintf (yyoutput, "<*>/<field2>/e printer");
738 fprintf (stdout, "<*>/<field2>/e destructor.\n");
742 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
743 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
746 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
747 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
750 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
751 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
754 fprintf (yyoutput, "<> destructor should not be called.\n");
760 'a' 'b' 'c' 'd' 'e' 'f' 'g'
762 USE(($1, $2, $3, $4, $5, $6, $7));
772 static char const input[] = "abcdef";
773 static size_t toknum;
774 if (! (toknum < sizeof input))
776 return input[toknum++];
780 yyerror (const char *msg)
782 fprintf (stderr, "%s\n", msg);
793 AT_CHECK([bison -o input.c input.y])
795 AT_PARSER_CHECK([./input], 1,
796 [[<*>/<field2>/e destructor.
797 <*>/<field2>/e destructor.
801 <*>/<field2>/e destructor.
805 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
806 Shifting token 'a' (<*>/<field2>/e printer)
808 Reading a token: Next token is token 'b' (<field1> printer)
809 Shifting token 'b' (<field1> printer)
811 Reading a token: Next token is token 'c' ('c' printer)
812 Shifting token 'c' ('c' printer)
814 Reading a token: Next token is token 'd' ('d' printer)
815 Shifting token 'd' ('d' printer)
817 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
818 Shifting token 'e' (<*>/<field2>/e printer)
820 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
821 Shifting token 'f' (<*>/<field2>/e printer)
823 Reading a token: Now at end of input.
824 syntax error, unexpected $end, expecting 'g'
825 Error: popping token 'f' (<*>/<field2>/e printer)
826 Stack now 0 1 3 5 6 7
827 Error: popping token 'e' (<*>/<field2>/e printer)
829 Error: popping token 'd' ('d' printer)
831 Error: popping token 'c' ('c' printer)
833 Error: popping token 'b' (<field1> printer)
835 Error: popping token 'a' (<*>/<field2>/e printer)
837 Cleanup: discarding lookahead token $end ()
845 ## ------------------------------------------------------------- ##
846 ## Default %printer and %destructor for user-defined end token. ##
847 ## ------------------------------------------------------------- ##
849 AT_SETUP([Default %printer and %destructor for user-defined end token])
851 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
852 # -----------------------------------------------------------------------------
853 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
855 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
856 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
858 AT_DATA_GRAMMAR([[input]]$1[[.y]],
863 @$.first_line = @$.last_line = 1;
864 @$.first_column = @$.last_column = 1;
870 static void yyerror (const char *msg);
871 static int yylex (void);
876 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
881 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
884 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
888 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
893 [[[%union { char tag; }
894 %type <tag> start END]]])[[
898 start: { $$ = 'S'; } ;
908 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
909 yylloc.first_line = yylloc.last_line = 1;
910 yylloc.first_column = yylloc.last_column = 1;
915 yyerror (const char *msg)
917 fprintf (stderr, "%s\n", msg);
928 AT_CHECK([bison -o input$1.c input$1.y])
929 AT_COMPILE([input$1])
930 AT_PARSER_CHECK([./input$1], 0,
931 [[<]]kind[[> for 'E' @ 1.
932 <]]kind[[> for 'S' @ 1.
936 Reducing stack by rule 1 (line 46):
937 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
940 Reading a token: Now at end of input.
941 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
944 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
945 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
949 m4_popdef([not_kind])
952 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
953 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
959 ## ------------------------------------------------------------------ ##
960 ## Default %printer and %destructor are not for error or $undefined. ##
961 ## ------------------------------------------------------------------ ##
963 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
965 # If Bison were to apply the default %printer and %destructor to the error
966 # token or to $undefined:
967 # - For the error token:
968 # - It would generate warnings for unused $n.
969 # - It would invoke the %printer and %destructor on the error token's
970 # semantic value, which would be initialized from the lookahead, which
971 # would be destroyed separately.
972 # - For $undefined, who knows what the semantic value would be.
974 AT_DATA_GRAMMAR([[input.y]],
980 static void yyerror (const char *msg);
981 static int yylex (void);
986 fprintf (yyoutput, "'%c'", $$);
989 fprintf (stderr, "DESTROY '%c'\n", $$);
996 /* In order to reveal the problems that this bug caused during parsing, add
998 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1006 static char const input[] = "abd";
1007 static size_t toknum;
1008 if (! (toknum < sizeof input))
1010 yylval = input[toknum++];
1015 yyerror (const char *msg)
1017 fprintf (stderr, "%s\n", msg);
1028 AT_CHECK([bison -o input.c input.y])
1030 AT_PARSER_CHECK([./input], [1], [],
1033 Reading a token: Next token is token 'a' ('a')
1034 Shifting token 'a' ('a')
1036 Reading a token: Next token is token 'b' ('b')
1038 Shifting token error ()
1040 Next token is token 'b' ('b')
1041 Shifting token 'b' ('b')
1043 Reading a token: Next token is token $undefined ()
1044 Error: popping token 'b' ('b')
1047 Error: popping token error ()
1049 Shifting token error ()
1051 Next token is token $undefined ()
1052 Error: discarding token $undefined ()
1053 Error: popping token error ()
1055 Shifting token error ()
1057 Reading a token: Now at end of input.
1058 Cleanup: discarding lookahead token $end ()
1060 Cleanup: popping token error ()
1061 Cleanup: popping token 'a' ('a')
1069 ## ------------------------------------------------------ ##
1070 ## Default %printer and %destructor are not for $accept. ##
1071 ## ------------------------------------------------------ ##
1073 AT_SETUP([Default %printer and %destructor are not for $accept])
1075 # If YYSTYPE is a union and Bison were to apply the default %printer and
1076 # %destructor to $accept:
1077 # - The %printer and %destructor code generated for $accept would always be
1078 # dead code because $accept is currently never shifted onto the stack.
1079 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1080 # to declare `%type <field> $accept'. (Also true for $undefined.)
1081 # - Thus, the compiler might complain that the user code assumes the wrong
1082 # type for $$ since the code might assume the type associated with a
1083 # specific union field, which is especially reasonable in C++ since that
1084 # type may be a base type. This test case checks for this problem. (Also
1085 # true for $undefined and the error token, so there are three warnings for
1086 # %printer and three for %destructor.)
1088 AT_DATA_GRAMMAR([[input.y]],
1089 [[%debug /* So that %printer is actually compiled. */
1093 # include <stdlib.h>
1094 static void yyerror (const char *msg);
1095 static int yylex (void);
1101 fprintf (yyoutput, "'%c'", chr);
1105 fprintf (stderr, "DESTROY '%c'\n", chr);
1108 %union { char chr; }
1113 start: { USE($$); } ;
1127 yyerror (const char *msg)
1129 fprintf (stderr, "%s\n", msg);
1139 AT_CHECK([bison -o input.c input.y])
1146 ## ------------------------------------------------------ ##
1147 ## Default %printer and %destructor for mid-rule values. ##
1148 ## ------------------------------------------------------ ##
1150 AT_SETUP([Default %printer and %destructor for mid-rule values])
1152 AT_DATA_GRAMMAR([[input.y]],
1153 [[%debug /* So that %printer is actually compiled. */
1157 # include <stdlib.h>
1158 static void yyerror (const char *msg);
1159 static int yylex (void);
1161 # define YYLTYPE int
1162 # define YYLLOC_DEFAULT(Current, Rhs, N)
1163 # define YY_LOCATION_PRINT(File, Loc)
1166 %printer { fprintf (yyoutput, "%d", @$); } <>
1167 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1168 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1169 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1174 { @$ = 1; } // Not set or used.
1175 { USE ($$); @$ = 2; } // Both set and used.
1176 { USE ($$); @$ = 3; } // Only set.
1177 { @$ = 4; } // Only used.
1179 { USE (($$, $2, $4, $5)); @$ = 0; }
1194 yyerror (const char *msg)
1196 fprintf (stderr, "%s\n", msg);
1207 AT_CHECK([bison -o input.c input.y], 0,,
1208 [[input.y:33.3-23: warning: unset value: $$
1209 input.y:30.3-35.37: warning: unused value: $3
1213 AT_PARSER_CHECK([./input], 1,,
1216 Reducing stack by rule 1 (line 30):
1217 -> $$ = nterm $@1 (: )
1220 Reducing stack by rule 2 (line 31):
1221 -> $$ = nterm @2 (: 2)
1224 Reducing stack by rule 3 (line 32):
1225 -> $$ = nterm @3 (: 3)
1228 Reducing stack by rule 4 (line 33):
1229 -> $$ = nterm @4 (: 4)
1232 Reading a token: Now at end of input.
1234 Error: popping nterm @4 (: 4)
1237 Error: popping nterm @3 (: 3)
1240 Error: popping nterm @2 (: 2)
1243 Error: popping nterm $@1 (: )
1245 Cleanup: discarding lookahead token $end (: )
1252 ## ----------------------- ##
1253 ## @$ implies %locations. ##
1254 ## ----------------------- ##
1256 # Bison once forgot to check for @$ in actions other than semantic actions.
1258 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1259 # -------------------------------------------------------
1260 m4_define([AT_CHECK_ACTION_LOCATIONS],
1261 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1263 AT_DATA_GRAMMAR([[input.y]],
1266 static int yylex (void);
1267 static void yyerror (char const *msg);
1273 printf ("%d\n", @$.first_line);
1274 } ]m4_if($1, [%initial-action], [], [[start]])[
1289 yyerror (char const *msg)
1291 fprintf (stderr, "%s\n", msg);
1301 AT_CHECK([[bison -o input.c input.y]])
1302 AT_COMPILE([[input]])
1306 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1307 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1308 AT_CHECK_ACTION_LOCATIONS([[%printer]])