1 # Executing Actions. -*- Autotest -*-
3 # Copyright (C) 2001-2012 Free Software Foundation, Inc.
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_BISON_OPTION_PUSHDEFS
32 AT_DATA_GRAMMAR([[input.y]],
40 exp: { putchar ('0'); }
41 '1' { putchar ('1'); }
42 '2' { putchar ('2'); }
43 '3' { putchar ('3'); }
44 '4' { putchar ('4'); }
45 '5' { putchar ('5'); }
46 '6' { putchar ('6'); }
47 '7' { putchar ('7'); }
48 '8' { putchar ('8'); }
49 '9' { putchar ('9'); }
54 ]AT_YYLEX_DEFINE(["123456789"])[
61 AT_BISON_OPTION_POPDEFS
63 AT_BISON_CHECK([-d -v -o input.c input.y])
65 AT_PARSER_CHECK([./input], 0,
75 ## ---------------- ##
77 ## ---------------- ##
79 AT_SETUP([Exotic Dollars])
81 AT_BISON_OPTION_PUSHDEFS
82 AT_DATA_GRAMMAR([[input.y]],
96 %type <val> a_1 a_2 a_5
97 sum_of_the_five_previous_values
100 exp: a_1 a_2 { $<val>$ = 3; } { $<val>$ = $<val>3 + 1; } a_5
101 sum_of_the_five_previous_values
103 USE (($1, $2, $<foo>3, $<foo>4, $5));
111 sum_of_the_five_previous_values:
113 $$ = $<val>0 + $<val>-1 + $<val>-2 + $<val>-3 + $<val>-4;
127 AT_BISON_CHECK([-d -v -o input.c input.y], 0)
129 AT_PARSER_CHECK([./input], 0,
133 # Make sure that fields after $n or $-n are parsed correctly. At one
134 # point while implementing dashes in symbol names, we were dropping
136 AT_DATA_GRAMMAR([[input.y]],
142 typedef struct { int val; } stype;
143 # define YYSTYPE stype
147 start: one two { $$.val = $1.val + $2.val; } sum ;
148 one: { $$.val = 1; } ;
149 two: { $$.val = 2; } ;
150 sum: { printf ("%d\n", $0.val + $-1.val + $-2.val); } ;
162 AT_FULL_COMPILE([input])
163 AT_PARSER_CHECK([[./input]], [[0]],
167 AT_BISON_OPTION_POPDEFS
172 ## -------------------------- ##
173 ## Printers and Destructors. ##
174 ## -------------------------- ##
176 # _AT_CHECK_PRINTER_AND_DESTRUCTOR($1, $2, $3, $4,
177 # BISON-DIRECTIVE, UNION-FLAG)
178 # -------------------------------------------------------------
179 m4_define([_AT_CHECK_PRINTER_AND_DESTRUCTOR],
180 [# Make sure complex $n work.
181 m4_if([$1$2$3$4], $[1]$[2]$[3]$[4], [],
182 [m4_fatal([$0: Invalid arguments: $@])])dnl
184 # Be sure to pass all the %directives to this macro to have correct
185 # helping macros. So don't put any directly in the Bison file.
186 AT_BISON_OPTION_PUSHDEFS([$5])
187 AT_DATA_GRAMMAR([[input.y]],
194 #define YYINITDEPTH 10
195 #define YYMAXDEPTH 10
196 #define RANGE(Location) ]AT_LALR1_CC_IF([(Location).begin.line, (Location).end.line],
197 [(Location).first_line, (Location).last_line])[
199 /* Display the symbol type Symbol. */
200 #define V(Symbol, Value, Location, Sep) \
201 fprintf (stderr, #Symbol " (%d@%d-%d)" Sep, Value, RANGE(Location))
205 ]m4_ifval([$6], [%union
209 AT_LALR1_CC_IF([%define global_tokens_and_yystype])
210 m4_ifval([$6], [[%code provides {]], [[%code {]])
211 AT_LALR1_CC_IF([typedef yy::location YYLTYPE;])[
213 ]AT_LALR1_CC_IF([], [AT_YYERROR_DECLARE])
216 ]m4_ifval([$6], [%type <ival> '(' 'x' 'y' ')' ';' thing line input END])[
218 /* FIXME: This %printer isn't actually tested. */
221 ]AT_LALR1_CC_IF([debug_stream () << $$;],
222 [fprintf (yyoutput, "%d", $$)])[;
224 input line thing 'x' 'y'
227 { fprintf (stderr, "Freeing nterm input (%d@%d-%d)\n", $$, RANGE (@$)); }
231 { fprintf (stderr, "Freeing nterm line (%d@%d-%d)\n", $$, RANGE (@$)); }
235 { fprintf (stderr, "Freeing nterm thing (%d@%d-%d)\n", $$, RANGE (@$)); }
239 { fprintf (stderr, "Freeing token 'x' (%d@%d-%d)\n", $$, RANGE (@$)); }
243 { fprintf (stderr, "Freeing token 'y' (%d@%d-%d)\n", $$, RANGE (@$)); }
248 { fprintf (stderr, "Freeing token END (%d@%d-%d)\n", $$, RANGE (@$)); }
253 This grammar is made to exercise error recovery.
254 "Lines" starting with `(' support error recovery, with
255 ')' as synchronizing token. Lines starting with 'x' can never
256 be recovered from if in error.
263 V(input, $$, @$, ": /* Nothing */\n");
265 | line input /* Right recursive to load the stack so that popping at
266 END can be exercised. */
269 V(input, $$, @$, ": ");
270 V(line, $1, @1, " ");
271 V(input, $2, @2, "\n");
276 thing thing thing ';'
279 V(line, $$, @$, ": ");
280 V(thing, $1, @1, " ");
281 V(thing, $2, @2, " ");
282 V(thing, $3, @3, " ");
285 | '(' thing thing ')'
288 V(line, $$, @$, ": ");
290 V(thing, $2, @2, " ");
291 V(thing, $3, @3, " ");
292 V(')', $4, @4, "\n");
297 V(line, $$, @$, ": ");
299 V(thing, $2, @2, " ");
300 V(')', $3, @3, "\n");
305 V(line, $$, @$, ": ");
307 fprintf (stderr, "error (@%d-%d) ", RANGE (@2));
308 V(')', $3, @3, "\n");
316 V(thing, $$, @$, ": ");
317 V('x', $1, @1, "\n");
321 /* Alias to ARGV[1]. */
322 const char *source = YY_NULL;
329 static unsigned int counter = 0;
331 int c = ]AT_VAL[]m4_ifval([$6], [.ival])[ = counter++;
332 /* As in BASIC, line numbers go from 10 to 10. */
333 ]AT_LOC_FIRST_LINE[ = ]AT_LOC_FIRST_COLUMN[ = 10 * c;
334 ]AT_LOC_LAST_LINE[ = ]AT_LOC_LAST_COLUMN[ = ]AT_LOC_FIRST_LINE[ + 9;
335 assert (0 <= c && c <= strlen (source));
337 fprintf (stderr, "sending: '%c'", source[c]);
339 fprintf (stderr, "sending: END");
340 fprintf (stderr, " (%d@%d-%d)\n", c, RANGE (]AT_LOC[));
344 [static bool yydebug;
349 parser.set_debug_level (yydebug);
350 return parser.parse ();
355 main (int argc, const char *argv[])
358 yydebug = !!getenv ("YYDEBUG");
364 case 0: fprintf (stderr, "Successful parse.\n"); break;
365 case 1: fprintf (stderr, "Parsing FAILED.\n"); break;
366 default: fprintf (stderr, "Parsing FAILED (status %d).\n", status); break;
372 AT_FULL_COMPILE([input])
375 # Check the location of "empty"
376 # -----------------------------
377 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
378 # an empty "line" nterm.
379 # FIXME: This location is not satisfying. Depend on the lookahead?
380 AT_PARSER_CHECK([./input '(x)'], 0, [],
381 [[sending: '(' (0@0-9)
382 sending: 'x' (1@10-19)
383 thing (1@10-19): 'x' (1@10-19)
384 sending: ')' (2@20-29)
385 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
386 sending: END (3@30-39)
387 input (0@29-29): /* Nothing */
388 input (2@0-29): line (0@0-29) input (0@29-29)
389 Freeing token END (3@30-39)
390 Freeing nterm input (2@0-29)
395 # Check locations in error recovery
396 # ---------------------------------
397 # '(y)' is an error, but can be recovered from. But what's the location
398 # of the error itself ('y'), and of the resulting reduction ('(error)').
399 AT_PARSER_CHECK([./input '(y)'], 0, [],
400 [[sending: '(' (0@0-9)
401 sending: 'y' (1@10-19)
402 10.10-19.18: syntax error, unexpected 'y', expecting 'x'
403 Freeing token 'y' (1@10-19)
404 sending: ')' (2@20-29)
405 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
406 sending: END (3@30-39)
407 input (0@29-29): /* Nothing */
408 input (2@0-29): line (-1@0-29) input (0@29-29)
409 Freeing token END (3@30-39)
410 Freeing nterm input (2@0-29)
415 # Syntax errors caught by the parser
416 # ----------------------------------
417 # Exercise the discarding of stack top and input until `error'
420 # '(', 'x', 'x', 'x', 'x', 'x', ')',
422 # Load the stack and provoke an error that cannot be caught by the
423 # grammar, to check that the stack is cleared. And make sure the
424 # lookahead is freed.
429 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1, [],
430 [[sending: '(' (0@0-9)
431 sending: 'x' (1@10-19)
432 thing (1@10-19): 'x' (1@10-19)
433 sending: 'x' (2@20-29)
434 thing (2@20-29): 'x' (2@20-29)
435 sending: 'x' (3@30-39)
436 30.30-39.38: syntax error, unexpected 'x', expecting ')'
437 Freeing nterm thing (2@20-29)
438 Freeing nterm thing (1@10-19)
439 Freeing token 'x' (3@30-39)
440 sending: 'x' (4@40-49)
441 Freeing token 'x' (4@40-49)
442 sending: 'x' (5@50-59)
443 Freeing token 'x' (5@50-59)
444 sending: ')' (6@60-69)
445 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
446 sending: '(' (7@70-79)
447 sending: 'x' (8@80-89)
448 thing (8@80-89): 'x' (8@80-89)
449 sending: ')' (9@90-99)
450 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
451 sending: '(' (10@100-109)
452 sending: 'x' (11@110-119)
453 thing (11@110-119): 'x' (11@110-119)
454 sending: ')' (12@120-129)
455 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
456 sending: 'y' (13@130-139)
457 input (0@129-129): /* Nothing */
458 input (2@100-129): line (10@100-129) input (0@129-129)
459 input (2@70-129): line (7@70-99) input (2@100-129)
460 input (2@0-129): line (-1@0-69) input (2@70-129)
461 130.130-139.138: syntax error, unexpected 'y', expecting END
462 Freeing nterm input (2@0-129)
463 Freeing token 'y' (13@130-139)
468 # Syntax error caught by the parser where lookahead = END
469 # --------------------------------------------------------
470 # Load the stack and provoke an error that cannot be caught by the
471 # grammar, to check that the stack is cleared. And make sure the
472 # lookahead is freed.
477 AT_PARSER_CHECK([./input '(x)(x)x'], 1, [],
478 [[sending: '(' (0@0-9)
479 sending: 'x' (1@10-19)
480 thing (1@10-19): 'x' (1@10-19)
481 sending: ')' (2@20-29)
482 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
483 sending: '(' (3@30-39)
484 sending: 'x' (4@40-49)
485 thing (4@40-49): 'x' (4@40-49)
486 sending: ')' (5@50-59)
487 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
488 sending: 'x' (6@60-69)
489 thing (6@60-69): 'x' (6@60-69)
490 sending: END (7@70-79)
491 70.70-79.78: syntax error, unexpected END, expecting 'x'
492 Freeing nterm thing (6@60-69)
493 Freeing nterm line (3@30-59)
494 Freeing nterm line (0@0-29)
495 Freeing token END (7@70-79)
500 # Check destruction upon stack overflow
501 # -------------------------------------
502 # Upon stack overflow, all symbols on the stack should be destroyed.
503 # Only check for yacc.c.
505 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2, [],
506 [[sending: '(' (0@0-9)
507 sending: 'x' (1@10-19)
508 thing (1@10-19): 'x' (1@10-19)
509 sending: ')' (2@20-29)
510 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
511 sending: '(' (3@30-39)
512 sending: 'x' (4@40-49)
513 thing (4@40-49): 'x' (4@40-49)
514 sending: ')' (5@50-59)
515 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
516 sending: '(' (6@60-69)
517 sending: 'x' (7@70-79)
518 thing (7@70-79): 'x' (7@70-79)
519 sending: ')' (8@80-89)
520 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
521 sending: '(' (9@90-99)
522 sending: 'x' (10@100-109)
523 thing (10@100-109): 'x' (10@100-109)
524 sending: ')' (11@110-119)
525 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
526 sending: '(' (12@120-129)
527 sending: 'x' (13@130-139)
528 thing (13@130-139): 'x' (13@130-139)
529 sending: ')' (14@140-149)
530 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
531 sending: '(' (15@150-159)
532 sending: 'x' (16@160-169)
533 thing (16@160-169): 'x' (16@160-169)
534 sending: ')' (17@170-179)
535 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
536 sending: '(' (18@180-189)
537 sending: 'x' (19@190-199)
538 thing (19@190-199): 'x' (19@190-199)
539 sending: ')' (20@200-209)
540 200.200-209.208: memory exhausted
541 Freeing nterm thing (19@190-199)
542 Freeing nterm line (15@150-179)
543 Freeing nterm line (12@120-149)
544 Freeing nterm line (9@90-119)
545 Freeing nterm line (6@60-89)
546 Freeing nterm line (3@30-59)
547 Freeing nterm line (0@0-29)
548 Parsing FAILED (status 2).
552 AT_BISON_OPTION_POPDEFS
553 ])# _AT_CHECK_PRINTER_AND_DESTRUCTOR
556 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
557 # ---------------------------------------------------------------------------
558 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
559 [AT_SETUP([Printers and Destructors$2]m4_ifval([$1], [[: $1]]))
562 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
573 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
574 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [ with union])
576 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
577 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [ with union])
579 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
580 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [ with union])
584 ## ----------------------------------------- ##
585 ## Default tagless %printer and %destructor. ##
586 ## ----------------------------------------- ##
588 # Check that the right %printer and %destructor are called, that they're not
589 # called for $end, and that $$ and @$ work correctly.
591 AT_SETUP([Default tagless %printer and %destructor])
592 AT_BISON_OPTION_PUSHDEFS([%locations])
593 AT_DATA_GRAMMAR([[input.y]],
607 fprintf (yyoutput, "<*> printer should not be called.\n");
611 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
614 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
618 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
621 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
625 fprintf (yyoutput, "<*> destructor should not be called.\n");
630 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
634 ]AT_YYLEX_DEFINE(["abcd"], [[yylval = res]])[
644 AT_BISON_CHECK([-o input.c input.y])
646 AT_PARSER_CHECK([./input], 1,
647 [[<> destructor for 'd' @ 4.
648 'b'/'c' destructor for 'c' @ 3.
649 'b'/'c' destructor for 'b' @ 2.
650 <> destructor for 'a' @ 1.
654 Reading a token: Next token is token 'a' (1.1: <> printer for 'a' @ 1)
655 Shifting token 'a' (1.1: <> printer for 'a' @ 1)
657 Reading a token: Next token is token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
658 Shifting token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
660 Reading a token: Next token is token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
661 Shifting token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
663 Reading a token: Next token is token 'd' (1.4: <> printer for 'd' @ 4)
664 Shifting token 'd' (1.4: <> printer for 'd' @ 4)
666 Reading a token: Now at end of input.
667 1.5: syntax error, unexpected $end, expecting 'e'
668 Error: popping token 'd' (1.4: <> printer for 'd' @ 4)
670 Error: popping token 'c' (1.3: 'b'/'c' printer for 'c' @ 3)
672 Error: popping token 'b' (1.2: 'b'/'c' printer for 'b' @ 2)
674 Error: popping token 'a' (1.1: <> printer for 'a' @ 1)
676 Cleanup: discarding lookahead token $end (1.5: )
680 AT_BISON_OPTION_POPDEFS
685 ## ------------------------------------------------------ ##
686 ## Default tagged and per-type %printer and %destructor. ##
687 ## ------------------------------------------------------ ##
689 AT_SETUP([Default tagged and per-type %printer and %destructor])
690 AT_BISON_OPTION_PUSHDEFS
691 AT_DATA_GRAMMAR([[input.y]],
704 fprintf (yyoutput, "<> printer should not be called.\n");
707 %union { int field0; int field1; int field2; }
708 %type <field0> start 'a' 'g'
712 fprintf (yyoutput, "<*>/<field2>/e printer");
715 fprintf (stdout, "<*>/<field2>/e destructor.\n");
719 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
720 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
723 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
724 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
727 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
728 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
731 fprintf (yyoutput, "<> destructor should not be called.\n");
737 'a' 'b' 'c' 'd' 'e' 'f' 'g'
739 USE(($1, $2, $3, $4, $5, $6, $7));
746 ]AT_YYLEX_DEFINE(["abcdef"])[
756 AT_BISON_CHECK([-o input.c input.y])
758 AT_PARSER_CHECK([./input], 1,
759 [[<*>/<field2>/e destructor.
760 <*>/<field2>/e destructor.
764 <*>/<field2>/e destructor.
768 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
769 Shifting token 'a' (<*>/<field2>/e printer)
771 Reading a token: Next token is token 'b' (<field1> printer)
772 Shifting token 'b' (<field1> printer)
774 Reading a token: Next token is token 'c' ('c' printer)
775 Shifting token 'c' ('c' printer)
777 Reading a token: Next token is token 'd' ('d' printer)
778 Shifting token 'd' ('d' printer)
780 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
781 Shifting token 'e' (<*>/<field2>/e printer)
783 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
784 Shifting token 'f' (<*>/<field2>/e printer)
786 Reading a token: Now at end of input.
787 syntax error, unexpected $end, expecting 'g'
788 Error: popping token 'f' (<*>/<field2>/e printer)
789 Stack now 0 1 3 5 6 7
790 Error: popping token 'e' (<*>/<field2>/e printer)
792 Error: popping token 'd' ('d' printer)
794 Error: popping token 'c' ('c' printer)
796 Error: popping token 'b' (<field1> printer)
798 Error: popping token 'a' (<*>/<field2>/e printer)
800 Cleanup: discarding lookahead token $end ()
804 AT_BISON_OPTION_POPDEFS
809 ## ------------------------------------------------------------- ##
810 ## Default %printer and %destructor for user-defined end token. ##
811 ## ------------------------------------------------------------- ##
813 AT_SETUP([Default %printer and %destructor for user-defined end token])
815 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
816 # -------------------------------------------------------------
817 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
819 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
820 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
822 AT_BISON_OPTION_PUSHDEFS([%locations])
823 AT_DATA_GRAMMAR([[input]]$1[[.y]],
828 @$.first_line = @$.last_line = 1;
829 @$.first_column = @$.last_column = 1;
841 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
846 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
849 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
853 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
858 [[[%union { char tag; }
859 %type <tag> start END]]])[[
863 start: { $$ = 'S'; } ;
873 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
874 yylloc.first_line = yylloc.last_line = 1;
875 yylloc.first_column = yylloc.last_column = 1;
887 AT_BISON_OPTION_POPDEFS
889 AT_BISON_CHECK([-o input$1.c input$1.y])
890 AT_COMPILE([input$1])
891 AT_PARSER_CHECK([./input$1], 0,
892 [[<]]kind[[> for 'E' @ 1.
893 <]]kind[[> for 'S' @ 1.
897 Reducing stack by rule 1 (line 46):
898 -> $$ = nterm start (1.1: <]]kind[[> for 'S' @ 1)
901 Reading a token: Now at end of input.
902 Shifting token END (1.1: <]]kind[[> for 'E' @ 1)
905 Cleanup: popping token END (1.1: <]]kind[[> for 'E' @ 1)
906 Cleanup: popping nterm start (1.1: <]]kind[[> for 'S' @ 1)
910 m4_popdef([not_kind])
913 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
914 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
920 ## ------------------------------------------------------------------ ##
921 ## Default %printer and %destructor are not for error or $undefined. ##
922 ## ------------------------------------------------------------------ ##
924 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
926 # If Bison were to apply the default %printer and %destructor to the error
927 # token or to $undefined:
928 # - For the error token:
929 # - It would generate warnings for unused $n.
930 # - It would invoke the %printer and %destructor on the error token's
931 # semantic value, which would be initialized from the lookahead, which
932 # would be destroyed separately.
933 # - For $undefined, who knows what the semantic value would be.
934 AT_BISON_OPTION_PUSHDEFS
935 AT_DATA_GRAMMAR([[input.y]],
947 fprintf (yyoutput, "'%c'", $$);
950 fprintf (stderr, "DESTROY '%c'\n", $$);
957 /* In order to reveal the problems that this bug caused during parsing, add
959 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
964 ]AT_YYLEX_DEFINE(["abd"], [yylval = res])[
972 AT_BISON_OPTION_POPDEFS
974 AT_BISON_CHECK([-o input.c input.y])
976 AT_PARSER_CHECK([./input], [1], [],
979 Reading a token: Next token is token 'a' ('a')
980 Shifting token 'a' ('a')
982 Reading a token: Next token is token 'b' ('b')
984 Shifting token error ()
986 Next token is token 'b' ('b')
987 Shifting token 'b' ('b')
989 Reading a token: Next token is token $undefined ()
990 Error: popping token 'b' ('b')
993 Error: popping token error ()
995 Shifting token error ()
997 Next token is token $undefined ()
998 Error: discarding token $undefined ()
999 Error: popping token error ()
1001 Shifting token error ()
1003 Reading a token: Now at end of input.
1004 Cleanup: discarding lookahead token $end ()
1006 Cleanup: popping token error ()
1007 Cleanup: popping token 'a' ('a')
1015 ## ------------------------------------------------------ ##
1016 ## Default %printer and %destructor are not for $accept. ##
1017 ## ------------------------------------------------------ ##
1019 AT_SETUP([Default %printer and %destructor are not for $accept])
1021 # If YYSTYPE is a union and Bison were to apply the default %printer and
1022 # %destructor to $accept:
1023 # - The %printer and %destructor code generated for $accept would always be
1024 # dead code because $accept is currently never shifted onto the stack.
1025 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1026 # to declare `%type <field> $accept'. (Also true for $undefined.)
1027 # - Thus, the compiler might complain that the user code assumes the wrong
1028 # type for $$ since the code might assume the type associated with a
1029 # specific union field, which is especially reasonable in C++ since that
1030 # type may be a base type. This test case checks for this problem. (Also
1031 # true for $undefined and the error token, so there are three warnings for
1032 # %printer and three for %destructor.)
1034 AT_BISON_OPTION_PUSHDEFS
1035 AT_DATA_GRAMMAR([[input.y]],
1036 [[%debug /* So that %printer is actually compiled. */
1040 # include <stdlib.h>
1041 ]AT_YYERROR_DECLARE[
1048 fprintf (yyoutput, "'%c'", chr);
1052 fprintf (stderr, "DESTROY '%c'\n", chr);
1055 %union { char chr; }
1060 start: { USE($$); } ;
1071 AT_BISON_OPTION_POPDEFS
1073 AT_BISON_CHECK([-o input.c input.y])
1080 ## ------------------------------------------------------ ##
1081 ## Default %printer and %destructor for mid-rule values. ##
1082 ## ------------------------------------------------------ ##
1084 AT_SETUP([Default %printer and %destructor for mid-rule values])
1086 AT_BISON_OPTION_PUSHDEFS
1087 AT_DATA_GRAMMAR([[input.y]],
1088 [[%debug /* So that %printer is actually compiled. */
1092 # include <stdlib.h>
1093 ]AT_YYERROR_DECLARE[
1096 # define YYLTYPE int
1097 # define YYLLOC_DEFAULT(Current, Rhs, N) (void)(Rhs)
1098 # define YY_LOCATION_PRINT(File, Loc)
1101 %printer { fprintf (yyoutput, "%d", @$); } <>
1102 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1103 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1104 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1109 { @$ = 1; } // Not set or used.
1110 { USE ($$); @$ = 2; } // Both set and used.
1111 { USE ($$); @$ = 3; } // Only set.
1112 { @$ = 4; } // Only used.
1114 { USE (($$, $2, $4, $5)); @$ = 0; }
1127 AT_BISON_OPTION_POPDEFS
1129 AT_BISON_CHECK([-o input.c input.y], 0,,
1130 [[input.y:33.3-23: warning: unset value: $$
1131 input.y:30.3-35.37: warning: unused value: $3
1135 AT_PARSER_CHECK([./input], 1,,
1138 Reducing stack by rule 1 (line 30):
1139 -> $$ = nterm $@1 (: )
1142 Reducing stack by rule 2 (line 31):
1143 -> $$ = nterm @2 (: 2)
1146 Reducing stack by rule 3 (line 32):
1147 -> $$ = nterm @3 (: 3)
1150 Reducing stack by rule 4 (line 33):
1151 -> $$ = nterm @4 (: 4)
1154 Reading a token: Now at end of input.
1156 Error: popping nterm @4 (: 4)
1159 Error: popping nterm @3 (: 3)
1162 Error: popping nterm @2 (: 2)
1165 Error: popping nterm $@1 (: )
1167 Cleanup: discarding lookahead token $end (: )
1174 ## ----------------------- ##
1175 ## @$ implies %locations. ##
1176 ## ----------------------- ##
1178 # Bison once forgot to check for @$ in actions other than semantic actions.
1180 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1181 # -------------------------------------------
1182 m4_define([AT_CHECK_ACTION_LOCATIONS],
1183 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1184 AT_BISON_OPTION_PUSHDEFS
1185 AT_DATA_GRAMMAR([[input.y]],
1188 ]AT_YYERROR_DECLARE[
1195 fprintf (stderr, "%d\n", @$.first_line);
1196 } ]m4_if($1, [%initial-action], [], [[start]])[
1218 AT_BISON_CHECK([[-o input.c input.y]])
1219 AT_COMPILE([[input]])
1220 AT_BISON_OPTION_POPDEFS
1223 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1224 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1225 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1228 ## ------------------------- ##
1229 ## Qualified $$ in actions. ##
1230 ## ------------------------- ##
1232 # Check that we can used qualified $$ (v.g., $<type>$) not only in
1233 # rule actions, but also where $$ is valid: %printer and %destructor.
1235 # FIXME: Not actually checking %destructor, but it's the same code as
1238 # To do that, use a semantic value that has two fields (sem_type),
1239 # declare symbols to have only one of these types (INT, float), and
1240 # use $<type>$ to get the other one. Including for symbols that are
1241 # not typed (UNTYPED).
1243 m4_pushdef([AT_TEST],
1244 [AT_SETUP([[Qualified $$ in actions: $1]])
1246 AT_BISON_OPTION_PUSHDEFS([%locations %skeleton "$1"])
1248 AT_DATA_GRAMMAR([[input.y]],
1250 %defines // FIXME: Mandated by lalr1.cc in Bison 2.6.
1251 %locations // FIXME: Mandated by lalr1.cc in Bison 2.6.
1255 typedef struct sem_type
1261 # define YYSTYPE sem_type
1264 # include <iostream>
1266 report (std::ostream& yyo, int ival, float fval)
1268 yyo << "ival: " << ival << ", fval: " << fval;
1273 report (FILE* yyo, int ival, float fval)
1275 fprintf (yyo, "ival: %d, fval: %1.1f", ival, fval);
1282 ]AT_YYERROR_DECLARE[
1289 %printer { report (yyo, $$, $<fval>$); } <ival>;
1290 %printer { report (yyo, $<ival>$, $$ ); } <fval>;
1291 %printer { report (yyo, $<ival>$, $<fval>$); } <>;
1294 /* The lalr1.cc skeleton, for backward compatibility, defines
1295 a constructor for position that initializes the filename. The
1296 glr.cc skeleton does not (and in fact cannot: location/position
1297 are stored in a union, from which objects with constructors are
1298 excluded in C++). */
1313 $$ = $<fval>1 + $<fval>2;
1314 $<ival>$ = $<ival>1 + $][2;
1318 ]AT_YYLEX_DEFINE(AT_SKEL_CC_IF([[{yy::parser::token::UNTYPED,
1319 yy::parser::token::INT,
1321 [[{UNTYPED, INT, EOF}]]),
1322 [AT_VAL.ival = toknum * 10; AT_VAL.fval = toknum / 10.0;])[
1327 p.set_debug_level(1);
1328 return p.parse ();]], [[
1330 return yyparse ();]])[
1334 AT_FULL_COMPILE([[input]])
1335 AT_PARSER_CHECK([./input], 0, [], [stderr])
1336 # Don't be too picky on the traces, GLR is not exactly the same. Keep
1337 # only the lines from the printer.
1339 # Don't care about locations. FIXME: remove their removal when Bison
1340 # supports C++ without locations.
1341 AT_CHECK([[sed -ne 's/([-0-9.]*: /(/;/ival:/p' stderr]], 0,
1342 [[Reading a token: Next token is token UNTYPED (ival: 10, fval: 0.1)
1343 Shifting token UNTYPED (ival: 10, fval: 0.1)
1344 Reading a token: Next token is token INT (ival: 20, fval: 0.2)
1345 Shifting token INT (ival: 20, fval: 0.2)
1346 $][1 = token UNTYPED (ival: 10, fval: 0.1)
1347 $][2 = token INT (ival: 20, fval: 0.2)
1348 -> $$ = nterm float (ival: 30, fval: 0.3)
1349 Cleanup: popping nterm float (ival: 30, fval: 0.3)
1352 AT_BISON_OPTION_POPDEFS
1362 m4_popdef([AT_TEST])
1364 ## ----------------------------------------------- ##
1365 ## Fix user actions without a trailing semicolon. ##
1366 ## ----------------------------------------------- ##
1368 AT_SETUP([[Fix user actions without a trailing semicolon]])
1370 # This feature is undocumented, but we accidentally broke it in 2.3a,
1371 # and there was a complaint at:
1372 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1373 AT_BISON_OPTION_PUSHDEFS
1376 start: test2 test1 test0 testc;
1379 : 'a' { semi; /* TEST:N:2 */ }
1380 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1381 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1382 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1383 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1384 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1385 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1386 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1387 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1390 : 'a' { semi; // TEST:N:1 ;
1391 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1392 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1393 } | 'd' { semi; no_semi // TEST:Y:1 ;
1394 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1395 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1396 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1397 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1398 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1401 : 'a' { semi; // TEST:N:1 {}
1402 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1403 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1404 } | 'd' { semi; no_semi // TEST:Y:1 {}
1405 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1406 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1407 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1408 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1409 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1414 #define TEST_MACRO_N \
1415 []"broken\" $ @ $$ @$ [];\
1419 #define TEST_MACRO_N \
1420 []"broken\" $ @ $$ @$ [];\
1423 AT_BISON_OPTION_POPDEFS
1425 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1426 [[input.y:8.48: warning: a ';' might be needed at the end of action code
1427 input.y:8.48: warning: future versions of Bison will not add the ';'
1428 input.y:9.48: warning: a ';' might be needed at the end of action code
1429 input.y:9.48: warning: future versions of Bison will not add the ';'
1430 input.y:10.48: warning: a ';' might be needed at the end of action code
1431 input.y:10.48: warning: future versions of Bison will not add the ';'
1432 input.y:11.48: warning: a ';' might be needed at the end of action code
1433 input.y:11.48: warning: future versions of Bison will not add the ';'
1434 input.y:12.48: warning: a ';' might be needed at the end of action code
1435 input.y:12.48: warning: future versions of Bison will not add the ';'
1436 input.y:13.48: warning: a ';' might be needed at the end of action code
1437 input.y:13.48: warning: future versions of Bison will not add the ';'
1438 input.y:20.1: warning: a ';' might be needed at the end of action code
1439 input.y:20.1: warning: future versions of Bison will not add the ';'
1440 input.y:21.1: warning: a ';' might be needed at the end of action code
1441 input.y:21.1: warning: future versions of Bison will not add the ';'
1442 input.y:22.1: warning: a ';' might be needed at the end of action code
1443 input.y:22.1: warning: future versions of Bison will not add the ';'
1444 input.y:23.1: warning: a ';' might be needed at the end of action code
1445 input.y:23.1: warning: future versions of Bison will not add the ';'
1446 input.y:24.1: warning: a ';' might be needed at the end of action code
1447 input.y:24.1: warning: future versions of Bison will not add the ';'
1448 input.y:25.1: warning: a ';' might be needed at the end of action code
1449 input.y:25.1: warning: future versions of Bison will not add the ';'
1450 input.y:31.1: warning: a ';' might be needed at the end of action code
1451 input.y:31.1: warning: future versions of Bison will not add the ';'
1452 input.y:32.1: warning: a ';' might be needed at the end of action code
1453 input.y:32.1: warning: future versions of Bison will not add the ';'
1454 input.y:33.1: warning: a ';' might be needed at the end of action code
1455 input.y:33.1: warning: future versions of Bison will not add the ';'
1456 input.y:34.1: warning: a ';' might be needed at the end of action code
1457 input.y:34.1: warning: future versions of Bison will not add the ';'
1458 input.y:35.1: warning: a ';' might be needed at the end of action code
1459 input.y:35.1: warning: future versions of Bison will not add the ';'
1460 input.y:36.1: warning: a ';' might be needed at the end of action code
1461 input.y:36.1: warning: future versions of Bison will not add the ';'
1464 AT_MATCHES_CHECK([input.c], [[/\* TEST:N:2 \*/ \}$]], [[3]])
1465 AT_MATCHES_CHECK([input.c], [[/\* TEST:Y:2 \*/ ;\}$]], [[6]])
1466 AT_MATCHES_CHECK([input.c], [[// TEST:N:1 [;{}]*\n\}$]], [[6]])
1467 AT_MATCHES_CHECK([input.c], [[// TEST:Y:1 [;{}]*\n;\}$]], [[12]])
1468 AT_MATCHES_CHECK([input.c], [[#define TEST_MACRO_N \\\n\[\]"broken\\" \$ \@ \$\$ \@\$ \[\];\\\nstring;"\}]], [[2]])
1473 ## -------------------------------------------------- ##
1474 ## Destroying lookahead assigned by semantic action. ##
1475 ## -------------------------------------------------- ##
1477 AT_SETUP([[Destroying lookahead assigned by semantic action]])
1479 AT_BISON_OPTION_PUSHDEFS
1480 AT_DATA_GRAMMAR([input.y],
1485 ]AT_YYERROR_DECLARE[
1490 %destructor { fprintf (stderr, "'a' destructor\n"); } 'a'
1491 %destructor { fprintf (stderr, "'b' destructor\n"); } 'b'
1495 // In a previous version of Bison, yychar assigned by the semantic
1496 // action below was not translated into yytoken before the lookahead was
1497 // discarded and thus before its destructor (selected according to
1498 // yytoken) was called in order to return from yyparse. This would
1499 // happen even if YYACCEPT was performed in a later semantic action as
1500 // long as only consistent states with default reductions were visited
1501 // in between. However, we leave YYACCEPT in the same semantic action
1502 // for this test in order to show that skeletons cannot simply translate
1503 // immediately after every semantic action because a semantic action
1504 // that has set yychar might not always return normally. Instead,
1505 // skeletons must translate before every use of yytoken.
1506 start: 'a' accept { USE($1); } ;
1508 assert (yychar == YYEMPTY);
1515 ]AT_YYLEX_DEFINE(["a"])[
1522 AT_BISON_OPTION_POPDEFS
1523 AT_BISON_CHECK([[-o input.c input.y]])
1524 AT_COMPILE([[input]])
1525 AT_PARSER_CHECK([[./input]], [[0]], [],
1536 AT_SETUP([[YYBACKUP]])
1538 AT_BISON_OPTION_PUSHDEFS([%pure-parser])
1540 AT_DATA_GRAMMAR([input.y],
1547 # include <stdlib.h>
1548 # include <assert.h>
1550 ]AT_YYERROR_DECLARE[
1559 'a' { printf ("a: %d\n", $1); }
1560 | 'b' { YYBACKUP('a', 123); }
1561 | 'c' 'd' { YYBACKUP('a', 456); }
1566 ]AT_YYLEX_DEFINE(["bcd"], [*lvalp = (toknum + 1) * 10])[
1571 yydebug = !!getenv("YYDEBUG");
1575 AT_BISON_OPTION_POPDEFS
1577 AT_BISON_CHECK([[-o input.c input.y]])
1578 AT_COMPILE([[input]])
1579 AT_PARSER_CHECK([[./input]], [[0]],