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. ##
54 ## ------------------------- ##
57 AT_SETUP([Early token definitions])
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 -o input.c input.y])
84 AT_COMPILE([input.o], [-c input.c])
90 ## ---------------- ##
92 ## ---------------- ##
95 AT_SETUP([Braces parsing])
98 [[/* Bison used to swallow the character after `}'. */
101 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
105 AT_CHECK([bison -v -o input.c input.y])
107 AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
112 ## ------------------ ##
113 ## Duplicate string. ##
114 ## ------------------ ##
117 AT_SETUP([Duplicate string])
120 [[/* `Bison -v' used to dump core when two tokens are defined with the same
121 string, as LE and GE below. */
128 exp: '(' exp ')' | NUM ;
132 AT_CHECK([bison -v -o input.c input.y], 0, [],
133 [[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string
139 ## ------------------- ##
140 ## Rule Line Numbers. ##
141 ## ------------------- ##
143 AT_SETUP([Rule Line Numbers])
145 AT_KEYWORDS([report])
177 AT_CHECK([bison -o input.c -v input.y])
179 # Check the contents of the report.
180 AT_CHECK([cat input.output], [],
194 Terminals, with rules where they appear
203 Nonterminals, with rules where they appear
208 on left: 2 4, on right: 0
210 on left: 1, on right: 2
212 on left: 3, on right: 4
217 0 $accept: . expr $end
219 'a' shift, and go to state 1
221 $default reduce using rule 3 (@2)
231 $default reduce using rule 1 (@1)
238 0 $accept: expr . $end
240 $end shift, and go to state 5
247 'c' shift, and go to state 6
254 'b' shift, and go to state 7
259 0 $accept: expr $end .
268 $default reduce using rule 4 (expr)
275 $default reduce using rule 2 (expr)
282 ## ---------------------- ##
283 ## Mixing %token styles. ##
284 ## ---------------------- ##
287 AT_SETUP([Mixing %token styles])
289 # Taken from the documentation.
291 [[%token <operator> OR "||"
292 %token <operator> LE 134 "<="
299 AT_CHECK([bison -v -o input.c input.y])
305 ## ---------------- ##
306 ## Invalid inputs. ##
307 ## ---------------- ##
310 AT_SETUP([Invalid inputs])
322 AT_CHECK([bison input.y], [1], [],
323 [[input.y:2.1: invalid character: `?'
324 input.y:3.14: invalid character: `}'
325 input.y:4.1: invalid character: `%'
326 input.y:4.2: invalid character: `&'
327 input.y:5.1-17: invalid directive: `%a-does-not-exist'
328 input.y:6.1: invalid character: `%'
329 input.y:6.2: invalid character: `-'
330 input.y:7.1-8.0: missing `%}' at end of file
336 AT_SETUP([Invalid inputs with {}])
348 AT_CHECK([bison input.y], [1], [],
349 [[input.y:3.1: missing `{' in "%destructor {...}"
350 input.y:4.1: missing `{' in "%initial-action {...}"
351 input.y:4.1: syntax error, unexpected %initial-action {...}, expecting string or identifier
358 ## ------------------- ##
359 ## Token definitions. ##
360 ## ------------------- ##
363 AT_SETUP([Token definitions])
365 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
366 AT_DATA_GRAMMAR([input.y],
369 void yyerror (const char *s);
373 %token MYEOF 0 "end of file"
378 %token SPECIAL "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!"
380 exp: "a" "\\\'\?\"\a\b\f\n\r\t\v\001\201\x001\x000081??!";
383 yyerror (char const *s)
385 fprintf (stderr, "%s\n", s);
401 AT_CHECK([bison -o input.c input.y])
404 [[syntax error, unexpected "\\'?\"\a\b\f\n\r\t\v\001\201\001\201?\?!", expecting a
406 AT_PARSER_CHECK([./input], 1, [], [experr])
411 ## -------------------- ##
412 ## Characters Escapes. ##
413 ## -------------------- ##
416 AT_SETUP([Characters Escapes])
418 AT_DATA_GRAMMAR([input.y],
420 void yyerror (const char *s);
430 # Pacify font-lock-mode: "
432 AT_CHECK([bison -o input.c input.y])
433 AT_COMPILE([input.o], [-c input.c])
442 # The generation of the reduction was once wrong in Bison, and made it
443 # miss some reductions. In the following test case, the reduction on
444 # `undef_id_tok' in state 1 was missing. This is stripped down from
445 # the actual web2c.y.
447 AT_SETUP([Web2c Report])
449 AT_KEYWORDS([report])
452 [[%token undef_id_tok const_id_tok
454 %start CONST_DEC_PART
463 | CONST_DEC_LIST CONST_DEC
467 { } undef_id_tok '=' const_id_tok ';'
472 AT_CHECK([bison -v input.y])
473 AT_CHECK([cat input.output], 0,
476 0 $accept: CONST_DEC_PART $end
478 1 CONST_DEC_PART: CONST_DEC_LIST
480 2 CONST_DEC_LIST: CONST_DEC
481 3 | CONST_DEC_LIST CONST_DEC
485 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
488 Terminals, with rules where they appear
498 Nonterminals, with rules where they appear
503 on left: 1, on right: 0
505 on left: 2 3, on right: 1 3
507 on left: 5, on right: 2 3
509 on left: 4, on right: 5
514 0 $accept: . CONST_DEC_PART $end
516 $default reduce using rule 4 (@1)
518 CONST_DEC_PART go to state 1
519 CONST_DEC_LIST go to state 2
520 CONST_DEC go to state 3
526 0 $accept: CONST_DEC_PART . $end
528 $end shift, and go to state 5
533 1 CONST_DEC_PART: CONST_DEC_LIST .
534 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
536 undef_id_tok reduce using rule 4 (@1)
537 $default reduce using rule 1 (CONST_DEC_PART)
539 CONST_DEC go to state 6
545 2 CONST_DEC_LIST: CONST_DEC .
547 $default reduce using rule 2 (CONST_DEC_LIST)
552 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
554 undef_id_tok shift, and go to state 7
559 0 $accept: CONST_DEC_PART $end .
566 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
568 $default reduce using rule 3 (CONST_DEC_LIST)
573 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
575 '=' shift, and go to state 8
580 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
582 const_id_tok shift, and go to state 9
587 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
589 ';' shift, and go to state 10
594 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
596 $default reduce using rule 5 (CONST_DEC)
602 ## --------------- ##
604 ## --------------- ##
606 # The generation of the mapping `state -> action' was once wrong in
607 # extremely specific situations. web2c.y exhibits this situation.
608 # Below is a stripped version of the grammar. It looks like one can
609 # simplify it further, but just don't: it is tuned to exhibit a bug,
610 # which disapears when applying sane grammar transformations.
612 # It used to be wrong on yydefact only:
614 # static const yytype_uint8 yydefact[] =
616 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
617 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
621 # but let's check all the tables.
624 AT_SETUP([Web2c Actions])
626 AT_KEYWORDS([report])
630 statement: struct_stat;
631 struct_stat: /* empty. */ | if else;
632 if: "if" "const" "then" statement;
633 else: "else" statement;
637 AT_CHECK([bison -v -o input.c input.y])
639 # Check only the tables. We don't use --no-parser, because it is
640 # still to be implemented in the experimental branch of Bison.
641 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
643 AT_CHECK([[cat tables.c]], 0,
644 [[static const yytype_uint8 yytranslate[] =
646 0, 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, 2, 2, 2, 2,
670 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
671 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
674 static const yytype_uint8 yyprhs[] =
678 static const yytype_int8 yyrhs[] =
680 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
681 4, 5, 8, -1, 6, 8, -1
683 static const yytype_uint8 yyrline[] =
687 static const char *const yytname[] =
689 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
690 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
692 static const yytype_uint16 yytoknum[] =
694 0, 256, 257, 258, 259, 260, 261
696 static const yytype_uint8 yyr1[] =
698 0, 7, 8, 9, 9, 10, 11
700 static const yytype_uint8 yyr2[] =
704 static const yytype_uint8 yydefact[] =
706 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
709 static const yytype_int8 yydefgoto[] =
713 static const yytype_int8 yypact[] =
715 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
718 static const yytype_int8 yypgoto[] =
722 static const yytype_uint8 yytable[] =
724 10, 1, 11, 5, 6, 0, 7, 9
726 static const yytype_int8 yycheck[] =
728 7, 3, 9, 4, 0, -1, 6, 5
730 static const yytype_uint8 yystos[] =
732 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
740 ## ------------------------- ##
741 ## yycheck Bound Violation. ##
742 ## ------------------------- ##
745 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
746 # --------------------------------
747 # The following grammar, taken from Andrew Suffield's GPL'd implementation
748 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
749 # yycheck's bounds where issuing a verbose error message. Keep this test
750 # so that possible bound checking compilers could check all the skeletons.
751 m4_define([_AT_DATA_DANCER_Y],
752 [AT_DATA_GRAMMAR([dancer.y],
754 static int yylex (AT_LALR1_CC_IF([int *], [void]));
757 static void yyerror (const char *);])
760 %token ARROW INVALID NUMBER STRING DATA
764 /* Grammar follows */
769 header: '<' from ARROW to '>' type ':'
770 | '<' ARROW to '>' type ':'
804 [/* A C++ error reporting function. */
806 yy::parser::error (const location&, const std::string& m)
808 std::cerr << m << std::endl;
815 parser.set_debug_level (!!YYDEBUG);
816 return parser.parse ();
820 yyerror (const char *s)
822 fprintf (stderr, "%s\n", s);
826 yylex (AT_LALR1_CC_IF([int *lval], [void]))
828 static int toknum = 0;
829 static int tokens[] =
833 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
834 return tokens[toknum++];
843 ])# _AT_DATA_DANCER_Y
846 # AT_CHECK_DANCER(BISON-OPTIONS)
847 # ------------------------------
848 # Generate the grammar, compile it, run it.
849 m4_define([AT_CHECK_DANCER],
850 [AT_SETUP([Dancer $1])
851 AT_BISON_OPTION_PUSHDEFS([$1])
852 _AT_DATA_DANCER_Y([$1])
853 AT_CHECK([bison -o dancer.c dancer.y])
855 [AT_CHECK([bison -o dancer.cc dancer.y])
856 AT_COMPILE_CXX([dancer])],
857 [AT_CHECK([bison -o dancer.c dancer.y])
858 AT_COMPILE([dancer])])
859 AT_PARSER_CHECK([./dancer], 1, [],
860 [syntax error, unexpected ':'
862 AT_BISON_OPTION_POPDEFS
867 AT_CHECK_DANCER([%glr-parser])
868 AT_CHECK_DANCER([%skeleton "lalr1.cc"])
871 ## ------------------------------------------ ##
872 ## Diagnostic that expects two alternatives. ##
873 ## ------------------------------------------ ##
876 # _AT_DATA_EXPECT2_Y(BISON-OPTIONS)
877 # --------------------------------
878 m4_define([_AT_DATA_EXPECT2_Y],
879 [AT_DATA_GRAMMAR([expect2.y],
881 static int yylex (AT_LALR1_CC_IF([int *], [void]));
884 static void yyerror (const char *);])
902 [/* A C++ error reporting function. */
904 yy::parser::error (const location&, const std::string& m)
906 std::cerr << m << std::endl;
913 return parser.parse ();
917 yyerror (const char *s)
919 fprintf (stderr, "%s\n", s);
923 yylex (AT_LALR1_CC_IF([int *lval], [void]))
925 static int toknum = 0;
926 static int tokens[] =
930 ]AT_LALR1_CC_IF([*lval = 0; /* Pacify GCC. */])[
931 return tokens[toknum++];
940 ])# _AT_DATA_EXPECT2_Y
943 # AT_CHECK_EXPECT2(BISON-OPTIONS)
944 # ------------------------------
945 # Generate the grammar, compile it, run it.
946 m4_define([AT_CHECK_EXPECT2],
947 [AT_SETUP([Expecting two tokens $1])
948 AT_BISON_OPTION_PUSHDEFS([$1])
949 _AT_DATA_EXPECT2_Y([$1])
950 AT_CHECK([bison -o expect2.c expect2.y])
952 [AT_CHECK([bison -o expect2.cc expect2.y])
953 AT_COMPILE_CXX([expect2])],
954 [AT_CHECK([bison -o expect2.c expect2.y])
955 AT_COMPILE([expect2])])
956 AT_PARSER_CHECK([./expect2], 1, [],
957 [syntax error, unexpected '+', expecting A or B
959 AT_BISON_OPTION_POPDEFS
964 AT_CHECK_EXPECT2([%glr-parser])
965 AT_CHECK_EXPECT2([%skeleton "lalr1.cc"])