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 2, or (at your option)
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, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 AT_BANNER([[Regression tests.]])
24 ## ------------------ ##
25 ## Trivial grammars. ##
26 ## ------------------ ##
28 AT_SETUP([Trivial grammars])
30 AT_DATA_GRAMMAR([input.y],
32 void yyerror (char const *);
44 AT_CHECK([bison -o input.c input.y])
45 AT_COMPILE([input.o], [-c input.c])
46 AT_COMPILE([input.o], [-DYYDEBUG -c input.c])
52 ## ----------------- ##
53 ## YYSTYPE typedef. ##
54 ## ----------------- ##
56 AT_SETUP([YYSTYPE typedef])
58 AT_DATA_GRAMMAR([input.y],
60 void yyerror (char const *);
62 typedef union { char const *val; } YYSTYPE;
69 program: { $$ = ""; };
72 AT_CHECK([bison -o input.c input.y])
73 AT_COMPILE([input.o], [-c input.c])
79 ## ------------------------------------- ##
80 ## Early token definitions with --yacc. ##
81 ## ------------------------------------- ##
84 AT_SETUP([Early token definitions with --yacc])
86 # Found in GCJ: they expect the tokens to be defined before the user
87 # prologue, so that they can use the token definitions in it.
89 AT_DATA_GRAMMAR([input.y],
91 void yyerror (const char *s);
101 # error "MY_TOKEN not defined."
110 AT_CHECK([bison -y -o input.c input.y])
111 AT_COMPILE([input.o], [-c input.c])
117 ## ---------------------------------------- ##
118 ## Early token definitions without --yacc. ##
119 ## ---------------------------------------- ##
122 AT_SETUP([Early token definitions without --yacc])
124 # Found in GCJ: they expect the tokens to be defined before the user
125 # prologue, so that they can use the token definitions in it.
127 AT_DATA_GRAMMAR([input.y],
130 void yyerror (const char *s);
132 void print_my_token (void);
141 print_my_token (void)
143 enum yytokentype my_token = MY_TOKEN;
144 printf ("%d\n", my_token);
153 AT_CHECK([bison -o input.c input.y])
154 AT_COMPILE([input.o], [-c input.c])
160 ## ---------------- ##
161 ## Braces parsing. ##
162 ## ---------------- ##
165 AT_SETUP([Braces parsing])
168 [[/* Bison used to swallow the character after `}'. */
171 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
175 AT_CHECK([bison -v -o input.c input.y])
177 AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
182 ## ------------------ ##
183 ## Duplicate string. ##
184 ## ------------------ ##
187 AT_SETUP([Duplicate string])
190 [[/* `Bison -v' used to dump core when two tokens are defined with the same
191 string, as LE and GE below. */
198 exp: '(' exp ')' | NUM ;
202 AT_CHECK([bison -v -o input.c input.y], 0, [],
203 [[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string
209 ## ------------------- ##
210 ## Rule Line Numbers. ##
211 ## ------------------- ##
213 AT_SETUP([Rule Line Numbers])
215 AT_KEYWORDS([report])
247 AT_CHECK([bison -o input.c -v input.y])
249 # Check the contents of the report.
250 AT_CHECK([cat input.output], [],
264 Terminals, with rules where they appear
273 Nonterminals, with rules where they appear
278 on left: 2 4, on right: 0
280 on left: 1, on right: 2
282 on left: 3, on right: 4
287 0 $accept: . expr $end
289 'a' shift, and go to state 1
291 $default reduce using rule 3 (@2)
301 $default reduce using rule 1 (@1)
308 0 $accept: expr . $end
310 $end shift, and go to state 5
317 'c' shift, and go to state 6
324 'b' shift, and go to state 7
329 0 $accept: expr $end .
338 $default reduce using rule 4 (expr)
345 $default reduce using rule 2 (expr)
352 ## ---------------------- ##
353 ## Mixing %token styles. ##
354 ## ---------------------- ##
357 AT_SETUP([Mixing %token styles])
359 # Taken from the documentation.
361 [[%token <operator> OR "||"
362 %token <operator> LE 134 "<="
369 AT_CHECK([bison -v -o input.c input.y])
375 ## ---------------- ##
376 ## Invalid inputs. ##
377 ## ---------------- ##
380 AT_SETUP([Invalid inputs])
392 AT_CHECK([bison input.y], [1], [],
393 [[input.y:2.1: invalid character: `?'
394 input.y:3.14: invalid character: `}'
395 input.y:4.1: invalid character: `%'
396 input.y:4.2: invalid character: `&'
397 input.y:5.1-17: invalid directive: `%a-does-not-exist'
398 input.y:6.1: invalid character: `%'
399 input.y:6.2: invalid character: `-'
400 input.y:7.1-8.0: missing `%}' at end of file
401 input.y:7.1-8.0: syntax error, unexpected %{...%}
407 AT_SETUP([Invalid inputs with {}])
419 AT_CHECK([bison input.y], [1], [],
420 [[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
427 ## ------------------- ##
428 ## Token definitions. ##
429 ## ------------------- ##
432 AT_SETUP([Token definitions])
434 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
435 AT_DATA_GRAMMAR([input.y],
438 void yyerror (const char *s);
442 %token MYEOF 0 "end of file"
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);
470 AT_CHECK([bison -o input.c input.y])
473 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
475 AT_PARSER_CHECK([./input], 1, [], [experr])
480 ## -------------------- ##
481 ## Characters Escapes. ##
482 ## -------------------- ##
485 AT_SETUP([Characters Escapes])
487 AT_DATA_GRAMMAR([input.y],
489 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;
706 %token CONST "const";
712 AT_CHECK([bison -v -o input.c input.y])
714 # Check only the tables. We don't use --no-parser, because it is
715 # still to be implemented in the experimental branch of Bison.
716 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
718 AT_CHECK([[cat tables.c]], 0,
719 [[static const yytype_uint8 yytranslate[] =
721 0, 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, 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, 1, 2, 3, 4,
749 static const yytype_uint8 yyprhs[] =
753 static const yytype_int8 yyrhs[] =
755 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
756 4, 5, 8, -1, 6, 8, -1
758 static const yytype_uint8 yyrline[] =
762 static const char *const yytname[] =
764 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
765 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
767 static const yytype_uint16 yytoknum[] =
769 0, 256, 257, 258, 259, 260, 261
771 static const yytype_uint8 yyr1[] =
773 0, 7, 8, 9, 9, 10, 11
775 static const yytype_uint8 yyr2[] =
779 static const yytype_uint8 yydefact[] =
781 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
784 static const yytype_int8 yydefgoto[] =
788 static const yytype_int8 yypact[] =
790 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
793 static const yytype_int8 yypgoto[] =
797 static const yytype_uint8 yytable[] =
799 10, 1, 11, 5, 6, 0, 7, 9
801 static const yytype_int8 yycheck[] =
803 7, 3, 9, 4, 0, -1, 6, 5
805 static const yytype_uint8 yystos[] =
807 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
815 ## ------------------------- ##
816 ## yycheck Bound Violation. ##
817 ## ------------------------- ##
820 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
821 # --------------------------------
822 # The following grammar, taken from Andrew Suffield's GPL'd implementation
823 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
824 # yycheck's bounds where issuing a verbose error message. Keep this test
825 # so that possible bound checking compilers could check all the skeletons.
826 m4_define([_AT_DATA_DANCER_Y],
827 [AT_DATA_GRAMMAR([dancer.y],
829 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 location&, const std::string& m)
883 std::cerr << m << std::endl;
890 parser.set_debug_level (!!YYDEBUG);
891 return parser.parse ();
895 yyerror (const char *s)
897 fprintf (stderr, "%s\n", s);
901 yylex (AT_LALR1_CC_IF([int *lval], [void]))
903 static int toknum = 0;
904 static int tokens[] =
908 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
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]));
959 static void yyerror (const char *);])
977 [/* A C++ error reporting function. */
979 yy::parser::error (const location&, const std::string& m)
981 std::cerr << m << std::endl;
988 return parser.parse ();
992 yyerror (const char *s)
994 fprintf (stderr, "%s\n", s);
998 yylex (AT_LALR1_CC_IF([int *lval], [void]))
1000 static int toknum = 0;
1001 static int tokens[] =
1005 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
1006 return tokens[toknum++];
1015 ])# _AT_DATA_EXPECT2_Y
1018 # AT_CHECK_EXPECT2(BISON-OPTIONS)
1019 # ------------------------------
1020 # Generate the grammar, compile it, run it.
1021 m4_define([AT_CHECK_EXPECT2],
1022 [AT_SETUP([Expecting two tokens $1])
1023 AT_BISON_OPTION_PUSHDEFS([$1])
1024 _AT_DATA_EXPECT2_Y([$1])
1025 AT_CHECK([bison -o expect2.c expect2.y])
1027 [AT_CHECK([bison -o expect2.cc expect2.y])
1028 AT_COMPILE_CXX([expect2])],
1029 [AT_CHECK([bison -o expect2.c expect2.y])
1030 AT_COMPILE([expect2])])
1031 AT_PARSER_CHECK([./expect2], 1, [],
1032 [syntax error, unexpected '+', expecting A or B
1034 AT_BISON_OPTION_POPDEFS
1039 AT_CHECK_EXPECT2([%glr-parser])
1040 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1044 ## --------------------------------------------- ##
1045 ## Braced code in declaration in rules section. ##
1046 ## --------------------------------------------- ##
1048 AT_SETUP([Braced code in declaration in rules section])
1050 # Bison once mistook braced code in a declaration in the rules section to be a
1053 AT_DATA_GRAMMAR([input.y],
1056 static void yyerror (char const *msg);
1057 static int yylex (void);
1066 printf ("Bison would once convert this action to a midrule because of the"
1067 " subsequent braced code.\n");
1071 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1072 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1077 yyerror (char const *msg)
1079 fprintf (stderr, "%s\n", msg);
1096 AT_CHECK([bison -t -o input.c input.y])
1098 AT_PARSER_CHECK([./input], 0,
1099 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1103 Reducing stack by rule 1 (line 22):
1104 -> $$ = nterm start ()
1107 Reading a token: Next token is token 'a' (PRINTER)
1108 syntax error, unexpected 'a', expecting $end
1109 Error: popping nterm start ()
1111 Cleanup: discarding lookahead token 'a' (PRINTER)
1120 ## --------------------------------- ##
1121 ## String alias declared after use. ##
1122 ## --------------------------------- ##
1124 AT_SETUP([String alias declared after use])
1126 # Bison once incorrectly asserted that the symbol number for either a token or
1127 # its alias was the highest symbol number so far at the point of the alias
1128 # declaration. That was true unless the declaration appeared after their first
1137 AT_CHECK([bison -t -o input.c input.y])
1143 ## --------------------------- ##
1144 ## Undeclared string literal. ##
1145 ## --------------------------- ##
1147 AT_SETUP([Undeclared string literal])
1149 # Bison once allowed a string literal to be used in the grammar without any
1150 # declaration assigning it as an alias of another token.
1157 AT_CHECK([bison -t -o input.c input.y], [1], [],
1158 [[input.y:2.8-12: "abc" undeclared