1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007, 2009-2012 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_BISON_OPTION_PUSHDEFS
54 AT_DATA_GRAMMAR([input.y],
62 #define YYERROR_VERBOSE 1
64 /* The current argument. */
65 static const char *input;
71 assert (toknum <= strlen (input));
72 return input[toknum++];
86 main (int argc, const char *argv[])
88 input = argc <= 1 ? "" : argv[1];
92 AT_BISON_OPTION_POPDEFS
94 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
95 [AT_BISON_CHECK([$1[ -o input.c input.y]])
98 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
100 AT_PARSER_CHECK([./input '0<0'])
101 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
102 [syntax error, unexpected '<'AT_EXPECTING
105 AT_PARSER_CHECK([./input '0>0'])
106 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
107 [syntax error, unexpected '>'AT_EXPECTING
110 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
111 [syntax error, unexpected '>'AT_EXPECTING
114 m4_popdef([AT_EXPECTING])])
116 # Expected token list is missing.
117 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
119 # We must disable default reductions in inconsistent states in order to
120 # have an explicit list of all expected tokens.
121 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reductions=consistent]],
124 # lr.default-reductions=consistent happens to work for this test case.
125 # However, for other grammars, lookahead sets can be merged for
126 # different left contexts, so it is still possible to have an incorrect
127 # expected list. Canonical LR is almost a general solution (that is, it
128 # can fail only when %nonassoc is used), so make sure it gives the same
130 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
132 # parse.lac=full is a completely general solution that does not require
133 # any of the above sacrifices. Of course, it does not extend the
134 # language-recognition power of LALR to (IE)LR, but it does ensure that
135 # the reported list of expected tokens matches what the given parser
136 # would have accepted in place of the unexpected token.
137 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
139 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
145 ## -------------------------------------- ##
146 ## %error-verbose and consistent errors. ##
147 ## -------------------------------------- ##
149 AT_SETUP([[%error-verbose and consistent errors]])
151 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
153 AT_BISON_OPTION_PUSHDEFS([$1])
155 m4_pushdef([AT_YYLEX_PROTOTYPE],
156 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
157 [[int yylex (YYSTYPE *lvalp)]])])
159 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
163 import java.io.IOException;
166 %code {]AT_SKEL_CC_IF([[
168 #include <string>]], [[
171 ]AT_YYERROR_DECLARE])[
172 ]AT_YYLEX_PROTOTYPE[;
176 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
186 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
190 `--------*/]AT_SKEL_JAVA_IF([[
192 public String input = "]$3[";
193 public int index = 0;
196 if (index < input.length ())
197 return input.charAt (index++);
201 public Object getLVal ()
203 return new Integer(1);
208 static char const *input = "]$3[";
220 `-------*/]AT_SKEL_JAVA_IF([[
224 public static void main (String args[]) throws IOException
226 YYParser p = new YYParser ();
229 }]], [AT_SKEL_CC_IF([[
235 return parser.parse ();
245 AT_FULL_COMPILE([[input]])
247 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
248 $5, [a], [[, expecting 'a']],
249 $5, [b], [[, expecting 'b']])])
251 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
252 [AT_PARSER_CHECK([[./input]], [[1]]]),
254 [[syntax error, unexpected ]$4[]AT_EXPECTING[
257 m4_popdef([AT_EXPECTING])
258 m4_popdef([AT_YYLEX_PROTOTYPE])
259 AT_BISON_OPTION_POPDEFS
263 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
266 start: consistent-error-on-a-a 'a' ;
268 consistent-error-on-a-a:
269 'a' default-reduction
270 | 'a' default-reduction 'a'
274 default-reduction: /*empty*/ ;
277 // Provide another context in which all rules are useful so that this
278 // test case looks a little more realistic.
279 start: 'b' consistent-error-on-a-a 'c' ;
282 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
284 # Unfortunately, no expected tokens are reported even though 'b' can be
285 # accepted. Nevertheless, the main point of this test is to make sure
286 # that at least the unexpected token is reported. In a previous version
287 # of Bison, it wasn't reported because the error is detected in a
288 # consistent state with an error action, and that case always triggered
289 # the simple "syntax error" message.
291 # The point isn't to test IELR here, but state merging happens to
292 # complicate this example.
293 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
294 [AT_PREVIOUS_STATE_GRAMMAR],
295 [AT_PREVIOUS_STATE_INPUT],
297 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
299 [AT_PREVIOUS_STATE_GRAMMAR],
300 [AT_PREVIOUS_STATE_INPUT],
302 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
304 [AT_PREVIOUS_STATE_GRAMMAR],
305 [AT_PREVIOUS_STATE_INPUT],
307 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
309 [AT_PREVIOUS_STATE_GRAMMAR],
310 [AT_PREVIOUS_STATE_INPUT],
311 [[end of input]], [[none]])
313 # Even canonical LR doesn't foresee the error for 'a'!
314 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
315 %define lr.default-reductions consistent]],
316 [AT_PREVIOUS_STATE_GRAMMAR],
317 [AT_PREVIOUS_STATE_INPUT],
319 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
320 %define lr.default-reductions accepting]],
321 [AT_PREVIOUS_STATE_GRAMMAR],
322 [AT_PREVIOUS_STATE_INPUT],
324 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
325 [AT_PREVIOUS_STATE_GRAMMAR],
326 [AT_PREVIOUS_STATE_INPUT],
329 # Only LAC gets it right.
330 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
331 %define parse.lac full]],
332 [AT_PREVIOUS_STATE_GRAMMAR],
333 [AT_PREVIOUS_STATE_INPUT],
335 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
336 %define parse.lac full]],
337 [AT_PREVIOUS_STATE_GRAMMAR],
338 [AT_PREVIOUS_STATE_INPUT],
341 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
342 m4_popdef([AT_PREVIOUS_STATE_INPUT])
344 m4_pushdef([AT_USER_ACTION_GRAMMAR],
347 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
348 // incorrectly for the 'b' set in the semantic action below. All 'a'
349 // tokens are returned by yylex, which sets $$ = 1.
352 fprintf (stderr, "Wrong destructor.\n");
355 // Rather than depend on an inconsistent state to induce reading a
356 // lookahead as in the previous grammar, just assign the lookahead in a
357 // semantic action. That lookahead isn't needed before either error
358 // action is encountered. In a previous version of Bison, this was a
359 // problem as it meant yychar was not translated into yytoken before
360 // either error action. The second error action thus invoked a
361 // destructor that it selected according to the incorrect yytoken. The
362 // first error action would have reported an incorrect unexpected token
363 // except that, due to the bug described in the previous grammar, the
364 // unexpected token was not reported at all.
365 start: error-reduce consistent-error 'a' { USE ($][3); } ;
368 'a' 'a' consistent-reduction consistent-error 'a'
369 { USE (($][1, $][2, $][5)); }
374 consistent-reduction: /*empty*/ {
375 assert (yychar == ]AT_SKEL_CC_IF([[yyempty_]], [[YYEMPTY]])[);
382 | /*empty*/ %prec 'a'
385 // Provide another context in which all rules are useful so that this
386 // test case looks a little more realistic.
387 start: 'b' consistent-error 'b' ;
389 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
391 AT_CONSISTENT_ERRORS_CHECK([[]],
392 [AT_USER_ACTION_GRAMMAR],
393 [AT_USER_ACTION_INPUT],
395 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
396 [AT_USER_ACTION_GRAMMAR],
397 [AT_USER_ACTION_INPUT],
399 AT_CONSISTENT_ERRORS_CHECK([[%language "c++"]],
400 [AT_USER_ACTION_GRAMMAR],
401 [AT_USER_ACTION_INPUT],
403 # No Java test because yychar cannot be manipulated by users.
405 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]],
406 [AT_USER_ACTION_GRAMMAR],
407 [AT_USER_ACTION_INPUT],
410 # Canonical LR doesn't foresee the error for 'a'!
411 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions accepting]],
412 [AT_USER_ACTION_GRAMMAR],
413 [AT_USER_ACTION_INPUT],
415 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
416 [AT_USER_ACTION_GRAMMAR],
417 [AT_USER_ACTION_INPUT],
420 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
421 [AT_USER_ACTION_GRAMMAR],
422 [AT_USER_ACTION_INPUT],
424 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
425 %define lr.default-reductions accepting]],
426 [AT_USER_ACTION_GRAMMAR],
427 [AT_USER_ACTION_INPUT],
430 m4_popdef([AT_USER_ACTION_GRAMMAR])
431 m4_popdef([AT_USER_ACTION_INPUT])
433 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
439 ## ------------------------------------------------------- ##
440 ## LAC: %nonassoc requires splitting canonical LR states. ##
441 ## ------------------------------------------------------- ##
443 # This test case demonstrates that, when %nonassoc is used, canonical
444 # LR(1) parser table construction followed by conflict resolution
445 # without further state splitting is not always sufficient to produce a
446 # parser that can detect all syntax errors as soon as possible on one
447 # token of lookahead. However, LAC solves the problem completely even
448 # with minimal LR parser tables.
450 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
451 AT_BISON_OPTION_PUSHDEFS
452 AT_DATA_GRAMMAR([[input.y]],
465 'a' problem 'a' // First context.
466 | 'b' problem 'b' // Second context.
467 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
476 // For the state reached after shifting the 'a' in these productions,
477 // lookahead sets are the same in both the first and second contexts.
478 // Thus, canonical LR reuses the same state for both contexts. However,
479 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
480 // error action only in the first context. In order to immediately
481 // detect the syntax error on 'a' here for only the first context, this
482 // canonical LR state would have to be split into two states, and the
483 // 'a' lookahead would have to be removed from only one of the states.
485 'a' // Reduction lookahead set is always ['a', 'b'].
487 | 'a' 'c' // 'c' is forgotten as an expected token.
490 reduce-nonassoc: %prec 'a';
494 ]AT_YYLEX_DEFINE(["aaa"])[
502 AT_BISON_OPTION_POPDEFS
504 # Show canonical LR's failure.
505 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
507 [[input.y: conflicts: 2 shift/reduce
509 AT_COMPILE([[input]])
510 AT_PARSER_CHECK([[./input]], [[1]], [[]],
511 [[syntax error, unexpected 'a', expecting 'b'
514 # It's corrected by LAC.
515 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
516 -o input.c input.y]], [[0]], [[]],
517 [[input.y: conflicts: 2 shift/reduce
519 AT_COMPILE([[input]])
520 AT_PARSER_CHECK([[./input]], [[1]], [[]],
521 [[syntax error, unexpected 'a', expecting 'b' or 'c'
524 # IELR is sufficient when LAC is used.
525 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
527 [[input.y: conflicts: 2 shift/reduce
529 AT_COMPILE([[input]])
530 AT_PARSER_CHECK([[./input]], [[1]], [[]],
531 [[syntax error, unexpected 'a', expecting 'b' or 'c'
536 ## ------------------------- ##
537 ## Unresolved SR Conflicts. ##
538 ## ------------------------- ##
540 AT_SETUP([Unresolved SR Conflicts])
542 AT_KEYWORDS([report])
547 exp: exp OP exp | NUM;
550 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
551 [input.y: conflicts: 1 shift/reduce
554 # Check the contents of the report.
555 AT_CHECK([cat input.output], [],
556 [[State 5 conflicts: 1 shift/reduce
567 Terminals, with rules where they appear
575 Nonterminals, with rules where they appear
580 on left: 1 2, on right: 0 1
585 0 $accept: . exp $end
589 NUM shift, and go to state 1
598 $default reduce using rule 2 (exp)
603 0 $accept: exp . $end
606 $end shift, and go to state 3
607 OP shift, and go to state 4
612 0 $accept: exp $end .
623 NUM shift, and go to state 1
631 1 | exp OP exp . [$end, OP]
633 OP shift, and go to state 4
635 OP [reduce using rule 1 (exp)]
636 $default reduce using rule 1 (exp)
643 ## ----------------------- ##
644 ## Resolved SR Conflicts. ##
645 ## ----------------------- ##
647 AT_SETUP([Resolved SR Conflicts])
649 AT_KEYWORDS([report])
655 exp: exp OP exp | NUM;
658 AT_BISON_CHECK([-o input.c --report=all input.y])
660 # Check the contents of the report.
661 AT_CHECK([cat input.output], [],
670 Terminals, with rules where they appear
678 Nonterminals, with rules where they appear
683 on left: 1 2, on right: 0 1
688 0 $accept: . exp $end
692 NUM shift, and go to state 1
701 $default reduce using rule 2 (exp)
706 0 $accept: exp . $end
709 $end shift, and go to state 3
710 OP shift, and go to state 4
715 0 $accept: exp $end .
726 NUM shift, and go to state 1
734 1 | exp OP exp . [$end, OP]
736 $default reduce using rule 1 (exp)
738 Conflict between rule 1 and token OP resolved as reduce (%left OP).
744 ## -------------------------------- ##
745 ## Defaulted Conflicted Reduction. ##
746 ## -------------------------------- ##
748 # When there are RR conflicts, some rules are disabled. Usually it is
749 # simply displayed as:
751 # $end reduce using rule 3 (num)
752 # $end [reduce using rule 4 (id)]
754 # But when `reduce 3' is the default action, we'd produce:
756 # $end [reduce using rule 4 (id)]
757 # $default reduce using rule 3 (num)
759 # In this precise case (a reduction is masked by the default
760 # reduction), we make the `reduce 3' explicit:
762 # $end reduce using rule 3 (num)
763 # $end [reduce using rule 4 (id)]
764 # $default reduce using rule 3 (num)
766 # Maybe that's not the best display, but then, please propose something
769 AT_SETUP([Defaulted Conflicted Reduction])
770 AT_KEYWORDS([report])
780 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
781 [[input.y: conflicts: 1 reduce/reduce
782 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
785 # Check the contents of the report.
786 AT_CHECK([cat input.output], [],
787 [[Rules useless in parser due to conflicts
792 State 1 conflicts: 1 reduce/reduce
807 Terminals, with rules where they appear
814 Nonterminals, with rules where they appear
819 on left: 1 2, on right: 0
821 on left: 3, on right: 1
823 on left: 4, on right: 2
828 0 $accept: . exp $end
834 '0' shift, and go to state 1
846 $end reduce using rule 3 (num)
847 $end [reduce using rule 4 (id)]
848 $default reduce using rule 3 (num)
853 0 $accept: exp . $end
855 $end shift, and go to state 5
862 $default reduce using rule 1 (exp)
869 $default reduce using rule 2 (exp)
874 0 $accept: exp $end .
884 ## -------------------- ##
885 ## %expect not enough. ##
886 ## -------------------- ##
888 AT_SETUP([%expect not enough])
894 exp: exp OP exp | NUM;
897 AT_BISON_CHECK([-o input.c input.y], 1, [],
898 [input.y: conflicts: 1 shift/reduce
899 input.y: error: expected 0 shift/reduce conflicts
904 ## --------------- ##
906 ## --------------- ##
908 AT_SETUP([%expect right])
914 exp: exp OP exp | NUM;
917 AT_BISON_CHECK([-o input.c input.y])
921 ## ------------------ ##
922 ## %expect too much. ##
923 ## ------------------ ##
925 AT_SETUP([%expect too much])
931 exp: exp OP exp | NUM;
934 AT_BISON_CHECK([-o input.c input.y], 1, [],
935 [input.y: conflicts: 1 shift/reduce
936 input.y: error: expected 2 shift/reduce conflicts
941 ## ------------------------------- ##
942 ## %expect with reduce conflicts. ##
943 ## ------------------------------- ##
945 AT_SETUP([%expect with reduce conflicts])
950 program: a 'a' | a a;
954 AT_BISON_CHECK([-o input.c input.y], 1, [],
955 [input.y: conflicts: 1 reduce/reduce
956 input.y: error: expected 0 reduce/reduce conflicts
961 ## ------------------------- ##
962 ## %prec with user strings. ##
963 ## ------------------------- ##
965 AT_SETUP([%prec with user string])
974 AT_BISON_CHECK([-o input.c input.y])
978 ## -------------------------------- ##
979 ## %no-default-prec without %prec. ##
980 ## -------------------------------- ##
982 AT_SETUP([%no-default-prec without %prec])
998 AT_BISON_CHECK([-o input.c input.y], 0, [],
999 [[input.y: conflicts: 4 shift/reduce
1004 ## ----------------------------- ##
1005 ## %no-default-prec with %prec. ##
1006 ## ----------------------------- ##
1008 AT_SETUP([%no-default-prec with %prec])
1010 AT_DATA([[input.y]],
1018 e: e '+' e %prec '+'
1024 AT_BISON_CHECK([-o input.c input.y])
1028 ## --------------- ##
1029 ## %default-prec. ##
1030 ## --------------- ##
1032 AT_SETUP([%default-prec])
1034 AT_DATA([[input.y]],
1048 AT_BISON_CHECK([-o input.c input.y])
1052 ## ---------------------------------------------- ##
1053 ## Unreachable States After Conflict Resolution. ##
1054 ## ---------------------------------------------- ##
1056 AT_SETUP([[Unreachable States After Conflict Resolution]])
1058 # If conflict resolution makes states unreachable, remove those states, report
1059 # rules that are then unused, and don't report conflicts in those states. Test
1060 # what happens when a nonterminal becomes useless as a result of state removal
1061 # since that causes lalr.o's goto map to be rewritten.
1063 AT_DATA([[input.y]],
1069 start: resolved_conflict 'a' reported_conflicts 'a' ;
1071 /* S/R conflict resolved as reduce, so the state with item
1072 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1073 * unreachable, and the associated production is useless. */
1079 /* S/R conflict that need not be reported since it is unreachable because of
1080 * the previous conflict resolution. Nonterminal unreachable1 and all its
1081 * productions are useless. */
1087 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1090 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1091 * when their states are renumbered due to state removal. */
1100 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1101 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1102 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
1103 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
1104 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1105 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1106 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1107 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1108 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1111 AT_CHECK([[cat input.output]], 0,
1112 [[Rules useless in parser due to conflicts
1114 2 resolved_conflict: 'a' unreachable1
1116 4 unreachable1: 'a' unreachable2
1119 6 unreachable2: /* empty */
1122 9 reported_conflicts: 'a'
1126 State 4 conflicts: 1 shift/reduce
1127 State 5 conflicts: 1 reduce/reduce
1132 0 $accept: start $end
1134 1 start: resolved_conflict 'a' reported_conflicts 'a'
1136 2 resolved_conflict: 'a' unreachable1
1139 4 unreachable1: 'a' unreachable2
1142 6 unreachable2: /* empty */
1145 8 reported_conflicts: 'a'
1150 Terminals, with rules where they appear
1157 Nonterminals, with rules where they appear
1162 on left: 1, on right: 0
1163 resolved_conflict (6)
1164 on left: 2 3, on right: 1
1166 on left: 4 5, on right: 2
1168 on left: 6 7, on right: 4
1169 reported_conflicts (9)
1170 on left: 8 9 10, on right: 1
1175 0 $accept: . start $end
1176 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1177 2 resolved_conflict: . 'a' unreachable1
1180 $default reduce using rule 3 (resolved_conflict)
1183 resolved_conflict go to state 2
1185 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1190 0 $accept: start . $end
1192 $end shift, and go to state 3
1197 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1199 'a' shift, and go to state 4
1204 0 $accept: start $end .
1211 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1212 8 reported_conflicts: . 'a'
1216 'a' shift, and go to state 5
1218 'a' [reduce using rule 10 (reported_conflicts)]
1220 reported_conflicts go to state 6
1225 8 reported_conflicts: 'a' . ['a']
1228 'a' reduce using rule 8 (reported_conflicts)
1229 'a' [reduce using rule 9 (reported_conflicts)]
1230 $default reduce using rule 8 (reported_conflicts)
1235 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1237 'a' shift, and go to state 7
1242 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1244 $default reduce using rule 1 (start)
1247 AT_DATA([[input-keep.y]],
1248 [[%define lr.keep-unreachable-states
1250 AT_CHECK([[cat input.y >> input-keep.y]])
1252 AT_BISON_CHECK([[input-keep.y]], 0, [],
1253 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
1254 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1255 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1256 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1257 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1263 ## ------------------------------------------------------------ ##
1264 ## Solved conflicts report for multiple reductions in a state. ##
1265 ## ------------------------------------------------------------ ##
1267 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1269 # Used to lose earlier solved conflict messages even within a single S/R/R.
1271 AT_DATA([[input.y]],
1287 empty_a: %prec 'a' ;
1288 empty_b: %prec 'b' ;
1289 empty_c1: %prec 'c' ;
1290 empty_c2: %prec 'c' ;
1291 empty_c3: %prec 'd' ;
1293 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1294 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1297 0 $accept: . start $end
1310 13 empty_c3: . ['c']
1312 'b' shift, and go to state 1
1314 'c' reduce using rule 13 (empty_c3)
1315 $default reduce using rule 9 (empty_a)
1318 empty_a go to state 3
1319 empty_b go to state 4
1320 empty_c1 go to state 5
1321 empty_c2 go to state 6
1322 empty_c3 go to state 7
1324 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1325 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1326 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1327 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1328 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1337 ## ------------------------------------------------------------ ##
1338 ## %nonassoc error actions for multiple reductions in a state. ##
1339 ## ------------------------------------------------------------ ##
1341 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1342 # multiple reductions in a state.
1344 # For a %nonassoc error action token, used to print the first remaining
1345 # reduction on that token without brackets.
1347 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1349 AT_DATA([[input.y]],
1350 [[%nonassoc 'a' 'b' 'c'
1362 empty_a: %prec 'a' ;
1363 empty_b: %prec 'b' ;
1364 empty_c1: %prec 'c' ;
1365 empty_c2: %prec 'c' ;
1366 empty_c3: %prec 'c' ;
1369 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1370 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1373 0 $accept: . start $end
1385 12 empty_c2: . ['c']
1386 13 empty_c3: . ['c']
1388 'a' error (nonassociative)
1389 'b' error (nonassociative)
1390 'c' error (nonassociative)
1392 'c' [reduce using rule 12 (empty_c2)]
1393 'c' [reduce using rule 13 (empty_c3)]
1396 empty_a go to state 2
1397 empty_b go to state 3
1398 empty_c1 go to state 4
1399 empty_c2 go to state 5
1400 empty_c3 go to state 6
1402 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1403 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1404 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1412 ## --------------------------------- ##
1413 ## -W versus %expect and %expect-rr ##
1414 ## --------------------------------- ##
1416 AT_SETUP([[-W versus %expect and %expect-rr]])
1418 AT_DATA([[sr-rr.y]],
1421 start: 'a' | A 'a' | B 'a' ;
1428 start: 'a' | A 'a' ;
1439 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1440 [[sr-rr.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1442 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1443 [[sr-rr.y: conflicts: 1 reduce/reduce
1445 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1446 [[sr-rr.y: conflicts: 1 shift/reduce
1449 [for gram in sr-rr sr rr; do
1450 for sr_exp_i in '' 0 1 2; do
1451 for rr_exp_i in '' 0 1 2; do
1452 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1454 # Build grammar file.
1459 if test -n "$sr_exp_i"; then
1461 file=$file-expect-$sr_exp
1462 directives="%expect $sr_exp"
1464 if test -n "$rr_exp_i"; then
1466 file=$file-expect-rr-$rr_exp
1467 directives="$directives %expect-rr $rr_exp"
1470 echo "$directives" > $file
1471 cat $gram.y >> $file
1473 # Count actual conflicts.
1477 if test $gram = sr || test $gram = sr-rr; then
1478 conflicts="1 shift/reduce"
1481 if test $gram = rr || test $gram = sr-rr; then
1482 if test -n "$conflicts"; then
1483 conflicts="$conflicts, "
1485 conflicts="${conflicts}1 reduce/reduce"
1490 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1491 ]AT_BISON_CHECK([[-Wnone $file]])[
1492 ]AT_BISON_CHECK([[-Werror $file]])[
1494 echo "$file: conflicts: $conflicts" > experr
1495 if test $sr_count -ne $sr_exp; then
1496 if test $sr_exp -ne 1; then s=s; else s= ; fi
1497 echo "$file: error: expected $sr_exp shift/reduce conflict$s" >> experr
1499 if test $rr_count -ne $rr_exp; then
1500 if test $rr_exp -ne 1; then s=s; else s= ; fi
1501 echo "$file: error: expected $rr_exp reduce/reduce conflict$s" >> experr
1503 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1504 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[