1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2012 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 AT_BANNER([[Conflicts.]])
21 ## ---------------- ##
23 ## ---------------- ##
25 # I once hacked Bison in such a way that it lost its reductions on the
26 # initial state (because it was confusing it with the last state). It
27 # took me a while to strip down my failures to this simple case. So
28 # make sure it finds the s/r conflict below.
30 AT_SETUP([S/R in initial])
36 e: 'e' | /* Nothing. */;
39 AT_BISON_CHECK([-o input.c input.y], 0, [],
40 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */ [-Wother]
43 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
44 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
45 e: 'e' | /* Nothing. */;
52 ## ------------------- ##
53 ## %nonassoc and eof. ##
54 ## ------------------- ##
56 AT_SETUP([%nonassoc and eof])
58 AT_BISON_OPTION_PUSHDEFS
59 AT_DATA_GRAMMAR([input.y],
67 #define YYERROR_VERBOSE 1
69 /* The current argument. */
70 static const char *input;
76 assert (toknum <= strlen (input));
77 return input[toknum++];
91 main (int argc, const char *argv[])
93 input = argc <= 1 ? "" : argv[1];
97 AT_BISON_OPTION_POPDEFS
99 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
100 [AT_BISON_CHECK([$1[ -o input.c input.y]])
103 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
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'])
111 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
112 [syntax error, unexpected '>'AT_EXPECTING
115 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
116 [syntax error, unexpected '>'AT_EXPECTING
119 m4_popdef([AT_EXPECTING])])
121 # Expected token list is missing.
122 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
124 # We must disable default reductions in inconsistent states in order to
125 # have an explicit list of all expected tokens.
126 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
129 # lr.default-reduction=consistent happens to work for this test case.
130 # However, for other grammars, lookahead sets can be merged for
131 # different left contexts, so it is still possible to have an incorrect
132 # expected list. Canonical LR is almost a general solution (that is, it
133 # can fail only when %nonassoc is used), so make sure it gives the same
135 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
137 # parse.lac=full is a completely general solution that does not require
138 # any of the above sacrifices. Of course, it does not extend the
139 # language-recognition power of LALR to (IE)LR, but it does ensure that
140 # the reported list of expected tokens matches what the given parser
141 # would have accepted in place of the unexpected token.
142 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
144 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
150 ## ------------------------------------------- ##
151 ## parse.error=verbose and consistent errors. ##
152 ## ------------------------------------------- ##
154 AT_SETUP([[parse.error=verbose and consistent errors]])
156 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
158 AT_BISON_OPTION_PUSHDEFS([$1])
160 m4_pushdef([AT_YYLEX_PROTOTYPE],
161 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
162 [[int yylex (YYSTYPE *lvalp)]])])
164 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
168 import java.io.IOException;
171 %code {]AT_SKEL_CC_IF([[
172 #include <string>]], [[
175 ]AT_YYERROR_DECLARE])[
176 ]AT_YYLEX_PROTOTYPE[;
180 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
184 %define parse.error verbose
190 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
194 `--------*/]AT_SKEL_JAVA_IF([[
196 public String input = "]$3[";
197 public int index = 0;
200 if (index < input.length ())
201 return input.charAt (index++);
205 public Object getLVal ()
207 return new Integer(1);
212 static char const *input = "]$3[";
224 `-------*/]AT_SKEL_JAVA_IF([[
228 public static void main (String args[]) throws IOException
230 YYParser p = new YYParser ();
233 }]], [AT_SKEL_CC_IF([[
239 return parser.parse ();
249 AT_FULL_COMPILE([[input]])
251 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
252 $5, [a], [[, expecting 'a']],
253 $5, [b], [[, expecting 'b']])])
255 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
256 [AT_PARSER_CHECK([[./input]], [[1]]]),
258 [[syntax error, unexpected ]$4[]AT_EXPECTING[
261 m4_popdef([AT_EXPECTING])
262 m4_popdef([AT_YYLEX_PROTOTYPE])
263 AT_BISON_OPTION_POPDEFS
267 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
270 start: consistent-error-on-a-a 'a' ;
272 consistent-error-on-a-a:
273 'a' default-reduction
274 | 'a' default-reduction 'a'
278 default-reduction: /*empty*/ ;
281 // Provide another context in which all rules are useful so that this
282 // test case looks a little more realistic.
283 start: 'b' consistent-error-on-a-a 'c' ;
286 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
288 # Unfortunately, no expected tokens are reported even though 'b' can be
289 # accepted. Nevertheless, the main point of this test is to make sure
290 # that at least the unexpected token is reported. In a previous version
291 # of Bison, it wasn't reported because the error is detected in a
292 # consistent state with an error action, and that case always triggered
293 # the simple "syntax error" message.
295 # The point isn't to test IELR here, but state merging happens to
296 # complicate this example.
297 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
298 [AT_PREVIOUS_STATE_GRAMMAR],
299 [AT_PREVIOUS_STATE_INPUT],
301 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
303 [AT_PREVIOUS_STATE_GRAMMAR],
304 [AT_PREVIOUS_STATE_INPUT],
306 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
308 [AT_PREVIOUS_STATE_GRAMMAR],
309 [AT_PREVIOUS_STATE_INPUT],
311 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
313 [AT_PREVIOUS_STATE_GRAMMAR],
314 [AT_PREVIOUS_STATE_INPUT],
315 [[end of input]], [[none]])
317 # Even canonical LR doesn't foresee the error for 'a'!
318 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
319 %define lr.default-reduction consistent]],
320 [AT_PREVIOUS_STATE_GRAMMAR],
321 [AT_PREVIOUS_STATE_INPUT],
323 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
324 %define lr.default-reduction accepting]],
325 [AT_PREVIOUS_STATE_GRAMMAR],
326 [AT_PREVIOUS_STATE_INPUT],
328 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
329 [AT_PREVIOUS_STATE_GRAMMAR],
330 [AT_PREVIOUS_STATE_INPUT],
333 # Only LAC gets it right.
334 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
335 %define parse.lac full]],
336 [AT_PREVIOUS_STATE_GRAMMAR],
337 [AT_PREVIOUS_STATE_INPUT],
339 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
340 %define parse.lac full]],
341 [AT_PREVIOUS_STATE_GRAMMAR],
342 [AT_PREVIOUS_STATE_INPUT],
345 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
346 m4_popdef([AT_PREVIOUS_STATE_INPUT])
348 m4_pushdef([AT_USER_ACTION_GRAMMAR],
351 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
352 // incorrectly for the 'b' set in the semantic action below. All 'a'
353 // tokens are returned by yylex, which sets $$ = 1.
356 fprintf (stderr, "Wrong destructor.\n");
359 // Rather than depend on an inconsistent state to induce reading a
360 // lookahead as in the previous grammar, just assign the lookahead in a
361 // semantic action. That lookahead isn't needed before either error
362 // action is encountered. In a previous version of Bison, this was a
363 // problem as it meant yychar was not translated into yytoken before
364 // either error action. The second error action thus invoked a
365 // destructor that it selected according to the incorrect yytoken. The
366 // first error action would have reported an incorrect unexpected token
367 // except that, due to the bug described in the previous grammar, the
368 // unexpected token was not reported at all.
369 start: error-reduce consistent-error 'a' { USE ($][3); } ;
372 'a' 'a' consistent-reduction consistent-error 'a'
373 { USE (($][1, $][2, $][5)); }
378 consistent-reduction: /*empty*/ {
379 assert (yychar == YYEMPTY);
386 | /*empty*/ %prec 'a'
389 // Provide another context in which all rules are useful so that this
390 // test case looks a little more realistic.
391 start: 'b' consistent-error 'b' ;
393 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
395 AT_CONSISTENT_ERRORS_CHECK([[]],
396 [AT_USER_ACTION_GRAMMAR],
397 [AT_USER_ACTION_INPUT],
399 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
400 [AT_USER_ACTION_GRAMMAR],
401 [AT_USER_ACTION_INPUT],
403 # No C++ or Java test because yychar cannot be manipulated by users.
405 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction 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-reduction 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-reduction 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: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
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: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
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: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
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: warning: 1 shift/reduce conflict [-Wconflicts-sr]
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 ## %precedence suffices. ##
746 ## ---------------------- ##
748 AT_SETUP([%precedence suffices])
755 "if" cond "then" stmt
756 | "if" cond "then" stmt "else" stmt
765 AT_BISON_CHECK([-o input.c input.y])
770 ## ------------------------------ ##
771 ## %precedence does not suffice. ##
772 ## ------------------------------ ##
774 AT_SETUP([%precedence does not suffice])
781 "if" cond "then" stmt
782 | "if" cond "then" stmt "else" stmt
792 AT_BISON_CHECK([-o input.c input.y], 0, [],
793 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
794 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother]
800 ## -------------------------------- ##
801 ## Defaulted Conflicted Reduction. ##
802 ## -------------------------------- ##
804 # When there are RR conflicts, some rules are disabled. Usually it is
805 # simply displayed as:
807 # $end reduce using rule 3 (num)
808 # $end [reduce using rule 4 (id)]
810 # But when `reduce 3' is the default action, we'd produce:
812 # $end [reduce using rule 4 (id)]
813 # $default reduce using rule 3 (num)
815 # In this precise case (a reduction is masked by the default
816 # reduction), we make the `reduce 3' explicit:
818 # $end reduce using rule 3 (num)
819 # $end [reduce using rule 4 (id)]
820 # $default reduce using rule 3 (num)
822 # Maybe that's not the best display, but then, please propose something
825 AT_SETUP([Defaulted Conflicted Reduction])
826 AT_KEYWORDS([report])
836 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
837 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
838 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother]
841 # Check the contents of the report.
842 AT_CHECK([cat input.output], [],
843 [[Rules useless in parser due to conflicts
848 State 1 conflicts: 1 reduce/reduce
863 Terminals, with rules where they appear
870 Nonterminals, with rules where they appear
875 on left: 1 2, on right: 0
877 on left: 3, on right: 1
879 on left: 4, on right: 2
884 0 $accept: . exp $end
890 '0' shift, and go to state 1
902 $end reduce using rule 3 (num)
903 $end [reduce using rule 4 (id)]
904 $default reduce using rule 3 (num)
909 0 $accept: exp . $end
911 $end shift, and go to state 5
918 $default reduce using rule 1 (exp)
925 $default reduce using rule 2 (exp)
930 0 $accept: exp $end .
940 ## -------------------- ##
941 ## %expect not enough. ##
942 ## -------------------- ##
944 AT_SETUP([%expect not enough])
950 exp: exp OP exp | NUM;
953 AT_BISON_CHECK([-o input.c input.y], 1, [],
954 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
959 ## --------------- ##
961 ## --------------- ##
963 AT_SETUP([%expect right])
969 exp: exp OP exp | NUM;
972 AT_BISON_CHECK([-o input.c input.y])
976 ## ------------------ ##
977 ## %expect too much. ##
978 ## ------------------ ##
980 AT_SETUP([%expect too much])
986 exp: exp OP exp | NUM;
989 AT_BISON_CHECK([-o input.c input.y], 1, [],
990 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
995 ## ------------------------------- ##
996 ## %expect with reduce conflicts. ##
997 ## ------------------------------- ##
999 AT_SETUP([%expect with reduce conflicts])
1004 program: a 'a' | a a;
1008 AT_BISON_CHECK([-o input.c input.y], 1, [],
1009 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1014 ## ------------------------- ##
1015 ## %prec with user strings. ##
1016 ## ------------------------- ##
1018 AT_SETUP([%prec with user string])
1020 AT_DATA([[input.y]],
1027 AT_BISON_CHECK([-o input.c input.y])
1031 ## -------------------------------- ##
1032 ## %no-default-prec without %prec. ##
1033 ## -------------------------------- ##
1035 AT_SETUP([%no-default-prec without %prec])
1037 AT_DATA([[input.y]],
1051 AT_BISON_CHECK([-o input.c input.y], 0, [],
1052 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1057 ## ----------------------------- ##
1058 ## %no-default-prec with %prec. ##
1059 ## ----------------------------- ##
1061 AT_SETUP([%no-default-prec with %prec])
1063 AT_DATA([[input.y]],
1071 e: e '+' e %prec '+'
1077 AT_BISON_CHECK([-o input.c input.y])
1081 ## --------------- ##
1082 ## %default-prec. ##
1083 ## --------------- ##
1085 AT_SETUP([%default-prec])
1087 AT_DATA([[input.y]],
1101 AT_BISON_CHECK([-o input.c input.y])
1105 ## ---------------------------------------------- ##
1106 ## Unreachable States After Conflict Resolution. ##
1107 ## ---------------------------------------------- ##
1109 AT_SETUP([[Unreachable States After Conflict Resolution]])
1111 # If conflict resolution makes states unreachable, remove those states, report
1112 # rules that are then unused, and don't report conflicts in those states. Test
1113 # what happens when a nonterminal becomes useless as a result of state removal
1114 # since that causes lalr.o's goto map to be rewritten.
1116 AT_DATA([[input.y]],
1122 start: resolved_conflict 'a' reported_conflicts 'a' ;
1124 /* S/R conflict resolved as reduce, so the state with item
1125 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1126 * unreachable, and the associated production is useless. */
1132 /* S/R conflict that need not be reported since it is unreachable because of
1133 * the previous conflict resolution. Nonterminal unreachable1 and all its
1134 * productions are useless. */
1140 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1143 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1144 * when their states are renumbered due to state removal. */
1153 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1154 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1155 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1156 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother]
1157 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother]
1158 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1159 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1160 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1161 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1162 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1165 AT_CHECK([[cat input.output]], 0,
1166 [[Rules useless in parser due to conflicts
1168 2 resolved_conflict: 'a' unreachable1
1170 4 unreachable1: 'a' unreachable2
1173 6 unreachable2: /* empty */
1176 9 reported_conflicts: 'a'
1180 State 4 conflicts: 1 shift/reduce
1181 State 5 conflicts: 1 reduce/reduce
1186 0 $accept: start $end
1188 1 start: resolved_conflict 'a' reported_conflicts 'a'
1190 2 resolved_conflict: 'a' unreachable1
1193 4 unreachable1: 'a' unreachable2
1196 6 unreachable2: /* empty */
1199 8 reported_conflicts: 'a'
1204 Terminals, with rules where they appear
1211 Nonterminals, with rules where they appear
1216 on left: 1, on right: 0
1217 resolved_conflict (6)
1218 on left: 2 3, on right: 1
1220 on left: 4 5, on right: 2
1222 on left: 6 7, on right: 4
1223 reported_conflicts (9)
1224 on left: 8 9 10, on right: 1
1229 0 $accept: . start $end
1230 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1231 2 resolved_conflict: . 'a' unreachable1
1234 $default reduce using rule 3 (resolved_conflict)
1237 resolved_conflict go to state 2
1239 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1244 0 $accept: start . $end
1246 $end shift, and go to state 3
1251 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1253 'a' shift, and go to state 4
1258 0 $accept: start $end .
1265 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1266 8 reported_conflicts: . 'a'
1270 'a' shift, and go to state 5
1272 'a' [reduce using rule 10 (reported_conflicts)]
1274 reported_conflicts go to state 6
1279 8 reported_conflicts: 'a' . ['a']
1282 'a' reduce using rule 8 (reported_conflicts)
1283 'a' [reduce using rule 9 (reported_conflicts)]
1284 $default reduce using rule 8 (reported_conflicts)
1289 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1291 'a' shift, and go to state 7
1296 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1298 $default reduce using rule 1 (start)
1301 AT_DATA([[input-keep.y]],
1302 [[%define lr.keep-unreachable-state
1304 AT_CHECK([[cat input.y >> input-keep.y]])
1306 AT_BISON_CHECK([[input-keep.y]], 0, [],
1307 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1308 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1309 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1310 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1311 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1312 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1318 ## ------------------------------------------------------------ ##
1319 ## Solved conflicts report for multiple reductions in a state. ##
1320 ## ------------------------------------------------------------ ##
1322 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1324 # Used to lose earlier solved conflict messages even within a single S/R/R.
1326 AT_DATA([[input.y]],
1342 empty_a: %prec 'a' ;
1343 empty_b: %prec 'b' ;
1344 empty_c1: %prec 'c' ;
1345 empty_c2: %prec 'c' ;
1346 empty_c3: %prec 'd' ;
1348 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1349 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1352 0 $accept: . start $end
1365 13 empty_c3: . ['c']
1367 'b' shift, and go to state 1
1369 'c' reduce using rule 13 (empty_c3)
1370 $default reduce using rule 9 (empty_a)
1373 empty_a go to state 3
1374 empty_b go to state 4
1375 empty_c1 go to state 5
1376 empty_c2 go to state 6
1377 empty_c3 go to state 7
1379 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1380 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1381 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1382 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1383 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1392 ## ------------------------------------------------------------ ##
1393 ## %nonassoc error actions for multiple reductions in a state. ##
1394 ## ------------------------------------------------------------ ##
1396 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1397 # multiple reductions in a state.
1399 # For a %nonassoc error action token, used to print the first remaining
1400 # reduction on that token without brackets.
1402 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1404 AT_DATA([[input.y]],
1405 [[%nonassoc 'a' 'b' 'c'
1417 empty_a: %prec 'a' ;
1418 empty_b: %prec 'b' ;
1419 empty_c1: %prec 'c' ;
1420 empty_c2: %prec 'c' ;
1421 empty_c3: %prec 'c' ;
1424 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1425 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1428 0 $accept: . start $end
1440 12 empty_c2: . ['c']
1441 13 empty_c3: . ['c']
1443 'a' error (nonassociative)
1444 'b' error (nonassociative)
1445 'c' error (nonassociative)
1447 'c' [reduce using rule 12 (empty_c2)]
1448 'c' [reduce using rule 13 (empty_c3)]
1451 empty_a go to state 2
1452 empty_b go to state 3
1453 empty_c1 go to state 4
1454 empty_c2 go to state 5
1455 empty_c3 go to state 6
1457 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1458 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1459 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1467 ## -------------------- ##
1468 ## %expect-rr non GLR. ##
1469 ## -------------------- ##
1471 AT_SETUP([[%expect-rr non GLR]])
1479 AT_BISON_CHECK([[1.y]], [[0]], [],
1480 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1489 AT_BISON_CHECK([[2.y]], [[0]], [],
1490 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1491 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1492 2.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother]
1498 ## ---------------------------------- ##
1499 ## -W versus %expect and %expect-rr. ##
1500 ## ---------------------------------- ##
1502 AT_SETUP([[-W versus %expect and %expect-rr]])
1504 AT_DATA([[sr-rr.y]],
1507 start: 'a' | A 'a' | B 'a' ;
1514 start: 'a' | A 'a' ;
1525 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1526 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1527 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1529 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1530 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1532 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1533 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1537 # This is piece of code is rather complex for a simple task: try every
1538 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1539 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1542 # Number and types of genuine conflicts in the grammar.
1543 for gram in sr-rr sr rr; do
1544 # Number of expected s/r conflicts.
1545 for sr_exp_i in '' 0 1 2; do
1546 # Number of expected r/r conflicts.
1547 for rr_exp_i in '' 0 1 2; do
1548 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1550 # Build grammar file.
1555 if test -n "$sr_exp_i"; then
1557 file=$file-expect-$sr_exp
1558 directives="%expect $sr_exp"
1560 if test -n "$rr_exp_i"; then
1562 file=$file-expect-rr-$rr_exp
1563 directives="$directives %expect-rr $rr_exp"
1566 echo "$directives" > $file
1567 cat $gram.y >> $file
1569 # Number of found conflicts.
1571 (sr) sr_count=1; rr_count=0;;
1572 (rr) sr_count=0; rr_count=1;;
1573 (sr-rr) sr_count=1; rr_count=1;;
1576 # Update number of expected conflicts: if %expect is given then
1577 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1579 case $sr_exp_i:$rr_exp_i in
1585 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1586 ]AT_BISON_CHECK([[-Wnone $file]])[
1587 ]AT_BISON_CHECK([[-Werror $file]])[
1590 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1591 echo "warning: $sr_count shift/reduce conflicts"
1592 elif test "$sr_exp_i" -ne "$sr_count"; then
1593 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1595 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1596 echo "warning: $rr_count reduce/reduce conflicts"
1597 elif test "$rr_exp_i" -ne "$rr_count"; then
1598 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1600 } | sed -e "s/^/$file: /" > experr
1601 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1602 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[