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 const char *input = "123456789";
63 yyerror (const char *msg)
65 fprintf (stderr, "%s\n", msg);
75 AT_CHECK([bison -d -v -o input.c input.y])
77 AT_PARSER_CHECK([./input], 0,
87 ## ---------------- ##
89 ## ---------------- ##
91 AT_SETUP([Exotic Dollars])
93 AT_DATA_GRAMMAR([[input.y]],
99 static void yyerror (const char *msg);
100 static int yylex (void);
109 %type <val> a_1 a_2 a_5
110 sum_of_the_five_previous_values
113 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
114 sum_of_the_five_previous_values
116 USE (($1, $2, $<foo>3, $<foo>4, $5));
124 sum_of_the_five_previous_values:
126 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
138 yyerror (const char *msg)
140 fprintf (stderr, "%s\n", msg);
150 AT_CHECK([bison -d -v -o input.c input.y], 0)
152 AT_PARSER_CHECK([./input], 0,
160 ## -------------------------- ##
161 ## Printers and Destructors. ##
162 ## -------------------------- ##
164 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4, BISON-DIRECTIVE, UNION-FLAG)
165 # -----------------------------------------------------------------------------
166 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
167 [# Make sure complex $n work.
168 m4_if([$1$2$3], $[1]$[2]$[3], [],
169 [m4_fatal([$0: Invalid arguments: $@])])dnl
171 # Be sure to pass all the %directives to this macro to have correct
172 # helping macros. So don't put any directly in the Bison file.
173 AT_BISON_OPTION_PUSHDEFS([$5])
174 AT_DATA_GRAMMAR([[input.y]],
180 #define YYINITDEPTH 10
181 #define YYMAXDEPTH 10
183 [#define RANGE(Location) (Location).begin.line, (Location).end.line],
184 [#define RANGE(Location) (Location).first_line, (Location).last_line])
188 m4_ifval([$6], [%union
192 AT_LALR1_CC_IF([%define "global_tokens_and_yystype"])
193 m4_ifval([$6], [[%end-header {]], [[%after-header {]])
194 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])
195 [static int yylex (]AT_LEX_FORMALS[);
196 ]AT_LALR1_CC_IF([], [static void yyerror (const char *msg);])
199 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
201 /* FIXME: This %printer isn't actually tested. */
204 ]AT_LALR1_CC_IF([debug_stream () << $$;],
205 [fprintf (yyoutput, "%d", $$)])[;
207 input line thing 'x' 'y'
210 { printf ("Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
214 { printf ("Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
218 { printf ("Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
222 { printf ("Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
226 { printf ("Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
231 { printf ("Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
236 This grammar is made to exercise error recovery.
237 "Lines" starting with `(' support error recovery, with
238 ')' as synchronizing token. Lines starting with 'x' can never
239 be recovered from if in error.
246 printf ("input (%d@%d-%d): /* Nothing */\n", $$, RANGE (@$));
248 | line input /* Right recursive to load the stack so that popping at
249 END can be exercised. */
252 printf ("input (%d@%d-%d): line (%d@%d-%d) input (%d@%d-%d)\n",
253 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2));
258 thing thing thing ';'
261 printf ("line (%d@%d-%d): thing (%d@%d-%d) thing (%d@%d-%d) thing (%d@%d-%d) ';' (%d@%d-%d)\n",
262 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2),
263 $3, RANGE (@3), $4, RANGE (@4));
265 | '(' thing thing ')'
268 printf ("line (%d@%d-%d): '(' (%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));
275 printf ("line (%d@%d-%d): '(' (%d@%d-%d) thing (%d@%d-%d) ')' (%d@%d-%d)\n",
276 $$, RANGE (@$), $1, RANGE (@1), $2, RANGE (@2), $3, RANGE (@3));
281 printf ("line (%d@%d-%d): '(' (%d@%d-%d) error (@%d-%d) ')' (%d@%d-%d)\n",
282 $$, RANGE (@$), $1, RANGE (@1), RANGE (@2), $3, RANGE (@3));
290 printf ("thing (%d@%d-%d): 'x' (%d@%d-%d)\n",
291 $$, RANGE (@$), $1, RANGE (@1));
295 /* Alias to ARGV[1]. */
296 const char *source = 0;
299 yylex (]AT_LEX_FORMALS[)
301 static unsigned int counter = 0;
303 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
304 /* As in BASIC, line numbers go from 10 to 10. */
306 [ AT_LOC.begin.line = AT_LOC.begin.column = 10 * c;
307 AT_LOC.end.line = AT_LOC.end.column = AT_LOC.begin.line + 9;
309 [ AT_LOC.first_line = AT_LOC.first_column = 10 * c;
310 AT_LOC.last_line = AT_LOC.last_column = AT_LOC.first_line + 9;
314 printf ("sending: '%c'", source[c]);
316 printf ("sending: END");
317 printf (" (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
322 [/* A C++ error reporting function. */
324 yy::parser::error (const location& l, const std::string& m)
326 printf ("%d-%d: %s\n", RANGE (l), m.c_str());
334 parser.set_debug_level (yydebug);
335 return parser.parse ();
339 yyerror (const char *msg)
341 printf ("%d-%d: %s\n", RANGE (yylloc), msg);
345 main (int argc, const char *argv[])
348 yydebug = !!getenv ("YYDEBUG");
354 case 0: printf ("Successful parse.\n"); break;
355 case 1: printf ("Parsing FAILED.\n"); break;
356 default: printf ("Parsing FAILED (status %d).\n", status); break;
363 [AT_CHECK([bison -o input.cc input.y])
364 AT_COMPILE_CXX([input])],
365 [AT_CHECK([bison -o input.c input.y])
366 AT_COMPILE([input])])
369 # Check the location of "empty"
370 # -----------------------------
371 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
372 # an empty "line" nterm.
373 # FIXME: This location is not satisfying. Depend on the lookahead?
374 AT_PARSER_CHECK([./input '(x)'], 0,
375 [[sending: '(' (0@0-9)
376 sending: 'x' (1@10-19)
377 thing (1@10-19): 'x' (1@10-19)
378 sending: ')' (2@20-29)
379 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
380 sending: END (3@30-39)
381 input (0@29-29): /* Nothing */
382 input (2@0-29): line (0@0-29) input (0@29-29)
383 Freeing token END (3@30-39)
384 Freeing nterm input (2@0-29)
389 # Check locations in error recovery
390 # ---------------------------------
391 # '(y)' is an error, but can be recovered from. But what's the location
392 # of the error itself ('y'), and of the resulting reduction ('(error)').
393 AT_PARSER_CHECK([./input '(y)'], 0,
394 [[sending: '(' (0@0-9)
395 sending: 'y' (1@10-19)
396 10-19: syntax error, unexpected 'y', expecting 'x'
397 Freeing token 'y' (1@10-19)
398 sending: ')' (2@20-29)
399 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
400 sending: END (3@30-39)
401 input (0@29-29): /* Nothing */
402 input (2@0-29): line (-1@0-29) input (0@29-29)
403 Freeing token END (3@30-39)
404 Freeing nterm input (2@0-29)
409 # Syntax errors caught by the parser
410 # ----------------------------------
411 # Exercise the discarding of stack top and input until `error'
414 # '(', 'x', 'x', 'x', 'x', 'x', ')',
416 # Load the stack and provoke an error that cannot be caught by the
417 # grammar, to check that the stack is cleared. And make sure the
418 # lookahead is freed.
423 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
424 [[sending: '(' (0@0-9)
425 sending: 'x' (1@10-19)
426 thing (1@10-19): 'x' (1@10-19)
427 sending: 'x' (2@20-29)
428 thing (2@20-29): 'x' (2@20-29)
429 sending: 'x' (3@30-39)
430 30-39: syntax error, unexpected 'x', expecting ')'
431 Freeing nterm thing (2@20-29)
432 Freeing nterm thing (1@10-19)
433 Freeing token 'x' (3@30-39)
434 sending: 'x' (4@40-49)
435 Freeing token 'x' (4@40-49)
436 sending: 'x' (5@50-59)
437 Freeing token 'x' (5@50-59)
438 sending: ')' (6@60-69)
439 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
440 sending: '(' (7@70-79)
441 sending: 'x' (8@80-89)
442 thing (8@80-89): 'x' (8@80-89)
443 sending: ')' (9@90-99)
444 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
445 sending: '(' (10@100-109)
446 sending: 'x' (11@110-119)
447 thing (11@110-119): 'x' (11@110-119)
448 sending: ')' (12@120-129)
449 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
450 sending: 'y' (13@130-139)
451 input (0@129-129): /* Nothing */
452 input (2@100-129): line (10@100-129) input (0@129-129)
453 input (2@70-129): line (7@70-99) input (2@100-129)
454 input (2@0-129): line (-1@0-69) input (2@70-129)
455 130-139: syntax error, unexpected 'y', expecting END
456 Freeing nterm input (2@0-129)
457 Freeing token 'y' (13@130-139)
462 # Syntax error caught by the parser where lookahead = END
463 # --------------------------------------------------------
464 # Load the stack and provoke an error that cannot be caught by the
465 # grammar, to check that the stack is cleared. And make sure the
466 # lookahead is freed.
471 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
472 [[sending: '(' (0@0-9)
473 sending: 'x' (1@10-19)
474 thing (1@10-19): 'x' (1@10-19)
475 sending: ')' (2@20-29)
476 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
477 sending: '(' (3@30-39)
478 sending: 'x' (4@40-49)
479 thing (4@40-49): 'x' (4@40-49)
480 sending: ')' (5@50-59)
481 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
482 sending: 'x' (6@60-69)
483 thing (6@60-69): 'x' (6@60-69)
484 sending: END (7@70-79)
485 70-79: syntax error, unexpected END, expecting 'x'
486 Freeing nterm thing (6@60-69)
487 Freeing nterm line (3@30-59)
488 Freeing nterm line (0@0-29)
489 Freeing token END (7@70-79)
494 # Check destruction upon stack overflow
495 # -------------------------------------
496 # Upon stack overflow, all symbols on the stack should be destroyed.
497 # Only check for yacc.c.
499 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
500 [[sending: '(' (0@0-9)
501 sending: 'x' (1@10-19)
502 thing (1@10-19): 'x' (1@10-19)
503 sending: ')' (2@20-29)
504 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
505 sending: '(' (3@30-39)
506 sending: 'x' (4@40-49)
507 thing (4@40-49): 'x' (4@40-49)
508 sending: ')' (5@50-59)
509 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
510 sending: '(' (6@60-69)
511 sending: 'x' (7@70-79)
512 thing (7@70-79): 'x' (7@70-79)
513 sending: ')' (8@80-89)
514 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
515 sending: '(' (9@90-99)
516 sending: 'x' (10@100-109)
517 thing (10@100-109): 'x' (10@100-109)
518 sending: ')' (11@110-119)
519 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
520 sending: '(' (12@120-129)
521 sending: 'x' (13@130-139)
522 thing (13@130-139): 'x' (13@130-139)
523 sending: ')' (14@140-149)
524 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
525 sending: '(' (15@150-159)
526 sending: 'x' (16@160-169)
527 thing (16@160-169): 'x' (16@160-169)
528 sending: ')' (17@170-179)
529 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
530 sending: '(' (18@180-189)
531 sending: 'x' (19@190-199)
532 thing (19@190-199): 'x' (19@190-199)
533 sending: ')' (20@200-209)
534 200-209: memory exhausted
535 Freeing nterm thing (19@190-199)
536 Freeing nterm line (15@150-179)
537 Freeing nterm line (12@120-149)
538 Freeing nterm line (9@90-119)
539 Freeing nterm line (6@60-89)
540 Freeing nterm line (3@30-59)
541 Freeing nterm line (0@0-29)
542 Parsing FAILED (status 2).
549 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
550 # ---------------------------------------------------------------------------
551 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
552 [AT_SETUP([Printers and Destructors $2: $1])
555 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
566 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
567 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
569 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
570 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
572 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
573 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
577 ## --------------------------------- ##
578 ## Default %printer and %destructor. ##
579 ## --------------------------------- ##
581 # Check that the right %printer and %destructor are called, that they're not
582 # called for $end, and that $$ and @$ work correctly.
584 AT_SETUP([Default %printer and %destructor])
586 AT_DATA_GRAMMAR([[input.y]],
591 @$.first_line = @$.last_line = 1;
592 @$.first_column = @$.last_column = 1;
598 static void yyerror (const char *msg);
599 static int yylex (void);
604 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
607 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
611 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
614 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
619 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
626 static const char *input = "abcd";
627 static int column = 1;
629 yylloc.first_line = yylloc.last_line = 1;
630 yylloc.first_column = yylloc.last_column = column++;
635 yyerror (const char *msg)
637 fprintf (stderr, "%s\n", msg);
648 AT_CHECK([bison -o input.c input.y])
650 AT_PARSER_CHECK([./input], 1,
651 [[Default destructor for 'd' @ 4.
652 'b'/'c' destructor for 'c' @ 3.
653 'b'/'c' destructor for 'b' @ 2.
654 Default destructor for 'a' @ 1.
658 Reading a token: Next token is token 'a' (1.1-1.1: Default printer for 'a' @ 1)
659 Shifting token 'a' (1.1-1.1: Default printer for 'a' @ 1)
661 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
662 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
664 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
665 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
667 Reading a token: Next token is token 'd' (1.4-1.4: Default printer for 'd' @ 4)
668 Shifting token 'd' (1.4-1.4: Default printer for 'd' @ 4)
670 Reading a token: Now at end of input.
671 syntax error, unexpected $end, expecting 'e'
672 Error: popping token 'd' (1.4-1.4: Default printer for 'd' @ 4)
674 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
676 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
678 Error: popping token 'a' (1.1-1.1: Default printer for 'a' @ 1)
680 Cleanup: discarding lookahead token $end (1.5-1.5: )
688 ## ----------------------------------- ##
689 ## Per-type %printer and %destructor. ##
690 ## ----------------------------------- ##
692 AT_SETUP([Per-type %printer and %destructor])
694 AT_DATA_GRAMMAR([[input.y]],
701 static void yyerror (const char *msg);
702 static int yylex (void);
706 %union { int field0; int field1; int field2; }
707 %type <field0> start 'a' 'g'
711 fprintf (yyoutput, "%%symbol-default/<field2>/e printer");
712 } %symbol-default 'e' <field2>
714 fprintf (stdout, "%%symbol-default/<field2>/e destructor.\n");
715 } %symbol-default 'e' <field2>
718 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
719 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
722 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
723 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
726 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
727 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
732 'a' 'b' 'c' 'd' 'e' 'f' 'g'
734 USE(($1, $2, $3, $4, $5, $6, $7));
744 static const char *input = "abcdef";
749 yyerror (const char *msg)
751 fprintf (stderr, "%s\n", msg);
762 AT_CHECK([bison -o input.c input.y])
764 AT_PARSER_CHECK([./input], 1,
765 [[%symbol-default/<field2>/e destructor.
766 %symbol-default/<field2>/e destructor.
770 %symbol-default/<field2>/e destructor.
774 Reading a token: Next token is token 'a' (%symbol-default/<field2>/e printer)
775 Shifting token 'a' (%symbol-default/<field2>/e printer)
777 Reading a token: Next token is token 'b' (<field1> printer)
778 Shifting token 'b' (<field1> printer)
780 Reading a token: Next token is token 'c' ('c' printer)
781 Shifting token 'c' ('c' printer)
783 Reading a token: Next token is token 'd' ('d' printer)
784 Shifting token 'd' ('d' printer)
786 Reading a token: Next token is token 'e' (%symbol-default/<field2>/e printer)
787 Shifting token 'e' (%symbol-default/<field2>/e printer)
789 Reading a token: Next token is token 'f' (%symbol-default/<field2>/e printer)
790 Shifting token 'f' (%symbol-default/<field2>/e printer)
792 Reading a token: Now at end of input.
793 syntax error, unexpected $end, expecting 'g'
794 Error: popping token 'f' (%symbol-default/<field2>/e printer)
795 Stack now 0 1 3 5 6 7
796 Error: popping token 'e' (%symbol-default/<field2>/e printer)
798 Error: popping token 'd' ('d' printer)
800 Error: popping token 'c' ('c' printer)
802 Error: popping token 'b' (<field1> printer)
804 Error: popping token 'a' (%symbol-default/<field2>/e printer)
806 Cleanup: discarding lookahead token $end ()
814 ## ------------------------------------------------------------- ##
815 ## Default %printer and %destructor for user-defined end token. ##
816 ## ------------------------------------------------------------- ##
818 AT_SETUP([Default %printer and %destructor for user-defined end token])
820 AT_DATA_GRAMMAR([[input.y]],
825 @$.first_line = @$.last_line = 1;
826 @$.first_column = @$.last_column = 1;
832 static void yyerror (const char *msg);
833 static int yylex (void);
839 fprintf (yyoutput, "Default printer for '%c' @ %d", $$, @$.first_column);
842 fprintf (stdout, "Default destructor for '%c' @ %d.\n", $$, @$.first_column);
847 start: { $$ = 'S'; } ;
855 yylloc.first_line = yylloc.last_line = 1;
856 yylloc.first_column = yylloc.last_column = 1;
861 yyerror (const char *msg)
863 fprintf (stderr, "%s\n", msg);
874 AT_CHECK([bison -o input.c input.y])
876 AT_PARSER_CHECK([./input], 0,
877 [[Default destructor for 'E' @ 1.
878 Default destructor for 'S' @ 1.
882 Reducing stack by rule 1 (line 37):
883 -> $$ = nterm start (1.1-1.1: Default printer for 'S' @ 1)
886 Reading a token: Now at end of input.
887 Shifting token END (1.1-1.1: Default printer for 'E' @ 1)
890 Cleanup: popping token END (1.1-1.1: Default printer for 'E' @ 1)
891 Cleanup: popping nterm start (1.1-1.1: Default printer for 'S' @ 1)
898 ## ------------------------------------------------------------------ ##
899 ## Default %printer and %destructor are not for error or $undefined. ##
900 ## ------------------------------------------------------------------ ##
902 AT_SETUP([Default %printer and %destructor are not for error or \$undefined])
904 # If Bison were to apply the default %printer and %destructor to the error
905 # token or to $undefined:
906 # - For the error token:
907 # - It would generate warnings for unused $n.
908 # - It would invoke the %printer and %destructor on the error token's
909 # semantic value, which would be initialized from the lookahead, which
910 # would be destroyed separately.
911 # - For $undefined, who knows what the semantic value would be.
913 AT_DATA_GRAMMAR([[input.y]],
918 static void yyerror (const char *msg);
919 static int yylex (void);
924 fprintf (yyoutput, "'%c'", $$);
927 fprintf (stderr, "DESTROY '%c'\n", $$);
934 /* In order to reveal the problems that this bug caused during parsing, add
936 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
944 static const char *input = "abd";
950 yyerror (const char *msg)
952 fprintf (stderr, "%s\n", msg);
963 AT_CHECK([bison -o input.c input.y])
965 AT_PARSER_CHECK([./input], [1], [],
968 Reading a token: Next token is token 'a' ('a')
969 Shifting token 'a' ('a')
971 Reading a token: Next token is token 'b' ('b')
973 Shifting token error ()
975 Next token is token 'b' ('b')
976 Shifting token 'b' ('b')
978 Reading a token: Next token is token $undefined ()
979 Error: popping token 'b' ('b')
982 Error: popping token error ()
984 Shifting token error ()
986 Next token is token $undefined ()
987 Error: discarding token $undefined ()
988 Error: popping token error ()
990 Shifting token error ()
992 Reading a token: Now at end of input.
993 Cleanup: discarding lookahead token $end ()
995 Cleanup: popping token error ()
996 Cleanup: popping token 'a' ('a')
1004 ## ------------------------------------------------------ ##
1005 ## Default %printer and %destructor are not for $accept. ##
1006 ## ------------------------------------------------------ ##
1008 AT_SETUP([Default %printer and %destructor are not for \$accept])
1010 # If YYSTYPE is a union and Bison were to apply the default %printer and
1011 # %destructor to $accept:
1012 # - The %printer and %destructor code generated for $accept would always be
1013 # dead code because $accept is currently never shifted onto the stack.
1014 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1015 # to declare `%type <field> $accept'. (Also true for $undefined.)
1016 # - Thus, the compiler might complain that the user code assumes the wrong
1017 # type for $$ since the code might assume the type associated with a
1018 # specific union field, which is especially reasonable in C++ since that
1019 # type may be a base type. This test case checks for this problem. (Also
1020 # true for $undefined and the error token, so there are three warnings for
1021 # %printer and three for %destructor.)
1023 AT_DATA_GRAMMAR([[input.y]],
1024 [[%debug /* So that %printer is actually compiled. */
1028 static void yyerror (const char *msg);
1029 static int yylex (void);
1035 fprintf (yyoutput, "'%c'", chr);
1039 fprintf (stderr, "DESTROY '%c'\n", chr);
1042 %union { char chr; }
1047 start: { USE($$); } ;
1058 yyerror (const char *msg)
1060 fprintf (stderr, "%s\n", msg);
1070 AT_CHECK([bison -o input.c input.y])