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 *);
43 AT_CHECK([bison -o input.c input.y])
44 AT_COMPILE([input.o], [-c input.c])
50 ## ------------------------- ##
51 ## Early token definitions. ##
52 ## ------------------------- ##
55 AT_SETUP([Early token definitions])
57 # Found in GCJ: they expect the tokens to be defined before the user
58 # prologue, so that they can use the token definitions in it.
60 AT_DATA_GRAMMAR([input.y],
62 void yyerror (const char *s);
72 # error "MY_TOKEN not defined."
81 AT_CHECK([bison -o input.c input.y])
82 AT_COMPILE([input.o], [-c input.c])
88 ## ---------------- ##
90 ## ---------------- ##
93 AT_SETUP([Braces parsing])
96 [[/* Bison used to swallow the character after `}'. */
99 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
103 AT_CHECK([bison -v -o input.c input.y])
105 AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
110 ## ------------------ ##
111 ## Duplicate string. ##
112 ## ------------------ ##
115 AT_SETUP([Duplicate string])
118 [[/* `Bison -v' used to dump core when two tokens are defined with the same
119 string, as LE and GE below. */
126 exp: '(' exp ')' | NUM ;
130 AT_CHECK([bison -v -o input.c input.y], 0, [],
131 [[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string
137 ## ------------------- ##
138 ## Rule Line Numbers. ##
139 ## ------------------- ##
141 AT_SETUP([Rule Line Numbers])
143 AT_KEYWORDS([report])
175 AT_CHECK([bison -o input.c -v input.y])
177 # Check the contents of the report.
178 AT_CHECK([cat input.output], [],
192 Terminals, with rules where they appear
201 Nonterminals, with rules where they appear
206 on left: 2 4, on right: 0
208 on left: 1, on right: 2
210 on left: 3, on right: 4
215 0 $accept: . expr $end
217 'a' shift, and go to state 1
219 $default reduce using rule 3 (@2)
229 $default reduce using rule 1 (@1)
236 0 $accept: expr . $end
238 $end shift, and go to state 5
245 'c' shift, and go to state 6
252 'b' shift, and go to state 7
257 0 $accept: expr $end .
266 $default reduce using rule 4 (expr)
273 $default reduce using rule 2 (expr)
280 ## ---------------------- ##
281 ## Mixing %token styles. ##
282 ## ---------------------- ##
285 AT_SETUP([Mixing %token styles])
287 # Taken from the documentation.
289 [[%token <operator> OR "||"
290 %token <operator> LE 134 "<="
297 AT_CHECK([bison -v -o input.c input.y])
303 ## ---------------- ##
304 ## Invalid inputs. ##
305 ## ---------------- ##
308 AT_SETUP([Invalid inputs])
320 AT_CHECK([bison input.y], [1], [],
321 [[input.y:2.1: invalid character: `?'
322 input.y:3.14: invalid character: `}'
323 input.y:4.1: invalid character: `%'
324 input.y:4.2: invalid character: `&'
325 input.y:5.1-17: invalid directive: `%a-does-not-exist'
326 input.y:6.1: invalid character: `%'
327 input.y:6.2: invalid character: `-'
328 input.y:7.1-8.0: missing `%}' at end of file
334 AT_SETUP([Invalid inputs with {}])
346 AT_CHECK([bison input.y], [1], [],
347 [[input.y:3.1: missing `{' in "%destructor {...}"
348 input.y:4.1: missing `{' in "%initial-action {...}"
349 input.y:4.1: syntax error, unexpected %initial-action {...}, expecting string or identifier
356 ## ------------------- ##
357 ## Token definitions. ##
358 ## ------------------- ##
361 AT_SETUP([Token definitions])
363 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
364 AT_DATA_GRAMMAR([input.y],
367 void yyerror (const char *s);
371 %token MYEOF 0 "end of file"
376 %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
378 exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
381 yyerror (char const *s)
383 fprintf (stderr, "%s\n", s);
399 AT_CHECK([bison -o input.c input.y])
402 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
404 AT_PARSER_CHECK([./input], 1, [], [experr])
409 ## -------------------- ##
410 ## Characters Escapes. ##
411 ## -------------------- ##
414 AT_SETUP([Characters Escapes])
416 AT_DATA_GRAMMAR([input.y],
418 void yyerror (const char *s);
428 # Pacify font-lock-mode: "
430 AT_CHECK([bison -o input.c input.y])
431 AT_COMPILE([input.o], [-c input.c])
440 # The generation of the reduction was once wrong in Bison, and made it
441 # miss some reductions. In the following test case, the reduction on
442 # `undef_id_tok' in state 1 was missing. This is stripped down from
443 # the actual web2c.y.
445 AT_SETUP([Web2c Report])
447 AT_KEYWORDS([report])
450 [[%token undef_id_tok const_id_tok
452 %start CONST_DEC_PART
461 | CONST_DEC_LIST CONST_DEC
465 { } undef_id_tok '=' const_id_tok ';'
470 AT_CHECK([bison -v input.y])
471 AT_CHECK([cat input.output], 0,
474 0 $accept: CONST_DEC_PART $end
476 1 CONST_DEC_PART: CONST_DEC_LIST
478 2 CONST_DEC_LIST: CONST_DEC
479 3 | CONST_DEC_LIST CONST_DEC
483 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
486 Terminals, with rules where they appear
496 Nonterminals, with rules where they appear
501 on left: 1, on right: 0
503 on left: 2 3, on right: 1 3
505 on left: 5, on right: 2 3
507 on left: 4, on right: 5
512 0 $accept: . CONST_DEC_PART $end
514 $default reduce using rule 4 (@1)
516 CONST_DEC_PART go to state 1
517 CONST_DEC_LIST go to state 2
518 CONST_DEC go to state 3
524 0 $accept: CONST_DEC_PART . $end
526 $end shift, and go to state 5
531 1 CONST_DEC_PART: CONST_DEC_LIST .
532 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
534 undef_id_tok reduce using rule 4 (@1)
535 $default reduce using rule 1 (CONST_DEC_PART)
537 CONST_DEC go to state 6
543 2 CONST_DEC_LIST: CONST_DEC .
545 $default reduce using rule 2 (CONST_DEC_LIST)
550 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
552 undef_id_tok shift, and go to state 7
557 0 $accept: CONST_DEC_PART $end .
564 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
566 $default reduce using rule 3 (CONST_DEC_LIST)
571 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
573 '=' shift, and go to state 8
578 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
580 const_id_tok shift, and go to state 9
585 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
587 ';' shift, and go to state 10
592 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
594 $default reduce using rule 5 (CONST_DEC)
600 ## --------------- ##
602 ## --------------- ##
604 # The generation of the mapping `state -> action' was once wrong in
605 # extremely specific situations. web2c.y exhibits this situation.
606 # Below is a stripped version of the grammar. It looks like one can
607 # simplify it further, but just don't: it is tuned to exhibit a bug,
608 # which disapears when applying sane grammar transformations.
610 # It used to be wrong on yydefact only:
612 # static const yytype_uint8 yydefact[] =
614 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
615 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
619 # but let's check all the tables.
622 AT_SETUP([Web2c Actions])
624 AT_KEYWORDS([report])
628 statement: struct_stat;
629 struct_stat: /* empty. */ | if else;
630 if: "if" "const" "then" statement;
631 else: "else" statement;
635 AT_CHECK([bison -v -o input.c input.y])
637 # Check only the tables. We don't use --no-parser, because it is
638 # still to be implemented in the experimental branch of Bison.
639 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
641 AT_CHECK([[cat tables.c]], 0,
642 [[static const yytype_uint8 yytranslate[] =
644 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
645 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
646 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
647 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
648 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
649 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
650 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
651 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
652 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
653 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
654 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
655 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
656 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
657 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
658 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
659 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
660 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
661 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
662 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
663 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
664 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
665 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
666 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
667 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
668 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
669 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
672 static const yytype_uint8 yyprhs[] =
676 static const yytype_int8 yyrhs[] =
678 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
679 4, 5, 8, -1, 6, 8, -1
681 static const yytype_uint8 yyrline[] =
685 static const char *const yytname[] =
687 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
688 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
690 static const yytype_uint16 yytoknum[] =
692 0, 256, 257, 258, 259, 260, 261
694 static const yytype_uint8 yyr1[] =
696 0, 7, 8, 9, 9, 10, 11
698 static const yytype_uint8 yyr2[] =
702 static const yytype_uint8 yydefact[] =
704 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
707 static const yytype_int8 yydefgoto[] =
711 static const yytype_int8 yypact[] =
713 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
716 static const yytype_int8 yypgoto[] =
720 static const yytype_uint8 yytable[] =
722 10, 1, 11, 5, 6, 0, 7, 9
724 static const yytype_int8 yycheck[] =
726 7, 3, 9, 4, 0, -1, 6, 5
728 static const yytype_uint8 yystos[] =
730 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
738 ## ------------------------- ##
739 ## yycheck Bound Violation. ##
740 ## ------------------------- ##
743 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
744 # --------------------------------
745 # The following grammar, taken from Andrew Suffield's GPL'd implementation
746 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
747 # yycheck's bounds where issuing a verbose error message. Keep this test
748 # so that possible bound checking compilers could check all the skeletons.
749 m4_define([_AT_DATA_DANCER_Y],
750 [AT_DATA_GRAMMAR([dancer.y],
752 static int yylex (AT_LALR1_CC_IF([int *], [void]));
755 static void yyerror (const char *);])
758 %token ARROW INVALID NUMBER STRING DATA
762 /* Grammar follows */
767 header: '<' from ARROW to '>' type ':'
768 | '<' ARROW to '>' type ':'
802 [/* A C++ error reporting function. */
804 yy::parser::error (const location&, const std::string& m)
806 std::cerr << m << std::endl;
813 parser.set_debug_level (!!YYDEBUG);
814 return parser.parse ();
818 yyerror (const char *s)
820 fprintf (stderr, "%s\n", s);
824 yylex (AT_LALR1_CC_IF([int *lval], [void]))
826 static int toknum = 0;
827 static int tokens[] =
831 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
832 return tokens[toknum++];
841 ])# _AT_DATA_DANCER_Y
844 # AT_CHECK_DANCER(BISON-OPTIONS)
845 # ------------------------------
846 # Generate the grammar, compile it, run it.
847 m4_define([AT_CHECK_DANCER],
848 [AT_SETUP([Dancer $1])
849 AT_BISON_OPTION_PUSHDEFS([$1])
850 _AT_DATA_DANCER_Y([$1])
851 AT_CHECK([bison -o dancer.c dancer.y])
853 [AT_CHECK([bison -o dancer.cc dancer.y])
854 AT_COMPILE_CXX([dancer])],
855 [AT_CHECK([bison -o dancer.c dancer.y])
856 AT_COMPILE([dancer])])
857 AT_PARSER_CHECK([./dancer], 1, [],
858 [syntax error, unexpected ':'
860 AT_BISON_OPTION_POPDEFS
865 AT_CHECK_DANCER([%glr-parser])
866 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
869 ## ------------------------------------------ ##
870 ## Diagnostic that expects two alternatives. ##
871 ## ------------------------------------------ ##
874 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
875 # --------------------------------
876 m4_define([_AT_DATA_EXPECT2_Y],
877 [AT_DATA_GRAMMAR([expect2.y],
879 static int yylex (AT_LALR1_CC_IF([int *], [void]));
882 static void yyerror (const char *);])
900 [/* A C++ error reporting function. */
902 yy::parser::error (const location&, const std::string& m)
904 std::cerr << m << std::endl;
911 return parser.parse ();
915 yyerror (const char *s)
917 fprintf (stderr, "%s\n", s);
921 yylex (AT_LALR1_CC_IF([int *lval], [void]))
923 static int toknum = 0;
924 static int tokens[] =
928 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
929 return tokens[toknum++];
938 ])# _AT_DATA_EXPECT2_Y
941 # AT_CHECK_EXPECT2(BISON-OPTIONS)
942 # ------------------------------
943 # Generate the grammar, compile it, run it.
944 m4_define([AT_CHECK_EXPECT2],
945 [AT_SETUP([Expecting two tokens $1])
946 AT_BISON_OPTION_PUSHDEFS([$1])
947 _AT_DATA_EXPECT2_Y([$1])
948 AT_CHECK([bison -o expect2.c expect2.y])
950 [AT_CHECK([bison -o expect2.cc expect2.y])
951 AT_COMPILE_CXX([expect2])],
952 [AT_CHECK([bison -o expect2.c expect2.y])
953 AT_COMPILE([expect2])])
954 AT_PARSER_CHECK([./expect2], 1, [],
955 [syntax error, unexpected '+', expecting A or B
957 AT_BISON_OPTION_POPDEFS
962 AT_CHECK_EXPECT2([%glr-parser])
963 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])