1 # Bison Regressions. -*- Autotest -*-
3 # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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_BISON_CHECK([-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_BISON_CHECK([-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_BISON_CHECK([-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_BISON_CHECK([-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_BISON_CHECK([-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_BISON_CHECK([-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_BISON_CHECK([-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_BISON_CHECK([-v -o input.c input.y])
373 ## ---------------- ##
374 ## Invalid inputs. ##
375 ## ---------------- ##
378 AT_SETUP([Invalid inputs])
390 AT_BISON_CHECK([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_BISON_CHECK([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??!"
447 %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
449 exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
452 yyerror (char const *s)
454 fprintf (stderr, "%s\n", s);
473 # Checking the warning message guarantees that the trigraph "??!" isn't
474 # unnecessarily escaped here even though it would need to be if encoded in a
475 # C-string literal. Also notice that unnecessary escaping, such as "\?", from
476 # the user specification is eliminated.
477 AT_BISON_CHECK([-o input.c input.y], [[0]], [[]],
478 [[input.y:22.8-14: warning: symbol SPECIAL redeclared
479 input.y:22.8-63: warning: symbol `"\\'?\"\a\b\f\n\r\t\v\001\201\001\201??!"' used more than once as a literal string
483 # Checking the error message here guarantees that yytname, which does contain
484 # C-string literals, does have the trigraph escaped correctly. Thus, the
485 # symbol name reported by the parser is exactly the same as that reported by
488 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201??!", expecting a
490 AT_PARSER_CHECK([./input], 1, [], [experr])
495 ## -------------------- ##
496 ## Characters Escapes. ##
497 ## -------------------- ##
500 AT_SETUP([Characters Escapes])
502 AT_DATA_GRAMMAR([input.y],
504 void yyerror (const char *s);
514 # Pacify font-lock-mode: "
516 AT_BISON_CHECK([-o input.c input.y])
517 AT_COMPILE([input.o], [-c input.c])
526 # The generation of the reduction was once wrong in Bison, and made it
527 # miss some reductions. In the following test case, the reduction on
528 # `undef_id_tok' in state 1 was missing. This is stripped down from
529 # the actual web2c.y.
531 AT_SETUP([Web2c Report])
533 AT_KEYWORDS([report])
536 [[%token undef_id_tok const_id_tok
538 %start CONST_DEC_PART
547 | CONST_DEC_LIST CONST_DEC
551 { } undef_id_tok '=' const_id_tok ';'
556 AT_BISON_CHECK([-v input.y])
557 AT_CHECK([cat input.output], 0,
560 0 $accept: CONST_DEC_PART $end
562 1 CONST_DEC_PART: CONST_DEC_LIST
564 2 CONST_DEC_LIST: CONST_DEC
565 3 | CONST_DEC_LIST CONST_DEC
569 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';'
572 Terminals, with rules where they appear
582 Nonterminals, with rules where they appear
587 on left: 1, on right: 0
589 on left: 2 3, on right: 1 3
591 on left: 5, on right: 2 3
593 on left: 4, on right: 5
598 0 $accept: . CONST_DEC_PART $end
600 $default reduce using rule 4 ($@1)
602 CONST_DEC_PART go to state 1
603 CONST_DEC_LIST go to state 2
604 CONST_DEC go to state 3
610 0 $accept: CONST_DEC_PART . $end
612 $end shift, and go to state 5
617 1 CONST_DEC_PART: CONST_DEC_LIST .
618 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
620 undef_id_tok reduce using rule 4 ($@1)
621 $default reduce using rule 1 (CONST_DEC_PART)
623 CONST_DEC go to state 6
629 2 CONST_DEC_LIST: CONST_DEC .
631 $default reduce using rule 2 (CONST_DEC_LIST)
636 5 CONST_DEC: $@1 . undef_id_tok '=' const_id_tok ';'
638 undef_id_tok shift, and go to state 7
643 0 $accept: CONST_DEC_PART $end .
650 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
652 $default reduce using rule 3 (CONST_DEC_LIST)
657 5 CONST_DEC: $@1 undef_id_tok . '=' const_id_tok ';'
659 '=' shift, and go to state 8
664 5 CONST_DEC: $@1 undef_id_tok '=' . const_id_tok ';'
666 const_id_tok shift, and go to state 9
671 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok . ';'
673 ';' shift, and go to state 10
678 5 CONST_DEC: $@1 undef_id_tok '=' const_id_tok ';' .
680 $default reduce using rule 5 (CONST_DEC)
686 ## --------------- ##
688 ## --------------- ##
690 # The generation of the mapping `state -> action' was once wrong in
691 # extremely specific situations. web2c.y exhibits this situation.
692 # Below is a stripped version of the grammar. It looks like one can
693 # simplify it further, but just don't: it is tuned to exhibit a bug,
694 # which disapears when applying sane grammar transformations.
696 # It used to be wrong on yydefact only:
698 # static const yytype_uint8 yydefact[] =
700 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
701 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
705 # but let's check all the tables.
708 AT_SETUP([Web2c Actions])
710 AT_KEYWORDS([report])
714 statement: struct_stat;
715 struct_stat: /* empty. */ | if else;
716 if: "if" "const" "then" statement;
717 else: "else" statement;
721 AT_BISON_CHECK([-v -o input.c input.y])
723 # Check only the tables.
724 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
726 AT_CHECK([[cat tables.c]], 0,
727 [[static const yytype_uint8 yytranslate[] =
729 0, 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, 2, 2, 2, 2,
742 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
743 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
744 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
745 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
746 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
747 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
748 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
749 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
750 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
751 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
752 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
753 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
754 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
757 static const yytype_uint8 yyrline[] =
761 static const char *const yytname[] =
763 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
764 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
766 static const yytype_uint16 yytoknum[] =
768 0, 256, 257, 258, 259, 260, 261
770 static const yytype_int8 yypact[] =
772 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
775 static const yytype_uint8 yydefact[] =
777 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
780 static const yytype_int8 yypgoto[] =
784 static const yytype_int8 yydefgoto[] =
788 static const yytype_uint8 yytable[] =
790 10, 1, 11, 5, 6, 0, 7, 9
792 static const yytype_int8 yycheck[] =
794 7, 3, 9, 4, 0, -1, 6, 5
796 static const yytype_uint8 yystos[] =
798 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
801 static const yytype_uint8 yyr1[] =
803 0, 7, 8, 9, 9, 10, 11
805 static const yytype_uint8 yyr2[] =
814 ## ------------------------- ##
815 ## yycheck Bound Violation. ##
816 ## ------------------------- ##
819 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
820 # --------------------------------
821 # The following grammar, taken from Andrew Suffield's GPL'd implementation
822 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
823 # yycheck's bounds where issuing a verbose error message. Keep this test
824 # so that possible bound checking compilers could check all the skeletons.
825 m4_define([_AT_DATA_DANCER_Y],
826 [AT_DATA_GRAMMAR([dancer.y],
828 static int yylex (AT_LALR1_CC_IF([int *], [void]));
832 static void yyerror (const char *);])
835 %token ARROW INVALID NUMBER STRING DATA
839 /* Grammar follows */
844 header: '<' from ARROW to '>' type ':'
845 | '<' ARROW to '>' type ':'
879 [/* A C++ error reporting function. */
881 yy::parser::error (const std::string& m)
883 std::cerr << m << std::endl;
891 parser.set_debug_level (YYDEBUG);
893 return parser.parse ();
897 yyerror (const char *s)
899 fprintf (stderr, "%s\n", s);
903 yylex (AT_LALR1_CC_IF([int *lval], [void]))
905 static int const tokens[] =
909 static size_t toknum;
910 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
911 if (! (toknum < sizeof tokens / sizeof *tokens))
913 return tokens[toknum++];
922 ])# _AT_DATA_DANCER_Y
925 # AT_CHECK_DANCER(BISON-OPTIONS)
926 # ------------------------------
927 # Generate the grammar, compile it, run it.
928 m4_define([AT_CHECK_DANCER],
929 [AT_SETUP([Dancer $1])
930 AT_BISON_OPTION_PUSHDEFS([$1])
931 _AT_DATA_DANCER_Y([$1])
932 AT_BISON_CHECK([-o dancer.c dancer.y])
933 AT_FULL_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 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_BISON_CHECK([-o expect2.c expect2.y])
1029 AT_FULL_COMPILE([expect2])
1030 AT_PARSER_CHECK([./expect2], 1, [],
1031 [syntax error, unexpected '+', expecting A or B
1033 AT_BISON_OPTION_POPDEFS
1038 AT_CHECK_EXPECT2([%glr-parser])
1039 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1043 ## --------------------------------------------- ##
1044 ## Braced code in declaration in rules section. ##
1045 ## --------------------------------------------- ##
1047 AT_SETUP([Braced code in declaration in rules section])
1049 # Bison once mistook braced code in a declaration in the rules section to be a
1052 AT_DATA_GRAMMAR([input.y],
1055 static void yyerror (char const *msg);
1056 static int yylex (void);
1065 printf ("Bison would once convert this action to a midrule because of the"
1066 " subsequent braced code.\n");
1070 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1071 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1076 yyerror (char const *msg)
1078 fprintf (stderr, "%s\n", msg);
1095 AT_BISON_CHECK([-t -o input.c input.y])
1097 AT_PARSER_CHECK([./input], 0,
1098 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1102 Reducing stack by rule 1 (line 20):
1103 -> $$ = nterm start ()
1106 Reading a token: Next token is token 'a' (PRINTER)
1107 syntax error, unexpected 'a', expecting $end
1108 Error: popping nterm start ()
1110 Cleanup: discarding lookahead token 'a' (PRINTER)
1119 ## --------------------------------- ##
1120 ## String alias declared after use. ##
1121 ## --------------------------------- ##
1123 AT_SETUP([String alias declared after use])
1125 # Bison once incorrectly asserted that the symbol number for either a token or
1126 # its alias was the highest symbol number so far at the point of the alias
1127 # declaration. That was true unless the declaration appeared after their first
1128 # uses and other tokens appeared in between.
1136 AT_BISON_CHECK([-t -o input.c input.y])
1142 ## -------------------------------- ##
1143 ## Extra lookahead sets in report. ##
1144 ## -------------------------------- ##
1146 AT_SETUP([[Extra lookahead sets in report]])
1148 # Bison prints each reduction's lookahead set only next to the associated
1149 # state's one item that (1) is associated with the same rule as the reduction
1150 # and (2) has its dot at the end of its RHS. Previously, Bison also
1151 # erroneously printed the lookahead set next to all of the state's other items
1152 # associated with the same rule. This bug affected only the `.output' file and
1153 # not the generated parser source code.
1155 AT_DATA([[input.y]],
1157 start: a | 'a' a 'a' ;
1161 AT_BISON_CHECK([[--report=all input.y]])
1162 AT_CHECK([[sed -n '/^state 1$/,/^state 2$/p' input.output]], [[0]],
1165 2 start: 'a' . a 'a'
1169 'a' shift, and go to state 4
1171 $default reduce using rule 3 (a)
1183 ## ---------------------------------------- ##
1184 ## Token number in precedence declaration. ##
1185 ## ---------------------------------------- ##
1187 AT_SETUP([[Token number in precedence declaration]])
1189 # POSIX says token numbers can be declared in %left, %right, and %nonassoc, but
1190 # we lost this in Bison 1.50.
1192 AT_DATA_GRAMMAR([input.y],
1195 void yyerror (char const *);
1200 %left TK1 1 TK2 2 "tok alias" 3
1204 start: TK1 sr_conflict "tok alias" ;
1214 yyerror (char const *msg)
1216 fprintf (stderr, "%s\n", msg);
1222 static int const input[] = { 1, 2, 3, 0 };
1223 static int const *inputp = input;
1234 AT_BISON_CHECK([[-o input.c input.y]], [[0]],,
1235 [[input.y:24.5-19: warning: rule useless in parser due to conflicts: sr_conflict: TK2 "tok alias"
1237 AT_COMPILE([[input]])
1238 AT_PARSER_CHECK([[./input]])