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
406 AT_SETUP([Invalid inputs with {}])
418 AT_CHECK([bison input.y], [1], [],
419 [[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
426 ## ------------------- ##
427 ## Token definitions. ##
428 ## ------------------- ##
431 AT_SETUP([Token definitions])
433 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
434 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);
469 AT_CHECK([bison -o input.c input.y])
472 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
474 AT_PARSER_CHECK([./input], 1, [], [experr])
479 ## -------------------- ##
480 ## Characters Escapes. ##
481 ## -------------------- ##
484 AT_SETUP([Characters Escapes])
486 AT_DATA_GRAMMAR([input.y],
488 void yyerror (const char *s);
498 # Pacify font-lock-mode: "
500 AT_CHECK([bison -o input.c input.y])
501 AT_COMPILE([input.o], [-c input.c])
510 # The generation of the reduction was once wrong in Bison, and made it
511 # miss some reductions. In the following test case, the reduction on
512 # `undef_id_tok' in state 1 was missing. This is stripped down from
513 # the actual web2c.y.
515 AT_SETUP([Web2c Report])
517 AT_KEYWORDS([report])
520 [[%token undef_id_tok const_id_tok
522 %start CONST_DEC_PART
531 | CONST_DEC_LIST CONST_DEC
535 { } undef_id_tok '=' const_id_tok ';'
540 AT_CHECK([bison -v input.y])
541 AT_CHECK([cat input.output], 0,
544 0 $accept: CONST_DEC_PART $end
546 1 CONST_DEC_PART: CONST_DEC_LIST
548 2 CONST_DEC_LIST: CONST_DEC
549 3 | CONST_DEC_LIST CONST_DEC
553 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
556 Terminals, with rules where they appear
566 Nonterminals, with rules where they appear
571 on left: 1, on right: 0
573 on left: 2 3, on right: 1 3
575 on left: 5, on right: 2 3
577 on left: 4, on right: 5
582 0 $accept: . CONST_DEC_PART $end
584 $default reduce using rule 4 (@1)
586 CONST_DEC_PART go to state 1
587 CONST_DEC_LIST go to state 2
588 CONST_DEC go to state 3
594 0 $accept: CONST_DEC_PART . $end
596 $end shift, and go to state 5
601 1 CONST_DEC_PART: CONST_DEC_LIST .
602 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
604 undef_id_tok reduce using rule 4 (@1)
605 $default reduce using rule 1 (CONST_DEC_PART)
607 CONST_DEC go to state 6
613 2 CONST_DEC_LIST: CONST_DEC .
615 $default reduce using rule 2 (CONST_DEC_LIST)
620 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
622 undef_id_tok shift, and go to state 7
627 0 $accept: CONST_DEC_PART $end .
634 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
636 $default reduce using rule 3 (CONST_DEC_LIST)
641 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
643 '=' shift, and go to state 8
648 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
650 const_id_tok shift, and go to state 9
655 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
657 ';' shift, and go to state 10
662 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
664 $default reduce using rule 5 (CONST_DEC)
670 ## --------------- ##
672 ## --------------- ##
674 # The generation of the mapping `state -> action' was once wrong in
675 # extremely specific situations. web2c.y exhibits this situation.
676 # Below is a stripped version of the grammar. It looks like one can
677 # simplify it further, but just don't: it is tuned to exhibit a bug,
678 # which disapears when applying sane grammar transformations.
680 # It used to be wrong on yydefact only:
682 # static const yytype_uint8 yydefact[] =
684 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
685 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
689 # but let's check all the tables.
692 AT_SETUP([Web2c Actions])
694 AT_KEYWORDS([report])
698 statement: struct_stat;
699 struct_stat: /* empty. */ | if else;
700 if: "if" "const" "then" statement;
701 else: "else" statement;
705 AT_CHECK([bison -v -o input.c input.y])
707 # Check only the tables. We don't use --no-parser, because it is
708 # still to be implemented in the experimental branch of Bison.
709 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
711 AT_CHECK([[cat tables.c]], 0,
712 [[static const yytype_uint8 yytranslate[] =
714 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
715 2, 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, 1, 2, 3, 4,
742 static const yytype_uint8 yyprhs[] =
746 static const yytype_int8 yyrhs[] =
748 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
749 4, 5, 8, -1, 6, 8, -1
751 static const yytype_uint8 yyrline[] =
755 static const char *const yytname[] =
757 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
758 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
760 static const yytype_uint16 yytoknum[] =
762 0, 256, 257, 258, 259, 260, 261
764 static const yytype_uint8 yyr1[] =
766 0, 7, 8, 9, 9, 10, 11
768 static const yytype_uint8 yyr2[] =
772 static const yytype_uint8 yydefact[] =
774 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
777 static const yytype_int8 yydefgoto[] =
781 static const yytype_int8 yypact[] =
783 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
786 static const yytype_int8 yypgoto[] =
790 static const yytype_uint8 yytable[] =
792 10, 1, 11, 5, 6, 0, 7, 9
794 static const yytype_int8 yycheck[] =
796 7, 3, 9, 4, 0, -1, 6, 5
798 static const yytype_uint8 yystos[] =
800 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
808 ## ------------------------- ##
809 ## yycheck Bound Violation. ##
810 ## ------------------------- ##
813 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
814 # --------------------------------
815 # The following grammar, taken from Andrew Suffield's GPL'd implementation
816 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
817 # yycheck's bounds where issuing a verbose error message. Keep this test
818 # so that possible bound checking compilers could check all the skeletons.
819 m4_define([_AT_DATA_DANCER_Y],
820 [AT_DATA_GRAMMAR([dancer.y],
822 static int yylex (AT_LALR1_CC_IF([int *], [void]));
825 static void yyerror (const char *);])
828 %token ARROW INVALID NUMBER STRING DATA
832 /* Grammar follows */
837 header: '<' from ARROW to '>' type ':'
838 | '<' ARROW to '>' type ':'
872 [/* A C++ error reporting function. */
874 yy::parser::error (const location&, const std::string& m)
876 std::cerr << m << std::endl;
883 parser.set_debug_level (!!YYDEBUG);
884 return parser.parse ();
888 yyerror (const char *s)
890 fprintf (stderr, "%s\n", s);
894 yylex (AT_LALR1_CC_IF([int *lval], [void]))
896 static int toknum = 0;
897 static int tokens[] =
901 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
902 return tokens[toknum++];
911 ])# _AT_DATA_DANCER_Y
914 # AT_CHECK_DANCER(BISON-OPTIONS)
915 # ------------------------------
916 # Generate the grammar, compile it, run it.
917 m4_define([AT_CHECK_DANCER],
918 [AT_SETUP([Dancer $1])
919 AT_BISON_OPTION_PUSHDEFS([$1])
920 _AT_DATA_DANCER_Y([$1])
921 AT_CHECK([bison -o dancer.c dancer.y])
923 [AT_CHECK([bison -o dancer.cc dancer.y])
924 AT_COMPILE_CXX([dancer])],
925 [AT_CHECK([bison -o dancer.c dancer.y])
926 AT_COMPILE([dancer])])
927 AT_PARSER_CHECK([./dancer], 1, [],
928 [syntax error, unexpected ':'
930 AT_BISON_OPTION_POPDEFS
935 AT_CHECK_DANCER([%glr-parser])
936 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
939 ## ------------------------------------------ ##
940 ## Diagnostic that expects two alternatives. ##
941 ## ------------------------------------------ ##
944 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
945 # --------------------------------
946 m4_define([_AT_DATA_EXPECT2_Y],
947 [AT_DATA_GRAMMAR([expect2.y],
949 static int yylex (AT_LALR1_CC_IF([int *], [void]));
952 static void yyerror (const char *);])
970 [/* A C++ error reporting function. */
972 yy::parser::error (const location&, const std::string& m)
974 std::cerr << m << std::endl;
981 return parser.parse ();
985 yyerror (const char *s)
987 fprintf (stderr, "%s\n", s);
991 yylex (AT_LALR1_CC_IF([int *lval], [void]))
993 static int toknum = 0;
994 static int tokens[] =
998 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
999 return tokens[toknum++];
1008 ])# _AT_DATA_EXPECT2_Y
1011 # AT_CHECK_EXPECT2(BISON-OPTIONS)
1012 # ------------------------------
1013 # Generate the grammar, compile it, run it.
1014 m4_define([AT_CHECK_EXPECT2],
1015 [AT_SETUP([Expecting two tokens $1])
1016 AT_BISON_OPTION_PUSHDEFS([$1])
1017 _AT_DATA_EXPECT2_Y([$1])
1018 AT_CHECK([bison -o expect2.c expect2.y])
1020 [AT_CHECK([bison -o expect2.cc expect2.y])
1021 AT_COMPILE_CXX([expect2])],
1022 [AT_CHECK([bison -o expect2.c expect2.y])
1023 AT_COMPILE([expect2])])
1024 AT_PARSER_CHECK([./expect2], 1, [],
1025 [syntax error, unexpected '+', expecting A or B
1027 AT_BISON_OPTION_POPDEFS
1032 AT_CHECK_EXPECT2([%glr-parser])
1033 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1037 ## --------------------------------------------- ##
1038 ## Braced code in declaration in rules section. ##
1039 ## --------------------------------------------- ##
1041 AT_SETUP([Braced code in declaration in rules section])
1043 # Bison once mistook braced code in a declaration in the rules section to be a
1046 AT_DATA_GRAMMAR([input.y],
1049 static void yyerror (char const *msg);
1050 static int yylex (void);
1059 printf ("Bison would once convert this action to a midrule because of the"
1060 " subsequent braced code.\n");
1064 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1065 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1070 yyerror (char const *msg)
1072 fprintf (stderr, "%s\n", msg);
1089 AT_CHECK([bison -t -o input.c input.y])
1091 AT_PARSER_CHECK([./input], 0,
1092 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1096 Reducing stack by rule 1 (line 22):
1097 -> $$ = nterm start ()
1100 Reading a token: Next token is token 'a' (PRINTER)
1101 syntax error, unexpected 'a', expecting $end
1102 Error: popping nterm start ()
1104 Cleanup: discarding lookahead token 'a' (PRINTER)