1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007, 2009-2011 Free Software Foundation,
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 3 of the License, or
9 # (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
19 AT_BANNER([[Conflicts.]])
22 ## ---------------- ##
24 ## ---------------- ##
26 # I once hacked Bison in such a way that it lost its reductions on the
27 # initial state (because it was confusing it with the last state). It
28 # took me a while to strip down my failures to this simple case. So
29 # make sure it finds the s/r conflict below.
31 AT_SETUP([S/R in initial])
37 e: 'e' | /* Nothing. */;
40 AT_BISON_CHECK([-o input.c input.y], 0, [],
41 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */
47 ## ------------------- ##
48 ## %nonassoc and eof. ##
49 ## ------------------- ##
51 AT_SETUP([%nonassoc and eof])
53 AT_DATA_GRAMMAR([input.y],
60 #define YYERROR_VERBOSE 1
62 yyerror (const char *msg)
64 fprintf (stderr, "%s\n", msg);
67 /* The current argument. */
68 static const char *input;
74 if (! (toknum <= strlen (input)))
76 return input[toknum++];
90 main (int argc, const char *argv[])
92 input = argc <= 1 ? "" : argv[1];
97 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
98 [AT_BISON_CHECK([$1[ -o input.c input.y]])
101 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
103 AT_PARSER_CHECK([./input '0<0'])
104 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
105 [syntax error, unexpected '<'AT_EXPECTING
108 AT_PARSER_CHECK([./input '0>0'])
109 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
110 [syntax error, unexpected '>'AT_EXPECTING
113 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
114 [syntax error, unexpected '>'AT_EXPECTING
117 m4_popdef([AT_EXPECTING])])
119 # Expected token list is missing.
120 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
122 # We must disable default reductions in inconsistent states in order to
123 # have an explicit list of all expected tokens.
124 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reductions=consistent]],
127 # lr.default-reductions=consistent happens to work for this test case.
128 # However, for other grammars, lookahead sets can be merged for
129 # different left contexts, so it is still possible to have an incorrect
130 # expected list. Canonical LR is almost a general solution (that is, it
131 # can fail only when %nonassoc is used), so make sure it gives the same
133 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
135 # parse.lac=full is a completely general solution that does not require
136 # any of the above sacrifices. Of course, it does not extend the
137 # language-recognition power of LALR to (IE)LR, but it does ensure that
138 # the reported list of expected tokens matches what the given parser
139 # would have accepted in place of the unexpected token.
140 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
142 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
148 ## -------------------------------------- ##
149 ## %error-verbose and consistent errors. ##
150 ## -------------------------------------- ##
152 AT_SETUP([[%error-verbose and consistent errors]])
154 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
156 AT_BISON_OPTION_PUSHDEFS([$1])
158 m4_pushdef([AT_YYLEX_PROTOTYPE],
159 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
160 [[int yylex (YYSTYPE *lvalp)]])])
162 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
166 import java.io.IOException;
169 %code {]AT_SKEL_CC_IF([[
171 #include <string>]], [[
174 void yyerror (char const *msg);]])[
175 ]AT_YYLEX_PROTOTYPE[;
179 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
189 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
193 `--------*/]AT_SKEL_JAVA_IF([[
195 public String input = "]$3[";
196 public int index = 0;
199 if (index < input.length ())
200 return input.charAt (index++);
204 public Object getLVal ()
206 return new Integer(1);
211 static char const *input = "]$3[";
218 `----------*/]AT_SKEL_JAVA_IF([[
220 public void yyerror (String msg)
222 System.err.println (msg);
227 %%]], [AT_SKEL_CC_IF([[
230 yy::parser::error (const yy::location &, std::string const &msg)
232 std::cerr << msg << std::endl;
236 yyerror (char const *msg)
238 fprintf (stderr, "%s\n", msg);
243 `-------*/]AT_SKEL_JAVA_IF([[
247 public static void main (String args[]) throws IOException
249 YYParser p = new YYParser ();
252 }]], [AT_SKEL_CC_IF([[
258 return parser.parse ();
268 AT_FULL_COMPILE([[input]])
270 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
271 $5, [a], [[, expecting 'a']],
272 $5, [b], [[, expecting 'b']])])
274 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
275 [AT_PARSER_CHECK([[./input]], [[1]]]),
277 [[syntax error, unexpected ]$4[]AT_EXPECTING[
280 m4_popdef([AT_EXPECTING])
281 m4_popdef([AT_YYLEX_PROTOTYPE])
282 AT_BISON_OPTION_POPDEFS
286 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
289 start: consistent-error-on-a-a 'a' ;
291 consistent-error-on-a-a:
292 'a' default-reduction
293 | 'a' default-reduction 'a'
297 default-reduction: /*empty*/ ;
300 // Provide another context in which all rules are useful so that this
301 // test case looks a little more realistic.
302 start: 'b' consistent-error-on-a-a 'c' ;
305 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
307 # Unfortunately, no expected tokens are reported even though 'b' can be
308 # accepted. Nevertheless, the main point of this test is to make sure
309 # that at least the unexpected token is reported. In a previous version
310 # of Bison, it wasn't reported because the error is detected in a
311 # consistent state with an error action, and that case always triggered
312 # the simple "syntax error" message.
314 # The point isn't to test IELR here, but state merging happens to
315 # complicate this example.
316 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
317 [AT_PREVIOUS_STATE_GRAMMAR],
318 [AT_PREVIOUS_STATE_INPUT],
320 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
322 [AT_PREVIOUS_STATE_GRAMMAR],
323 [AT_PREVIOUS_STATE_INPUT],
325 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
327 [AT_PREVIOUS_STATE_GRAMMAR],
328 [AT_PREVIOUS_STATE_INPUT],
330 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
332 [AT_PREVIOUS_STATE_GRAMMAR],
333 [AT_PREVIOUS_STATE_INPUT],
334 [[end of input]], [[none]])
336 # Even canonical LR doesn't foresee the error for 'a'!
337 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
338 %define lr.default-reductions consistent]],
339 [AT_PREVIOUS_STATE_GRAMMAR],
340 [AT_PREVIOUS_STATE_INPUT],
342 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
343 %define lr.default-reductions accepting]],
344 [AT_PREVIOUS_STATE_GRAMMAR],
345 [AT_PREVIOUS_STATE_INPUT],
347 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
348 [AT_PREVIOUS_STATE_GRAMMAR],
349 [AT_PREVIOUS_STATE_INPUT],
352 # Only LAC gets it right.
353 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
354 %define parse.lac full]],
355 [AT_PREVIOUS_STATE_GRAMMAR],
356 [AT_PREVIOUS_STATE_INPUT],
358 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
359 %define parse.lac full]],
360 [AT_PREVIOUS_STATE_GRAMMAR],
361 [AT_PREVIOUS_STATE_INPUT],
364 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
365 m4_popdef([AT_PREVIOUS_STATE_INPUT])
367 m4_pushdef([AT_USER_ACTION_GRAMMAR],
370 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
371 // incorrectly for the 'b' set in the semantic action below. All 'a'
372 // tokens are returned by yylex, which sets $$ = 1.
375 fprintf (stderr, "Wrong destructor.\n");
378 // Rather than depend on an inconsistent state to induce reading a
379 // lookahead as in the previous grammar, just assign the lookahead in a
380 // semantic action. That lookahead isn't needed before either error
381 // action is encountered. In a previous version of Bison, this was a
382 // problem as it meant yychar was not translated into yytoken before
383 // either error action. The second error action thus invoked a
384 // destructor that it selected according to the incorrect yytoken. The
385 // first error action would have reported an incorrect unexpected token
386 // except that, due to the bug described in the previous grammar, the
387 // unexpected token was not reported at all.
388 start: error-reduce consistent-error 'a' { USE ($][3); } ;
391 'a' 'a' consistent-reduction consistent-error 'a'
392 { USE (($][1, $][2, $][5)); }
397 consistent-reduction: /*empty*/ {
398 assert (yychar == ]AT_SKEL_CC_IF([[yyempty_]], [[YYEMPTY]])[);
405 | /*empty*/ %prec 'a'
408 // Provide another context in which all rules are useful so that this
409 // test case looks a little more realistic.
410 start: 'b' consistent-error 'b' ;
412 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
414 AT_CONSISTENT_ERRORS_CHECK([[]],
415 [AT_USER_ACTION_GRAMMAR],
416 [AT_USER_ACTION_INPUT],
418 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
419 [AT_USER_ACTION_GRAMMAR],
420 [AT_USER_ACTION_INPUT],
422 AT_CONSISTENT_ERRORS_CHECK([[%language "c++"]],
423 [AT_USER_ACTION_GRAMMAR],
424 [AT_USER_ACTION_INPUT],
426 # No Java test because yychar cannot be manipulated by users.
428 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]],
429 [AT_USER_ACTION_GRAMMAR],
430 [AT_USER_ACTION_INPUT],
433 # Canonical LR doesn't foresee the error for 'a'!
434 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions accepting]],
435 [AT_USER_ACTION_GRAMMAR],
436 [AT_USER_ACTION_INPUT],
438 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
439 [AT_USER_ACTION_GRAMMAR],
440 [AT_USER_ACTION_INPUT],
443 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
444 [AT_USER_ACTION_GRAMMAR],
445 [AT_USER_ACTION_INPUT],
447 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
448 %define lr.default-reductions accepting]],
449 [AT_USER_ACTION_GRAMMAR],
450 [AT_USER_ACTION_INPUT],
453 m4_popdef([AT_USER_ACTION_GRAMMAR])
454 m4_popdef([AT_USER_ACTION_INPUT])
456 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
462 ## ------------------------------------------------------- ##
463 ## LAC: %nonassoc requires splitting canonical LR states. ##
464 ## ------------------------------------------------------- ##
466 # This test case demonstrates that, when %nonassoc is used, canonical
467 # LR(1) parser table construction followed by conflict resolution
468 # without further state splitting is not always sufficient to produce a
469 # parser that can detect all syntax errors as soon as possible on one
470 # token of lookahead. However, LAC solves the problem completely even
471 # with minimal LR parser tables.
473 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
475 AT_DATA_GRAMMAR([[input.y]],
478 void yyerror (char const *);
488 'a' problem 'a' // First context.
489 | 'b' problem 'b' // Second context.
490 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
499 // For the state reached after shifting the 'a' in these productions,
500 // lookahead sets are the same in both the first and second contexts.
501 // Thus, canonical LR reuses the same state for both contexts. However,
502 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
503 // error action only in the first context. In order to immediately
504 // detect the syntax error on 'a' here for only the first context, this
505 // canonical LR state would have to be split into two states, and the
506 // 'a' lookahead would have to be removed from only one of the states.
508 'a' // Reduction lookahead set is always ['a', 'b'].
510 | 'a' 'c' // 'c' is forgotten as an expected token.
513 reduce-nonassoc: %prec 'a';
518 yyerror (char const *msg)
520 fprintf (stderr, "%s\n", msg);
526 char const *input = "aaa";
537 # Show canonical LR's failure.
538 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
540 [[input.y: conflicts: 2 shift/reduce
542 AT_COMPILE([[input]])
543 AT_PARSER_CHECK([[./input]], [[1]], [[]],
544 [[syntax error, unexpected 'a', expecting 'b'
547 # It's corrected by LAC.
548 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
549 -o input.c input.y]], [[0]], [[]],
550 [[input.y: conflicts: 2 shift/reduce
552 AT_COMPILE([[input]])
553 AT_PARSER_CHECK([[./input]], [[1]], [[]],
554 [[syntax error, unexpected 'a', expecting 'b' or 'c'
557 # IELR is sufficient when LAC is used.
558 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
560 [[input.y: conflicts: 2 shift/reduce
562 AT_COMPILE([[input]])
563 AT_PARSER_CHECK([[./input]], [[1]], [[]],
564 [[syntax error, unexpected 'a', expecting 'b' or 'c'
569 ## ------------------------- ##
570 ## Unresolved SR Conflicts. ##
571 ## ------------------------- ##
573 AT_SETUP([Unresolved SR Conflicts])
575 AT_KEYWORDS([report])
580 exp: exp OP exp | NUM;
583 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
584 [input.y: conflicts: 1 shift/reduce
587 # Check the contents of the report.
588 AT_CHECK([cat input.output], [],
589 [[State 5 conflicts: 1 shift/reduce
600 Terminals, with rules where they appear
608 Nonterminals, with rules where they appear
613 on left: 1 2, on right: 0 1
618 0 $accept: . exp $end
622 NUM shift, and go to state 1
631 $default reduce using rule 2 (exp)
636 0 $accept: exp . $end
639 $end shift, and go to state 3
640 OP shift, and go to state 4
645 0 $accept: exp $end .
656 NUM shift, and go to state 1
664 1 | exp OP exp . [$end, OP]
666 OP shift, and go to state 4
668 OP [reduce using rule 1 (exp)]
669 $default reduce using rule 1 (exp)
676 ## ----------------------- ##
677 ## Resolved SR Conflicts. ##
678 ## ----------------------- ##
680 AT_SETUP([Resolved SR Conflicts])
682 AT_KEYWORDS([report])
688 exp: exp OP exp | NUM;
691 AT_BISON_CHECK([-o input.c --report=all input.y])
693 # Check the contents of the report.
694 AT_CHECK([cat input.output], [],
703 Terminals, with rules where they appear
711 Nonterminals, with rules where they appear
716 on left: 1 2, on right: 0 1
721 0 $accept: . exp $end
725 NUM shift, and go to state 1
734 $default reduce using rule 2 (exp)
739 0 $accept: exp . $end
742 $end shift, and go to state 3
743 OP shift, and go to state 4
748 0 $accept: exp $end .
759 NUM shift, and go to state 1
767 1 | exp OP exp . [$end, OP]
769 $default reduce using rule 1 (exp)
771 Conflict between rule 1 and token OP resolved as reduce (%left OP).
777 ## -------------------------------- ##
778 ## Defaulted Conflicted Reduction. ##
779 ## -------------------------------- ##
781 # When there are RR conflicts, some rules are disabled. Usually it is
782 # simply displayed as:
784 # $end reduce using rule 3 (num)
785 # $end [reduce using rule 4 (id)]
787 # But when `reduce 3' is the default action, we'd produce:
789 # $end [reduce using rule 4 (id)]
790 # $default reduce using rule 3 (num)
792 # In this precise case (a reduction is masked by the default
793 # reduction), we make the `reduce 3' explicit:
795 # $end reduce using rule 3 (num)
796 # $end [reduce using rule 4 (id)]
797 # $default reduce using rule 3 (num)
799 # Maybe that's not the best display, but then, please propose something
802 AT_SETUP([Defaulted Conflicted Reduction])
803 AT_KEYWORDS([report])
813 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
814 [[input.y: conflicts: 1 reduce/reduce
815 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
818 # Check the contents of the report.
819 AT_CHECK([cat input.output], [],
820 [[Rules useless in parser due to conflicts
825 State 1 conflicts: 1 reduce/reduce
840 Terminals, with rules where they appear
847 Nonterminals, with rules where they appear
852 on left: 1 2, on right: 0
854 on left: 3, on right: 1
856 on left: 4, on right: 2
861 0 $accept: . exp $end
867 '0' shift, and go to state 1
879 $end reduce using rule 3 (num)
880 $end [reduce using rule 4 (id)]
881 $default reduce using rule 3 (num)
886 0 $accept: exp . $end
888 $end shift, and go to state 5
895 $default reduce using rule 1 (exp)
902 $default reduce using rule 2 (exp)
907 0 $accept: exp $end .
917 ## -------------------- ##
918 ## %expect not enough. ##
919 ## -------------------- ##
921 AT_SETUP([%expect not enough])
927 exp: exp OP exp | NUM;
930 AT_BISON_CHECK([-o input.c input.y], 1, [],
931 [input.y: conflicts: 1 shift/reduce
932 input.y: expected 0 shift/reduce conflicts
937 ## --------------- ##
939 ## --------------- ##
941 AT_SETUP([%expect right])
947 exp: exp OP exp | NUM;
950 AT_BISON_CHECK([-o input.c input.y])
954 ## ------------------ ##
955 ## %expect too much. ##
956 ## ------------------ ##
958 AT_SETUP([%expect too much])
964 exp: exp OP exp | NUM;
967 AT_BISON_CHECK([-o input.c input.y], 1, [],
968 [input.y: conflicts: 1 shift/reduce
969 input.y: expected 2 shift/reduce conflicts
974 ## ------------------------------- ##
975 ## %expect with reduce conflicts. ##
976 ## ------------------------------- ##
978 AT_SETUP([%expect with reduce conflicts])
983 program: a 'a' | a a;
987 AT_BISON_CHECK([-o input.c input.y], 1, [],
988 [input.y: conflicts: 1 reduce/reduce
989 input.y: expected 0 reduce/reduce conflicts
994 ## ------------------------- ##
995 ## %prec with user strings. ##
996 ## ------------------------- ##
998 AT_SETUP([%prec with user string])
1000 AT_DATA([[input.y]],
1007 AT_BISON_CHECK([-o input.c input.y])
1011 ## -------------------------------- ##
1012 ## %no-default-prec without %prec. ##
1013 ## -------------------------------- ##
1015 AT_SETUP([%no-default-prec without %prec])
1017 AT_DATA([[input.y]],
1031 AT_BISON_CHECK([-o input.c input.y], 0, [],
1032 [[input.y: conflicts: 4 shift/reduce
1037 ## ----------------------------- ##
1038 ## %no-default-prec with %prec. ##
1039 ## ----------------------------- ##
1041 AT_SETUP([%no-default-prec with %prec])
1043 AT_DATA([[input.y]],
1051 e: e '+' e %prec '+'
1057 AT_BISON_CHECK([-o input.c input.y])
1061 ## --------------- ##
1062 ## %default-prec. ##
1063 ## --------------- ##
1065 AT_SETUP([%default-prec])
1067 AT_DATA([[input.y]],
1081 AT_BISON_CHECK([-o input.c input.y])
1085 ## ---------------------------------------------- ##
1086 ## Unreachable States After Conflict Resolution. ##
1087 ## ---------------------------------------------- ##
1089 AT_SETUP([[Unreachable States After Conflict Resolution]])
1091 # If conflict resolution makes states unreachable, remove those states, report
1092 # rules that are then unused, and don't report conflicts in those states. Test
1093 # what happens when a nonterminal becomes useless as a result of state removal
1094 # since that causes lalr.o's goto map to be rewritten.
1096 AT_DATA([[input.y]],
1102 start: resolved_conflict 'a' reported_conflicts 'a' ;
1104 /* S/R conflict resolved as reduce, so the state with item
1105 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1106 * unreachable, and the associated production is useless. */
1112 /* S/R conflict that need not be reported since it is unreachable because of
1113 * the previous conflict resolution. Nonterminal unreachable1 and all its
1114 * productions are useless. */
1120 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1123 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1124 * when their states are renumbered due to state removal. */
1133 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1134 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1135 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
1136 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
1137 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1138 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1139 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1140 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1141 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1144 AT_CHECK([[cat input.output]], 0,
1145 [[Rules useless in parser due to conflicts
1147 2 resolved_conflict: 'a' unreachable1
1149 4 unreachable1: 'a' unreachable2
1152 6 unreachable2: /* empty */
1155 9 reported_conflicts: 'a'
1159 State 4 conflicts: 1 shift/reduce
1160 State 5 conflicts: 1 reduce/reduce
1165 0 $accept: start $end
1167 1 start: resolved_conflict 'a' reported_conflicts 'a'
1169 2 resolved_conflict: 'a' unreachable1
1172 4 unreachable1: 'a' unreachable2
1175 6 unreachable2: /* empty */
1178 8 reported_conflicts: 'a'
1183 Terminals, with rules where they appear
1190 Nonterminals, with rules where they appear
1195 on left: 1, on right: 0
1196 resolved_conflict (6)
1197 on left: 2 3, on right: 1
1199 on left: 4 5, on right: 2
1201 on left: 6 7, on right: 4
1202 reported_conflicts (9)
1203 on left: 8 9 10, on right: 1
1208 0 $accept: . start $end
1209 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1210 2 resolved_conflict: . 'a' unreachable1
1213 $default reduce using rule 3 (resolved_conflict)
1216 resolved_conflict go to state 2
1218 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1223 0 $accept: start . $end
1225 $end shift, and go to state 3
1230 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1232 'a' shift, and go to state 4
1237 0 $accept: start $end .
1244 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1245 8 reported_conflicts: . 'a'
1249 'a' shift, and go to state 5
1251 'a' [reduce using rule 10 (reported_conflicts)]
1253 reported_conflicts go to state 6
1258 8 reported_conflicts: 'a' . ['a']
1261 'a' reduce using rule 8 (reported_conflicts)
1262 'a' [reduce using rule 9 (reported_conflicts)]
1263 $default reduce using rule 8 (reported_conflicts)
1268 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1270 'a' shift, and go to state 7
1275 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1277 $default reduce using rule 1 (start)
1280 AT_DATA([[input-keep.y]],
1281 [[%define lr.keep-unreachable-states
1283 AT_CHECK([[cat input.y >> input-keep.y]])
1285 AT_BISON_CHECK([[input-keep.y]], 0, [],
1286 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
1287 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1288 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1289 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1290 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1296 ## ------------------------------------------------------------ ##
1297 ## Solved conflicts report for multiple reductions in a state. ##
1298 ## ------------------------------------------------------------ ##
1300 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1302 # Used to lose earlier solved conflict messages even within a single S/R/R.
1304 AT_DATA([[input.y]],
1320 empty_a: %prec 'a' ;
1321 empty_b: %prec 'b' ;
1322 empty_c1: %prec 'c' ;
1323 empty_c2: %prec 'c' ;
1324 empty_c3: %prec 'd' ;
1326 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1327 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1330 0 $accept: . start $end
1343 13 empty_c3: . ['c']
1345 'b' shift, and go to state 1
1347 'c' reduce using rule 13 (empty_c3)
1348 $default reduce using rule 9 (empty_a)
1351 empty_a go to state 3
1352 empty_b go to state 4
1353 empty_c1 go to state 5
1354 empty_c2 go to state 6
1355 empty_c3 go to state 7
1357 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1358 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1359 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1360 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1361 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1370 ## ------------------------------------------------------------ ##
1371 ## %nonassoc error actions for multiple reductions in a state. ##
1372 ## ------------------------------------------------------------ ##
1374 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1375 # multiple reductions in a state.
1377 # For a %nonassoc error action token, used to print the first remaining
1378 # reduction on that token without brackets.
1380 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1382 AT_DATA([[input.y]],
1383 [[%nonassoc 'a' 'b' 'c'
1395 empty_a: %prec 'a' ;
1396 empty_b: %prec 'b' ;
1397 empty_c1: %prec 'c' ;
1398 empty_c2: %prec 'c' ;
1399 empty_c3: %prec 'c' ;
1402 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1403 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1406 0 $accept: . start $end
1418 12 empty_c2: . ['c']
1419 13 empty_c3: . ['c']
1421 'a' error (nonassociative)
1422 'b' error (nonassociative)
1423 'c' error (nonassociative)
1425 'c' [reduce using rule 12 (empty_c2)]
1426 'c' [reduce using rule 13 (empty_c3)]
1429 empty_a go to state 2
1430 empty_b go to state 3
1431 empty_c1 go to state 4
1432 empty_c2 go to state 5
1433 empty_c3 go to state 6
1435 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1436 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1437 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1445 ## --------------------------------- ##
1446 ## -W versus %expect and %expect-rr ##
1447 ## --------------------------------- ##
1449 AT_SETUP([[-W versus %expect and %expect-rr]])
1451 AT_DATA([[sr-rr.y]],
1454 start: 'a' | A 'a' | B 'a' ;
1461 start: 'a' | A 'a' ;
1472 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1473 [[sr-rr.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1475 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1476 [[sr-rr.y: conflicts: 1 reduce/reduce
1478 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1479 [[sr-rr.y: conflicts: 1 shift/reduce
1482 [for gram in sr-rr sr rr; do
1483 for sr_exp_i in '' 0 1 2; do
1484 for rr_exp_i in '' 0 1 2; do
1485 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1487 # Build grammar file.
1492 if test -n "$sr_exp_i"; then
1494 file=$file-expect-$sr_exp
1495 directives="%expect $sr_exp"
1497 if test -n "$rr_exp_i"; then
1499 file=$file-expect-rr-$rr_exp
1500 directives="$directives %expect-rr $rr_exp"
1503 echo "$directives" > $file
1504 cat $gram.y >> $file
1506 # Count actual conflicts.
1510 if test $gram = sr || test $gram = sr-rr; then
1511 conflicts="1 shift/reduce"
1514 if test $gram = rr || test $gram = sr-rr; then
1515 if test -n "$conflicts"; then
1516 conflicts="$conflicts, "
1518 conflicts="${conflicts}1 reduce/reduce"
1523 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1524 ]AT_BISON_CHECK([[-Wnone $file]])[
1525 ]AT_BISON_CHECK([[-Werror $file]])[
1527 echo "$file: conflicts: $conflicts" > experr
1528 if test $sr_count -ne $sr_exp; then
1529 if test $sr_exp -ne 1; then s=s; else s= ; fi
1530 echo "$file: expected $sr_exp shift/reduce conflict$s" >> experr
1532 if test $rr_count -ne $rr_exp; then
1533 if test $rr_exp -ne 1; then s=s; else s= ; fi
1534 echo "$file: expected $rr_exp reduce/reduce conflict$s" >> experr
1536 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1537 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[