1 # Executing Actions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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_BISON_CHECK([-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_BISON_CHECK([-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;
370 AT_FULL_COMPILE([input])
373 # Check the location of "empty"
374 # -----------------------------
375 # I.e., epsilon-reductions, as in "(x)" which ends by reducing
376 # an empty "line" nterm.
377 # FIXME: This location is not satisfying. Depend on the lookahead?
378 AT_PARSER_CHECK([./input '(x)'], 0,
379 [[sending: '(' (0@0-9)
380 sending: 'x' (1@10-19)
381 thing (1@10-19): 'x' (1@10-19)
382 sending: ')' (2@20-29)
383 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
384 sending: END (3@30-39)
385 input (0@29-29): /* Nothing */
386 input (2@0-29): line (0@0-29) input (0@29-29)
387 Freeing token END (3@30-39)
388 Freeing nterm input (2@0-29)
393 # Check locations in error recovery
394 # ---------------------------------
395 # '(y)' is an error, but can be recovered from. But what's the location
396 # of the error itself ('y'), and of the resulting reduction ('(error)').
397 AT_PARSER_CHECK([./input '(y)'], 0,
398 [[sending: '(' (0@0-9)
399 sending: 'y' (1@10-19)
400 10-19: syntax error, unexpected 'y', expecting 'x'
401 Freeing token 'y' (1@10-19)
402 sending: ')' (2@20-29)
403 line (-1@0-29): '(' (0@0-9) error (@10-19) ')' (2@20-29)
404 sending: END (3@30-39)
405 input (0@29-29): /* Nothing */
406 input (2@0-29): line (-1@0-29) input (0@29-29)
407 Freeing token END (3@30-39)
408 Freeing nterm input (2@0-29)
413 # Syntax errors caught by the parser
414 # ----------------------------------
415 # Exercise the discarding of stack top and input until `error'
418 # '(', 'x', 'x', 'x', 'x', 'x', ')',
420 # Load the stack and provoke an error that cannot be caught by the
421 # grammar, to check that the stack is cleared. And make sure the
422 # lookahead is freed.
427 AT_PARSER_CHECK([./input '(xxxxx)(x)(x)y'], 1,
428 [[sending: '(' (0@0-9)
429 sending: 'x' (1@10-19)
430 thing (1@10-19): 'x' (1@10-19)
431 sending: 'x' (2@20-29)
432 thing (2@20-29): 'x' (2@20-29)
433 sending: 'x' (3@30-39)
434 30-39: syntax error, unexpected 'x', expecting ')'
435 Freeing nterm thing (2@20-29)
436 Freeing nterm thing (1@10-19)
437 Freeing token 'x' (3@30-39)
438 sending: 'x' (4@40-49)
439 Freeing token 'x' (4@40-49)
440 sending: 'x' (5@50-59)
441 Freeing token 'x' (5@50-59)
442 sending: ')' (6@60-69)
443 line (-1@0-69): '(' (0@0-9) error (@10-59) ')' (6@60-69)
444 sending: '(' (7@70-79)
445 sending: 'x' (8@80-89)
446 thing (8@80-89): 'x' (8@80-89)
447 sending: ')' (9@90-99)
448 line (7@70-99): '(' (7@70-79) thing (8@80-89) ')' (9@90-99)
449 sending: '(' (10@100-109)
450 sending: 'x' (11@110-119)
451 thing (11@110-119): 'x' (11@110-119)
452 sending: ')' (12@120-129)
453 line (10@100-129): '(' (10@100-109) thing (11@110-119) ')' (12@120-129)
454 sending: 'y' (13@130-139)
455 input (0@129-129): /* Nothing */
456 input (2@100-129): line (10@100-129) input (0@129-129)
457 input (2@70-129): line (7@70-99) input (2@100-129)
458 input (2@0-129): line (-1@0-69) input (2@70-129)
459 130-139: syntax error, unexpected 'y', expecting END
460 Freeing nterm input (2@0-129)
461 Freeing token 'y' (13@130-139)
466 # Syntax error caught by the parser where lookahead = END
467 # --------------------------------------------------------
468 # Load the stack and provoke an error that cannot be caught by the
469 # grammar, to check that the stack is cleared. And make sure the
470 # lookahead is freed.
475 AT_PARSER_CHECK([./input '(x)(x)x'], 1,
476 [[sending: '(' (0@0-9)
477 sending: 'x' (1@10-19)
478 thing (1@10-19): 'x' (1@10-19)
479 sending: ')' (2@20-29)
480 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
481 sending: '(' (3@30-39)
482 sending: 'x' (4@40-49)
483 thing (4@40-49): 'x' (4@40-49)
484 sending: ')' (5@50-59)
485 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
486 sending: 'x' (6@60-69)
487 thing (6@60-69): 'x' (6@60-69)
488 sending: END (7@70-79)
489 70-79: syntax error, unexpected END, expecting 'x'
490 Freeing nterm thing (6@60-69)
491 Freeing nterm line (3@30-59)
492 Freeing nterm line (0@0-29)
493 Freeing token END (7@70-79)
498 # Check destruction upon stack overflow
499 # -------------------------------------
500 # Upon stack overflow, all symbols on the stack should be destroyed.
501 # Only check for yacc.c.
503 AT_PARSER_CHECK([./input '(x)(x)(x)(x)(x)(x)(x)'], 2,
504 [[sending: '(' (0@0-9)
505 sending: 'x' (1@10-19)
506 thing (1@10-19): 'x' (1@10-19)
507 sending: ')' (2@20-29)
508 line (0@0-29): '(' (0@0-9) thing (1@10-19) ')' (2@20-29)
509 sending: '(' (3@30-39)
510 sending: 'x' (4@40-49)
511 thing (4@40-49): 'x' (4@40-49)
512 sending: ')' (5@50-59)
513 line (3@30-59): '(' (3@30-39) thing (4@40-49) ')' (5@50-59)
514 sending: '(' (6@60-69)
515 sending: 'x' (7@70-79)
516 thing (7@70-79): 'x' (7@70-79)
517 sending: ')' (8@80-89)
518 line (6@60-89): '(' (6@60-69) thing (7@70-79) ')' (8@80-89)
519 sending: '(' (9@90-99)
520 sending: 'x' (10@100-109)
521 thing (10@100-109): 'x' (10@100-109)
522 sending: ')' (11@110-119)
523 line (9@90-119): '(' (9@90-99) thing (10@100-109) ')' (11@110-119)
524 sending: '(' (12@120-129)
525 sending: 'x' (13@130-139)
526 thing (13@130-139): 'x' (13@130-139)
527 sending: ')' (14@140-149)
528 line (12@120-149): '(' (12@120-129) thing (13@130-139) ')' (14@140-149)
529 sending: '(' (15@150-159)
530 sending: 'x' (16@160-169)
531 thing (16@160-169): 'x' (16@160-169)
532 sending: ')' (17@170-179)
533 line (15@150-179): '(' (15@150-159) thing (16@160-169) ')' (17@170-179)
534 sending: '(' (18@180-189)
535 sending: 'x' (19@190-199)
536 thing (19@190-199): 'x' (19@190-199)
537 sending: ')' (20@200-209)
538 200-209: memory exhausted
539 Freeing nterm thing (19@190-199)
540 Freeing nterm line (15@150-179)
541 Freeing nterm line (12@120-149)
542 Freeing nterm line (9@90-119)
543 Freeing nterm line (6@60-89)
544 Freeing nterm line (3@30-59)
545 Freeing nterm line (0@0-29)
546 Parsing FAILED (status 2).
553 # AT_CHECK_PRINTER_AND_DESTRUCTOR([BISON-OPTIONS], [UNION-FLAG], [SKIP_FLAG])
554 # ---------------------------------------------------------------------------
555 m4_define([AT_CHECK_PRINTER_AND_DESTRUCTOR],
556 [AT_SETUP([Printers and Destructors $2: $1])
559 _AT_CHECK_PRINTER_AND_DESTRUCTOR($[1], $[2], $[3], $[4],
570 AT_CHECK_PRINTER_AND_DESTRUCTOR([])
571 AT_CHECK_PRINTER_AND_DESTRUCTOR([], [with union])
573 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"])
574 AT_CHECK_PRINTER_AND_DESTRUCTOR([%defines %skeleton "lalr1.cc"], [with union])
576 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser])
577 AT_CHECK_PRINTER_AND_DESTRUCTOR([%glr-parser], [with union])
581 ## ----------------------------------------- ##
582 ## Default tagless %printer and %destructor. ##
583 ## ----------------------------------------- ##
585 # Check that the right %printer and %destructor are called, that they're not
586 # called for $end, and that $$ and @$ work correctly.
588 AT_SETUP([Default tagless %printer and %destructor])
590 AT_DATA_GRAMMAR([[input.y]],
595 @$.first_line = @$.last_line = 1;
596 @$.first_column = @$.last_column = 1;
602 static void yyerror (const char *msg);
603 static int yylex (void);
608 fprintf (yyoutput, "<*> printer should not be called.\n");
612 fprintf (yyoutput, "<> printer for '%c' @ %d", $$, @$.first_column);
615 fprintf (stdout, "<> destructor for '%c' @ %d.\n", $$, @$.first_column);
619 fprintf (yyoutput, "'b'/'c' printer for '%c' @ %d", $$, @$.first_column);
622 fprintf (stdout, "'b'/'c' destructor for '%c' @ %d.\n", $$, @$.first_column);
626 fprintf (yyoutput, "<*> destructor should not be called.\n");
631 start: 'a' 'b' 'c' 'd' 'e' { $$ = 'S'; USE(($1, $2, $3, $4, $5)); } ;
638 static char const input[] = "abcd";
639 static size_t toknum;
640 if (! (toknum < sizeof input))
642 yylval = input[toknum++];
643 yylloc.first_line = yylloc.last_line = 1;
644 yylloc.first_column = yylloc.last_column = toknum;
649 yyerror (const char *msg)
651 fprintf (stderr, "%s\n", msg);
662 AT_BISON_CHECK([-o input.c input.y])
664 AT_PARSER_CHECK([./input], 1,
665 [[<> destructor for 'd' @ 4.
666 'b'/'c' destructor for 'c' @ 3.
667 'b'/'c' destructor for 'b' @ 2.
668 <> destructor for 'a' @ 1.
672 Reading a token: Next token is token 'a' (1.1-1.1: <> printer for 'a' @ 1)
673 Shifting token 'a' (1.1-1.1: <> printer for 'a' @ 1)
675 Reading a token: Next token is token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
676 Shifting token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
678 Reading a token: Next token is token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
679 Shifting token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
681 Reading a token: Next token is token 'd' (1.4-1.4: <> printer for 'd' @ 4)
682 Shifting token 'd' (1.4-1.4: <> printer for 'd' @ 4)
684 Reading a token: Now at end of input.
685 syntax error, unexpected $end, expecting 'e'
686 Error: popping token 'd' (1.4-1.4: <> printer for 'd' @ 4)
688 Error: popping token 'c' (1.3-1.3: 'b'/'c' printer for 'c' @ 3)
690 Error: popping token 'b' (1.2-1.2: 'b'/'c' printer for 'b' @ 2)
692 Error: popping token 'a' (1.1-1.1: <> printer for 'a' @ 1)
694 Cleanup: discarding lookahead token $end (1.5-1.5: )
702 ## ------------------------------------------------------ ##
703 ## Default tagged and per-type %printer and %destructor. ##
704 ## ------------------------------------------------------ ##
706 AT_SETUP([Default tagged and per-type %printer and %destructor])
708 AT_DATA_GRAMMAR([[input.y]],
715 static void yyerror (const char *msg);
716 static int yylex (void);
721 fprintf (yyoutput, "<> printer should not be called.\n");
724 %union { int field0; int field1; int field2; }
725 %type <field0> start 'a' 'g'
729 fprintf (yyoutput, "<*>/<field2>/e printer");
732 fprintf (stdout, "<*>/<field2>/e destructor.\n");
736 %printer { fprintf (yyoutput, "<field1> printer"); } <field1>
737 %destructor { fprintf (stdout, "<field1> destructor.\n"); } <field1>
740 %printer { fprintf (yyoutput, "'c' printer"); } 'c'
741 %destructor { fprintf (stdout, "'c' destructor.\n"); } 'c'
744 %printer { fprintf (yyoutput, "'d' printer"); } 'd'
745 %destructor { fprintf (stdout, "'d' destructor.\n"); } 'd'
748 fprintf (yyoutput, "<> destructor should not be called.\n");
754 'a' 'b' 'c' 'd' 'e' 'f' 'g'
756 USE(($1, $2, $3, $4, $5, $6, $7));
766 static char const input[] = "abcdef";
767 static size_t toknum;
768 if (! (toknum < sizeof input))
770 return input[toknum++];
774 yyerror (const char *msg)
776 fprintf (stderr, "%s\n", msg);
787 AT_BISON_CHECK([-o input.c input.y])
789 AT_PARSER_CHECK([./input], 1,
790 [[<*>/<field2>/e destructor.
791 <*>/<field2>/e destructor.
795 <*>/<field2>/e destructor.
799 Reading a token: Next token is token 'a' (<*>/<field2>/e printer)
800 Shifting token 'a' (<*>/<field2>/e printer)
802 Reading a token: Next token is token 'b' (<field1> printer)
803 Shifting token 'b' (<field1> printer)
805 Reading a token: Next token is token 'c' ('c' printer)
806 Shifting token 'c' ('c' printer)
808 Reading a token: Next token is token 'd' ('d' printer)
809 Shifting token 'd' ('d' printer)
811 Reading a token: Next token is token 'e' (<*>/<field2>/e printer)
812 Shifting token 'e' (<*>/<field2>/e printer)
814 Reading a token: Next token is token 'f' (<*>/<field2>/e printer)
815 Shifting token 'f' (<*>/<field2>/e printer)
817 Reading a token: Now at end of input.
818 syntax error, unexpected $end, expecting 'g'
819 Error: popping token 'f' (<*>/<field2>/e printer)
820 Stack now 0 1 3 5 6 7
821 Error: popping token 'e' (<*>/<field2>/e printer)
823 Error: popping token 'd' ('d' printer)
825 Error: popping token 'c' ('c' printer)
827 Error: popping token 'b' (<field1> printer)
829 Error: popping token 'a' (<*>/<field2>/e printer)
831 Cleanup: discarding lookahead token $end ()
839 ## ------------------------------------------------------------- ##
840 ## Default %printer and %destructor for user-defined end token. ##
841 ## ------------------------------------------------------------- ##
843 AT_SETUP([Default %printer and %destructor for user-defined end token])
845 # _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(TYPED)
846 # -----------------------------------------------------------------------------
847 m4_define([_AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN],
849 [m4_pushdef([kind], []) m4_pushdef([not_kind], [*])],
850 [m4_pushdef([kind], [*]) m4_pushdef([not_kind], [])])
852 AT_DATA_GRAMMAR([[input]]$1[[.y]],
857 @$.first_line = @$.last_line = 1;
858 @$.first_column = @$.last_column = 1;
864 static void yyerror (const char *msg);
865 static int yylex (void);
870 fprintf (yyoutput, "<]]not_kind[[> destructor should not be called.\n");
875 fprintf (yyoutput, "<]]kind[[> for '%c' @ %d", $$, @$.first_column);
878 fprintf (stdout, "<]]kind[[> for '%c' @ %d.\n", $$, @$.first_column);
882 fprintf (yyoutput, "<]]not_kind[[> printer should not be called.\n");
887 [[[%union { char tag; }
888 %type <tag> start END]]])[[
892 start: { $$ = 'S'; } ;
902 yylval]]m4_if($1, 0,, [[[.tag]]])[[ = 'E';
903 yylloc.first_line = yylloc.last_line = 1;
904 yylloc.first_column = yylloc.last_column = 1;
909 yyerror (const char *msg)
911 fprintf (stderr, "%s\n", msg);
922 AT_BISON_CHECK([-o input$1.c input$1.y])
923 AT_COMPILE([input$1])
924 AT_PARSER_CHECK([./input$1], 0,
925 [[<]]kind[[> for 'E' @ 1.
926 <]]kind[[> for 'S' @ 1.
930 Reducing stack by rule 1 (line 46):
931 -> $$ = nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
934 Reading a token: Now at end of input.
935 Shifting token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
938 Cleanup: popping token END (1.1-1.1: <]]kind[[> for 'E' @ 1)
939 Cleanup: popping nterm start (1.1-1.1: <]]kind[[> for 'S' @ 1)
943 m4_popdef([not_kind])
946 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(0)
947 _AT_CHECK_DEFAULT_PRINTER_AND_DESTRUCTOR_FOR_END_TOKEN(1)
953 ## ------------------------------------------------------------------ ##
954 ## Default %printer and %destructor are not for error or $undefined. ##
955 ## ------------------------------------------------------------------ ##
957 AT_SETUP([Default %printer and %destructor are not for error or $undefined])
959 # If Bison were to apply the default %printer and %destructor to the error
960 # token or to $undefined:
961 # - For the error token:
962 # - It would generate warnings for unused $n.
963 # - It would invoke the %printer and %destructor on the error token's
964 # semantic value, which would be initialized from the lookahead, which
965 # would be destroyed separately.
966 # - For $undefined, who knows what the semantic value would be.
968 AT_DATA_GRAMMAR([[input.y]],
974 static void yyerror (const char *msg);
975 static int yylex (void);
980 fprintf (yyoutput, "'%c'", $$);
983 fprintf (stderr, "DESTROY '%c'\n", $$);
990 /* In order to reveal the problems that this bug caused during parsing, add
992 | 'a' error 'b' 'c' { USE(($1, $3, $4)); $$ = 'S'; }
1000 static char const input[] = "abd";
1001 static size_t toknum;
1002 if (! (toknum < sizeof input))
1004 yylval = input[toknum++];
1009 yyerror (const char *msg)
1011 fprintf (stderr, "%s\n", msg);
1022 AT_BISON_CHECK([-o input.c input.y])
1024 AT_PARSER_CHECK([./input], [1], [],
1027 Reading a token: Next token is token 'a' ('a')
1028 Shifting token 'a' ('a')
1030 Reading a token: Next token is token 'b' ('b')
1032 Shifting token error ()
1034 Next token is token 'b' ('b')
1035 Shifting token 'b' ('b')
1037 Reading a token: Next token is token $undefined ()
1038 Error: popping token 'b' ('b')
1041 Error: popping token error ()
1043 Shifting token error ()
1045 Next token is token $undefined ()
1046 Error: discarding token $undefined ()
1047 Error: popping token error ()
1049 Shifting token error ()
1051 Reading a token: Now at end of input.
1052 Cleanup: discarding lookahead token $end ()
1054 Cleanup: popping token error ()
1055 Cleanup: popping token 'a' ('a')
1063 ## ------------------------------------------------------ ##
1064 ## Default %printer and %destructor are not for $accept. ##
1065 ## ------------------------------------------------------ ##
1067 AT_SETUP([Default %printer and %destructor are not for $accept])
1069 # If YYSTYPE is a union and Bison were to apply the default %printer and
1070 # %destructor to $accept:
1071 # - The %printer and %destructor code generated for $accept would always be
1072 # dead code because $accept is currently never shifted onto the stack.
1073 # - $$ for $accept would always be of type YYSTYPE because it's not possible
1074 # to declare `%type <field> $accept'. (Also true for $undefined.)
1075 # - Thus, the compiler might complain that the user code assumes the wrong
1076 # type for $$ since the code might assume the type associated with a
1077 # specific union field, which is especially reasonable in C++ since that
1078 # type may be a base type. This test case checks for this problem. (Also
1079 # true for $undefined and the error token, so there are three warnings for
1080 # %printer and three for %destructor.)
1082 AT_DATA_GRAMMAR([[input.y]],
1083 [[%debug /* So that %printer is actually compiled. */
1087 # include <stdlib.h>
1088 static void yyerror (const char *msg);
1089 static int yylex (void);
1095 fprintf (yyoutput, "'%c'", chr);
1099 fprintf (stderr, "DESTROY '%c'\n", chr);
1102 %union { char chr; }
1107 start: { USE($$); } ;
1121 yyerror (const char *msg)
1123 fprintf (stderr, "%s\n", msg);
1133 AT_BISON_CHECK([-o input.c input.y])
1140 ## ------------------------------------------------------ ##
1141 ## Default %printer and %destructor for mid-rule values. ##
1142 ## ------------------------------------------------------ ##
1144 AT_SETUP([Default %printer and %destructor for mid-rule values])
1146 AT_DATA_GRAMMAR([[input.y]],
1147 [[%debug /* So that %printer is actually compiled. */
1151 # include <stdlib.h>
1152 static void yyerror (const char *msg);
1153 static int yylex (void);
1155 # define YYLTYPE int
1156 # define YYLLOC_DEFAULT(Current, Rhs, N)
1157 # define YY_LOCATION_PRINT(File, Loc)
1160 %printer { fprintf (yyoutput, "%d", @$); } <>
1161 %destructor { fprintf (stderr, "DESTROY %d\n", @$); } <>
1162 %printer { fprintf (yyoutput, "<*> printer should not be called"); } <*>
1163 %destructor { fprintf (yyoutput, "<*> destructor should not be called"); } <*>
1168 { @$ = 1; } // Not set or used.
1169 { USE ($$); @$ = 2; } // Both set and used.
1170 { USE ($$); @$ = 3; } // Only set.
1171 { @$ = 4; } // Only used.
1173 { USE (($$, $2, $4, $5)); @$ = 0; }
1188 yyerror (const char *msg)
1190 fprintf (stderr, "%s\n", msg);
1201 AT_BISON_CHECK([-o input.c input.y], 0,,
1202 [[input.y:33.3-23: warning: unset value: $$
1203 input.y:30.3-35.37: warning: unused value: $3
1207 AT_PARSER_CHECK([./input], 1,,
1210 Reducing stack by rule 1 (line 30):
1211 -> $$ = nterm $@1 (: )
1214 Reducing stack by rule 2 (line 31):
1215 -> $$ = nterm @2 (: 2)
1218 Reducing stack by rule 3 (line 32):
1219 -> $$ = nterm @3 (: 3)
1222 Reducing stack by rule 4 (line 33):
1223 -> $$ = nterm @4 (: 4)
1226 Reading a token: Now at end of input.
1228 Error: popping nterm @4 (: 4)
1231 Error: popping nterm @3 (: 3)
1234 Error: popping nterm @2 (: 2)
1237 Error: popping nterm $@1 (: )
1239 Cleanup: discarding lookahead token $end (: )
1246 ## ----------------------- ##
1247 ## @$ implies %locations. ##
1248 ## ----------------------- ##
1250 # Bison once forgot to check for @$ in actions other than semantic actions.
1252 # AT_CHECK_ACTION_LOCATIONS(ACTION-DIRECTIVE)
1253 # -------------------------------------------------------
1254 m4_define([AT_CHECK_ACTION_LOCATIONS],
1255 [AT_SETUP([[@$ in ]$1[ implies %locations]])
1257 AT_DATA_GRAMMAR([[input.y]],
1260 static int yylex (void);
1261 static void yyerror (char const *msg);
1267 printf ("%d\n", @$.first_line);
1268 } ]m4_if($1, [%initial-action], [], [[start]])[
1283 yyerror (char const *msg)
1285 fprintf (stderr, "%s\n", msg);
1295 AT_BISON_CHECK([[-o input.c input.y]])
1296 AT_COMPILE([[input]])
1300 AT_CHECK_ACTION_LOCATIONS([[%initial-action]])
1301 AT_CHECK_ACTION_LOCATIONS([[%destructor]])
1302 AT_CHECK_ACTION_LOCATIONS([[%printer]])
1305 ## ----------------------------------------------- ##
1306 ## Fix user actions without a trailing semicolon. ##
1307 ## ----------------------------------------------- ##
1309 AT_SETUP([[Fix user actions without a trailing semicolon]])
1311 # This feature is undocumented, but we accidentally broke it in 2.3a,
1312 # and there was a complaint at:
1313 # <http://lists.gnu.org/archive/html/bug-bison/2008-11/msg00001.html>.
1317 start: test2 test1 test0 testc;
1320 : 'a' { semi; /* TEST:N:2 */ }
1321 | 'b' { if (0) {no_semi} /* TEST:N:2 */ }
1322 | 'c' { if (0) {semi;} /* TEST:N:2 */ }
1323 | 'd' { semi; no_semi /* TEST:Y:2 */ }
1324 | 'e' { semi(); no_semi() /* TEST:Y:2 */ }
1325 | 'f' { semi[]; no_semi[] /* TEST:Y:2 */ }
1326 | 'g' { semi++; no_semi++ /* TEST:Y:2 */ }
1327 | 'h' { {no_semi} no_semi /* TEST:Y:2 */ }
1328 | 'i' { {semi;} no_semi /* TEST:Y:2 */ }
1331 : 'a' { semi; // TEST:N:1 ;
1332 } | 'b' { if (0) {no_semi} // TEST:N:1 ;
1333 } | 'c' { if (0) {semi;} // TEST:N:1 ;
1334 } | 'd' { semi; no_semi // TEST:Y:1 ;
1335 } | 'e' { semi(); no_semi() // TEST:Y:1 ;
1336 } | 'f' { semi[]; no_semi[] // TEST:Y:1 ;
1337 } | 'g' { semi++; no_semi++ // TEST:Y:1 ;
1338 } | 'h' { {no_semi} no_semi // TEST:Y:1 ;
1339 } | 'i' { {semi;} no_semi // TEST:Y:1 ;
1342 : 'a' { semi; // TEST:N:1 {}
1343 } | 'b' { if (0) {no_semi} // TEST:N:1 {}
1344 } | 'c' { if (0) {semi;} // TEST:N:1 {}
1345 } | 'd' { semi; no_semi // TEST:Y:1 {}
1346 } | 'e' { semi(); no_semi() // TEST:Y:1 {}
1347 } | 'f' { semi[]; no_semi[] // TEST:Y:1 {}
1348 } | 'g' { semi++; no_semi++ // TEST:Y:1 {}
1349 } | 'h' { {no_semi} no_semi // TEST:Y:1 {}
1350 } | 'i' { {semi;} no_semi // TEST:Y:1 {}
1355 #define TEST_MACRO_N \
1356 []"broken\" $ @ $$ @$ [];\
1360 #define TEST_MACRO_N \
1361 []"broken\" $ @ $$ @$ [];\
1365 AT_BISON_CHECK([[-o input.c input.y]], [0], [],
1366 [[input.y:8.48: warning: a `;' might be needed at the end of action code
1367 input.y:8.48: warning: future versions of Bison will not add the `;'
1368 input.y:9.48: warning: a `;' might be needed at the end of action code
1369 input.y:9.48: warning: future versions of Bison will not add the `;'
1370 input.y:10.48: warning: a `;' might be needed at the end of action code
1371 input.y:10.48: warning: future versions of Bison will not add the `;'
1372 input.y:11.48: warning: a `;' might be needed at the end of action code
1373 input.y:11.48: warning: future versions of Bison will not add the `;'
1374 input.y:12.48: warning: a `;' might be needed at the end of action code
1375 input.y:12.48: warning: future versions of Bison will not add the `;'
1376 input.y:13.48: warning: a `;' might be needed at the end of action code
1377 input.y:13.48: warning: future versions of Bison will not add the `;'
1378 input.y:20.1: warning: a `;' might be needed at the end of action code
1379 input.y:20.1: warning: future versions of Bison will not add the `;'
1380 input.y:21.1: warning: a `;' might be needed at the end of action code
1381 input.y:21.1: warning: future versions of Bison will not add the `;'
1382 input.y:22.1: warning: a `;' might be needed at the end of action code
1383 input.y:22.1: warning: future versions of Bison will not add the `;'
1384 input.y:23.1: warning: a `;' might be needed at the end of action code
1385 input.y:23.1: warning: future versions of Bison will not add the `;'
1386 input.y:24.1: warning: a `;' might be needed at the end of action code
1387 input.y:24.1: warning: future versions of Bison will not add the `;'
1388 input.y:25.1: warning: a `;' might be needed at the end of action code
1389 input.y:25.1: warning: future versions of Bison will not add the `;'
1390 input.y:31.1: warning: a `;' might be needed at the end of action code
1391 input.y:31.1: warning: future versions of Bison will not add the `;'
1392 input.y:32.1: warning: a `;' might be needed at the end of action code
1393 input.y:32.1: warning: future versions of Bison will not add the `;'
1394 input.y:33.1: warning: a `;' might be needed at the end of action code
1395 input.y:33.1: warning: future versions of Bison will not add the `;'
1396 input.y:34.1: warning: a `;' might be needed at the end of action code
1397 input.y:34.1: warning: future versions of Bison will not add the `;'
1398 input.y:35.1: warning: a `;' might be needed at the end of action code
1399 input.y:35.1: warning: future versions of Bison will not add the `;'
1400 input.y:36.1: warning: a `;' might be needed at the end of action code
1401 input.y:36.1: warning: future versions of Bison will not add the `;'
1403 AT_CHECK([[grep -c '/\* TEST:N:2 \*/ }$' input.c]], [0], [[3
1405 AT_CHECK([[grep -c '/\* TEST:Y:2 \*/ ;}$' input.c]], [0], [[6
1407 AT_CHECK([[sed -n '/TEST:N:1/{N
1408 s/\n/<NL>/gp}' input.c | grep -c '// TEST:N:1 [;{}]*<NL>}$']], [0], [[6
1410 AT_CHECK([[sed -n '/TEST:Y:1/{N
1411 s/\n/<NL>/gp}' input.c | grep -c '// TEST:Y:1 [;{}]*<NL>;}$']], [0], [[12
1413 AT_CHECK([[sed -n '/^#define TEST_MACRO_N/{N
1415 s/\n/<NL>/gp}' input.c | grep -F -xc '#define TEST_MACRO_N \<NL>[]"broken\" $ @ $$ @$ [];\<NL>string;"}']],