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]],
80 AT_FULL_COMPILE([input])
84 AT_BISON_OPTION_POPDEFS
89 ## ---------------- ##
91 ## ---------------- ##
93 # I once hacked Bison in such a way that it lost its reductions on the
94 # initial state (because it was confusing it with the last state). It
95 # took me a while to strip down my failures to this simple case. So
96 # make sure it finds the s/r conflict below.
98 AT_SETUP([S/R in initial])
104 e: 'e' | /* Nothing. */;
107 AT_BISON_CHECK([-o input.c input.y], 0, [],
108 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */ [-Wother]
111 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
112 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
113 e: 'e' | /* Nothing. */;
120 ## ------------------- ##
121 ## %nonassoc and eof. ##
122 ## ------------------- ##
124 AT_SETUP([%nonassoc and eof])
126 AT_BISON_OPTION_PUSHDEFS
127 AT_DATA_GRAMMAR([input.y],
135 #define YYERROR_VERBOSE 1
137 /* The current argument. */
138 static const char *input;
143 static size_t toknum;
144 assert (toknum <= strlen (input));
145 return input[toknum++];
159 main (int argc, const char *argv[])
161 input = argc <= 1 ? "" : argv[1];
165 AT_BISON_OPTION_POPDEFS
167 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
168 [AT_BISON_CHECK([$1[ -o input.c input.y]])
171 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
173 AT_PARSER_CHECK([./input '0<0'])
174 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
175 [syntax error, unexpected '<'AT_EXPECTING
178 AT_PARSER_CHECK([./input '0>0'])
179 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
180 [syntax error, unexpected '>'AT_EXPECTING
183 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
184 [syntax error, unexpected '>'AT_EXPECTING
187 m4_popdef([AT_EXPECTING])])
189 # Expected token list is missing.
190 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
192 # We must disable default reductions in inconsistent states in order to
193 # have an explicit list of all expected tokens.
194 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
197 # lr.default-reduction=consistent happens to work for this test case.
198 # However, for other grammars, lookahead sets can be merged for
199 # different left contexts, so it is still possible to have an incorrect
200 # expected list. Canonical LR is almost a general solution (that is, it
201 # can fail only when %nonassoc is used), so make sure it gives the same
203 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
205 # parse.lac=full is a completely general solution that does not require
206 # any of the above sacrifices. Of course, it does not extend the
207 # language-recognition power of LALR to (IE)LR, but it does ensure that
208 # the reported list of expected tokens matches what the given parser
209 # would have accepted in place of the unexpected token.
210 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
212 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
218 ## ------------------------------------------- ##
219 ## parse.error=verbose and consistent errors. ##
220 ## ------------------------------------------- ##
222 AT_SETUP([[parse.error=verbose and consistent errors]])
224 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
226 AT_BISON_OPTION_PUSHDEFS([$1])
228 m4_pushdef([AT_YYLEX_PROTOTYPE],
229 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
230 [[int yylex (YYSTYPE *lvalp)]])])
232 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
236 import java.io.IOException;
239 %code {]AT_SKEL_CC_IF([[
240 #include <string>]], [[
243 ]AT_YYERROR_DECLARE])[
244 ]AT_YYLEX_PROTOTYPE[;
248 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
252 %define parse.error verbose
258 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
262 `--------*/]AT_SKEL_JAVA_IF([[
264 public String input = "]$3[";
265 public int index = 0;
268 if (index < input.length ())
269 return input.charAt (index++);
273 public Object getLVal ()
275 return new Integer(1);
280 static char const *input = "]$3[";
296 AT_FULL_COMPILE([[input]])
298 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
299 $5, [a], [[, expecting 'a']],
300 $5, [b], [[, expecting 'b']])])
302 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
303 [AT_PARSER_CHECK([[./input]], [[1]]]),
305 [[syntax error, unexpected ]$4[]AT_EXPECTING[
308 m4_popdef([AT_EXPECTING])
309 m4_popdef([AT_YYLEX_PROTOTYPE])
310 AT_BISON_OPTION_POPDEFS
314 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
317 start: consistent-error-on-a-a 'a' ;
319 consistent-error-on-a-a:
320 'a' default-reduction
321 | 'a' default-reduction 'a'
325 default-reduction: /*empty*/ ;
328 // Provide another context in which all rules are useful so that this
329 // test case looks a little more realistic.
330 start: 'b' consistent-error-on-a-a 'c' ;
333 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
335 # Unfortunately, no expected tokens are reported even though 'b' can be
336 # accepted. Nevertheless, the main point of this test is to make sure
337 # that at least the unexpected token is reported. In a previous version
338 # of Bison, it wasn't reported because the error is detected in a
339 # consistent state with an error action, and that case always triggered
340 # the simple "syntax error" message.
342 # The point isn't to test IELR here, but state merging happens to
343 # complicate this example.
344 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
345 [AT_PREVIOUS_STATE_GRAMMAR],
346 [AT_PREVIOUS_STATE_INPUT],
348 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
350 [AT_PREVIOUS_STATE_GRAMMAR],
351 [AT_PREVIOUS_STATE_INPUT],
353 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
355 [AT_PREVIOUS_STATE_GRAMMAR],
356 [AT_PREVIOUS_STATE_INPUT],
358 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
360 [AT_PREVIOUS_STATE_GRAMMAR],
361 [AT_PREVIOUS_STATE_INPUT],
362 [[end of input]], [[none]])
364 # Even canonical LR doesn't foresee the error for 'a'!
365 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
366 %define lr.default-reduction consistent]],
367 [AT_PREVIOUS_STATE_GRAMMAR],
368 [AT_PREVIOUS_STATE_INPUT],
370 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
371 %define lr.default-reduction accepting]],
372 [AT_PREVIOUS_STATE_GRAMMAR],
373 [AT_PREVIOUS_STATE_INPUT],
375 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
376 [AT_PREVIOUS_STATE_GRAMMAR],
377 [AT_PREVIOUS_STATE_INPUT],
380 # Only LAC gets it right.
381 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
382 %define parse.lac full]],
383 [AT_PREVIOUS_STATE_GRAMMAR],
384 [AT_PREVIOUS_STATE_INPUT],
386 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
387 %define parse.lac full]],
388 [AT_PREVIOUS_STATE_GRAMMAR],
389 [AT_PREVIOUS_STATE_INPUT],
392 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
393 m4_popdef([AT_PREVIOUS_STATE_INPUT])
395 m4_pushdef([AT_USER_ACTION_GRAMMAR],
398 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
399 // incorrectly for the 'b' set in the semantic action below. All 'a'
400 // tokens are returned by yylex, which sets $$ = 1.
403 fprintf (stderr, "Wrong destructor.\n");
406 // Rather than depend on an inconsistent state to induce reading a
407 // lookahead as in the previous grammar, just assign the lookahead in a
408 // semantic action. That lookahead isn't needed before either error
409 // action is encountered. In a previous version of Bison, this was a
410 // problem as it meant yychar was not translated into yytoken before
411 // either error action. The second error action thus invoked a
412 // destructor that it selected according to the incorrect yytoken. The
413 // first error action would have reported an incorrect unexpected token
414 // except that, due to the bug described in the previous grammar, the
415 // unexpected token was not reported at all.
416 start: error-reduce consistent-error 'a' { USE ($][3); } ;
419 'a' 'a' consistent-reduction consistent-error 'a'
420 { USE (($][1, $][2, $][5)); }
425 consistent-reduction: /*empty*/ {
426 assert (yychar == YYEMPTY);
433 | /*empty*/ %prec 'a'
436 // Provide another context in which all rules are useful so that this
437 // test case looks a little more realistic.
438 start: 'b' consistent-error 'b' ;
440 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
442 AT_CONSISTENT_ERRORS_CHECK([[]],
443 [AT_USER_ACTION_GRAMMAR],
444 [AT_USER_ACTION_INPUT],
446 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
447 [AT_USER_ACTION_GRAMMAR],
448 [AT_USER_ACTION_INPUT],
450 # No C++ or Java test because yychar cannot be manipulated by users.
452 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
453 [AT_USER_ACTION_GRAMMAR],
454 [AT_USER_ACTION_INPUT],
457 # Canonical LR doesn't foresee the error for 'a'!
458 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
459 [AT_USER_ACTION_GRAMMAR],
460 [AT_USER_ACTION_INPUT],
462 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
463 [AT_USER_ACTION_GRAMMAR],
464 [AT_USER_ACTION_INPUT],
467 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
468 [AT_USER_ACTION_GRAMMAR],
469 [AT_USER_ACTION_INPUT],
471 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
472 %define lr.default-reduction accepting]],
473 [AT_USER_ACTION_GRAMMAR],
474 [AT_USER_ACTION_INPUT],
477 m4_popdef([AT_USER_ACTION_GRAMMAR])
478 m4_popdef([AT_USER_ACTION_INPUT])
480 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
486 ## ------------------------------------------------------- ##
487 ## LAC: %nonassoc requires splitting canonical LR states. ##
488 ## ------------------------------------------------------- ##
490 # This test case demonstrates that, when %nonassoc is used, canonical
491 # LR(1) parser table construction followed by conflict resolution
492 # without further state splitting is not always sufficient to produce a
493 # parser that can detect all syntax errors as soon as possible on one
494 # token of lookahead. However, LAC solves the problem completely even
495 # with minimal LR parser tables.
497 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
498 AT_BISON_OPTION_PUSHDEFS
499 AT_DATA_GRAMMAR([[input.y]],
512 'a' problem 'a' // First context.
513 | 'b' problem 'b' // Second context.
514 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
523 // For the state reached after shifting the 'a' in these productions,
524 // lookahead sets are the same in both the first and second contexts.
525 // Thus, canonical LR reuses the same state for both contexts. However,
526 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
527 // error action only in the first context. In order to immediately
528 // detect the syntax error on 'a' here for only the first context, this
529 // canonical LR state would have to be split into two states, and the
530 // 'a' lookahead would have to be removed from only one of the states.
532 'a' // Reduction lookahead set is always ['a', 'b'].
534 | 'a' 'c' // 'c' is forgotten as an expected token.
537 reduce-nonassoc: %prec 'a';
541 ]AT_YYLEX_DEFINE(["aaa"])[
544 AT_BISON_OPTION_POPDEFS
546 # Show canonical LR's failure.
547 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
549 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
551 AT_COMPILE([[input]])
552 AT_PARSER_CHECK([[./input]], [[1]], [[]],
553 [[syntax error, unexpected 'a', expecting 'b'
556 # It's corrected by LAC.
557 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
558 -o input.c input.y]], [[0]], [[]],
559 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
561 AT_COMPILE([[input]])
562 AT_PARSER_CHECK([[./input]], [[1]], [[]],
563 [[syntax error, unexpected 'a', expecting 'b' or 'c'
566 # IELR is sufficient when LAC is used.
567 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
569 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
571 AT_COMPILE([[input]])
572 AT_PARSER_CHECK([[./input]], [[1]], [[]],
573 [[syntax error, unexpected 'a', expecting 'b' or 'c'
578 ## ------------------------- ##
579 ## Unresolved SR Conflicts. ##
580 ## ------------------------- ##
582 AT_SETUP([Unresolved SR Conflicts])
584 AT_KEYWORDS([report])
589 exp: exp OP exp | NUM;
592 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
593 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
596 # Check the contents of the report.
597 AT_CHECK([cat input.output], [],
598 [[State 5 conflicts: 1 shift/reduce
609 Terminals, with rules where they appear
617 Nonterminals, with rules where they appear
622 on left: 1 2, on right: 0 1
627 0 $accept: . exp $end
631 NUM shift, and go to state 1
640 $default reduce using rule 2 (exp)
645 0 $accept: exp . $end
648 $end shift, and go to state 3
649 OP shift, and go to state 4
654 0 $accept: exp $end .
665 NUM shift, and go to state 1
673 1 | exp OP exp . [$end, OP]
675 OP shift, and go to state 4
677 OP [reduce using rule 1 (exp)]
678 $default reduce using rule 1 (exp)
685 ## ----------------------- ##
686 ## Resolved SR Conflicts. ##
687 ## ----------------------- ##
689 AT_SETUP([Resolved SR Conflicts])
691 AT_KEYWORDS([report])
697 exp: exp OP exp | NUM;
700 AT_BISON_CHECK([-o input.c --report=all input.y])
702 # Check the contents of the report.
703 AT_CHECK([cat input.output], [],
712 Terminals, with rules where they appear
720 Nonterminals, with rules where they appear
725 on left: 1 2, on right: 0 1
730 0 $accept: . exp $end
734 NUM shift, and go to state 1
743 $default reduce using rule 2 (exp)
748 0 $accept: exp . $end
751 $end shift, and go to state 3
752 OP shift, and go to state 4
757 0 $accept: exp $end .
768 NUM shift, and go to state 1
776 1 | exp OP exp . [$end, OP]
778 $default reduce using rule 1 (exp)
780 Conflict between rule 1 and token OP resolved as reduce (%left OP).
786 ## ---------------------- ##
787 ## %precedence suffices. ##
788 ## ---------------------- ##
790 AT_SETUP([%precedence suffices])
797 "if" cond "then" stmt
798 | "if" cond "then" stmt "else" stmt
807 AT_BISON_CHECK([-o input.c input.y])
812 ## ------------------------------ ##
813 ## %precedence does not suffice. ##
814 ## ------------------------------ ##
816 AT_SETUP([%precedence does not suffice])
823 "if" cond "then" stmt
824 | "if" cond "then" stmt "else" stmt
834 AT_BISON_CHECK([-o input.c input.y], 0, [],
835 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
836 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother]
842 ## -------------------------------- ##
843 ## Defaulted Conflicted Reduction. ##
844 ## -------------------------------- ##
846 # When there are RR conflicts, some rules are disabled. Usually it is
847 # simply displayed as:
849 # $end reduce using rule 3 (num)
850 # $end [reduce using rule 4 (id)]
852 # But when `reduce 3' is the default action, we'd produce:
854 # $end [reduce using rule 4 (id)]
855 # $default reduce using rule 3 (num)
857 # In this precise case (a reduction is masked by the default
858 # reduction), we make the `reduce 3' explicit:
860 # $end reduce using rule 3 (num)
861 # $end [reduce using rule 4 (id)]
862 # $default reduce using rule 3 (num)
864 # Maybe that's not the best display, but then, please propose something
867 AT_SETUP([Defaulted Conflicted Reduction])
868 AT_KEYWORDS([report])
878 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
879 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
880 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother]
883 # Check the contents of the report.
884 AT_CHECK([cat input.output], [],
885 [[Rules useless in parser due to conflicts
890 State 1 conflicts: 1 reduce/reduce
905 Terminals, with rules where they appear
912 Nonterminals, with rules where they appear
917 on left: 1 2, on right: 0
919 on left: 3, on right: 1
921 on left: 4, on right: 2
926 0 $accept: . exp $end
932 '0' shift, and go to state 1
944 $end reduce using rule 3 (num)
945 $end [reduce using rule 4 (id)]
946 $default reduce using rule 3 (num)
951 0 $accept: exp . $end
953 $end shift, and go to state 5
960 $default reduce using rule 1 (exp)
967 $default reduce using rule 2 (exp)
972 0 $accept: exp $end .
982 ## -------------------- ##
983 ## %expect not enough. ##
984 ## -------------------- ##
986 AT_SETUP([%expect not enough])
992 exp: exp OP exp | NUM;
995 AT_BISON_CHECK([-o input.c input.y], 1, [],
996 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1001 ## --------------- ##
1002 ## %expect right. ##
1003 ## --------------- ##
1005 AT_SETUP([%expect right])
1011 exp: exp OP exp | NUM;
1014 AT_BISON_CHECK([-o input.c input.y])
1018 ## ------------------ ##
1019 ## %expect too much. ##
1020 ## ------------------ ##
1022 AT_SETUP([%expect too much])
1028 exp: exp OP exp | NUM;
1031 AT_BISON_CHECK([-o input.c input.y], 1, [],
1032 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1037 ## ------------------------------- ##
1038 ## %expect with reduce conflicts. ##
1039 ## ------------------------------- ##
1041 AT_SETUP([%expect with reduce conflicts])
1046 program: a 'a' | a a;
1050 AT_BISON_CHECK([-o input.c input.y], 1, [],
1051 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1056 ## ------------------------- ##
1057 ## %prec with user strings. ##
1058 ## ------------------------- ##
1060 AT_SETUP([%prec with user string])
1062 AT_DATA([[input.y]],
1069 AT_BISON_CHECK([-o input.c input.y])
1073 ## -------------------------------- ##
1074 ## %no-default-prec without %prec. ##
1075 ## -------------------------------- ##
1077 AT_SETUP([%no-default-prec without %prec])
1079 AT_DATA([[input.y]],
1093 AT_BISON_CHECK([-o input.c input.y], 0, [],
1094 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1099 ## ----------------------------- ##
1100 ## %no-default-prec with %prec. ##
1101 ## ----------------------------- ##
1103 AT_SETUP([%no-default-prec with %prec])
1105 AT_DATA([[input.y]],
1113 e: e '+' e %prec '+'
1119 AT_BISON_CHECK([-o input.c input.y])
1123 ## --------------- ##
1124 ## %default-prec. ##
1125 ## --------------- ##
1127 AT_SETUP([%default-prec])
1129 AT_DATA([[input.y]],
1143 AT_BISON_CHECK([-o input.c input.y])
1147 ## ---------------------------------------------- ##
1148 ## Unreachable States After Conflict Resolution. ##
1149 ## ---------------------------------------------- ##
1151 AT_SETUP([[Unreachable States After Conflict Resolution]])
1153 # If conflict resolution makes states unreachable, remove those states, report
1154 # rules that are then unused, and don't report conflicts in those states. Test
1155 # what happens when a nonterminal becomes useless as a result of state removal
1156 # since that causes lalr.o's goto map to be rewritten.
1158 AT_DATA([[input.y]],
1164 start: resolved_conflict 'a' reported_conflicts 'a' ;
1166 /* S/R conflict resolved as reduce, so the state with item
1167 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1168 * unreachable, and the associated production is useless. */
1174 /* S/R conflict that need not be reported since it is unreachable because of
1175 * the previous conflict resolution. Nonterminal unreachable1 and all its
1176 * productions are useless. */
1182 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1185 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1186 * when their states are renumbered due to state removal. */
1195 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1196 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1197 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1198 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother]
1199 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother]
1200 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1201 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1202 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1203 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1204 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1207 AT_CHECK([[cat input.output]], 0,
1208 [[Rules useless in parser due to conflicts
1210 2 resolved_conflict: 'a' unreachable1
1212 4 unreachable1: 'a' unreachable2
1215 6 unreachable2: /* empty */
1218 9 reported_conflicts: 'a'
1222 State 4 conflicts: 1 shift/reduce
1223 State 5 conflicts: 1 reduce/reduce
1228 0 $accept: start $end
1230 1 start: resolved_conflict 'a' reported_conflicts 'a'
1232 2 resolved_conflict: 'a' unreachable1
1235 4 unreachable1: 'a' unreachable2
1238 6 unreachable2: /* empty */
1241 8 reported_conflicts: 'a'
1246 Terminals, with rules where they appear
1253 Nonterminals, with rules where they appear
1258 on left: 1, on right: 0
1259 resolved_conflict (6)
1260 on left: 2 3, on right: 1
1262 on left: 4 5, on right: 2
1264 on left: 6 7, on right: 4
1265 reported_conflicts (9)
1266 on left: 8 9 10, on right: 1
1271 0 $accept: . start $end
1272 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1273 2 resolved_conflict: . 'a' unreachable1
1276 $default reduce using rule 3 (resolved_conflict)
1279 resolved_conflict go to state 2
1281 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1286 0 $accept: start . $end
1288 $end shift, and go to state 3
1293 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1295 'a' shift, and go to state 4
1300 0 $accept: start $end .
1307 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1308 8 reported_conflicts: . 'a'
1312 'a' shift, and go to state 5
1314 'a' [reduce using rule 10 (reported_conflicts)]
1316 reported_conflicts go to state 6
1321 8 reported_conflicts: 'a' . ['a']
1324 'a' reduce using rule 8 (reported_conflicts)
1325 'a' [reduce using rule 9 (reported_conflicts)]
1326 $default reduce using rule 8 (reported_conflicts)
1331 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1333 'a' shift, and go to state 7
1338 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1340 $default reduce using rule 1 (start)
1343 AT_DATA([[input-keep.y]],
1344 [[%define lr.keep-unreachable-state
1346 AT_CHECK([[cat input.y >> input-keep.y]])
1348 AT_BISON_CHECK([[input-keep.y]], 0, [],
1349 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1350 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1351 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1352 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1353 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1354 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1360 ## ------------------------------------------------------------ ##
1361 ## Solved conflicts report for multiple reductions in a state. ##
1362 ## ------------------------------------------------------------ ##
1364 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1366 # Used to lose earlier solved conflict messages even within a single S/R/R.
1368 AT_DATA([[input.y]],
1384 empty_a: %prec 'a' ;
1385 empty_b: %prec 'b' ;
1386 empty_c1: %prec 'c' ;
1387 empty_c2: %prec 'c' ;
1388 empty_c3: %prec 'd' ;
1390 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1391 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1394 0 $accept: . start $end
1407 13 empty_c3: . ['c']
1409 'b' shift, and go to state 1
1411 'c' reduce using rule 13 (empty_c3)
1412 $default reduce using rule 9 (empty_a)
1415 empty_a go to state 3
1416 empty_b go to state 4
1417 empty_c1 go to state 5
1418 empty_c2 go to state 6
1419 empty_c3 go to state 7
1421 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1422 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1423 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1424 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1425 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1434 ## ------------------------------------------------------------ ##
1435 ## %nonassoc error actions for multiple reductions in a state. ##
1436 ## ------------------------------------------------------------ ##
1438 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1439 # multiple reductions in a state.
1441 # For a %nonassoc error action token, used to print the first remaining
1442 # reduction on that token without brackets.
1444 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1446 AT_DATA([[input.y]],
1447 [[%nonassoc 'a' 'b' 'c'
1459 empty_a: %prec 'a' ;
1460 empty_b: %prec 'b' ;
1461 empty_c1: %prec 'c' ;
1462 empty_c2: %prec 'c' ;
1463 empty_c3: %prec 'c' ;
1466 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1467 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1470 0 $accept: . start $end
1482 12 empty_c2: . ['c']
1483 13 empty_c3: . ['c']
1485 'a' error (nonassociative)
1486 'b' error (nonassociative)
1487 'c' error (nonassociative)
1489 'c' [reduce using rule 12 (empty_c2)]
1490 'c' [reduce using rule 13 (empty_c3)]
1493 empty_a go to state 2
1494 empty_b go to state 3
1495 empty_c1 go to state 4
1496 empty_c2 go to state 5
1497 empty_c3 go to state 6
1499 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1500 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1501 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1509 ## -------------------- ##
1510 ## %expect-rr non GLR. ##
1511 ## -------------------- ##
1513 AT_SETUP([[%expect-rr non GLR]])
1521 AT_BISON_CHECK([[1.y]], [[0]], [],
1522 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1531 AT_BISON_CHECK([[2.y]], [[0]], [],
1532 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1533 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1534 2.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother]
1540 ## ---------------------------------- ##
1541 ## -W versus %expect and %expect-rr. ##
1542 ## ---------------------------------- ##
1544 AT_SETUP([[-W versus %expect and %expect-rr]])
1546 AT_DATA([[sr-rr.y]],
1549 start: 'a' | A 'a' | B 'a' ;
1556 start: 'a' | A 'a' ;
1567 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1568 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1569 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1571 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1572 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1574 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1575 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1579 # This is piece of code is rather complex for a simple task: try every
1580 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1581 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1584 # Number and types of genuine conflicts in the grammar.
1585 for gram in sr-rr sr rr; do
1586 # Number of expected s/r conflicts.
1587 for sr_exp_i in '' 0 1 2; do
1588 # Number of expected r/r conflicts.
1589 for rr_exp_i in '' 0 1 2; do
1590 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1592 # Build grammar file.
1597 if test -n "$sr_exp_i"; then
1599 file=$file-expect-$sr_exp
1600 directives="%expect $sr_exp"
1602 if test -n "$rr_exp_i"; then
1604 file=$file-expect-rr-$rr_exp
1605 directives="$directives %expect-rr $rr_exp"
1608 echo "$directives" > $file
1609 cat $gram.y >> $file
1611 # Number of found conflicts.
1613 (sr) sr_count=1; rr_count=0;;
1614 (rr) sr_count=0; rr_count=1;;
1615 (sr-rr) sr_count=1; rr_count=1;;
1618 # Update number of expected conflicts: if %expect is given then
1619 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1621 case $sr_exp_i:$rr_exp_i in
1627 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1628 ]AT_BISON_CHECK([[-Wnone $file]])[
1629 ]AT_BISON_CHECK([[-Werror $file]])[
1632 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1633 echo "warning: $sr_count shift/reduce conflicts"
1634 elif test "$sr_exp_i" -ne "$sr_count"; then
1635 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1637 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1638 echo "warning: $rr_count reduce/reduce conflicts"
1639 elif test "$rr_exp_i" -ne "$rr_count"; then
1640 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1642 } | sed -e "s/^/$file: /" > experr
1643 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1644 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[