1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2013 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 ## ------------------------ ##
22 ## Token declaration order. ##
23 ## ------------------------ ##
25 # This test checks that token are declared left to right when in a precedence
28 AT_SETUP([Token declaration order])
30 AT_BISON_OPTION_PUSHDEFS
32 AT_DATA_GRAMMAR([[input.y]],
81 AT_FULL_COMPILE([input])
83 AT_PARSER_CHECK([./input])
85 AT_BISON_OPTION_POPDEFS
90 ## ---------------- ##
92 ## ---------------- ##
94 # I once hacked Bison in such a way that it lost its reductions on the
95 # initial state (because it was confusing it with the last state). It
96 # took me a while to strip down my failures to this simple case. So
97 # make sure it finds the s/r conflict below.
99 AT_SETUP([S/R in initial])
105 e: 'e' | /* Nothing. */;
108 AT_BISON_CHECK([-o input.c input.y], 0, [],
109 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */ [-Wother]
112 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
113 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
114 e: 'e' | /* Nothing. */;
121 ## ------------------- ##
122 ## %nonassoc and eof. ##
123 ## ------------------- ##
125 AT_SETUP([%nonassoc and eof])
127 AT_BISON_OPTION_PUSHDEFS
128 AT_DATA_GRAMMAR([input.y],
136 #define YYERROR_VERBOSE 1
138 /* The current argument. */
139 static const char *input;
144 static size_t toknum;
145 assert (toknum <= strlen (input));
146 return input[toknum++];
160 main (int argc, const char *argv[])
162 input = argc <= 1 ? "" : argv[1];
166 AT_BISON_OPTION_POPDEFS
168 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
169 [AT_BISON_CHECK([$1[ -o input.c input.y]])
172 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
174 AT_PARSER_CHECK([./input '0<0'])
175 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
176 [syntax error, unexpected '<'AT_EXPECTING
179 AT_PARSER_CHECK([./input '0>0'])
180 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
181 [syntax error, unexpected '>'AT_EXPECTING
184 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
185 [syntax error, unexpected '>'AT_EXPECTING
188 m4_popdef([AT_EXPECTING])])
190 # Expected token list is missing.
191 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
193 # We must disable default reductions in inconsistent states in order to
194 # have an explicit list of all expected tokens.
195 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
198 # lr.default-reduction=consistent happens to work for this test case.
199 # However, for other grammars, lookahead sets can be merged for
200 # different left contexts, so it is still possible to have an incorrect
201 # expected list. Canonical LR is almost a general solution (that is, it
202 # can fail only when %nonassoc is used), so make sure it gives the same
204 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
206 # parse.lac=full is a completely general solution that does not require
207 # any of the above sacrifices. Of course, it does not extend the
208 # language-recognition power of LALR to (IE)LR, but it does ensure that
209 # the reported list of expected tokens matches what the given parser
210 # would have accepted in place of the unexpected token.
211 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
213 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
219 ## ------------------------------------------- ##
220 ## parse.error=verbose and consistent errors. ##
221 ## ------------------------------------------- ##
223 AT_SETUP([[parse.error=verbose and consistent errors]])
225 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
227 AT_BISON_OPTION_PUSHDEFS([$1])
229 m4_pushdef([AT_YYLEX_PROTOTYPE],
230 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
231 [[int yylex (YYSTYPE *lvalp)]])])
233 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
237 import java.io.IOException;
240 %code {]AT_SKEL_CC_IF([[
241 #include <string>]], [[
244 ]AT_YYERROR_DECLARE])[
245 ]AT_YYLEX_PROTOTYPE[;
249 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
253 %define parse.error verbose
259 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
263 `--------*/]AT_SKEL_JAVA_IF([[
265 public String input = "]$3[";
266 public int index = 0;
269 if (index < input.length ())
270 return input.charAt (index++);
274 public Object getLVal ()
276 return new Integer(1);
281 static char const *input = "]$3[";
297 AT_FULL_COMPILE([[input]])
299 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
300 $5, [a], [[, expecting 'a']],
301 $5, [b], [[, expecting 'b']])])
303 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
304 [AT_PARSER_CHECK([[./input]], [[1]]]),
306 [[syntax error, unexpected ]$4[]AT_EXPECTING[
309 m4_popdef([AT_EXPECTING])
310 m4_popdef([AT_YYLEX_PROTOTYPE])
311 AT_BISON_OPTION_POPDEFS
315 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
318 start: consistent-error-on-a-a 'a' ;
320 consistent-error-on-a-a:
321 'a' default-reduction
322 | 'a' default-reduction 'a'
326 default-reduction: /*empty*/ ;
329 // Provide another context in which all rules are useful so that this
330 // test case looks a little more realistic.
331 start: 'b' consistent-error-on-a-a 'c' ;
334 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
336 # Unfortunately, no expected tokens are reported even though 'b' can be
337 # accepted. Nevertheless, the main point of this test is to make sure
338 # that at least the unexpected token is reported. In a previous version
339 # of Bison, it wasn't reported because the error is detected in a
340 # consistent state with an error action, and that case always triggered
341 # the simple "syntax error" message.
343 # The point isn't to test IELR here, but state merging happens to
344 # complicate this example.
345 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
346 [AT_PREVIOUS_STATE_GRAMMAR],
347 [AT_PREVIOUS_STATE_INPUT],
349 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
351 [AT_PREVIOUS_STATE_GRAMMAR],
352 [AT_PREVIOUS_STATE_INPUT],
354 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
356 [AT_PREVIOUS_STATE_GRAMMAR],
357 [AT_PREVIOUS_STATE_INPUT],
359 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
361 [AT_PREVIOUS_STATE_GRAMMAR],
362 [AT_PREVIOUS_STATE_INPUT],
363 [[end of input]], [[none]])
365 # Even canonical LR doesn't foresee the error for 'a'!
366 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
367 %define lr.default-reduction consistent]],
368 [AT_PREVIOUS_STATE_GRAMMAR],
369 [AT_PREVIOUS_STATE_INPUT],
371 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
372 %define lr.default-reduction accepting]],
373 [AT_PREVIOUS_STATE_GRAMMAR],
374 [AT_PREVIOUS_STATE_INPUT],
376 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
377 [AT_PREVIOUS_STATE_GRAMMAR],
378 [AT_PREVIOUS_STATE_INPUT],
381 # Only LAC gets it right.
382 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
383 %define parse.lac full]],
384 [AT_PREVIOUS_STATE_GRAMMAR],
385 [AT_PREVIOUS_STATE_INPUT],
387 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
388 %define parse.lac full]],
389 [AT_PREVIOUS_STATE_GRAMMAR],
390 [AT_PREVIOUS_STATE_INPUT],
393 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
394 m4_popdef([AT_PREVIOUS_STATE_INPUT])
396 m4_pushdef([AT_USER_ACTION_GRAMMAR],
399 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
400 // incorrectly for the 'b' set in the semantic action below. All 'a'
401 // tokens are returned by yylex, which sets $$ = 1.
404 fprintf (stderr, "Wrong destructor.\n");
407 // Rather than depend on an inconsistent state to induce reading a
408 // lookahead as in the previous grammar, just assign the lookahead in a
409 // semantic action. That lookahead isn't needed before either error
410 // action is encountered. In a previous version of Bison, this was a
411 // problem as it meant yychar was not translated into yytoken before
412 // either error action. The second error action thus invoked a
413 // destructor that it selected according to the incorrect yytoken. The
414 // first error action would have reported an incorrect unexpected token
415 // except that, due to the bug described in the previous grammar, the
416 // unexpected token was not reported at all.
417 start: error-reduce consistent-error 'a' { USE ($][3); } ;
420 'a' 'a' consistent-reduction consistent-error 'a'
421 { USE (($][1, $][2, $][5)); }
426 consistent-reduction: /*empty*/ {
427 assert (yychar == YYEMPTY);
434 | /*empty*/ %prec 'a'
437 // Provide another context in which all rules are useful so that this
438 // test case looks a little more realistic.
439 start: 'b' consistent-error 'b' ;
441 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
443 AT_CONSISTENT_ERRORS_CHECK([[]],
444 [AT_USER_ACTION_GRAMMAR],
445 [AT_USER_ACTION_INPUT],
447 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
448 [AT_USER_ACTION_GRAMMAR],
449 [AT_USER_ACTION_INPUT],
451 # No C++ or Java test because yychar cannot be manipulated by users.
453 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
454 [AT_USER_ACTION_GRAMMAR],
455 [AT_USER_ACTION_INPUT],
458 # Canonical LR doesn't foresee the error for 'a'!
459 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
460 [AT_USER_ACTION_GRAMMAR],
461 [AT_USER_ACTION_INPUT],
463 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
464 [AT_USER_ACTION_GRAMMAR],
465 [AT_USER_ACTION_INPUT],
468 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
469 [AT_USER_ACTION_GRAMMAR],
470 [AT_USER_ACTION_INPUT],
472 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
473 %define lr.default-reduction accepting]],
474 [AT_USER_ACTION_GRAMMAR],
475 [AT_USER_ACTION_INPUT],
478 m4_popdef([AT_USER_ACTION_GRAMMAR])
479 m4_popdef([AT_USER_ACTION_INPUT])
481 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
487 ## ------------------------------------------------------- ##
488 ## LAC: %nonassoc requires splitting canonical LR states. ##
489 ## ------------------------------------------------------- ##
491 # This test case demonstrates that, when %nonassoc is used, canonical
492 # LR(1) parser table construction followed by conflict resolution
493 # without further state splitting is not always sufficient to produce a
494 # parser that can detect all syntax errors as soon as possible on one
495 # token of lookahead. However, LAC solves the problem completely even
496 # with minimal LR parser tables.
498 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
499 AT_BISON_OPTION_PUSHDEFS
500 AT_DATA_GRAMMAR([[input.y]],
513 'a' problem 'a' // First context.
514 | 'b' problem 'b' // Second context.
515 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
524 // For the state reached after shifting the 'a' in these productions,
525 // lookahead sets are the same in both the first and second contexts.
526 // Thus, canonical LR reuses the same state for both contexts. However,
527 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
528 // error action only in the first context. In order to immediately
529 // detect the syntax error on 'a' here for only the first context, this
530 // canonical LR state would have to be split into two states, and the
531 // 'a' lookahead would have to be removed from only one of the states.
533 'a' // Reduction lookahead set is always ['a', 'b'].
535 | 'a' 'c' // 'c' is forgotten as an expected token.
538 reduce-nonassoc: %prec 'a';
542 ]AT_YYLEX_DEFINE(["aaa"])[
545 AT_BISON_OPTION_POPDEFS
547 # Show canonical LR's failure.
548 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
550 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
552 AT_COMPILE([[input]])
553 AT_PARSER_CHECK([[./input]], [[1]], [[]],
554 [[syntax error, unexpected 'a', expecting 'b'
557 # It's corrected by LAC.
558 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
559 -o input.c input.y]], [[0]], [[]],
560 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
562 AT_COMPILE([[input]])
563 AT_PARSER_CHECK([[./input]], [[1]], [[]],
564 [[syntax error, unexpected 'a', expecting 'b' or 'c'
567 # IELR is sufficient when LAC is used.
568 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
570 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
572 AT_COMPILE([[input]])
573 AT_PARSER_CHECK([[./input]], [[1]], [[]],
574 [[syntax error, unexpected 'a', expecting 'b' or 'c'
579 ## ------------------------- ##
580 ## Unresolved SR Conflicts. ##
581 ## ------------------------- ##
583 AT_SETUP([Unresolved SR Conflicts])
585 AT_KEYWORDS([report])
590 exp: exp OP exp | NUM;
593 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
594 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
597 # Check the contents of the report.
598 AT_CHECK([cat input.output], [],
599 [[State 5 conflicts: 1 shift/reduce
610 Terminals, with rules where they appear
618 Nonterminals, with rules where they appear
623 on left: 1 2, on right: 0 1
628 0 $accept: . exp $end
632 NUM shift, and go to state 1
641 $default reduce using rule 2 (exp)
646 0 $accept: exp . $end
649 $end shift, and go to state 3
650 OP shift, and go to state 4
655 0 $accept: exp $end .
666 NUM shift, and go to state 1
674 1 | exp OP exp . [$end, OP]
676 OP shift, and go to state 4
678 OP [reduce using rule 1 (exp)]
679 $default reduce using rule 1 (exp)
686 ## ----------------------- ##
687 ## Resolved SR Conflicts. ##
688 ## ----------------------- ##
690 AT_SETUP([Resolved SR Conflicts])
692 AT_KEYWORDS([report])
698 exp: exp OP exp | NUM;
701 AT_BISON_CHECK([-o input.c --report=all input.y])
703 # Check the contents of the report.
704 AT_CHECK([cat input.output], [],
713 Terminals, with rules where they appear
721 Nonterminals, with rules where they appear
726 on left: 1 2, on right: 0 1
731 0 $accept: . exp $end
735 NUM shift, and go to state 1
744 $default reduce using rule 2 (exp)
749 0 $accept: exp . $end
752 $end shift, and go to state 3
753 OP shift, and go to state 4
758 0 $accept: exp $end .
769 NUM shift, and go to state 1
777 1 | exp OP exp . [$end, OP]
779 $default reduce using rule 1 (exp)
781 Conflict between rule 1 and token OP resolved as reduce (%left OP).
787 ## ---------------------- ##
788 ## %precedence suffices. ##
789 ## ---------------------- ##
791 AT_SETUP([%precedence suffices])
798 "if" cond "then" stmt
799 | "if" cond "then" stmt "else" stmt
808 AT_BISON_CHECK([-o input.c input.y])
813 ## ------------------------------ ##
814 ## %precedence does not suffice. ##
815 ## ------------------------------ ##
817 AT_SETUP([%precedence does not suffice])
824 "if" cond "then" stmt
825 | "if" cond "then" stmt "else" stmt
835 AT_BISON_CHECK([-o input.c input.y], 0, [],
836 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
837 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother]
843 ## -------------------------------- ##
844 ## Defaulted Conflicted Reduction. ##
845 ## -------------------------------- ##
847 # When there are RR conflicts, some rules are disabled. Usually it is
848 # simply displayed as:
850 # $end reduce using rule 3 (num)
851 # $end [reduce using rule 4 (id)]
853 # But when `reduce 3' is the default action, we'd produce:
855 # $end [reduce using rule 4 (id)]
856 # $default reduce using rule 3 (num)
858 # In this precise case (a reduction is masked by the default
859 # reduction), we make the `reduce 3' explicit:
861 # $end reduce using rule 3 (num)
862 # $end [reduce using rule 4 (id)]
863 # $default reduce using rule 3 (num)
865 # Maybe that's not the best display, but then, please propose something
868 AT_SETUP([Defaulted Conflicted Reduction])
869 AT_KEYWORDS([report])
879 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
880 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
881 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother]
884 # Check the contents of the report.
885 AT_CHECK([cat input.output], [],
886 [[Rules useless in parser due to conflicts
891 State 1 conflicts: 1 reduce/reduce
906 Terminals, with rules where they appear
913 Nonterminals, with rules where they appear
918 on left: 1 2, on right: 0
920 on left: 3, on right: 1
922 on left: 4, on right: 2
927 0 $accept: . exp $end
933 '0' shift, and go to state 1
945 $end reduce using rule 3 (num)
946 $end [reduce using rule 4 (id)]
947 $default reduce using rule 3 (num)
952 0 $accept: exp . $end
954 $end shift, and go to state 5
961 $default reduce using rule 1 (exp)
968 $default reduce using rule 2 (exp)
973 0 $accept: exp $end .
983 ## -------------------- ##
984 ## %expect not enough. ##
985 ## -------------------- ##
987 AT_SETUP([%expect not enough])
993 exp: exp OP exp | NUM;
996 AT_BISON_CHECK([-o input.c input.y], 1, [],
997 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1002 ## --------------- ##
1003 ## %expect right. ##
1004 ## --------------- ##
1006 AT_SETUP([%expect right])
1012 exp: exp OP exp | NUM;
1015 AT_BISON_CHECK([-o input.c input.y])
1019 ## ------------------ ##
1020 ## %expect too much. ##
1021 ## ------------------ ##
1023 AT_SETUP([%expect too much])
1029 exp: exp OP exp | NUM;
1032 AT_BISON_CHECK([-o input.c input.y], 1, [],
1033 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1038 ## ------------------------------- ##
1039 ## %expect with reduce conflicts. ##
1040 ## ------------------------------- ##
1042 AT_SETUP([%expect with reduce conflicts])
1047 program: a 'a' | a a;
1051 AT_BISON_CHECK([-o input.c input.y], 1, [],
1052 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1057 ## ------------------------- ##
1058 ## %prec with user strings. ##
1059 ## ------------------------- ##
1061 AT_SETUP([%prec with user string])
1063 AT_DATA([[input.y]],
1070 AT_BISON_CHECK([-o input.c input.y])
1074 ## -------------------------------- ##
1075 ## %no-default-prec without %prec. ##
1076 ## -------------------------------- ##
1078 AT_SETUP([%no-default-prec without %prec])
1080 AT_DATA([[input.y]],
1094 AT_BISON_CHECK([-o input.c input.y], 0, [],
1095 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1100 ## ----------------------------- ##
1101 ## %no-default-prec with %prec. ##
1102 ## ----------------------------- ##
1104 AT_SETUP([%no-default-prec with %prec])
1106 AT_DATA([[input.y]],
1114 e: e '+' e %prec '+'
1120 AT_BISON_CHECK([-o input.c input.y])
1124 ## --------------- ##
1125 ## %default-prec. ##
1126 ## --------------- ##
1128 AT_SETUP([%default-prec])
1130 AT_DATA([[input.y]],
1144 AT_BISON_CHECK([-o input.c input.y])
1148 ## ---------------------------------------------- ##
1149 ## Unreachable States After Conflict Resolution. ##
1150 ## ---------------------------------------------- ##
1152 AT_SETUP([[Unreachable States After Conflict Resolution]])
1154 # If conflict resolution makes states unreachable, remove those states, report
1155 # rules that are then unused, and don't report conflicts in those states. Test
1156 # what happens when a nonterminal becomes useless as a result of state removal
1157 # since that causes lalr.o's goto map to be rewritten.
1159 AT_DATA([[input.y]],
1165 start: resolved_conflict 'a' reported_conflicts 'a' ;
1167 /* S/R conflict resolved as reduce, so the state with item
1168 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1169 * unreachable, and the associated production is useless. */
1175 /* S/R conflict that need not be reported since it is unreachable because of
1176 * the previous conflict resolution. Nonterminal unreachable1 and all its
1177 * productions are useless. */
1183 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1186 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1187 * when their states are renumbered due to state removal. */
1196 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1197 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1198 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1199 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother]
1200 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother]
1201 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1202 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1203 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1204 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1205 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1208 AT_CHECK([[cat input.output]], 0,
1209 [[Rules useless in parser due to conflicts
1211 2 resolved_conflict: 'a' unreachable1
1213 4 unreachable1: 'a' unreachable2
1216 6 unreachable2: /* empty */
1219 9 reported_conflicts: 'a'
1223 State 4 conflicts: 1 shift/reduce
1224 State 5 conflicts: 1 reduce/reduce
1229 0 $accept: start $end
1231 1 start: resolved_conflict 'a' reported_conflicts 'a'
1233 2 resolved_conflict: 'a' unreachable1
1236 4 unreachable1: 'a' unreachable2
1239 6 unreachable2: /* empty */
1242 8 reported_conflicts: 'a'
1247 Terminals, with rules where they appear
1254 Nonterminals, with rules where they appear
1259 on left: 1, on right: 0
1260 resolved_conflict (6)
1261 on left: 2 3, on right: 1
1263 on left: 4 5, on right: 2
1265 on left: 6 7, on right: 4
1266 reported_conflicts (9)
1267 on left: 8 9 10, on right: 1
1272 0 $accept: . start $end
1273 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1274 2 resolved_conflict: . 'a' unreachable1
1277 $default reduce using rule 3 (resolved_conflict)
1280 resolved_conflict go to state 2
1282 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1287 0 $accept: start . $end
1289 $end shift, and go to state 3
1294 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1296 'a' shift, and go to state 4
1301 0 $accept: start $end .
1308 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1309 8 reported_conflicts: . 'a'
1313 'a' shift, and go to state 5
1315 'a' [reduce using rule 10 (reported_conflicts)]
1317 reported_conflicts go to state 6
1322 8 reported_conflicts: 'a' . ['a']
1325 'a' reduce using rule 8 (reported_conflicts)
1326 'a' [reduce using rule 9 (reported_conflicts)]
1327 $default reduce using rule 8 (reported_conflicts)
1332 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1334 'a' shift, and go to state 7
1339 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1341 $default reduce using rule 1 (start)
1344 AT_DATA([[input-keep.y]],
1345 [[%define lr.keep-unreachable-state
1347 AT_CHECK([[cat input.y >> input-keep.y]])
1349 AT_BISON_CHECK([[input-keep.y]], 0, [],
1350 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1351 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1352 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1353 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1354 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1355 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1361 ## ------------------------------------------------------------ ##
1362 ## Solved conflicts report for multiple reductions in a state. ##
1363 ## ------------------------------------------------------------ ##
1365 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1367 # Used to lose earlier solved conflict messages even within a single S/R/R.
1369 AT_DATA([[input.y]],
1385 empty_a: %prec 'a' ;
1386 empty_b: %prec 'b' ;
1387 empty_c1: %prec 'c' ;
1388 empty_c2: %prec 'c' ;
1389 empty_c3: %prec 'd' ;
1391 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1392 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1395 0 $accept: . start $end
1408 13 empty_c3: . ['c']
1410 'b' shift, and go to state 1
1412 'c' reduce using rule 13 (empty_c3)
1413 $default reduce using rule 9 (empty_a)
1416 empty_a go to state 3
1417 empty_b go to state 4
1418 empty_c1 go to state 5
1419 empty_c2 go to state 6
1420 empty_c3 go to state 7
1422 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1423 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1424 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1425 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1426 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1435 ## ------------------------------------------------------------ ##
1436 ## %nonassoc error actions for multiple reductions in a state. ##
1437 ## ------------------------------------------------------------ ##
1439 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1440 # multiple reductions in a state.
1442 # For a %nonassoc error action token, used to print the first remaining
1443 # reduction on that token without brackets.
1445 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1447 AT_DATA([[input.y]],
1448 [[%nonassoc 'a' 'b' 'c'
1460 empty_a: %prec 'a' ;
1461 empty_b: %prec 'b' ;
1462 empty_c1: %prec 'c' ;
1463 empty_c2: %prec 'c' ;
1464 empty_c3: %prec 'c' ;
1467 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1468 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1471 0 $accept: . start $end
1483 12 empty_c2: . ['c']
1484 13 empty_c3: . ['c']
1486 'a' error (nonassociative)
1487 'b' error (nonassociative)
1488 'c' error (nonassociative)
1490 'c' [reduce using rule 12 (empty_c2)]
1491 'c' [reduce using rule 13 (empty_c3)]
1494 empty_a go to state 2
1495 empty_b go to state 3
1496 empty_c1 go to state 4
1497 empty_c2 go to state 5
1498 empty_c3 go to state 6
1500 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1501 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1502 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1510 ## -------------------- ##
1511 ## %expect-rr non GLR. ##
1512 ## -------------------- ##
1514 AT_SETUP([[%expect-rr non GLR]])
1522 AT_BISON_CHECK([[1.y]], [[0]], [],
1523 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1532 AT_BISON_CHECK([[2.y]], [[0]], [],
1533 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1534 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1535 2.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother]
1541 ## ---------------------------------- ##
1542 ## -W versus %expect and %expect-rr. ##
1543 ## ---------------------------------- ##
1545 AT_SETUP([[-W versus %expect and %expect-rr]])
1547 AT_DATA([[sr-rr.y]],
1550 start: 'a' | A 'a' | B 'a' ;
1557 start: 'a' | A 'a' ;
1568 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1569 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1570 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1572 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1573 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1575 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1576 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1580 # This is piece of code is rather complex for a simple task: try every
1581 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1582 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1585 # Number and types of genuine conflicts in the grammar.
1586 for gram in sr-rr sr rr; do
1587 # Number of expected s/r conflicts.
1588 for sr_exp_i in '' 0 1 2; do
1589 # Number of expected r/r conflicts.
1590 for rr_exp_i in '' 0 1 2; do
1591 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1593 # Build grammar file.
1598 if test -n "$sr_exp_i"; then
1600 file=$file-expect-$sr_exp
1601 directives="%expect $sr_exp"
1603 if test -n "$rr_exp_i"; then
1605 file=$file-expect-rr-$rr_exp
1606 directives="$directives %expect-rr $rr_exp"
1609 echo "$directives" > $file
1610 cat $gram.y >> $file
1612 # Number of found conflicts.
1614 (sr) sr_count=1; rr_count=0;;
1615 (rr) sr_count=0; rr_count=1;;
1616 (sr-rr) sr_count=1; rr_count=1;;
1619 # Update number of expected conflicts: if %expect is given then
1620 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1622 case $sr_exp_i:$rr_exp_i in
1628 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1629 ]AT_BISON_CHECK([[-Wnone $file]])[
1630 ]AT_BISON_CHECK([[-Werror $file]])[
1633 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1634 echo "warning: $sr_count shift/reduce conflicts"
1635 elif test "$sr_exp_i" -ne "$sr_count"; then
1636 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1638 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1639 echo "warning: $rr_count reduce/reduce conflicts"
1640 elif test "$rr_exp_i" -ne "$rr_count"; then
1641 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1643 } | sed -e "s/^/$file: /" > experr
1644 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1645 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[