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)
299 2 expr: 'a' . $@1 'b'
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
322 2 expr: 'a' $@1 . 'b'
324 'b' shift, and go to state 7
329 0 $accept: expr $end .
338 $default reduce using rule 4 (expr)
343 2 expr: 'a' $@1 'b' .
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.
713 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
715 AT_CHECK([[cat tables.c]], 0,
716 [[static const yytype_uint8 yytranslate[] =
718 0, 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, 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, 1, 2, 3, 4,
746 static const yytype_uint8 yyprhs[] =
750 static const yytype_int8 yyrhs[] =
752 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
753 4, 5, 8, -1, 6, 8, -1
755 static const yytype_uint8 yyrline[] =
759 static const char *const yytname[] =
761 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
762 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
764 static const yytype_uint16 yytoknum[] =
766 0, 256, 257, 258, 259, 260, 261
768 static const yytype_uint8 yyr1[] =
770 0, 7, 8, 9, 9, 10, 11
772 static const yytype_uint8 yyr2[] =
776 static const yytype_uint8 yydefact[] =
778 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
781 static const yytype_int8 yydefgoto[] =
785 static const yytype_int8 yypact[] =
787 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
790 static const yytype_int8 yypgoto[] =
794 static const yytype_uint8 yytable[] =
796 10, 1, 11, 5, 6, 0, 7, 9
798 static const yytype_int8 yycheck[] =
800 7, 3, 9, 4, 0, -1, 6, 5
802 static const yytype_uint8 yystos[] =
804 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
812 ## ------------------------- ##
813 ## yycheck Bound Violation. ##
814 ## ------------------------- ##
817 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
818 # --------------------------------
819 # The following grammar, taken from Andrew Suffield's GPL'd implementation
820 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
821 # yycheck's bounds where issuing a verbose error message. Keep this test
822 # so that possible bound checking compilers could check all the skeletons.
823 m4_define([_AT_DATA_DANCER_Y],
824 [AT_DATA_GRAMMAR([dancer.y],
826 static int yylex (AT_LALR1_CC_IF([int *], [void]));
830 static void yyerror (const char *);])
833 %token ARROW INVALID NUMBER STRING DATA
837 /* Grammar follows */
842 header: '<' from ARROW to '>' type ':'
843 | '<' ARROW to '>' type ':'
877 [/* A C++ error reporting function. */
879 yy::parser::error (const location&, const std::string& m)
881 std::cerr << m << std::endl;
889 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 const tokens[] =
907 static size_t toknum;
908 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
909 if (! (toknum < sizeof tokens / sizeof *tokens))
911 return tokens[toknum++];
920 ])# _AT_DATA_DANCER_Y
923 # AT_CHECK_DANCER(BISON-OPTIONS)
924 # ------------------------------
925 # Generate the grammar, compile it, run it.
926 m4_define([AT_CHECK_DANCER],
927 [AT_SETUP([Dancer $1])
928 AT_BISON_OPTION_PUSHDEFS([$1])
929 _AT_DATA_DANCER_Y([$1])
930 AT_CHECK([bison -o dancer.c dancer.y])
932 [AT_CHECK([bison -o dancer.cc dancer.y])
933 AT_COMPILE_CXX([dancer])],
934 [AT_CHECK([bison -o dancer.c dancer.y])
935 AT_COMPILE([dancer])])
936 AT_PARSER_CHECK([./dancer], 1, [],
937 [syntax error, unexpected ':'
939 AT_BISON_OPTION_POPDEFS
944 AT_CHECK_DANCER([%glr-parser])
945 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
948 ## ------------------------------------------ ##
949 ## Diagnostic that expects two alternatives. ##
950 ## ------------------------------------------ ##
953 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
954 # --------------------------------
955 m4_define([_AT_DATA_EXPECT2_Y],
956 [AT_DATA_GRAMMAR([expect2.y],
958 static int yylex (AT_LALR1_CC_IF([int *], [void]));
962 static void yyerror (const char *);])
980 [/* A C++ error reporting function. */
982 yy::parser::error (const location&, const std::string& m)
984 std::cerr << m << std::endl;
991 return parser.parse ();
995 yyerror (const char *s)
997 fprintf (stderr, "%s\n", s);
1001 yylex (AT_LALR1_CC_IF([int *lval], [void]))
1003 static int const tokens[] =
1007 static size_t toknum;
1008 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
1009 if (! (toknum < sizeof tokens / sizeof *tokens))
1011 return tokens[toknum++];
1020 ])# _AT_DATA_EXPECT2_Y
1023 # AT_CHECK_EXPECT2(BISON-OPTIONS)
1024 # ------------------------------
1025 # Generate the grammar, compile it, run it.
1026 m4_define([AT_CHECK_EXPECT2],
1027 [AT_SETUP([Expecting two tokens $1])
1028 AT_BISON_OPTION_PUSHDEFS([$1])
1029 _AT_DATA_EXPECT2_Y([$1])
1030 AT_CHECK([bison -o expect2.c expect2.y])
1032 [AT_CHECK([bison -o expect2.cc expect2.y])
1033 AT_COMPILE_CXX([expect2])],
1034 [AT_CHECK([bison -o expect2.c expect2.y])
1035 AT_COMPILE([expect2])])
1036 AT_PARSER_CHECK([./expect2], 1, [],
1037 [syntax error, unexpected '+', expecting A or B
1039 AT_BISON_OPTION_POPDEFS
1044 AT_CHECK_EXPECT2([%glr-parser])
1045 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])
1049 ## --------------------------------------------- ##
1050 ## Braced code in declaration in rules section. ##
1051 ## --------------------------------------------- ##
1053 AT_SETUP([Braced code in declaration in rules section])
1055 # Bison once mistook braced code in a declaration in the rules section to be a
1058 AT_DATA_GRAMMAR([input.y],
1061 static void yyerror (char const *msg);
1062 static int yylex (void);
1071 printf ("Bison would once convert this action to a midrule because of the"
1072 " subsequent braced code.\n");
1076 %destructor { fprintf (stderr, "DESTRUCTOR\n"); } 'a';
1077 %printer { fprintf (yyoutput, "PRINTER"); } 'a';
1082 yyerror (char const *msg)
1084 fprintf (stderr, "%s\n", msg);
1101 AT_CHECK([bison -t -o input.c input.y])
1103 AT_PARSER_CHECK([./input], 0,
1104 [[Bison would once convert this action to a midrule because of the subsequent braced code.
1108 Reducing stack by rule 1 (line 20):
1109 -> $$ = nterm start ()
1112 Reading a token: Next token is token 'a' (PRINTER)
1113 syntax error, unexpected 'a', expecting $end
1114 Error: popping nterm start ()
1116 Cleanup: discarding lookahead token 'a' (PRINTER)
1125 ## --------------------------------- ##
1126 ## String alias declared after use. ##
1127 ## --------------------------------- ##
1129 AT_SETUP([String alias declared after use])
1131 # Bison once incorrectly asserted that the symbol number for either a token or
1132 # its alias was the highest symbol number so far at the point of the alias
1133 # declaration. That was true unless the declaration appeared after their first
1134 # uses and other tokens appeared in between.
1142 AT_CHECK([bison -t -o input.c input.y])