1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007, 2009-2013 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 */
44 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
45 [[input.y:4.9: warning: rule useless in parser due to conflicts
46 e: 'e' | /* Nothing. */;
53 ## ------------------- ##
54 ## %nonassoc and eof. ##
55 ## ------------------- ##
57 AT_SETUP([%nonassoc and eof])
59 AT_BISON_OPTION_PUSHDEFS
60 AT_DATA_GRAMMAR([input.y],
68 #define YYERROR_VERBOSE 1
70 /* The current argument. */
71 static const char *input;
77 assert (toknum <= strlen (input));
78 return input[toknum++];
92 main (int argc, const char *argv[])
94 input = argc <= 1 ? "" : argv[1];
98 AT_BISON_OPTION_POPDEFS
100 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
101 [AT_BISON_CHECK([$1[ -o input.c input.y]])
104 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
106 AT_PARSER_CHECK([./input '0<0'])
107 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
108 [syntax error, unexpected '<'AT_EXPECTING
111 AT_PARSER_CHECK([./input '0>0'])
112 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
113 [syntax error, unexpected '>'AT_EXPECTING
116 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
117 [syntax error, unexpected '>'AT_EXPECTING
120 m4_popdef([AT_EXPECTING])])
122 # Expected token list is missing.
123 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
125 # We must disable default reductions in inconsistent states in order to
126 # have an explicit list of all expected tokens.
127 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reductions=consistent]],
130 # lr.default-reductions=consistent happens to work for this test case.
131 # However, for other grammars, lookahead sets can be merged for
132 # different left contexts, so it is still possible to have an incorrect
133 # expected list. Canonical LR is almost a general solution (that is, it
134 # can fail only when %nonassoc is used), so make sure it gives the same
136 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
138 # parse.lac=full is a completely general solution that does not require
139 # any of the above sacrifices. Of course, it does not extend the
140 # language-recognition power of LALR to (IE)LR, but it does ensure that
141 # the reported list of expected tokens matches what the given parser
142 # would have accepted in place of the unexpected token.
143 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
145 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
151 ## -------------------------------------- ##
152 ## %error-verbose and consistent errors. ##
153 ## -------------------------------------- ##
155 AT_SETUP([[%error-verbose and consistent errors]])
157 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
159 AT_BISON_OPTION_PUSHDEFS([$1])
161 m4_pushdef([AT_YYLEX_PROTOTYPE],
162 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
163 [[int yylex (YYSTYPE *lvalp)]])])
165 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
169 import java.io.IOException;
172 %code {]AT_SKEL_CC_IF([[
174 #include <string>]], [[
177 ]AT_YYERROR_DECLARE])[
178 ]AT_YYLEX_PROTOTYPE[;
182 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
192 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
196 `--------*/]AT_SKEL_JAVA_IF([[
198 public String input = "]$3[";
199 public int index = 0;
202 if (index < input.length ())
203 return input.charAt (index++);
207 public Object getLVal ()
209 return new Integer(1);
214 static char const *input = "]$3[";
226 `-------*/]AT_SKEL_JAVA_IF([[
230 public static void main (String args[]) throws IOException
232 YYParser p = new YYParser ();
235 }]], [AT_SKEL_CC_IF([[
241 return parser.parse ();
251 AT_FULL_COMPILE([[input]])
253 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
254 $5, [a], [[, expecting 'a']],
255 $5, [b], [[, expecting 'b']])])
257 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
258 [AT_PARSER_CHECK([[./input]], [[1]]]),
260 [[syntax error, unexpected ]$4[]AT_EXPECTING[
263 m4_popdef([AT_EXPECTING])
264 m4_popdef([AT_YYLEX_PROTOTYPE])
265 AT_BISON_OPTION_POPDEFS
269 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
272 start: consistent-error-on-a-a 'a' ;
274 consistent-error-on-a-a:
275 'a' default-reduction
276 | 'a' default-reduction 'a'
280 default-reduction: /*empty*/ ;
283 // Provide another context in which all rules are useful so that this
284 // test case looks a little more realistic.
285 start: 'b' consistent-error-on-a-a 'c' ;
288 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
290 # Unfortunately, no expected tokens are reported even though 'b' can be
291 # accepted. Nevertheless, the main point of this test is to make sure
292 # that at least the unexpected token is reported. In a previous version
293 # of Bison, it wasn't reported because the error is detected in a
294 # consistent state with an error action, and that case always triggered
295 # the simple "syntax error" message.
297 # The point isn't to test IELR here, but state merging happens to
298 # complicate this example.
299 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
300 [AT_PREVIOUS_STATE_GRAMMAR],
301 [AT_PREVIOUS_STATE_INPUT],
303 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
305 [AT_PREVIOUS_STATE_GRAMMAR],
306 [AT_PREVIOUS_STATE_INPUT],
308 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
310 [AT_PREVIOUS_STATE_GRAMMAR],
311 [AT_PREVIOUS_STATE_INPUT],
313 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
315 [AT_PREVIOUS_STATE_GRAMMAR],
316 [AT_PREVIOUS_STATE_INPUT],
317 [[end of input]], [[none]])
319 # Even canonical LR doesn't foresee the error for 'a'!
320 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
321 %define lr.default-reductions consistent]],
322 [AT_PREVIOUS_STATE_GRAMMAR],
323 [AT_PREVIOUS_STATE_INPUT],
325 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
326 %define lr.default-reductions accepting]],
327 [AT_PREVIOUS_STATE_GRAMMAR],
328 [AT_PREVIOUS_STATE_INPUT],
330 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
331 [AT_PREVIOUS_STATE_GRAMMAR],
332 [AT_PREVIOUS_STATE_INPUT],
335 # Only LAC gets it right.
336 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
337 %define parse.lac full]],
338 [AT_PREVIOUS_STATE_GRAMMAR],
339 [AT_PREVIOUS_STATE_INPUT],
341 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
342 %define parse.lac full]],
343 [AT_PREVIOUS_STATE_GRAMMAR],
344 [AT_PREVIOUS_STATE_INPUT],
347 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
348 m4_popdef([AT_PREVIOUS_STATE_INPUT])
350 m4_pushdef([AT_USER_ACTION_GRAMMAR],
353 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
354 // incorrectly for the 'b' set in the semantic action below. All 'a'
355 // tokens are returned by yylex, which sets $$ = 1.
358 fprintf (stderr, "Wrong destructor.\n");
361 // Rather than depend on an inconsistent state to induce reading a
362 // lookahead as in the previous grammar, just assign the lookahead in a
363 // semantic action. That lookahead isn't needed before either error
364 // action is encountered. In a previous version of Bison, this was a
365 // problem as it meant yychar was not translated into yytoken before
366 // either error action. The second error action thus invoked a
367 // destructor that it selected according to the incorrect yytoken. The
368 // first error action would have reported an incorrect unexpected token
369 // except that, due to the bug described in the previous grammar, the
370 // unexpected token was not reported at all.
371 start: error-reduce consistent-error 'a' { USE ($][3); } ;
374 'a' 'a' consistent-reduction consistent-error 'a'
375 { USE (($][1, $][2, $][5)); }
380 consistent-reduction: /*empty*/ {
381 assert (yychar == ]AT_SKEL_CC_IF([[yyempty_]], [[YYEMPTY]])[);
388 | /*empty*/ %prec 'a'
391 // Provide another context in which all rules are useful so that this
392 // test case looks a little more realistic.
393 start: 'b' consistent-error 'b' ;
395 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
397 AT_CONSISTENT_ERRORS_CHECK([[]],
398 [AT_USER_ACTION_GRAMMAR],
399 [AT_USER_ACTION_INPUT],
401 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
402 [AT_USER_ACTION_GRAMMAR],
403 [AT_USER_ACTION_INPUT],
405 AT_CONSISTENT_ERRORS_CHECK([[%language "c++"]],
406 [AT_USER_ACTION_GRAMMAR],
407 [AT_USER_ACTION_INPUT],
409 # No Java test because yychar cannot be manipulated by users.
411 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]],
412 [AT_USER_ACTION_GRAMMAR],
413 [AT_USER_ACTION_INPUT],
416 # Canonical LR doesn't foresee the error for 'a'!
417 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions accepting]],
418 [AT_USER_ACTION_GRAMMAR],
419 [AT_USER_ACTION_INPUT],
421 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
422 [AT_USER_ACTION_GRAMMAR],
423 [AT_USER_ACTION_INPUT],
426 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
427 [AT_USER_ACTION_GRAMMAR],
428 [AT_USER_ACTION_INPUT],
430 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
431 %define lr.default-reductions accepting]],
432 [AT_USER_ACTION_GRAMMAR],
433 [AT_USER_ACTION_INPUT],
436 m4_popdef([AT_USER_ACTION_GRAMMAR])
437 m4_popdef([AT_USER_ACTION_INPUT])
439 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
445 ## ------------------------------------------------------- ##
446 ## LAC: %nonassoc requires splitting canonical LR states. ##
447 ## ------------------------------------------------------- ##
449 # This test case demonstrates that, when %nonassoc is used, canonical
450 # LR(1) parser table construction followed by conflict resolution
451 # without further state splitting is not always sufficient to produce a
452 # parser that can detect all syntax errors as soon as possible on one
453 # token of lookahead. However, LAC solves the problem completely even
454 # with minimal LR parser tables.
456 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
457 AT_BISON_OPTION_PUSHDEFS
458 AT_DATA_GRAMMAR([[input.y]],
471 'a' problem 'a' // First context.
472 | 'b' problem 'b' // Second context.
473 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
482 // For the state reached after shifting the 'a' in these productions,
483 // lookahead sets are the same in both the first and second contexts.
484 // Thus, canonical LR reuses the same state for both contexts. However,
485 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
486 // error action only in the first context. In order to immediately
487 // detect the syntax error on 'a' here for only the first context, this
488 // canonical LR state would have to be split into two states, and the
489 // 'a' lookahead would have to be removed from only one of the states.
491 'a' // Reduction lookahead set is always ['a', 'b'].
493 | 'a' 'c' // 'c' is forgotten as an expected token.
496 reduce-nonassoc: %prec 'a';
500 ]AT_YYLEX_DEFINE(["aaa"])[
508 AT_BISON_OPTION_POPDEFS
510 # Show canonical LR's failure.
511 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
513 [[input.y: conflicts: 2 shift/reduce
515 AT_COMPILE([[input]])
516 AT_PARSER_CHECK([[./input]], [[1]], [[]],
517 [[syntax error, unexpected 'a', expecting 'b'
520 # It's corrected by LAC.
521 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
522 -o input.c input.y]], [[0]], [[]],
523 [[input.y: conflicts: 2 shift/reduce
525 AT_COMPILE([[input]])
526 AT_PARSER_CHECK([[./input]], [[1]], [[]],
527 [[syntax error, unexpected 'a', expecting 'b' or 'c'
530 # IELR is sufficient when LAC is used.
531 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
533 [[input.y: conflicts: 2 shift/reduce
535 AT_COMPILE([[input]])
536 AT_PARSER_CHECK([[./input]], [[1]], [[]],
537 [[syntax error, unexpected 'a', expecting 'b' or 'c'
542 ## ------------------------- ##
543 ## Unresolved SR Conflicts. ##
544 ## ------------------------- ##
546 AT_SETUP([Unresolved SR Conflicts])
548 AT_KEYWORDS([report])
553 exp: exp OP exp | NUM;
556 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
557 [input.y: conflicts: 1 shift/reduce
560 # Check the contents of the report.
561 AT_CHECK([cat input.output], [],
562 [[State 5 conflicts: 1 shift/reduce
573 Terminals, with rules where they appear
581 Nonterminals, with rules where they appear
586 on left: 1 2, on right: 0 1
591 0 $accept: . exp $end
595 NUM shift, and go to state 1
604 $default reduce using rule 2 (exp)
609 0 $accept: exp . $end
612 $end shift, and go to state 3
613 OP shift, and go to state 4
618 0 $accept: exp $end .
629 NUM shift, and go to state 1
637 1 | exp OP exp . [$end, OP]
639 OP shift, and go to state 4
641 OP [reduce using rule 1 (exp)]
642 $default reduce using rule 1 (exp)
649 ## ----------------------- ##
650 ## Resolved SR Conflicts. ##
651 ## ----------------------- ##
653 AT_SETUP([Resolved SR Conflicts])
655 AT_KEYWORDS([report])
661 exp: exp OP exp | NUM;
664 AT_BISON_CHECK([-o input.c --report=all input.y])
666 # Check the contents of the report.
667 AT_CHECK([cat input.output], [],
676 Terminals, with rules where they appear
684 Nonterminals, with rules where they appear
689 on left: 1 2, on right: 0 1
694 0 $accept: . exp $end
698 NUM shift, and go to state 1
707 $default reduce using rule 2 (exp)
712 0 $accept: exp . $end
715 $end shift, and go to state 3
716 OP shift, and go to state 4
721 0 $accept: exp $end .
732 NUM shift, and go to state 1
740 1 | exp OP exp . [$end, OP]
742 $default reduce using rule 1 (exp)
744 Conflict between rule 1 and token OP resolved as reduce (%left OP).
750 ## -------------------------------- ##
751 ## Defaulted Conflicted Reduction. ##
752 ## -------------------------------- ##
754 # When there are RR conflicts, some rules are disabled. Usually it is
755 # simply displayed as:
757 # $end reduce using rule 3 (num)
758 # $end [reduce using rule 4 (id)]
760 # But when `reduce 3' is the default action, we'd produce:
762 # $end [reduce using rule 4 (id)]
763 # $default reduce using rule 3 (num)
765 # In this precise case (a reduction is masked by the default
766 # reduction), we make the `reduce 3' explicit:
768 # $end reduce using rule 3 (num)
769 # $end [reduce using rule 4 (id)]
770 # $default reduce using rule 3 (num)
772 # Maybe that's not the best display, but then, please propose something
775 AT_SETUP([Defaulted Conflicted Reduction])
776 AT_KEYWORDS([report])
786 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
787 [[input.y: conflicts: 1 reduce/reduce
788 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
791 # Check the contents of the report.
792 AT_CHECK([cat input.output], [],
793 [[Rules useless in parser due to conflicts
798 State 1 conflicts: 1 reduce/reduce
813 Terminals, with rules where they appear
820 Nonterminals, with rules where they appear
825 on left: 1 2, on right: 0
827 on left: 3, on right: 1
829 on left: 4, on right: 2
834 0 $accept: . exp $end
840 '0' shift, and go to state 1
852 $end reduce using rule 3 (num)
853 $end [reduce using rule 4 (id)]
854 $default reduce using rule 3 (num)
859 0 $accept: exp . $end
861 $end shift, and go to state 5
868 $default reduce using rule 1 (exp)
875 $default reduce using rule 2 (exp)
880 0 $accept: exp $end .
890 ## -------------------- ##
891 ## %expect not enough. ##
892 ## -------------------- ##
894 AT_SETUP([%expect not enough])
900 exp: exp OP exp | NUM;
903 AT_BISON_CHECK([-o input.c input.y], 1, [],
904 [input.y: conflicts: 1 shift/reduce
905 input.y: error: expected 0 shift/reduce conflicts
910 ## --------------- ##
912 ## --------------- ##
914 AT_SETUP([%expect right])
920 exp: exp OP exp | NUM;
923 AT_BISON_CHECK([-o input.c input.y])
927 ## ------------------ ##
928 ## %expect too much. ##
929 ## ------------------ ##
931 AT_SETUP([%expect too much])
937 exp: exp OP exp | NUM;
940 AT_BISON_CHECK([-o input.c input.y], 1, [],
941 [input.y: conflicts: 1 shift/reduce
942 input.y: error: expected 2 shift/reduce conflicts
947 ## ------------------------------- ##
948 ## %expect with reduce conflicts. ##
949 ## ------------------------------- ##
951 AT_SETUP([%expect with reduce conflicts])
956 program: a 'a' | a a;
960 AT_BISON_CHECK([-o input.c input.y], 1, [],
961 [input.y: conflicts: 1 reduce/reduce
962 input.y: error: expected 0 reduce/reduce conflicts
967 ## ------------------------- ##
968 ## %prec with user strings. ##
969 ## ------------------------- ##
971 AT_SETUP([%prec with user string])
980 AT_BISON_CHECK([-o input.c input.y])
984 ## -------------------------------- ##
985 ## %no-default-prec without %prec. ##
986 ## -------------------------------- ##
988 AT_SETUP([%no-default-prec without %prec])
1004 AT_BISON_CHECK([-o input.c input.y], 0, [],
1005 [[input.y: conflicts: 4 shift/reduce
1010 ## ----------------------------- ##
1011 ## %no-default-prec with %prec. ##
1012 ## ----------------------------- ##
1014 AT_SETUP([%no-default-prec with %prec])
1016 AT_DATA([[input.y]],
1024 e: e '+' e %prec '+'
1030 AT_BISON_CHECK([-o input.c input.y])
1034 ## --------------- ##
1035 ## %default-prec. ##
1036 ## --------------- ##
1038 AT_SETUP([%default-prec])
1040 AT_DATA([[input.y]],
1054 AT_BISON_CHECK([-o input.c input.y])
1058 ## ---------------------------------------------- ##
1059 ## Unreachable States After Conflict Resolution. ##
1060 ## ---------------------------------------------- ##
1062 AT_SETUP([[Unreachable States After Conflict Resolution]])
1064 # If conflict resolution makes states unreachable, remove those states, report
1065 # rules that are then unused, and don't report conflicts in those states. Test
1066 # what happens when a nonterminal becomes useless as a result of state removal
1067 # since that causes lalr.o's goto map to be rewritten.
1069 AT_DATA([[input.y]],
1075 start: resolved_conflict 'a' reported_conflicts 'a' ;
1077 /* S/R conflict resolved as reduce, so the state with item
1078 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1079 * unreachable, and the associated production is useless. */
1085 /* S/R conflict that need not be reported since it is unreachable because of
1086 * the previous conflict resolution. Nonterminal unreachable1 and all its
1087 * productions are useless. */
1093 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1096 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1097 * when their states are renumbered due to state removal. */
1106 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1107 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1108 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
1109 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
1110 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1111 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1112 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1113 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1114 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1117 AT_CHECK([[cat input.output]], 0,
1118 [[Rules useless in parser due to conflicts
1120 2 resolved_conflict: 'a' unreachable1
1122 4 unreachable1: 'a' unreachable2
1125 6 unreachable2: /* empty */
1128 9 reported_conflicts: 'a'
1132 State 4 conflicts: 1 shift/reduce
1133 State 5 conflicts: 1 reduce/reduce
1138 0 $accept: start $end
1140 1 start: resolved_conflict 'a' reported_conflicts 'a'
1142 2 resolved_conflict: 'a' unreachable1
1145 4 unreachable1: 'a' unreachable2
1148 6 unreachable2: /* empty */
1151 8 reported_conflicts: 'a'
1156 Terminals, with rules where they appear
1163 Nonterminals, with rules where they appear
1168 on left: 1, on right: 0
1169 resolved_conflict (6)
1170 on left: 2 3, on right: 1
1172 on left: 4 5, on right: 2
1174 on left: 6 7, on right: 4
1175 reported_conflicts (9)
1176 on left: 8 9 10, on right: 1
1181 0 $accept: . start $end
1182 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1183 2 resolved_conflict: . 'a' unreachable1
1186 $default reduce using rule 3 (resolved_conflict)
1189 resolved_conflict go to state 2
1191 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1196 0 $accept: start . $end
1198 $end shift, and go to state 3
1203 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1205 'a' shift, and go to state 4
1210 0 $accept: start $end .
1217 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1218 8 reported_conflicts: . 'a'
1222 'a' shift, and go to state 5
1224 'a' [reduce using rule 10 (reported_conflicts)]
1226 reported_conflicts go to state 6
1231 8 reported_conflicts: 'a' . ['a']
1234 'a' reduce using rule 8 (reported_conflicts)
1235 'a' [reduce using rule 9 (reported_conflicts)]
1236 $default reduce using rule 8 (reported_conflicts)
1241 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1243 'a' shift, and go to state 7
1248 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1250 $default reduce using rule 1 (start)
1253 AT_DATA([[input-keep.y]],
1254 [[%define lr.keep-unreachable-states
1256 AT_CHECK([[cat input.y >> input-keep.y]])
1258 AT_BISON_CHECK([[input-keep.y]], 0, [],
1259 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
1260 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1261 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1262 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1263 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1269 ## ------------------------------------------------------------ ##
1270 ## Solved conflicts report for multiple reductions in a state. ##
1271 ## ------------------------------------------------------------ ##
1273 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1275 # Used to lose earlier solved conflict messages even within a single S/R/R.
1277 AT_DATA([[input.y]],
1293 empty_a: %prec 'a' ;
1294 empty_b: %prec 'b' ;
1295 empty_c1: %prec 'c' ;
1296 empty_c2: %prec 'c' ;
1297 empty_c3: %prec 'd' ;
1299 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1300 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1303 0 $accept: . start $end
1316 13 empty_c3: . ['c']
1318 'b' shift, and go to state 1
1320 'c' reduce using rule 13 (empty_c3)
1321 $default reduce using rule 9 (empty_a)
1324 empty_a go to state 3
1325 empty_b go to state 4
1326 empty_c1 go to state 5
1327 empty_c2 go to state 6
1328 empty_c3 go to state 7
1330 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1331 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1332 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1333 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1334 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1343 ## ------------------------------------------------------------ ##
1344 ## %nonassoc error actions for multiple reductions in a state. ##
1345 ## ------------------------------------------------------------ ##
1347 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1348 # multiple reductions in a state.
1350 # For a %nonassoc error action token, used to print the first remaining
1351 # reduction on that token without brackets.
1353 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1355 AT_DATA([[input.y]],
1356 [[%nonassoc 'a' 'b' 'c'
1368 empty_a: %prec 'a' ;
1369 empty_b: %prec 'b' ;
1370 empty_c1: %prec 'c' ;
1371 empty_c2: %prec 'c' ;
1372 empty_c3: %prec 'c' ;
1375 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1376 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1379 0 $accept: . start $end
1391 12 empty_c2: . ['c']
1392 13 empty_c3: . ['c']
1394 'a' error (nonassociative)
1395 'b' error (nonassociative)
1396 'c' error (nonassociative)
1398 'c' [reduce using rule 12 (empty_c2)]
1399 'c' [reduce using rule 13 (empty_c3)]
1402 empty_a go to state 2
1403 empty_b go to state 3
1404 empty_c1 go to state 4
1405 empty_c2 go to state 5
1406 empty_c3 go to state 6
1408 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1409 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1410 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1418 ## --------------------------------- ##
1419 ## -W versus %expect and %expect-rr ##
1420 ## --------------------------------- ##
1422 AT_SETUP([[-W versus %expect and %expect-rr]])
1424 AT_DATA([[sr-rr.y]],
1427 start: 'a' | A 'a' | B 'a' ;
1434 start: 'a' | A 'a' ;
1445 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1446 [[sr-rr.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1448 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1449 [[sr-rr.y: conflicts: 1 reduce/reduce
1451 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1452 [[sr-rr.y: conflicts: 1 shift/reduce
1455 [for gram in sr-rr sr rr; do
1456 for sr_exp_i in '' 0 1 2; do
1457 for rr_exp_i in '' 0 1 2; do
1458 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1460 # Build grammar file.
1465 if test -n "$sr_exp_i"; then
1467 file=$file-expect-$sr_exp
1468 directives="%expect $sr_exp"
1470 if test -n "$rr_exp_i"; then
1472 file=$file-expect-rr-$rr_exp
1473 directives="$directives %expect-rr $rr_exp"
1476 echo "$directives" > $file
1477 cat $gram.y >> $file
1479 # Count actual conflicts.
1483 if test $gram = sr || test $gram = sr-rr; then
1484 conflicts="1 shift/reduce"
1487 if test $gram = rr || test $gram = sr-rr; then
1488 if test -n "$conflicts"; then
1489 conflicts="$conflicts, "
1491 conflicts="${conflicts}1 reduce/reduce"
1496 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1497 ]AT_BISON_CHECK([[-Wnone $file]])[
1498 ]AT_BISON_CHECK([[-Werror $file]])[
1500 echo "$file: conflicts: $conflicts" > experr
1501 if test $sr_count -ne $sr_exp; then
1502 if test $sr_exp -ne 1; then s=s; else s= ; fi
1503 echo "$file: error: expected $sr_exp shift/reduce conflict$s" >> experr
1505 if test $rr_count -ne $rr_exp; then
1506 if test $rr_exp -ne 1; then s=s; else s= ; fi
1507 echo "$file: error: expected $rr_exp reduce/reduce conflict$s" >> experr
1509 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1510 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[