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);
499 # Pacify font-lock-mode: "
501 AT_CHECK([bison -o input.c input.y])
502 AT_COMPILE([input.o], [-c input.c])
511 # The generation of the reduction was once wrong in Bison, and made it
512 # miss some reductions. In the following test case, the reduction on
513 # `undef_id_tok' in state 1 was missing. This is stripped down from
514 # the actual web2c.y.
516 AT_SETUP([Web2c Report])
518 AT_KEYWORDS([report])
521 [[%token undef_id_tok const_id_tok
523 %start CONST_DEC_PART
532 | CONST_DEC_LIST CONST_DEC
536 { } undef_id_tok '=' const_id_tok ';'
541 AT_CHECK([bison -v input.y])
542 AT_CHECK([cat input.output], 0,
545 0 $accept: CONST_DEC_PART $end
547 1 CONST_DEC_PART: CONST_DEC_LIST
549 2 CONST_DEC_LIST: CONST_DEC
550 3 | CONST_DEC_LIST CONST_DEC
554 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
557 Terminals, with rules where they appear
567 Nonterminals, with rules where they appear
572 on left: 1, on right: 0
574 on left: 2 3, on right: 1 3
576 on left: 5, on right: 2 3
578 on left: 4, on right: 5
583 0 $accept: . CONST_DEC_PART $end
585 $default reduce using rule 4 (@1)
587 CONST_DEC_PART go to state 1
588 CONST_DEC_LIST go to state 2
589 CONST_DEC go to state 3
595 0 $accept: CONST_DEC_PART . $end
597 $end shift, and go to state 5
602 1 CONST_DEC_PART: CONST_DEC_LIST .
603 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
605 undef_id_tok reduce using rule 4 (@1)
606 $default reduce using rule 1 (CONST_DEC_PART)
608 CONST_DEC go to state 6
614 2 CONST_DEC_LIST: CONST_DEC .
616 $default reduce using rule 2 (CONST_DEC_LIST)
621 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
623 undef_id_tok shift, and go to state 7
628 0 $accept: CONST_DEC_PART $end .
635 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
637 $default reduce using rule 3 (CONST_DEC_LIST)
642 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
644 '=' shift, and go to state 8
649 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
651 const_id_tok shift, and go to state 9
656 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
658 ';' shift, and go to state 10
663 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
665 $default reduce using rule 5 (CONST_DEC)
671 ## --------------- ##
673 ## --------------- ##
675 # The generation of the mapping `state -> action' was once wrong in
676 # extremely specific situations. web2c.y exhibits this situation.
677 # Below is a stripped version of the grammar. It looks like one can
678 # simplify it further, but just don't: it is tuned to exhibit a bug,
679 # which disapears when applying sane grammar transformations.
681 # It used to be wrong on yydefact only:
683 # static const yytype_uint8 yydefact[] =
685 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
686 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
690 # but let's check all the tables.
693 AT_SETUP([Web2c Actions])
695 AT_KEYWORDS([report])
699 statement: struct_stat;
700 struct_stat: /* empty. */ | if else;
701 if: "if" "const" "then" statement;
702 else: "else" statement;
706 AT_CHECK([bison -v -o input.c input.y])
708 # Check only the tables. We don't use --no-parser, because it is
709 # still to be implemented in the experimental branch of Bison.
710 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
712 AT_CHECK([[cat tables.c]], 0,
713 [[static const yytype_uint8 yytranslate[] =
715 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
716 2, 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, 1, 2, 3, 4,
743 static const yytype_uint8 yyprhs[] =
747 static const yytype_int8 yyrhs[] =
749 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
750 4, 5, 8, -1, 6, 8, -1
752 static const yytype_uint8 yyrline[] =
756 static const char *const yytname[] =
758 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
759 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
761 static const yytype_uint16 yytoknum[] =
763 0, 256, 257, 258, 259, 260, 261
765 static const yytype_uint8 yyr1[] =
767 0, 7, 8, 9, 9, 10, 11
769 static const yytype_uint8 yyr2[] =
773 static const yytype_uint8 yydefact[] =
775 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
778 static const yytype_int8 yydefgoto[] =
782 static const yytype_int8 yypact[] =
784 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
787 static const yytype_int8 yypgoto[] =
791 static const yytype_uint8 yytable[] =
793 10, 1, 11, 5, 6, 0, 7, 9
795 static const yytype_int8 yycheck[] =
797 7, 3, 9, 4, 0, -1, 6, 5
799 static const yytype_uint8 yystos[] =
801 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
809 ## ------------------------- ##
810 ## yycheck Bound Violation. ##
811 ## ------------------------- ##
814 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
815 # --------------------------------
816 # The following grammar, taken from Andrew Suffield's GPL'd implementation
817 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
818 # yycheck's bounds where issuing a verbose error message. Keep this test
819 # so that possible bound checking compilers could check all the skeletons.
820 m4_define([_AT_DATA_DANCER_Y],
821 [AT_DATA_GRAMMAR([dancer.y],
823 static int yylex (AT_LALR1_CC_IF([int *], [void]));
826 static void yyerror (const char *);])
829 %token ARROW INVALID NUMBER STRING DATA
833 /* Grammar follows */
838 header: '<' from ARROW to '>' type ':'
839 | '<' ARROW to '>' type ':'
873 [/* A C++ error reporting function. */
875 yy::parser::error (const location&, const std::string& m)
877 std::cerr << m << std::endl;
884 parser.set_debug_level (!!YYDEBUG);
885 return parser.parse ();
889 yyerror (const char *s)
891 fprintf (stderr, "%s\n", s);
895 yylex (AT_LALR1_CC_IF([int *lval], [void]))
897 static int toknum = 0;
898 static int tokens[] =
902 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
903 return tokens[toknum++];
912 ])# _AT_DATA_DANCER_Y
915 # AT_CHECK_DANCER(BISON-OPTIONS)
916 # ------------------------------
917 # Generate the grammar, compile it, run it.
918 m4_define([AT_CHECK_DANCER],
919 [AT_SETUP([Dancer $1])
920 AT_BISON_OPTION_PUSHDEFS([$1])
921 _AT_DATA_DANCER_Y([$1])
922 AT_CHECK([bison -o dancer.c dancer.y])
924 [AT_CHECK([bison -o dancer.cc dancer.y])
925 AT_COMPILE_CXX([dancer])],
926 [AT_CHECK([bison -o dancer.c dancer.y])
927 AT_COMPILE([dancer])])
928 AT_PARSER_CHECK([./dancer], 1, [],
929 [syntax error, unexpected ':'
931 AT_BISON_OPTION_POPDEFS
936 AT_CHECK_DANCER([%glr-parser])
937 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
940 ## ------------------------------------------ ##
941 ## Diagnostic that expects two alternatives. ##
942 ## ------------------------------------------ ##
945 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
946 # --------------------------------
947 m4_define([_AT_DATA_EXPECT2_Y],
948 [AT_DATA_GRAMMAR([expect2.y],
950 static int yylex (AT_LALR1_CC_IF([int *], [void]));
953 static void yyerror (const char *);])
971 [/* A C++ error reporting function. */
973 yy::parser::error (const location&, const std::string& m)
975 std::cerr << m << std::endl;
982 return parser.parse ();
986 yyerror (const char *s)
988 fprintf (stderr, "%s\n", s);
992 yylex (AT_LALR1_CC_IF([int *lval], [void]))
994 static int toknum = 0;
995 static int tokens[] =
999 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
1000 return tokens[toknum++];
1009 ])# _AT_DATA_EXPECT2_Y
1012 # AT_CHECK_EXPECT2(BISON-OPTIONS)
1013 # ------------------------------
1014 # Generate the grammar, compile it, run it.
1015 m4_define([AT_CHECK_EXPECT2],
1016 [AT_SETUP([Expecting two tokens $1])
1017 AT_BISON_OPTION_PUSHDEFS([$1])
1018 _AT_DATA_EXPECT2_Y([$1])
1019 AT_CHECK([bison -o expect2.c expect2.y])
1021 [AT_CHECK([bison -o expect2.cc expect2.y])
1022 AT_COMPILE_CXX([expect2])],
1023 [AT_CHECK([bison -o expect2.c expect2.y])
1024 AT_COMPILE([expect2])])
1025 AT_PARSER_CHECK([./expect2], 1, [],
1026 [syntax error, unexpected '+', expecting A or B
1028 AT_BISON_OPTION_POPDEFS
1033 AT_CHECK_EXPECT2([%glr-parser])
1034 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1038 ## --------------------------------------------- ##
1039 ## Braced code in declaration in rules section. ##
1040 ## --------------------------------------------- ##
1042 AT_SETUP([Braced code in declaration in rules section])
1044 # Bison once mistook braced code in a declaration in the rules section to be a
1047 AT_DATA_GRAMMAR([input.y],
1050 static void yyerror (char const *msg);
1051 static int yylex (void);
1060 printf ("Bison would once convert this action to a midrule because of the"
1061 " subsequent braced code.\n");
1065 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1066 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1071 yyerror (char const *msg)
1073 fprintf (stderr, "%s\n", msg);
1090 AT_CHECK([bison -t -o input.c input.y])
1092 AT_PARSER_CHECK([./input], 0,
1093 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1097 Reducing stack by rule 1 (line 22):
1098 -> $$ = nterm start ()
1101 Reading a token: Next token is token 'a' (PRINTER)
1102 syntax error, unexpected 'a', expecting $end
1103 Error: popping nterm start ()
1105 Cleanup: discarding lookahead token 'a' (PRINTER)