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 ## Early token definitions with --yacc. ##
54 ## ------------------------------------- ##
57 AT_SETUP([Early token definitions with --yacc])
59 # Found in GCJ: they expect the tokens to be defined before the user
60 # prologue, so that they can use the token definitions in it.
62 AT_DATA_GRAMMAR([input.y],
64 void yyerror (const char *s);
74 # error "MY_TOKEN not defined."
83 AT_CHECK([bison -y -o input.c input.y])
84 AT_COMPILE([input.o], [-c input.c])
90 ## ---------------------------------------- ##
91 ## Early token definitions without --yacc. ##
92 ## ---------------------------------------- ##
95 AT_SETUP([Early token definitions without --yacc])
97 # Found in GCJ: they expect the tokens to be defined before the user
98 # prologue, so that they can use the token definitions in it.
100 AT_DATA_GRAMMAR([input.y],
103 void yyerror (const char *s);
105 void print_my_token (void);
114 print_my_token (void)
116 enum yytokentype my_token = MY_TOKEN;
117 printf ("%d\n", my_token);
126 AT_CHECK([bison -o input.c input.y])
127 AT_COMPILE([input.o], [-c input.c])
133 ## ---------------- ##
134 ## Braces parsing. ##
135 ## ---------------- ##
138 AT_SETUP([Braces parsing])
141 [[/* Bison used to swallow the character after `}'. */
144 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
148 AT_CHECK([bison -v -o input.c input.y])
150 AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
155 ## ------------------ ##
156 ## Duplicate string. ##
157 ## ------------------ ##
160 AT_SETUP([Duplicate string])
163 [[/* `Bison -v' used to dump core when two tokens are defined with the same
164 string, as LE and GE below. */
171 exp: '(' exp ')' | NUM ;
175 AT_CHECK([bison -v -o input.c input.y], 0, [],
176 [[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string
182 ## ------------------- ##
183 ## Rule Line Numbers. ##
184 ## ------------------- ##
186 AT_SETUP([Rule Line Numbers])
188 AT_KEYWORDS([report])
220 AT_CHECK([bison -o input.c -v input.y])
222 # Check the contents of the report.
223 AT_CHECK([cat input.output], [],
237 Terminals, with rules where they appear
246 Nonterminals, with rules where they appear
251 on left: 2 4, on right: 0
253 on left: 1, on right: 2
255 on left: 3, on right: 4
260 0 $accept: . expr $end
262 'a' shift, and go to state 1
264 $default reduce using rule 3 (@2)
274 $default reduce using rule 1 (@1)
281 0 $accept: expr . $end
283 $end shift, and go to state 5
290 'c' shift, and go to state 6
297 'b' shift, and go to state 7
302 0 $accept: expr $end .
311 $default reduce using rule 4 (expr)
318 $default reduce using rule 2 (expr)
325 ## ---------------------- ##
326 ## Mixing %token styles. ##
327 ## ---------------------- ##
330 AT_SETUP([Mixing %token styles])
332 # Taken from the documentation.
334 [[%token <operator> OR "||"
335 %token <operator> LE 134 "<="
342 AT_CHECK([bison -v -o input.c input.y])
348 ## ---------------- ##
349 ## Invalid inputs. ##
350 ## ---------------- ##
353 AT_SETUP([Invalid inputs])
365 AT_CHECK([bison input.y], [1], [],
366 [[input.y:2.1: invalid character: `?'
367 input.y:3.14: invalid character: `}'
368 input.y:4.1: invalid character: `%'
369 input.y:4.2: invalid character: `&'
370 input.y:5.1-17: invalid directive: `%a-does-not-exist'
371 input.y:6.1: invalid character: `%'
372 input.y:6.2: invalid character: `-'
373 input.y:7.1-8.0: missing `%}' at end of file
379 AT_SETUP([Invalid inputs with {}])
391 AT_CHECK([bison input.y], [1], [],
392 [[input.y:3.1-15: syntax error, unexpected %initial-action, expecting {...}
399 ## ------------------- ##
400 ## Token definitions. ##
401 ## ------------------- ##
404 AT_SETUP([Token definitions])
406 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
407 AT_DATA_GRAMMAR([input.y],
410 void yyerror (const char *s);
414 %token MYEOF 0 "end of file"
419 %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
421 exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
424 yyerror (char const *s)
426 fprintf (stderr, "%s\n", s);
442 AT_CHECK([bison -o input.c input.y])
445 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
447 AT_PARSER_CHECK([./input], 1, [], [experr])
452 ## -------------------- ##
453 ## Characters Escapes. ##
454 ## -------------------- ##
457 AT_SETUP([Characters Escapes])
459 AT_DATA_GRAMMAR([input.y],
461 void yyerror (const char *s);
471 # Pacify font-lock-mode: "
473 AT_CHECK([bison -o input.c input.y])
474 AT_COMPILE([input.o], [-c input.c])
483 # The generation of the reduction was once wrong in Bison, and made it
484 # miss some reductions. In the following test case, the reduction on
485 # `undef_id_tok' in state 1 was missing. This is stripped down from
486 # the actual web2c.y.
488 AT_SETUP([Web2c Report])
490 AT_KEYWORDS([report])
493 [[%token undef_id_tok const_id_tok
495 %start CONST_DEC_PART
504 | CONST_DEC_LIST CONST_DEC
508 { } undef_id_tok '=' const_id_tok ';'
513 AT_CHECK([bison -v input.y])
514 AT_CHECK([cat input.output], 0,
517 0 $accept: CONST_DEC_PART $end
519 1 CONST_DEC_PART: CONST_DEC_LIST
521 2 CONST_DEC_LIST: CONST_DEC
522 3 | CONST_DEC_LIST CONST_DEC
526 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
529 Terminals, with rules where they appear
539 Nonterminals, with rules where they appear
544 on left: 1, on right: 0
546 on left: 2 3, on right: 1 3
548 on left: 5, on right: 2 3
550 on left: 4, on right: 5
555 0 $accept: . CONST_DEC_PART $end
557 $default reduce using rule 4 (@1)
559 CONST_DEC_PART go to state 1
560 CONST_DEC_LIST go to state 2
561 CONST_DEC go to state 3
567 0 $accept: CONST_DEC_PART . $end
569 $end shift, and go to state 5
574 1 CONST_DEC_PART: CONST_DEC_LIST .
575 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
577 undef_id_tok reduce using rule 4 (@1)
578 $default reduce using rule 1 (CONST_DEC_PART)
580 CONST_DEC go to state 6
586 2 CONST_DEC_LIST: CONST_DEC .
588 $default reduce using rule 2 (CONST_DEC_LIST)
593 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
595 undef_id_tok shift, and go to state 7
600 0 $accept: CONST_DEC_PART $end .
607 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
609 $default reduce using rule 3 (CONST_DEC_LIST)
614 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
616 '=' shift, and go to state 8
621 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
623 const_id_tok shift, and go to state 9
628 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
630 ';' shift, and go to state 10
635 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
637 $default reduce using rule 5 (CONST_DEC)
643 ## --------------- ##
645 ## --------------- ##
647 # The generation of the mapping `state -> action' was once wrong in
648 # extremely specific situations. web2c.y exhibits this situation.
649 # Below is a stripped version of the grammar. It looks like one can
650 # simplify it further, but just don't: it is tuned to exhibit a bug,
651 # which disapears when applying sane grammar transformations.
653 # It used to be wrong on yydefact only:
655 # static const yytype_uint8 yydefact[] =
657 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
658 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
662 # but let's check all the tables.
665 AT_SETUP([Web2c Actions])
667 AT_KEYWORDS([report])
671 statement: struct_stat;
672 struct_stat: /* empty. */ | if else;
673 if: "if" "const" "then" statement;
674 else: "else" statement;
678 AT_CHECK([bison -v -o input.c input.y])
680 # Check only the tables. We don't use --no-parser, because it is
681 # still to be implemented in the experimental branch of Bison.
682 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
684 AT_CHECK([[cat tables.c]], 0,
685 [[static const yytype_uint8 yytranslate[] =
687 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
688 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
689 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
690 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
691 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
692 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
693 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
694 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
695 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
696 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
697 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
698 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
699 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
700 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
701 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
702 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
703 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
704 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
705 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
706 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
707 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
708 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
709 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
710 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
711 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
712 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
715 static const yytype_uint8 yyprhs[] =
719 static const yytype_int8 yyrhs[] =
721 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
722 4, 5, 8, -1, 6, 8, -1
724 static const yytype_uint8 yyrline[] =
728 static const char *const yytname[] =
730 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
731 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
733 static const yytype_uint16 yytoknum[] =
735 0, 256, 257, 258, 259, 260, 261
737 static const yytype_uint8 yyr1[] =
739 0, 7, 8, 9, 9, 10, 11
741 static const yytype_uint8 yyr2[] =
745 static const yytype_uint8 yydefact[] =
747 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
750 static const yytype_int8 yydefgoto[] =
754 static const yytype_int8 yypact[] =
756 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
759 static const yytype_int8 yypgoto[] =
763 static const yytype_uint8 yytable[] =
765 10, 1, 11, 5, 6, 0, 7, 9
767 static const yytype_int8 yycheck[] =
769 7, 3, 9, 4, 0, -1, 6, 5
771 static const yytype_uint8 yystos[] =
773 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
781 ## ------------------------- ##
782 ## yycheck Bound Violation. ##
783 ## ------------------------- ##
786 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
787 # --------------------------------
788 # The following grammar, taken from Andrew Suffield's GPL'd implementation
789 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
790 # yycheck's bounds where issuing a verbose error message. Keep this test
791 # so that possible bound checking compilers could check all the skeletons.
792 m4_define([_AT_DATA_DANCER_Y],
793 [AT_DATA_GRAMMAR([dancer.y],
795 static int yylex (AT_LALR1_CC_IF([int *], [void]));
798 static void yyerror (const char *);])
801 %token ARROW INVALID NUMBER STRING DATA
805 /* Grammar follows */
810 header: '<' from ARROW to '>' type ':'
811 | '<' ARROW to '>' type ':'
845 [/* A C++ error reporting function. */
847 yy::parser::error (const location&, const std::string& m)
849 std::cerr << m << std::endl;
856 parser.set_debug_level (!!YYDEBUG);
857 return parser.parse ();
861 yyerror (const char *s)
863 fprintf (stderr, "%s\n", s);
867 yylex (AT_LALR1_CC_IF([int *lval], [void]))
869 static int toknum = 0;
870 static int tokens[] =
874 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
875 return tokens[toknum++];
884 ])# _AT_DATA_DANCER_Y
887 # AT_CHECK_DANCER(BISON-OPTIONS)
888 # ------------------------------
889 # Generate the grammar, compile it, run it.
890 m4_define([AT_CHECK_DANCER],
891 [AT_SETUP([Dancer $1])
892 AT_BISON_OPTION_PUSHDEFS([$1])
893 _AT_DATA_DANCER_Y([$1])
894 AT_CHECK([bison -o dancer.c dancer.y])
896 [AT_CHECK([bison -o dancer.cc dancer.y])
897 AT_COMPILE_CXX([dancer])],
898 [AT_CHECK([bison -o dancer.c dancer.y])
899 AT_COMPILE([dancer])])
900 AT_PARSER_CHECK([./dancer], 1, [],
901 [syntax error, unexpected ':'
903 AT_BISON_OPTION_POPDEFS
908 AT_CHECK_DANCER([%glr-parser])
909 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
912 ## ------------------------------------------ ##
913 ## Diagnostic that expects two alternatives. ##
914 ## ------------------------------------------ ##
917 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
918 # --------------------------------
919 m4_define([_AT_DATA_EXPECT2_Y],
920 [AT_DATA_GRAMMAR([expect2.y],
922 static int yylex (AT_LALR1_CC_IF([int *], [void]));
925 static void yyerror (const char *);])
943 [/* A C++ error reporting function. */
945 yy::parser::error (const location&, const std::string& m)
947 std::cerr << m << std::endl;
954 return parser.parse ();
958 yyerror (const char *s)
960 fprintf (stderr, "%s\n", s);
964 yylex (AT_LALR1_CC_IF([int *lval], [void]))
966 static int toknum = 0;
967 static int tokens[] =
971 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
972 return tokens[toknum++];
981 ])# _AT_DATA_EXPECT2_Y
984 # AT_CHECK_EXPECT2(BISON-OPTIONS)
985 # ------------------------------
986 # Generate the grammar, compile it, run it.
987 m4_define([AT_CHECK_EXPECT2],
988 [AT_SETUP([Expecting two tokens $1])
989 AT_BISON_OPTION_PUSHDEFS([$1])
990 _AT_DATA_EXPECT2_Y([$1])
991 AT_CHECK([bison -o expect2.c expect2.y])
993 [AT_CHECK([bison -o expect2.cc expect2.y])
994 AT_COMPILE_CXX([expect2])],
995 [AT_CHECK([bison -o expect2.c expect2.y])
996 AT_COMPILE([expect2])])
997 AT_PARSER_CHECK([./expect2], 1, [],
998 [syntax error, unexpected '+', expecting A or B
1000 AT_BISON_OPTION_POPDEFS
1005 AT_CHECK_EXPECT2([%glr-parser])
1006 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1010 ## --------------------------------------------- ##
1011 ## Braced code in declaration in rules section. ##
1012 ## --------------------------------------------- ##
1014 AT_SETUP([Braced code in declaration in rules section])
1016 # Bison once mistook braced code in a declaration in the rules section to be a
1019 AT_DATA_GRAMMAR([input.y],
1022 static void yyerror (char const *msg);
1023 static int yylex (void);
1032 printf ("Bison would once convert this action to a midrule because of the"
1033 " subsequent braced code.\n");
1037 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1038 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1043 yyerror (char const *msg)
1045 fprintf (stderr, "%s\n", msg);
1062 AT_CHECK([bison -t -o input.c input.y])
1064 AT_PARSER_CHECK([./input], 0,
1065 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1069 Reducing stack by rule 1 (line 22):
1070 -> $$ = nterm start ()
1073 Reading a token: Next token is token 'a' (PRINTER)
1074 syntax error, unexpected 'a', expecting $end
1075 Error: popping nterm start ()
1077 Cleanup: discarding lookahead token 'a' (PRINTER)