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],
439 void yyerror (const char *s);
443 %token MYEOF 0 "end of file"
448 %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
450 exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
453 yyerror (char const *s)
455 fprintf (stderr, "%s\n", s);
474 AT_CHECK([bison -o input.c input.y])
477 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
479 AT_PARSER_CHECK([./input], 1, [], [experr])
484 ## -------------------- ##
485 ## Characters Escapes. ##
486 ## -------------------- ##
489 AT_SETUP([Characters Escapes])
491 AT_DATA_GRAMMAR([input.y],
493 void yyerror (const char *s);
503 # Pacify font-lock-mode: "
505 AT_CHECK([bison -o input.c input.y])
506 AT_COMPILE([input.o], [-c input.c])
515 # The generation of the reduction was once wrong in Bison, and made it
516 # miss some reductions. In the following test case, the reduction on
517 # `undef_id_tok' in state 1 was missing. This is stripped down from
518 # the actual web2c.y.
520 AT_SETUP([Web2c Report])
522 AT_KEYWORDS([report])
525 [[%token undef_id_tok const_id_tok
527 %start CONST_DEC_PART
536 | CONST_DEC_LIST CONST_DEC
540 { } undef_id_tok '=' const_id_tok ';'
545 AT_CHECK([bison -v input.y])
546 AT_CHECK([cat input.output], 0,
549 0 $accept: CONST_DEC_PART $end
551 1 CONST_DEC_PART: CONST_DEC_LIST
553 2 CONST_DEC_LIST: CONST_DEC
554 3 | CONST_DEC_LIST CONST_DEC
558 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
561 Terminals, with rules where they appear
571 Nonterminals, with rules where they appear
576 on left: 1, on right: 0
578 on left: 2 3, on right: 1 3
580 on left: 5, on right: 2 3
582 on left: 4, on right: 5
587 0 $accept: . CONST_DEC_PART $end
589 $default reduce using rule 4 (@1)
591 CONST_DEC_PART go to state 1
592 CONST_DEC_LIST go to state 2
593 CONST_DEC go to state 3
599 0 $accept: CONST_DEC_PART . $end
601 $end shift, and go to state 5
606 1 CONST_DEC_PART: CONST_DEC_LIST .
607 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
609 undef_id_tok reduce using rule 4 (@1)
610 $default reduce using rule 1 (CONST_DEC_PART)
612 CONST_DEC go to state 6
618 2 CONST_DEC_LIST: CONST_DEC .
620 $default reduce using rule 2 (CONST_DEC_LIST)
625 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
627 undef_id_tok shift, and go to state 7
632 0 $accept: CONST_DEC_PART $end .
639 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
641 $default reduce using rule 3 (CONST_DEC_LIST)
646 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
648 '=' shift, and go to state 8
653 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
655 const_id_tok shift, and go to state 9
660 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
662 ';' shift, and go to state 10
667 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
669 $default reduce using rule 5 (CONST_DEC)
675 ## --------------- ##
677 ## --------------- ##
679 # The generation of the mapping `state -> action' was once wrong in
680 # extremely specific situations. web2c.y exhibits this situation.
681 # Below is a stripped version of the grammar. It looks like one can
682 # simplify it further, but just don't: it is tuned to exhibit a bug,
683 # which disapears when applying sane grammar transformations.
685 # It used to be wrong on yydefact only:
687 # static const yytype_uint8 yydefact[] =
689 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
690 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
694 # but let's check all the tables.
697 AT_SETUP([Web2c Actions])
699 AT_KEYWORDS([report])
703 statement: struct_stat;
704 struct_stat: /* empty. */ | if else;
705 if: "if" "const" "then" statement;
706 else: "else" statement;
710 AT_CHECK([bison -v -o input.c input.y])
712 # Check only the tables. We don't use --no-parser, because it is
713 # still to be implemented in the experimental branch of Bison.
714 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
716 AT_CHECK([[cat tables.c]], 0,
717 [[static const yytype_uint8 yytranslate[] =
719 0, 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, 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, 1, 2, 3, 4,
747 static const yytype_uint8 yyprhs[] =
751 static const yytype_int8 yyrhs[] =
753 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
754 4, 5, 8, -1, 6, 8, -1
756 static const yytype_uint8 yyrline[] =
760 static const char *const yytname[] =
762 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
763 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
765 static const yytype_uint16 yytoknum[] =
767 0, 256, 257, 258, 259, 260, 261
769 static const yytype_uint8 yyr1[] =
771 0, 7, 8, 9, 9, 10, 11
773 static const yytype_uint8 yyr2[] =
777 static const yytype_uint8 yydefact[] =
779 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
782 static const yytype_int8 yydefgoto[] =
786 static const yytype_int8 yypact[] =
788 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
791 static const yytype_int8 yypgoto[] =
795 static const yytype_uint8 yytable[] =
797 10, 1, 11, 5, 6, 0, 7, 9
799 static const yytype_int8 yycheck[] =
801 7, 3, 9, 4, 0, -1, 6, 5
803 static const yytype_uint8 yystos[] =
805 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
813 ## ------------------------- ##
814 ## yycheck Bound Violation. ##
815 ## ------------------------- ##
818 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
819 # --------------------------------
820 # The following grammar, taken from Andrew Suffield's GPL'd implementation
821 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
822 # yycheck's bounds where issuing a verbose error message. Keep this test
823 # so that possible bound checking compilers could check all the skeletons.
824 m4_define([_AT_DATA_DANCER_Y],
825 [AT_DATA_GRAMMAR([dancer.y],
827 static int yylex (AT_LALR1_CC_IF([int *], [void]));
831 static void yyerror (const char *);])
834 %token ARROW INVALID NUMBER STRING DATA
838 /* Grammar follows */
843 header: '<' from ARROW to '>' type ':'
844 | '<' ARROW to '>' type ':'
878 [/* A C++ error reporting function. */
880 yy::parser::error (const location&, const std::string& m)
882 std::cerr << m << std::endl;
890 parser.set_debug_level (YYDEBUG);
892 return parser.parse ();
896 yyerror (const char *s)
898 fprintf (stderr, "%s\n", s);
902 yylex (AT_LALR1_CC_IF([int *lval], [void]))
904 static int const tokens[] =
908 static size_t toknum;
909 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
910 if (! (toknum < sizeof tokens / sizeof *tokens))
912 return tokens[toknum++];
921 ])# _AT_DATA_DANCER_Y
924 # AT_CHECK_DANCER(BISON-OPTIONS)
925 # ------------------------------
926 # Generate the grammar, compile it, run it.
927 m4_define([AT_CHECK_DANCER],
928 [AT_SETUP([Dancer $1])
929 AT_BISON_OPTION_PUSHDEFS([$1])
930 _AT_DATA_DANCER_Y([$1])
931 AT_CHECK([bison -o dancer.c dancer.y])
933 [AT_CHECK([bison -o dancer.cc dancer.y])
934 AT_COMPILE_CXX([dancer])],
935 [AT_CHECK([bison -o dancer.c dancer.y])
936 AT_COMPILE([dancer])])
937 AT_PARSER_CHECK([./dancer], 1, [],
938 [syntax error, unexpected ':'
940 AT_BISON_OPTION_POPDEFS
945 AT_CHECK_DANCER([%glr-parser])
946 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
949 ## ------------------------------------------ ##
950 ## Diagnostic that expects two alternatives. ##
951 ## ------------------------------------------ ##
954 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
955 # --------------------------------
956 m4_define([_AT_DATA_EXPECT2_Y],
957 [AT_DATA_GRAMMAR([expect2.y],
959 static int yylex (AT_LALR1_CC_IF([int *], [void]));
963 static void yyerror (const char *);])
981 [/* A C++ error reporting function. */
983 yy::parser::error (const location&, const std::string& m)
985 std::cerr << m << std::endl;
992 return parser.parse ();
996 yyerror (const char *s)
998 fprintf (stderr, "%s\n", s);
1002 yylex (AT_LALR1_CC_IF([int *lval], [void]))
1004 static int const tokens[] =
1008 static size_t toknum;
1009 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
1010 if (! (toknum < sizeof tokens / sizeof *tokens))
1012 return tokens[toknum++];
1021 ])# _AT_DATA_EXPECT2_Y
1024 # AT_CHECK_EXPECT2(BISON-OPTIONS)
1025 # ------------------------------
1026 # Generate the grammar, compile it, run it.
1027 m4_define([AT_CHECK_EXPECT2],
1028 [AT_SETUP([Expecting two tokens $1])
1029 AT_BISON_OPTION_PUSHDEFS([$1])
1030 _AT_DATA_EXPECT2_Y([$1])
1031 AT_CHECK([bison -o expect2.c expect2.y])
1033 [AT_CHECK([bison -o expect2.cc expect2.y])
1034 AT_COMPILE_CXX([expect2])],
1035 [AT_CHECK([bison -o expect2.c expect2.y])
1036 AT_COMPILE([expect2])])
1037 AT_PARSER_CHECK([./expect2], 1, [],
1038 [syntax error, unexpected '+', expecting A or B
1040 AT_BISON_OPTION_POPDEFS
1045 AT_CHECK_EXPECT2([%glr-parser])
1046 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1050 ## --------------------------------------------- ##
1051 ## Braced code in declaration in rules section. ##
1052 ## --------------------------------------------- ##
1054 AT_SETUP([Braced code in declaration in rules section])
1056 # Bison once mistook braced code in a declaration in the rules section to be a
1059 AT_DATA_GRAMMAR([input.y],
1062 static void yyerror (char const *msg);
1063 static int yylex (void);
1072 printf ("Bison would once convert this action to a midrule because of the"
1073 " subsequent braced code.\n");
1077 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1078 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1083 yyerror (char const *msg)
1085 fprintf (stderr, "%s\n", msg);
1102 AT_CHECK([bison -t -o input.c input.y])
1104 AT_PARSER_CHECK([./input], 0,
1105 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1109 Reducing stack by rule 1 (line 20):
1110 -> $$ = nterm start ()
1113 Reading a token: Next token is token 'a' (PRINTER)
1114 syntax error, unexpected 'a', expecting $end
1115 Error: popping nterm start ()
1117 Cleanup: discarding lookahead token 'a' (PRINTER)
1126 ## --------------------------------- ##
1127 ## String alias declared after use. ##
1128 ## --------------------------------- ##
1130 AT_SETUP([String alias declared after use])
1132 # Bison once incorrectly asserted that the symbol number for either a token or
1133 # its alias was the highest symbol number so far at the point of the alias
1134 # declaration. That was true unless the declaration appeared after their first
1135 # uses and other tokens appeared in between.
1143 AT_CHECK([bison -t -o input.c input.y])