1 # Bison Regressions. -*- Autotest -*-
3 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006 Free Software
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 AT_BANNER([[Regression tests.]])
22 ## ------------------ ##
23 ## Trivial grammars. ##
24 ## ------------------ ##
26 AT_SETUP([Trivial grammars])
28 AT_DATA_GRAMMAR([input.y],
30 void yyerror (char const *);
42 AT_CHECK([bison -o input.c input.y])
43 AT_COMPILE([input.o], [-c input.c])
44 AT_COMPILE([input.o], [-DYYDEBUG -c input.c])
50 ## ----------------- ##
51 ## YYSTYPE typedef. ##
52 ## ----------------- ##
54 AT_SETUP([YYSTYPE typedef])
56 AT_DATA_GRAMMAR([input.y],
58 void yyerror (char const *);
60 typedef union { char const *val; } YYSTYPE;
67 program: { $$ = ""; };
70 AT_CHECK([bison -o input.c input.y])
71 AT_COMPILE([input.o], [-c input.c])
77 ## ------------------------------------- ##
78 ## Early token definitions with --yacc. ##
79 ## ------------------------------------- ##
82 AT_SETUP([Early token definitions with --yacc])
84 # Found in GCJ: they expect the tokens to be defined before the user
85 # prologue, so that they can use the token definitions in it.
87 AT_DATA_GRAMMAR([input.y],
89 void yyerror (const char *s);
99 # error "MY_TOKEN not defined."
108 AT_CHECK([bison -y -o input.c input.y])
109 AT_COMPILE([input.o], [-c input.c])
115 ## ---------------------------------------- ##
116 ## Early token definitions without --yacc. ##
117 ## ---------------------------------------- ##
120 AT_SETUP([Early token definitions without --yacc])
122 # Found in GCJ: they expect the tokens to be defined before the user
123 # prologue, so that they can use the token definitions in it.
125 AT_DATA_GRAMMAR([input.y],
128 void yyerror (const char *s);
130 void print_my_token (void);
139 print_my_token (void)
141 enum yytokentype my_token = MY_TOKEN;
142 printf ("%d\n", my_token);
151 AT_CHECK([bison -o input.c input.y])
152 AT_COMPILE([input.o], [-c input.c])
158 ## ---------------- ##
159 ## Braces parsing. ##
160 ## ---------------- ##
163 AT_SETUP([Braces parsing])
166 [[/* Bison used to swallow the character after `}'. */
169 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
173 AT_CHECK([bison -v -o input.c input.y])
175 AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
180 ## ------------------ ##
181 ## Duplicate string. ##
182 ## ------------------ ##
185 AT_SETUP([Duplicate string])
188 [[/* `Bison -v' used to dump core when two tokens are defined with the same
189 string, as LE and GE below. */
196 exp: '(' exp ')' | NUM ;
200 AT_CHECK([bison -v -o input.c input.y], 0, [],
201 [[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string
207 ## ------------------- ##
208 ## Rule Line Numbers. ##
209 ## ------------------- ##
211 AT_SETUP([Rule Line Numbers])
213 AT_KEYWORDS([report])
245 AT_CHECK([bison -o input.c -v input.y])
247 # Check the contents of the report.
248 AT_CHECK([cat input.output], [],
262 Terminals, with rules where they appear
271 Nonterminals, with rules where they appear
276 on left: 2 4, on right: 0
278 on left: 1, on right: 2
280 on left: 3, on right: 4
285 0 $accept: . expr $end
287 'a' shift, and go to state 1
289 $default reduce using rule 3 ($@2)
297 2 expr: 'a' . $@1 'b'
299 $default reduce using rule 1 ($@1)
306 0 $accept: expr . $end
308 $end shift, and go to state 5
315 'c' shift, and go to state 6
320 2 expr: 'a' $@1 . 'b'
322 'b' shift, and go to state 7
327 0 $accept: expr $end .
336 $default reduce using rule 4 (expr)
341 2 expr: 'a' $@1 'b' .
343 $default reduce using rule 2 (expr)
350 ## ---------------------- ##
351 ## Mixing %token styles. ##
352 ## ---------------------- ##
355 AT_SETUP([Mixing %token styles])
357 # Taken from the documentation.
359 [[%token <operator> OR "||"
360 %token <operator> LE 134 "<="
367 AT_CHECK([bison -v -o input.c input.y])
373 ## ---------------- ##
374 ## Invalid inputs. ##
375 ## ---------------- ##
378 AT_SETUP([Invalid inputs])
390 AT_CHECK([bison input.y], [1], [],
391 [[input.y:2.1: invalid character: `?'
392 input.y:3.14: invalid character: `}'
393 input.y:4.1: invalid character: `%'
394 input.y:4.2: invalid character: `&'
395 input.y:5.1-17: invalid directive: `%a-does-not-exist'
396 input.y:6.1: invalid character: `%'
397 input.y:6.2: invalid character: `-'
398 input.y:7.1-8.0: missing `%}' at end of file
399 input.y:7.1-8.0: syntax error, unexpected %{...%}
405 AT_SETUP([Invalid inputs with {}])
417 AT_CHECK([bison input.y], [1], [],
418 [[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
425 ## ------------------- ##
426 ## Token definitions. ##
427 ## ------------------- ##
430 AT_SETUP([Token definitions])
432 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
433 AT_DATA_GRAMMAR([input.y],
437 void yyerror (const char *s);
441 %token MYEOF 0 "end of file"
446 %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
448 exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
451 yyerror (char const *s)
453 fprintf (stderr, "%s\n", s);
472 AT_CHECK([bison -o input.c input.y])
475 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
477 AT_PARSER_CHECK([./input], 1, [], [experr])
482 ## -------------------- ##
483 ## Characters Escapes. ##
484 ## -------------------- ##
487 AT_SETUP([Characters Escapes])
489 AT_DATA_GRAMMAR([input.y],
491 void yyerror (const char *s);
501 # Pacify font-lock-mode: "
503 AT_CHECK([bison -o input.c input.y])
504 AT_COMPILE([input.o], [-c input.c])
513 # The generation of the reduction was once wrong in Bison, and made it
514 # miss some reductions. In the following test case, the reduction on
515 # `undef_id_tok' in state 1 was missing. This is stripped down from
516 # the actual web2c.y.
518 AT_SETUP([Web2c Report])
520 AT_KEYWORDS([report])
523 [[%token undef_id_tok const_id_tok
525 %start CONST_DEC_PART
534 | CONST_DEC_LIST CONST_DEC
538 { } undef_id_tok '=' const_id_tok ';'
543 AT_CHECK([bison -v input.y])
544 AT_CHECK([cat input.output], 0,
547 0 $accept: CONST_DEC_PART $end
549 1 CONST_DEC_PART: CONST_DEC_LIST
551 2 CONST_DEC_LIST: CONST_DEC
552 3 | CONST_DEC_LIST CONST_DEC
556 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';'
559 Terminals, with rules where they appear
569 Nonterminals, with rules where they appear
574 on left: 1, on right: 0
576 on left: 2 3, on right: 1 3
578 on left: 5, on right: 2 3
580 on left: 4, on right: 5
585 0 $accept: . CONST_DEC_PART $end
587 $default reduce using rule 4 ($@1)
589 CONST_DEC_PART go to state 1
590 CONST_DEC_LIST go to state 2
591 CONST_DEC go to state 3
597 0 $accept: CONST_DEC_PART . $end
599 $end shift, and go to state 5
604 1 CONST_DEC_PART: CONST_DEC_LIST .
605 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
607 undef_id_tok reduce using rule 4 ($@1)
608 $default reduce using rule 1 (CONST_DEC_PART)
610 CONST_DEC go to state 6
616 2 CONST_DEC_LIST: CONST_DEC .
618 $default reduce using rule 2 (CONST_DEC_LIST)
623 5 CONST_DEC: $@1 . undef_id_tok '=' const_id_tok ';'
625 undef_id_tok shift, and go to state 7
630 0 $accept: CONST_DEC_PART $end .
637 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
639 $default reduce using rule 3 (CONST_DEC_LIST)
644 5 CONST_DEC: $@1 undef_id_tok . '=' const_id_tok ';'
646 '=' shift, and go to state 8
651 5 CONST_DEC: $@1 undef_id_tok '=' . const_id_tok ';'
653 const_id_tok shift, and go to state 9
658 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok . ';'
660 ';' shift, and go to state 10
665 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';' .
667 $default reduce using rule 5 (CONST_DEC)
673 ## --------------- ##
675 ## --------------- ##
677 # The generation of the mapping `state -> action' was once wrong in
678 # extremely specific situations. web2c.y exhibits this situation.
679 # Below is a stripped version of the grammar. It looks like one can
680 # simplify it further, but just don't: it is tuned to exhibit a bug,
681 # which disapears when applying sane grammar transformations.
683 # It used to be wrong on yydefact only:
685 # static const yytype_uint8 yydefact[] =
687 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
688 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
692 # but let's check all the tables.
695 AT_SETUP([Web2c Actions])
697 AT_KEYWORDS([report])
701 statement: struct_stat;
702 struct_stat: /* empty. */ | if else;
703 if: "if" "const" "then" statement;
704 else: "else" statement;
708 AT_CHECK([bison -v -o input.c input.y])
710 # Check only the tables.
711 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
713 AT_CHECK([[cat tables.c]], 0,
714 [[static const yytype_uint8 yytranslate[] =
716 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
717 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
718 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
719 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
720 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
721 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
722 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
723 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
724 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
725 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
726 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
727 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
728 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
729 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
730 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
731 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
732 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
733 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
734 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
735 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
736 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
737 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
738 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
739 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
740 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
741 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
744 static const yytype_uint8 yyprhs[] =
748 static const yytype_int8 yyrhs[] =
750 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
751 4, 5, 8, -1, 6, 8, -1
753 static const yytype_uint8 yyrline[] =
757 static const char *const yytname[] =
759 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
760 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
762 static const yytype_uint16 yytoknum[] =
764 0, 256, 257, 258, 259, 260, 261
766 static const yytype_uint8 yyr1[] =
768 0, 7, 8, 9, 9, 10, 11
770 static const yytype_uint8 yyr2[] =
774 static const yytype_uint8 yydefact[] =
776 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
779 static const yytype_int8 yydefgoto[] =
783 static const yytype_int8 yypact[] =
785 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
788 static const yytype_int8 yypgoto[] =
792 static const yytype_uint8 yytable[] =
794 10, 1, 11, 5, 6, 0, 7, 9
796 static const yytype_int8 yycheck[] =
798 7, 3, 9, 4, 0, -1, 6, 5
800 static const yytype_uint8 yystos[] =
802 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
810 ## ------------------------- ##
811 ## yycheck Bound Violation. ##
812 ## ------------------------- ##
815 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
816 # --------------------------------
817 # The following grammar, taken from Andrew Suffield's GPL'd implementation
818 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
819 # yycheck's bounds where issuing a verbose error message. Keep this test
820 # so that possible bound checking compilers could check all the skeletons.
821 m4_define([_AT_DATA_DANCER_Y],
822 [AT_DATA_GRAMMAR([dancer.y],
824 static int yylex (AT_LALR1_CC_IF([int *], [void]));
828 static void yyerror (const char *);])
831 %token ARROW INVALID NUMBER STRING DATA
835 /* Grammar follows */
840 header: '<' from ARROW to '>' type ':'
841 | '<' ARROW to '>' type ':'
875 [/* A C++ error reporting function. */
877 yy::parser::error (const location&, const std::string& m)
879 std::cerr << m << std::endl;
887 parser.set_debug_level (YYDEBUG);
889 return parser.parse ();
893 yyerror (const char *s)
895 fprintf (stderr, "%s\n", s);
899 yylex (AT_LALR1_CC_IF([int *lval], [void]))
901 static int const tokens[] =
905 static size_t toknum;
906 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
907 if (! (toknum < sizeof tokens / sizeof *tokens))
909 return tokens[toknum++];
918 ])# _AT_DATA_DANCER_Y
921 # AT_CHECK_DANCER(BISON-OPTIONS)
922 # ------------------------------
923 # Generate the grammar, compile it, run it.
924 m4_define([AT_CHECK_DANCER],
925 [AT_SETUP([Dancer $1])
926 AT_BISON_OPTION_PUSHDEFS([$1])
927 _AT_DATA_DANCER_Y([$1])
928 AT_CHECK([bison -o dancer.c dancer.y])
930 [AT_CHECK([bison -o dancer.cc dancer.y])
931 AT_COMPILE_CXX([dancer])],
932 [AT_CHECK([bison -o dancer.c dancer.y])
933 AT_COMPILE([dancer])])
934 AT_PARSER_CHECK([./dancer], 1, [],
935 [syntax error, unexpected ':'
937 AT_BISON_OPTION_POPDEFS
942 AT_CHECK_DANCER([%glr-parser])
943 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
946 ## ------------------------------------------ ##
947 ## Diagnostic that expects two alternatives. ##
948 ## ------------------------------------------ ##
951 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
952 # --------------------------------
953 m4_define([_AT_DATA_EXPECT2_Y],
954 [AT_DATA_GRAMMAR([expect2.y],
956 static int yylex (AT_LALR1_CC_IF([int *], [void]));
960 static void yyerror (const char *);])
978 [/* A C++ error reporting function. */
980 yy::parser::error (const location&, const std::string& m)
982 std::cerr << m << std::endl;
989 return parser.parse ();
993 yyerror (const char *s)
995 fprintf (stderr, "%s\n", s);
999 yylex (AT_LALR1_CC_IF([int *lval], [void]))
1001 static int const tokens[] =
1005 static size_t toknum;
1006 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
1007 if (! (toknum < sizeof tokens / sizeof *tokens))
1009 return tokens[toknum++];
1018 ])# _AT_DATA_EXPECT2_Y
1021 # AT_CHECK_EXPECT2(BISON-OPTIONS)
1022 # ------------------------------
1023 # Generate the grammar, compile it, run it.
1024 m4_define([AT_CHECK_EXPECT2],
1025 [AT_SETUP([Expecting two tokens $1])
1026 AT_BISON_OPTION_PUSHDEFS([$1])
1027 _AT_DATA_EXPECT2_Y([$1])
1028 AT_CHECK([bison -o expect2.c expect2.y])
1030 [AT_CHECK([bison -o expect2.cc expect2.y])
1031 AT_COMPILE_CXX([expect2])],
1032 [AT_CHECK([bison -o expect2.c expect2.y])
1033 AT_COMPILE([expect2])])
1034 AT_PARSER_CHECK([./expect2], 1, [],
1035 [syntax error, unexpected '+', expecting A or B
1037 AT_BISON_OPTION_POPDEFS
1042 AT_CHECK_EXPECT2([%glr-parser])
1043 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1047 ## --------------------------------------------- ##
1048 ## Braced code in declaration in rules section. ##
1049 ## --------------------------------------------- ##
1051 AT_SETUP([Braced code in declaration in rules section])
1053 # Bison once mistook braced code in a declaration in the rules section to be a
1056 AT_DATA_GRAMMAR([input.y],
1059 static void yyerror (char const *msg);
1060 static int yylex (void);
1069 printf ("Bison would once convert this action to a midrule because of the"
1070 " subsequent braced code.\n");
1074 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1075 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1080 yyerror (char const *msg)
1082 fprintf (stderr, "%s\n", msg);
1099 AT_CHECK([bison -t -o input.c input.y])
1101 AT_PARSER_CHECK([./input], 0,
1102 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1106 Reducing stack by rule 1 (line 20):
1107 -> $$ = nterm start ()
1110 Reading a token: Next token is token 'a' (PRINTER)
1111 syntax error, unexpected 'a', expecting $end
1112 Error: popping nterm start ()
1114 Cleanup: discarding lookahead token 'a' (PRINTER)
1123 ## --------------------------------- ##
1124 ## String alias declared after use. ##
1125 ## --------------------------------- ##
1127 AT_SETUP([String alias declared after use])
1129 # Bison once incorrectly asserted that the symbol number for either a token or
1130 # its alias was the highest symbol number so far at the point of the alias
1131 # declaration. That was true unless the declaration appeared after their first
1132 # uses and other tokens appeared in between.
1140 AT_CHECK([bison -t -o input.c input.y])
1146 ## -------------------------------- ##
1147 ## Extra lookahead sets in report. ##
1148 ## -------------------------------- ##
1150 AT_SETUP([[Extra lookahead sets in report]])
1152 # Bison prints each reduction's lookahead set only next to the associated
1153 # state's one item that (1) is associated with the same rule as the reduction
1154 # and (2) has its dot at the end of its RHS. Previously, Bison also
1155 # erroneously printed the lookahead set next to all of the state's other items
1156 # associated with the same rule. This bug affected only the `.output' file and
1157 # not the generated parser source code.
1159 AT_DATA([[input.y]],
1161 start: a | 'a' a 'a' ;
1165 AT_CHECK([[bison --report=all input.y]])
1166 AT_CHECK([[sed -n '/^state 1$/,/^state 2$/p' input.output]], [[0]],
1169 2 start: 'a' . a 'a'
1173 'a' shift, and go to state 4
1175 $default reduce using rule 3 (a)