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 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_CHECK([bison -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_CHECK([bison -d -v -o input.c input.y], 0)
157 AT_PARSER_CHECK([./input], 0,
165 ## -------------------------- ##
166 ## Printers and Destructors. ##
167 ## -------------------------- ##
169 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
170 # -----------------------------------------------------------------------------
171 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
172 [# Make sure complex $n work.
173 m4_if([$1$2$3], $[1]$[2]$[3], [],
174 [m4_fatal([$0: Invalid arguments: $@])])dnl
176 # Be sure to pass all the %directives to this macro to have correct
177 # helping macros. So don't put any directly in the Bison file.
178 AT_BISON_OPTION_PUSHDEFS([$5])
179 AT_DATA_GRAMMAR([[input.y]],
186 #define YYINITDEPTH 10
187 #define YYMAXDEPTH 10
189 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
190 [#define RANGE(Location) (Location).first_line, (Location).last_line])
194 m4_ifval([$6], [%union
198 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
199 m4_ifval([$6], [[%code provides {]], [[%code {]])
200 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
201 [static int yylex (]AT_LEX_FORMALS[);
202 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
205 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
207 /* FIXME: This %printer isn't actually tested. */
210 ]AT_LALR1_CC_IF([debug_stream () << $$;],
211 [fprintf (yyoutput, "%d", $$)])[;
213 input line thing 'x' 'y'
216 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
220 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
224 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
228 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
232 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
237 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
242 This grammar is made to exercise error recovery.
243 "Lines" starting with `(' support error recovery, with
244 ')' as synchronizing token. Lines starting with 'x' can never
245 be recovered from if in error.
252 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
254 | line input /* Right recursive to load the stack so that popping at
255 END can be exercised. */
258 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
259 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
264 thing thing thing ';'
267 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
268 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
269 $3, RANGE (@3), $4, RANGE (@4));
271 | '(' thing thing ')'
274 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
275 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
276 $3, RANGE (@3), $4, RANGE (@4));
281 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
282 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
287 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
288 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
296 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
297 $$, RANGE (@$), $1, RANGE (@1));
301 /* Alias to ARGV[1]. */
302 const char *source = 0;
305 yylex (]AT_LEX_FORMALS[)
307 static unsigned int counter = 0;
309 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
310 /* As in BASIC, line numbers go from 10 to 10. */
312 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
313 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
315 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
316 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
319 if (! (0 <= c && c <= strlen (source)))
322 printf ("sending: '%c'", source[c]);
324 printf ("sending: END");
325 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
330 [/* A C++ error reporting function. */
332 yy::parser::error (const location& l, const std::string& m)
334 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
342 parser.set_debug_level (yydebug);
343 return parser.parse ();
347 yyerror (const char *msg)
349 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
353 main (int argc, const char *argv[])
356 yydebug = !!getenv ("YYDEBUG");
362 case 0: printf ("Successful parse.\n"); break;
363 case 1: printf ("Parsing FAILED.\n"); break;
364 default: printf ("Parsing FAILED (status %d).\n", status); break;
371 [AT_CHECK([bison -o input.cc input.y])
372 AT_COMPILE_CXX([input])],
373 [AT_CHECK([bison -o input.c input.y])
374 AT_COMPILE([input])])
377 # Check the location of "empty"
378 # -----------------------------
379 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
380 # an empty "line" nterm.
381 # FIXME: This location is not satisfying. Depend on the lookahead?
382 AT_PARSER_CHECK([./input '(x)'], 0,
383 [[sending: '(' (0@0-9)
384 sending: 'x' (1@10-19)
385 thing (1@10-19): 'x' (1@10-19)
386 sending: ')' (2@20-29)
387 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
388 sending: END (3@30-39)
389 input (0@29-29): /* Nothing */
390 input (2@0-29): line (0@0-29) input (0@29-29)
391 Freeing token END (3@30-39)
392 Freeing nterm input (2@0-29)
397 # Check locations in error recovery
398 # ---------------------------------
399 # '(y)' is an error, but can be recovered from. But what's the location
400 # of the error itself ('y'), and of the resulting reduction ('(error)').
401 AT_PARSER_CHECK([./input '(y)'], 0,
402 [[sending: '(' (0@0-9)
403 sending: 'y' (1@10-19)
404 10-19: syntax error, unexpected 'y', expecting 'x'
405 Freeing token 'y' (1@10-19)
406 sending: ')' (2@20-29)
407 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
408 sending: END (3@30-39)
409 input (0@29-29): /* Nothing */
410 input (2@0-29): line (-1@0-29) input (0@29-29)
411 Freeing token END (3@30-39)
412 Freeing nterm input (2@0-29)
417 # Syntax errors caught by the parser
418 # ----------------------------------
419 # Exercise the discarding of stack top and input until `error'
422 # '(', 'x', 'x', 'x', 'x', 'x', ')',
424 # Load the stack and provoke an error that cannot be caught by the
425 # grammar, to check that the stack is cleared. And make sure the
426 # lookahead is freed.
431 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
432 [[sending: '(' (0@0-9)
433 sending: 'x' (1@10-19)
434 thing (1@10-19): 'x' (1@10-19)
435 sending: 'x' (2@20-29)
436 thing (2@20-29): 'x' (2@20-29)
437 sending: 'x' (3@30-39)
438 30-39: syntax error, unexpected 'x', expecting ')'
439 Freeing nterm thing (2@20-29)
440 Freeing nterm thing (1@10-19)
441 Freeing token 'x' (3@30-39)
442 sending: 'x' (4@40-49)
443 Freeing token 'x' (4@40-49)
444 sending: 'x' (5@50-59)
445 Freeing token 'x' (5@50-59)
446 sending: ')' (6@60-69)
447 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
448 sending: '(' (7@70-79)
449 sending: 'x' (8@80-89)
450 thing (8@80-89): 'x' (8@80-89)
451 sending: ')' (9@90-99)
452 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
453 sending: '(' (10@100-109)
454 sending: 'x' (11@110-119)
455 thing (11@110-119): 'x' (11@110-119)
456 sending: ')' (12@120-129)
457 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
458 sending: 'y' (13@130-139)
459 input (0@129-129): /* Nothing */
460 input (2@100-129): line (10@100-129) input (0@129-129)
461 input (2@70-129): line (7@70-99) input (2@100-129)
462 input (2@0-129): line (-1@0-69) input (2@70-129)
463 130-139: syntax error, unexpected 'y', expecting END
464 Freeing nterm input (2@0-129)
465 Freeing token 'y' (13@130-139)
470 # Syntax error caught by the parser where lookahead = END
471 # --------------------------------------------------------
472 # Load the stack and provoke an error that cannot be caught by the
473 # grammar, to check that the stack is cleared. And make sure the
474 # lookahead is freed.
479 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
480 [[sending: '(' (0@0-9)
481 sending: 'x' (1@10-19)
482 thing (1@10-19): 'x' (1@10-19)
483 sending: ')' (2@20-29)
484 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
485 sending: '(' (3@30-39)
486 sending: 'x' (4@40-49)
487 thing (4@40-49): 'x' (4@40-49)
488 sending: ')' (5@50-59)
489 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
490 sending: 'x' (6@60-69)
491 thing (6@60-69): 'x' (6@60-69)
492 sending: END (7@70-79)
493 70-79: syntax error, unexpected END, expecting 'x'
494 Freeing nterm thing (6@60-69)
495 Freeing nterm line (3@30-59)
496 Freeing nterm line (0@0-29)
497 Freeing token END (7@70-79)
502 # Check destruction upon stack overflow
503 # -------------------------------------
504 # Upon stack overflow, all symbols on the stack should be destroyed.
505 # Only check for yacc.c.
507 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
508 [[sending: '(' (0@0-9)
509 sending: 'x' (1@10-19)
510 thing (1@10-19): 'x' (1@10-19)
511 sending: ')' (2@20-29)
512 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
513 sending: '(' (3@30-39)
514 sending: 'x' (4@40-49)
515 thing (4@40-49): 'x' (4@40-49)
516 sending: ')' (5@50-59)
517 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
518 sending: '(' (6@60-69)
519 sending: 'x' (7@70-79)
520 thing (7@70-79): 'x' (7@70-79)
521 sending: ')' (8@80-89)
522 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
523 sending: '(' (9@90-99)
524 sending: 'x' (10@100-109)
525 thing (10@100-109): 'x' (10@100-109)
526 sending: ')' (11@110-119)
527 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
528 sending: '(' (12@120-129)
529 sending: 'x' (13@130-139)
530 thing (13@130-139): 'x' (13@130-139)
531 sending: ')' (14@140-149)
532 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
533 sending: '(' (15@150-159)
534 sending: 'x' (16@160-169)
535 thing (16@160-169): 'x' (16@160-169)
536 sending: ')' (17@170-179)
537 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
538 sending: '(' (18@180-189)
539 sending: 'x' (19@190-199)
540 thing (19@190-199): 'x' (19@190-199)
541 sending: ')' (20@200-209)
542 200-209: memory exhausted
543 Freeing nterm thing (19@190-199)
544 Freeing nterm line (15@150-179)
545 Freeing nterm line (12@120-149)
546 Freeing nterm line (9@90-119)
547 Freeing nterm line (6@60-89)
548 Freeing nterm line (3@30-59)
549 Freeing nterm line (0@0-29)
550 Parsing FAILED (status 2).
557 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
558 # ---------------------------------------------------------------------------
559 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
560 [AT_SETUP([Printers and Destructors $2: $1])
563 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
574 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
575 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
577 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
578 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
580 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
581 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
585 ## ----------------------------------------- ##
586 ## Default tagless %printer and %destructor. ##
587 ## ----------------------------------------- ##
589 # Check that the right %printer and %destructor are called, that they're not
590 # called for $end, and that $$ and @$ work correctly.
592 AT_SETUP([Default tagless %printer and %destructor])
594 AT_DATA_GRAMMAR([[input.y]],
599 @$.first_line = @$.last_line = 1;
600 @$.first_column = @$.last_column = 1;
606 static void yyerror (const char *msg);
607 static int yylex (void);
612 fprintf (yyoutput, "<*> printer should not be called.\n");
616 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
619 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
623 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
626 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
630 fprintf (yyoutput, "<*> destructor should not be called.\n");
635 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
642 static char const input[] = "abcd";
643 static size_t toknum;
644 if (! (toknum < sizeof input))
646 yylval = input[toknum++];
647 yylloc.first_line = yylloc.last_line = 1;
648 yylloc.first_column = yylloc.last_column = toknum;
653 yyerror (const char *msg)
655 fprintf (stderr, "%s\n", msg);
666 AT_CHECK([bison -o input.c input.y])
668 AT_PARSER_CHECK([./input], 1,
669 [[<> destructor for 'd' @ 4.
670 'b'/'c' destructor for 'c' @ 3.
671 'b'/'c' destructor for 'b' @ 2.
672 <> destructor for 'a' @ 1.
676 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
677 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
679 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
680 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
682 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
683 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
685 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
686 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
688 Reading a token: Now at end of input.
689 syntax error, unexpected $end, expecting 'e'
690 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
692 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
694 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
696 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
698 Cleanup: discarding lookahead token $end (1.5-1.5: )
706 ## ------------------------------------------------------ ##
707 ## Default tagged and per-type %printer and %destructor. ##
708 ## ------------------------------------------------------ ##
710 AT_SETUP([Default tagged and per-type %printer and %destructor])
712 AT_DATA_GRAMMAR([[input.y]],
719 static void yyerror (const char *msg);
720 static int yylex (void);
725 fprintf (yyoutput, "<> printer should not be called.\n");
728 %union { int field0; int field1; int field2; }
729 %type <field0> start 'a' 'g'
733 fprintf (yyoutput, "<*>/<field2>/e printer");
736 fprintf (stdout, "<*>/<field2>/e destructor.\n");
740 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
741 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
744 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
745 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
748 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
749 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
752 fprintf (yyoutput, "<> destructor should not be called.\n");
758 'a' 'b' 'c' 'd' 'e' 'f' 'g'
760 USE(($1, $2, $3, $4, $5, $6, $7));
770 static char const input[] = "abcdef";
771 static size_t toknum;
772 if (! (toknum < sizeof input))
774 return input[toknum++];
778 yyerror (const char *msg)
780 fprintf (stderr, "%s\n", msg);
791 AT_CHECK([bison -o input.c input.y])
793 AT_PARSER_CHECK([./input], 1,
794 [[<*>/<field2>/e destructor.
795 <*>/<field2>/e destructor.
799 <*>/<field2>/e destructor.
803 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
804 Shifting token 'a' (<*>/<field2>/e printer)
806 Reading a token: Next token is token 'b' (<field1> printer)
807 Shifting token 'b' (<field1> printer)
809 Reading a token: Next token is token 'c' ('c' printer)
810 Shifting token 'c' ('c' printer)
812 Reading a token: Next token is token 'd' ('d' printer)
813 Shifting token 'd' ('d' printer)
815 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
816 Shifting token 'e' (<*>/<field2>/e printer)
818 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
819 Shifting token 'f' (<*>/<field2>/e printer)
821 Reading a token: Now at end of input.
822 syntax error, unexpected $end, expecting 'g'
823 Error: popping token 'f' (<*>/<field2>/e printer)
824 Stack now 0 1 3 5 6 7
825 Error: popping token 'e' (<*>/<field2>/e printer)
827 Error: popping token 'd' ('d' printer)
829 Error: popping token 'c' ('c' printer)
831 Error: popping token 'b' (<field1> printer)
833 Error: popping token 'a' (<*>/<field2>/e printer)
835 Cleanup: discarding lookahead token $end ()
843 ## ------------------------------------------------------------- ##
844 ## Default %printer and %destructor for user-defined end token. ##
845 ## ------------------------------------------------------------- ##
847 AT_SETUP([Default %printer and %destructor for user-defined end token])
849 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
850 # -----------------------------------------------------------------------------
851 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
853 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
854 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
856 AT_DATA_GRAMMAR([[input]]$1[[.y]],
861 @$.first_line = @$.last_line = 1;
862 @$.first_column = @$.last_column = 1;
868 static void yyerror (const char *msg);
869 static int yylex (void);
874 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
879 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
882 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
886 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
891 [[[%union { char tag; }
892 %type <tag> start END]]])[[
896 start: { $$ = 'S'; } ;
906 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
907 yylloc.first_line = yylloc.last_line = 1;
908 yylloc.first_column = yylloc.last_column = 1;
913 yyerror (const char *msg)
915 fprintf (stderr, "%s\n", msg);
926 AT_CHECK([bison -o input$1.c input$1.y])
927 AT_COMPILE([input$1])
928 AT_PARSER_CHECK([./input$1], 0,
929 [[<]]kind[[> for 'E' @ 1.
930 <]]kind[[> for 'S' @ 1.
934 Reducing stack by rule 1 (line 46):
935 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
938 Reading a token: Now at end of input.
939 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
942 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
943 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
947 m4_popdef([not_kind])
950 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
951 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
957 ## ------------------------------------------------------------------ ##
958 ## Default %printer and %destructor are not for error or $undefined. ##
959 ## ------------------------------------------------------------------ ##
961 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
963 # If Bison were to apply the default %printer and %destructor to the error
964 # token or to $undefined:
965 # - For the error token:
966 # - It would generate warnings for unused $n.
967 # - It would invoke the %printer and %destructor on the error token's
968 # semantic value, which would be initialized from the lookahead, which
969 # would be destroyed separately.
970 # - For $undefined, who knows what the semantic value would be.
972 AT_DATA_GRAMMAR([[input.y]],
978 static void yyerror (const char *msg);
979 static int yylex (void);
984 fprintf (yyoutput, "'%c'", $$);
987 fprintf (stderr, "DESTROY '%c'\n", $$);
994 /* In order to reveal the problems that this bug caused during parsing, add
996 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1004 static char const input[] = "abd";
1005 static size_t toknum;
1006 if (! (toknum < sizeof input))
1008 yylval = input[toknum++];
1013 yyerror (const char *msg)
1015 fprintf (stderr, "%s\n", msg);
1026 AT_CHECK([bison -o input.c input.y])
1028 AT_PARSER_CHECK([./input], [1], [],
1031 Reading a token: Next token is token 'a' ('a')
1032 Shifting token 'a' ('a')
1034 Reading a token: Next token is token 'b' ('b')
1036 Shifting token error ()
1038 Next token is token 'b' ('b')
1039 Shifting token 'b' ('b')
1041 Reading a token: Next token is token $undefined ()
1042 Error: popping token 'b' ('b')
1045 Error: popping token error ()
1047 Shifting token error ()
1049 Next token is token $undefined ()
1050 Error: discarding token $undefined ()
1051 Error: popping token error ()
1053 Shifting token error ()
1055 Reading a token: Now at end of input.
1056 Cleanup: discarding lookahead token $end ()
1058 Cleanup: popping token error ()
1059 Cleanup: popping token 'a' ('a')
1067 ## ------------------------------------------------------ ##
1068 ## Default %printer and %destructor are not for $accept. ##
1069 ## ------------------------------------------------------ ##
1071 AT_SETUP([Default %printer and %destructor are not for $accept])
1073 # If YYSTYPE is a union and Bison were to apply the default %printer and
1074 # %destructor to $accept:
1075 # - The %printer and %destructor code generated for $accept would always be
1076 # dead code because $accept is currently never shifted onto the stack.
1077 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1078 # to declare `%type <field> $accept'. (Also true for $undefined.)
1079 # - Thus, the compiler might complain that the user code assumes the wrong
1080 # type for $$ since the code might assume the type associated with a
1081 # specific union field, which is especially reasonable in C++ since that
1082 # type may be a base type. This test case checks for this problem. (Also
1083 # true for $undefined and the error token, so there are three warnings for
1084 # %printer and three for %destructor.)
1086 AT_DATA_GRAMMAR([[input.y]],
1087 [[%debug /* So that %printer is actually compiled. */
1091 # include <stdlib.h>
1092 static void yyerror (const char *msg);
1093 static int yylex (void);
1099 fprintf (yyoutput, "'%c'", chr);
1103 fprintf (stderr, "DESTROY '%c'\n", chr);
1106 %union { char chr; }
1111 start: { USE($$); } ;
1125 yyerror (const char *msg)
1127 fprintf (stderr, "%s\n", msg);
1137 AT_CHECK([bison -o input.c input.y])
1144 ## ------------------------------------------------------ ##
1145 ## Default %printer and %destructor for mid-rule values. ##
1146 ## ------------------------------------------------------ ##
1148 AT_SETUP([Default %printer and %destructor for mid-rule values])
1150 AT_DATA_GRAMMAR([[input.y]],
1151 [[%debug /* So that %printer is actually compiled. */
1155 # include <stdlib.h>
1156 static void yyerror (const char *msg);
1157 static int yylex (void);
1159 # define YYLTYPE int
1160 # define YYLLOC_DEFAULT(Current, Rhs, N)
1161 # define YY_LOCATION_PRINT(File, Loc)
1164 %printer { fprintf (yyoutput, "%d", @$); } <>
1165 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1166 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1167 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1172 { @$ = 1; } // Not set or used.
1173 { USE ($$); @$ = 2; } // Both set and used.
1174 { USE ($$); @$ = 3; } // Only set.
1175 { @$ = 4; } // Only used.
1177 { USE (($$, $2, $4, $5)); @$ = 0; }
1192 yyerror (const char *msg)
1194 fprintf (stderr, "%s\n", msg);
1205 AT_CHECK([bison -o input.c input.y], 0,,
1206 [[input.y:33.3-23: warning: unset value: $$
1207 input.y:30.3-35.37: warning: unused value: $3
1211 AT_PARSER_CHECK([./input], 1,,
1214 Reducing stack by rule 1 (line 30):
1215 -> $$ = nterm $@1 (: )
1218 Reducing stack by rule 2 (line 31):
1219 -> $$ = nterm @2 (: 2)
1222 Reducing stack by rule 3 (line 32):
1223 -> $$ = nterm @3 (: 3)
1226 Reducing stack by rule 4 (line 33):
1227 -> $$ = nterm @4 (: 4)
1230 Reading a token: Now at end of input.
1232 Error: popping nterm @4 (: 4)
1235 Error: popping nterm @3 (: 3)
1238 Error: popping nterm @2 (: 2)
1241 Error: popping nterm $@1 (: )
1243 Cleanup: discarding lookahead token $end (: )
1250 ## ----------------------- ##
1251 ## @$ implies %locations. ##
1252 ## ----------------------- ##
1254 # Bison once forgot to check for @$ in actions other than semantic actions.
1256 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1257 # -------------------------------------------------------
1258 m4_define([AT_CHECK_ACTION_LOCATIONS],
1259 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1261 AT_DATA_GRAMMAR([[input.y]],
1264 static int yylex (void);
1265 static void yyerror (char const *msg);
1271 printf ("%d\n", @$.first_line);
1272 } ]m4_if($1, [%initial-action], [], [[start]])[
1287 yyerror (char const *msg)
1289 fprintf (stderr, "%s\n", msg);
1299 AT_CHECK([[bison -o input.c input.y]])
1300 AT_COMPILE([[input]])
1304 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1305 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1306 AT_CHECK_ACTION_LOCATIONS([[%printer]])