1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 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 2, or (at your option)
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, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 AT_BANNER([[User Actions.]])
21 ## ------------------ ##
22 ## Mid-rule actions. ##
23 ## ------------------ ##
25 AT_SETUP([Mid-rule actions])
27 # Bison once forgot the mid-rule actions. It was because the action
28 # was attached to the host rule (the one with the mid-rule action),
29 # instead of being attached to the empty rule dedicated to this
32 AT_DATA_GRAMMAR([[input.y]],
38 static void yyerror (const char *msg);
39 static int yylex (void);
42 exp: { putchar ('0'); }
43 '1' { putchar ('1'); }
44 '2' { putchar ('2'); }
45 '3' { putchar ('3'); }
46 '4' { putchar ('4'); }
47 '5' { putchar ('5'); }
48 '6' { putchar ('6'); }
49 '7' { putchar ('7'); }
50 '8' { putchar ('8'); }
51 '9' { putchar ('9'); }
58 static char const input[] = "123456789";
60 if (! (toknum < sizeof input))
62 return input[toknum++];
66 yyerror (const char *msg)
68 fprintf (stderr, "%s\n", msg);
78 AT_CHECK([bison -d -v -o input.c input.y])
80 AT_PARSER_CHECK([./input], 0,
90 ## ---------------- ##
92 ## ---------------- ##
94 AT_SETUP([Exotic Dollars])
96 AT_DATA_GRAMMAR([[input.y]],
102 static void yyerror (const char *msg);
103 static int yylex (void);
112 %type <val> a_1 a_2 a_5
113 sum_of_the_five_previous_values
116 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
117 sum_of_the_five_previous_values
119 USE (($1, $2, $<foo>3, $<foo>4, $5));
127 sum_of_the_five_previous_values:
129 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
144 yyerror (const char *msg)
146 fprintf (stderr, "%s\n", msg);
156 AT_CHECK([bison -d -v -o input.c input.y], 0)
158 AT_PARSER_CHECK([./input], 0,
166 ## -------------------------- ##
167 ## Printers and Destructors. ##
168 ## -------------------------- ##
170 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
171 # -----------------------------------------------------------------------------
172 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
173 [# Make sure complex $n work.
174 m4_if([$1$2$3], $[1]$[2]$[3], [],
175 [m4_fatal([$0: Invalid arguments: $@])])dnl
177 # Be sure to pass all the %directives to this macro to have correct
178 # helping macros. So don't put any directly in the Bison file.
179 AT_BISON_OPTION_PUSHDEFS([$5])
180 AT_DATA_GRAMMAR([[input.y]],
187 #define YYINITDEPTH 10
188 #define YYMAXDEPTH 10
190 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
191 [#define RANGE(Location) (Location).first_line, (Location).last_line])
195 m4_ifval([$6], [%union
199 AT_LALR1_CC_IF([%define "global_tokens_and_yystype"])
200 m4_ifval([$6], [[%provides {]], [[%code {]])
201 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
202 [static int yylex (]AT_LEX_FORMALS[);
203 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
206 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
208 /* FIXME: This %printer isn't actually tested. */
211 ]AT_LALR1_CC_IF([debug_stream () << $$;],
212 [fprintf (yyoutput, "%d", $$)])[;
214 input line thing 'x' 'y'
217 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
221 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
225 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
229 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
233 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
238 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
243 This grammar is made to exercise error recovery.
244 "Lines" starting with `(' support error recovery, with
245 ')' as synchronizing token. Lines starting with 'x' can never
246 be recovered from if in error.
253 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
255 | line input /* Right recursive to load the stack so that popping at
256 END can be exercised. */
259 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
260 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
265 thing thing thing ';'
268 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
269 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
270 $3, RANGE (@3), $4, RANGE (@4));
272 | '(' thing thing ')'
275 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
276 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
277 $3, RANGE (@3), $4, RANGE (@4));
282 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
283 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
288 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
289 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
297 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
298 $$, RANGE (@$), $1, RANGE (@1));
302 /* Alias to ARGV[1]. */
303 const char *source = 0;
306 yylex (]AT_LEX_FORMALS[)
308 static unsigned int counter = 0;
310 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
311 /* As in BASIC, line numbers go from 10 to 10. */
313 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
314 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
316 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
317 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
320 if (! (0 <= c && c <= strlen (source)))
323 printf ("sending: '%c'", source[c]);
325 printf ("sending: END");
326 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
331 [/* A C++ error reporting function. */
333 yy::parser::error (const location& l, const std::string& m)
335 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
343 parser.set_debug_level (yydebug);
344 return parser.parse ();
348 yyerror (const char *msg)
350 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
354 main (int argc, const char *argv[])
357 yydebug = !!getenv ("YYDEBUG");
363 case 0: printf ("Successful parse.\n"); break;
364 case 1: printf ("Parsing FAILED.\n"); break;
365 default: printf ("Parsing FAILED (status %d).\n", status); break;
372 [AT_CHECK([bison -o input.cc input.y])
373 AT_COMPILE_CXX([input])],
374 [AT_CHECK([bison -o input.c input.y])
375 AT_COMPILE([input])])
378 # Check the location of "empty"
379 # -----------------------------
380 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
381 # an empty "line" nterm.
382 # FIXME: This location is not satisfying. Depend on the lookahead?
383 AT_PARSER_CHECK([./input '(x)'], 0,
384 [[sending: '(' (0@0-9)
385 sending: 'x' (1@10-19)
386 thing (1@10-19): 'x' (1@10-19)
387 sending: ')' (2@20-29)
388 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
389 sending: END (3@30-39)
390 input (0@29-29): /* Nothing */
391 input (2@0-29): line (0@0-29) input (0@29-29)
392 Freeing token END (3@30-39)
393 Freeing nterm input (2@0-29)
398 # Check locations in error recovery
399 # ---------------------------------
400 # '(y)' is an error, but can be recovered from. But what's the location
401 # of the error itself ('y'), and of the resulting reduction ('(error)').
402 AT_PARSER_CHECK([./input '(y)'], 0,
403 [[sending: '(' (0@0-9)
404 sending: 'y' (1@10-19)
405 10-19: syntax error, unexpected 'y', expecting 'x'
406 Freeing token 'y' (1@10-19)
407 sending: ')' (2@20-29)
408 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
409 sending: END (3@30-39)
410 input (0@29-29): /* Nothing */
411 input (2@0-29): line (-1@0-29) input (0@29-29)
412 Freeing token END (3@30-39)
413 Freeing nterm input (2@0-29)
418 # Syntax errors caught by the parser
419 # ----------------------------------
420 # Exercise the discarding of stack top and input until `error'
423 # '(', 'x', 'x', 'x', 'x', 'x', ')',
425 # Load the stack and provoke an error that cannot be caught by the
426 # grammar, to check that the stack is cleared. And make sure the
427 # lookahead is freed.
432 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
433 [[sending: '(' (0@0-9)
434 sending: 'x' (1@10-19)
435 thing (1@10-19): 'x' (1@10-19)
436 sending: 'x' (2@20-29)
437 thing (2@20-29): 'x' (2@20-29)
438 sending: 'x' (3@30-39)
439 30-39: syntax error, unexpected 'x', expecting ')'
440 Freeing nterm thing (2@20-29)
441 Freeing nterm thing (1@10-19)
442 Freeing token 'x' (3@30-39)
443 sending: 'x' (4@40-49)
444 Freeing token 'x' (4@40-49)
445 sending: 'x' (5@50-59)
446 Freeing token 'x' (5@50-59)
447 sending: ')' (6@60-69)
448 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
449 sending: '(' (7@70-79)
450 sending: 'x' (8@80-89)
451 thing (8@80-89): 'x' (8@80-89)
452 sending: ')' (9@90-99)
453 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
454 sending: '(' (10@100-109)
455 sending: 'x' (11@110-119)
456 thing (11@110-119): 'x' (11@110-119)
457 sending: ')' (12@120-129)
458 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
459 sending: 'y' (13@130-139)
460 input (0@129-129): /* Nothing */
461 input (2@100-129): line (10@100-129) input (0@129-129)
462 input (2@70-129): line (7@70-99) input (2@100-129)
463 input (2@0-129): line (-1@0-69) input (2@70-129)
464 130-139: syntax error, unexpected 'y', expecting END
465 Freeing nterm input (2@0-129)
466 Freeing token 'y' (13@130-139)
471 # Syntax error caught by the parser where lookahead = END
472 # --------------------------------------------------------
473 # Load the stack and provoke an error that cannot be caught by the
474 # grammar, to check that the stack is cleared. And make sure the
475 # lookahead is freed.
480 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
481 [[sending: '(' (0@0-9)
482 sending: 'x' (1@10-19)
483 thing (1@10-19): 'x' (1@10-19)
484 sending: ')' (2@20-29)
485 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
486 sending: '(' (3@30-39)
487 sending: 'x' (4@40-49)
488 thing (4@40-49): 'x' (4@40-49)
489 sending: ')' (5@50-59)
490 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
491 sending: 'x' (6@60-69)
492 thing (6@60-69): 'x' (6@60-69)
493 sending: END (7@70-79)
494 70-79: syntax error, unexpected END, expecting 'x'
495 Freeing nterm thing (6@60-69)
496 Freeing nterm line (3@30-59)
497 Freeing nterm line (0@0-29)
498 Freeing token END (7@70-79)
503 # Check destruction upon stack overflow
504 # -------------------------------------
505 # Upon stack overflow, all symbols on the stack should be destroyed.
506 # Only check for yacc.c.
508 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
509 [[sending: '(' (0@0-9)
510 sending: 'x' (1@10-19)
511 thing (1@10-19): 'x' (1@10-19)
512 sending: ')' (2@20-29)
513 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
514 sending: '(' (3@30-39)
515 sending: 'x' (4@40-49)
516 thing (4@40-49): 'x' (4@40-49)
517 sending: ')' (5@50-59)
518 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
519 sending: '(' (6@60-69)
520 sending: 'x' (7@70-79)
521 thing (7@70-79): 'x' (7@70-79)
522 sending: ')' (8@80-89)
523 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
524 sending: '(' (9@90-99)
525 sending: 'x' (10@100-109)
526 thing (10@100-109): 'x' (10@100-109)
527 sending: ')' (11@110-119)
528 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
529 sending: '(' (12@120-129)
530 sending: 'x' (13@130-139)
531 thing (13@130-139): 'x' (13@130-139)
532 sending: ')' (14@140-149)
533 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
534 sending: '(' (15@150-159)
535 sending: 'x' (16@160-169)
536 thing (16@160-169): 'x' (16@160-169)
537 sending: ')' (17@170-179)
538 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
539 sending: '(' (18@180-189)
540 sending: 'x' (19@190-199)
541 thing (19@190-199): 'x' (19@190-199)
542 sending: ')' (20@200-209)
543 200-209: memory exhausted
544 Freeing nterm thing (19@190-199)
545 Freeing nterm line (15@150-179)
546 Freeing nterm line (12@120-149)
547 Freeing nterm line (9@90-119)
548 Freeing nterm line (6@60-89)
549 Freeing nterm line (3@30-59)
550 Freeing nterm line (0@0-29)
551 Parsing FAILED (status 2).
558 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
559 # ---------------------------------------------------------------------------
560 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
561 [AT_SETUP([Printers and Destructors $2: $1])
564 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
575 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
576 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
578 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
579 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
581 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
582 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
586 ## --------------------------------- ##
587 ## Default %printer and %destructor. ##
588 ## --------------------------------- ##
590 # Check that the right %printer and %destructor are called, that they're not
591 # called for $end, and that $$ and @$ work correctly.
593 AT_SETUP([Default %printer and %destructor])
595 AT_DATA_GRAMMAR([[input.y]],
600 @$.first_line = @$.last_line = 1;
601 @$.first_column = @$.last_column = 1;
607 static void yyerror (const char *msg);
608 static int yylex (void);
613 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
616 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
620 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
623 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
628 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
635 static char const input[] = "abcd";
636 static size_t toknum;
637 if (! (toknum < sizeof input))
639 yylval = input[toknum++];
640 yylloc.first_line = yylloc.last_line = 1;
641 yylloc.first_column = yylloc.last_column = toknum;
646 yyerror (const char *msg)
648 fprintf (stderr, "%s\n", msg);
659 AT_CHECK([bison -o input.c input.y])
661 AT_PARSER_CHECK([./input], 1,
662 [[Default destructor for 'd' @ 4.
663 'b'/'c' destructor for 'c' @ 3.
664 'b'/'c' destructor for 'b' @ 2.
665 Default destructor for 'a' @ 1.
669 Reading a token: Next token is token 'a' (1.1-1.1: Default printer for 'a' @ 1)
670 Shifting token 'a' (1.1-1.1: Default printer for 'a' @ 1)
672 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
673 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
675 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
676 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
678 Reading a token: Next token is token 'd' (1.4-1.4: Default printer for 'd' @ 4)
679 Shifting token 'd' (1.4-1.4: Default printer for 'd' @ 4)
681 Reading a token: Now at end of input.
682 syntax error, unexpected $end, expecting 'e'
683 Error: popping token 'd' (1.4-1.4: Default printer for 'd' @ 4)
685 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
687 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
689 Error: popping token 'a' (1.1-1.1: Default printer for 'a' @ 1)
691 Cleanup: discarding lookahead token $end (1.5-1.5: )
699 ## ----------------------------------- ##
700 ## Per-type %printer and %destructor. ##
701 ## ----------------------------------- ##
703 AT_SETUP([Per-type %printer and %destructor])
705 AT_DATA_GRAMMAR([[input.y]],
712 static void yyerror (const char *msg);
713 static int yylex (void);
717 %union { int field0; int field1; int field2; }
718 %type <field0> start 'a' 'g'
722 fprintf (yyoutput, "%%symbol-default/<field2>/e printer");
723 } %symbol-default 'e' <field2>
725 fprintf (stdout, "%%symbol-default/<field2>/e destructor.\n");
726 } %symbol-default 'e' <field2>
729 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
730 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
733 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
734 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
737 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
738 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
743 'a' 'b' 'c' 'd' 'e' 'f' 'g'
745 USE(($1, $2, $3, $4, $5, $6, $7));
755 static char const input[] = "abcdef";
756 static size_t toknum;
757 if (! (toknum < sizeof input))
759 return input[toknum++];
763 yyerror (const char *msg)
765 fprintf (stderr, "%s\n", msg);
776 AT_CHECK([bison -o input.c input.y])
778 AT_PARSER_CHECK([./input], 1,
779 [[%symbol-default/<field2>/e destructor.
780 %symbol-default/<field2>/e destructor.
784 %symbol-default/<field2>/e destructor.
788 Reading a token: Next token is token 'a' (%symbol-default/<field2>/e printer)
789 Shifting token 'a' (%symbol-default/<field2>/e printer)
791 Reading a token: Next token is token 'b' (<field1> printer)
792 Shifting token 'b' (<field1> printer)
794 Reading a token: Next token is token 'c' ('c' printer)
795 Shifting token 'c' ('c' printer)
797 Reading a token: Next token is token 'd' ('d' printer)
798 Shifting token 'd' ('d' printer)
800 Reading a token: Next token is token 'e' (%symbol-default/<field2>/e printer)
801 Shifting token 'e' (%symbol-default/<field2>/e printer)
803 Reading a token: Next token is token 'f' (%symbol-default/<field2>/e printer)
804 Shifting token 'f' (%symbol-default/<field2>/e printer)
806 Reading a token: Now at end of input.
807 syntax error, unexpected $end, expecting 'g'
808 Error: popping token 'f' (%symbol-default/<field2>/e printer)
809 Stack now 0 1 3 5 6 7
810 Error: popping token 'e' (%symbol-default/<field2>/e printer)
812 Error: popping token 'd' ('d' printer)
814 Error: popping token 'c' ('c' printer)
816 Error: popping token 'b' (<field1> printer)
818 Error: popping token 'a' (%symbol-default/<field2>/e printer)
820 Cleanup: discarding lookahead token $end ()
828 ## ------------------------------------------------------------- ##
829 ## Default %printer and %destructor for user-defined end token. ##
830 ## ------------------------------------------------------------- ##
832 AT_SETUP([Default %printer and %destructor for user-defined end token])
834 AT_DATA_GRAMMAR([[input.y]],
839 @$.first_line = @$.last_line = 1;
840 @$.first_column = @$.last_column = 1;
846 static void yyerror (const char *msg);
847 static int yylex (void);
853 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
856 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
861 start: { $$ = 'S'; } ;
872 yylloc.first_line = yylloc.last_line = 1;
873 yylloc.first_column = yylloc.last_column = 1;
878 yyerror (const char *msg)
880 fprintf (stderr, "%s\n", msg);
891 AT_CHECK([bison -o input.c input.y])
893 AT_PARSER_CHECK([./input], 0,
894 [[Default destructor for 'E' @ 1.
895 Default destructor for 'S' @ 1.
899 Reducing stack by rule 1 (line 35):
900 -> $$ = nterm start (1.1-1.1: Default printer for 'S' @ 1)
903 Reading a token: Now at end of input.
904 Shifting token END (1.1-1.1: Default printer for 'E' @ 1)
907 Cleanup: popping token END (1.1-1.1: Default printer for 'E' @ 1)
908 Cleanup: popping nterm start (1.1-1.1: Default printer for 'S' @ 1)
915 ## ------------------------------------------------------------------ ##
916 ## Default %printer and %destructor are not for error or $undefined. ##
917 ## ------------------------------------------------------------------ ##
919 AT_SETUP([Default %printer and %destructor are not for error or \$undefined])
921 # If Bison were to apply the default %printer and %destructor to the error
922 # token or to $undefined:
923 # - For the error token:
924 # - It would generate warnings for unused $n.
925 # - It would invoke the %printer and %destructor on the error token's
926 # semantic value, which would be initialized from the lookahead, which
927 # would be destroyed separately.
928 # - For $undefined, who knows what the semantic value would be.
930 AT_DATA_GRAMMAR([[input.y]],
936 static void yyerror (const char *msg);
937 static int yylex (void);
942 fprintf (yyoutput, "'%c'", $$);
945 fprintf (stderr, "DESTROY '%c'\n", $$);
952 /* In order to reveal the problems that this bug caused during parsing, add
954 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
962 static char const input[] = "abd";
963 static size_t toknum;
964 if (! (toknum < sizeof input))
966 yylval = input[toknum++];
971 yyerror (const char *msg)
973 fprintf (stderr, "%s\n", msg);
984 AT_CHECK([bison -o input.c input.y])
986 AT_PARSER_CHECK([./input], [1], [],
989 Reading a token: Next token is token 'a' ('a')
990 Shifting token 'a' ('a')
992 Reading a token: Next token is token 'b' ('b')
994 Shifting token error ()
996 Next token is token 'b' ('b')
997 Shifting token 'b' ('b')
999 Reading a token: Next token is token $undefined ()
1000 Error: popping token 'b' ('b')
1003 Error: popping token error ()
1005 Shifting token error ()
1007 Next token is token $undefined ()
1008 Error: discarding token $undefined ()
1009 Error: popping token error ()
1011 Shifting token error ()
1013 Reading a token: Now at end of input.
1014 Cleanup: discarding lookahead token $end ()
1016 Cleanup: popping token error ()
1017 Cleanup: popping token 'a' ('a')
1025 ## ------------------------------------------------------ ##
1026 ## Default %printer and %destructor are not for $accept. ##
1027 ## ------------------------------------------------------ ##
1029 AT_SETUP([Default %printer and %destructor are not for \$accept])
1031 # If YYSTYPE is a union and Bison were to apply the default %printer and
1032 # %destructor to $accept:
1033 # - The %printer and %destructor code generated for $accept would always be
1034 # dead code because $accept is currently never shifted onto the stack.
1035 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1036 # to declare `%type <field> $accept'. (Also true for $undefined.)
1037 # - Thus, the compiler might complain that the user code assumes the wrong
1038 # type for $$ since the code might assume the type associated with a
1039 # specific union field, which is especially reasonable in C++ since that
1040 # type may be a base type. This test case checks for this problem. (Also
1041 # true for $undefined and the error token, so there are three warnings for
1042 # %printer and three for %destructor.)
1044 AT_DATA_GRAMMAR([[input.y]],
1045 [[%debug /* So that %printer is actually compiled. */
1049 # include <stdlib.h>
1050 static void yyerror (const char *msg);
1051 static int yylex (void);
1057 fprintf (yyoutput, "'%c'", chr);
1061 fprintf (stderr, "DESTROY '%c'\n", chr);
1064 %union { char chr; }
1069 start: { USE($$); } ;
1083 yyerror (const char *msg)
1085 fprintf (stderr, "%s\n", msg);
1095 AT_CHECK([bison -o input.c input.y])
1102 ## ------------------------------------------------------ ##
1103 ## Default %printer and %destructor for mid-rule values. ##
1104 ## ------------------------------------------------------ ##
1106 AT_SETUP([Default %printer and %destructor for mid-rule values])
1108 AT_DATA_GRAMMAR([[input.y]],
1109 [[%debug /* So that %printer is actually compiled. */
1113 # include <stdlib.h>
1114 static void yyerror (const char *msg);
1115 static int yylex (void);
1117 # define YYLTYPE int
1118 # define YYLLOC_DEFAULT(Current, Rhs, N)
1119 # define YY_LOCATION_PRINT(File, Loc)
1122 %printer { fprintf (yyoutput, "%d", @$); } %symbol-default
1123 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } %symbol-default
1128 { @$ = 1; } // Not set or used.
1129 { USE ($$); @$ = 2; } // Both set and used.
1130 { USE ($$); @$ = 3; } // Only set.
1131 { @$ = 4; } // Only used.
1133 { USE (($$, $2, $4, $5)); @$ = 0; }
1148 yyerror (const char *msg)
1150 fprintf (stderr, "%s\n", msg);
1161 AT_CHECK([bison -o input.c input.y], 0,,
1162 [[input.y:31.3-23: warning: unset value: $$
1163 input.y:28.3-33.37: warning: unused value: $3
1167 AT_PARSER_CHECK([./input], 1,,
1170 Reducing stack by rule 1 (line 28):
1171 -> $$ = nterm $@1 (: )
1174 Reducing stack by rule 2 (line 29):
1175 -> $$ = nterm @2 (: 2)
1178 Reducing stack by rule 3 (line 30):
1179 -> $$ = nterm @3 (: 3)
1182 Reducing stack by rule 4 (line 31):
1183 -> $$ = nterm @4 (: 4)
1186 Reading a token: Now at end of input.
1188 Error: popping nterm @4 (: 4)
1191 Error: popping nterm @3 (: 3)
1194 Error: popping nterm @2 (: 2)
1197 Error: popping nterm $@1 (: )
1199 Cleanup: discarding lookahead token $end (: )