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.]])
20 ## ------------------------- ##
21 ## Token declaration order. ##
22 ## ------------------------- ##
24 # This test checks that token are declared left to right when in a precedence
27 AT_SETUP([Token declaration order])
29 AT_BISON_OPTION_PUSHDEFS
31 AT_DATA_GRAMMAR([[input.y]],
80 AT_BISON_CHECK([-o input.c input.y])
83 AT_PARSER_CHECK([./input])
85 AT_BISON_OPTION_POPDEFS
90 ## ------------------------------- ##
91 ## Useless associativity warning. ##
92 ## ------------------------------- ##
94 AT_SETUP([Useless associativity warning])
115 AT_BISON_CHECK([-Wprecedence input.y], 0, [],
116 [[input.y:1.11-13: warning: useless precedence and associativity for "=" [-Wprecedence]
117 input.y:3.7-9: warning: useless associativity for "*", use %precedence [-Wprecedence]
118 input.y:4.13-15: warning: useless precedence for "(" [-Wprecedence]
124 ## ---------------------------- ##
125 ## Useless precedence warning. ##
126 ## ---------------------------- ##
128 AT_SETUP([Useless precedence warning])
158 AT_BISON_CHECK([-Wprecedence -fcaret -o input.c input.y], 0, [],
159 [[input.y:2.13: warning: useless precedence for Z [-Wprecedence]
162 input.y:5.7: warning: useless precedence and associativity for W [-Wprecedence]
165 input.y:6.8: warning: useless precedence and associativity for V [-Wprecedence]
168 input.y:7.11: warning: useless precedence and associativity for U [-Wprecedence]
176 ## ---------------- ##
177 ## S/R in initial. ##
178 ## ---------------- ##
180 # I once hacked Bison in such a way that it lost its reductions on the
181 # initial state (because it was confusing it with the last state). It
182 # took me a while to strip down my failures to this simple case. So
183 # make sure it finds the s/r conflict below.
185 AT_SETUP([S/R in initial])
191 e: 'e' | /* Nothing. */;
194 AT_BISON_CHECK([-o input.c input.y], 0, [],
195 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
198 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
199 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
200 e: 'e' | /* Nothing. */;
207 ## ------------------- ##
208 ## %nonassoc and eof. ##
209 ## ------------------- ##
211 AT_SETUP([%nonassoc and eof])
213 AT_BISON_OPTION_PUSHDEFS
214 AT_DATA_GRAMMAR([input.y],
222 #define YYERROR_VERBOSE 1
224 /* The current argument. */
225 static const char *input;
230 static size_t toknum;
231 assert (toknum <= strlen (input));
232 return input[toknum++];
246 main (int argc, const char *argv[])
248 input = argc <= 1 ? "" : argv[1];
252 AT_BISON_OPTION_POPDEFS
254 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
255 [AT_BISON_CHECK([$1[ -o input.c input.y]])
258 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
260 AT_PARSER_CHECK([./input '0<0'])
261 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
262 [syntax error, unexpected '<'AT_EXPECTING
265 AT_PARSER_CHECK([./input '0>0'])
266 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
267 [syntax error, unexpected '>'AT_EXPECTING
270 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
271 [syntax error, unexpected '>'AT_EXPECTING
274 m4_popdef([AT_EXPECTING])])
276 # Expected token list is missing.
277 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
279 # We must disable default reductions in inconsistent states in order to
280 # have an explicit list of all expected tokens.
281 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
284 # lr.default-reduction=consistent happens to work for this test case.
285 # However, for other grammars, lookahead sets can be merged for
286 # different left contexts, so it is still possible to have an incorrect
287 # expected list. Canonical LR is almost a general solution (that is, it
288 # can fail only when %nonassoc is used), so make sure it gives the same
290 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
292 # parse.lac=full is a completely general solution that does not require
293 # any of the above sacrifices. Of course, it does not extend the
294 # language-recognition power of LALR to (IE)LR, but it does ensure that
295 # the reported list of expected tokens matches what the given parser
296 # would have accepted in place of the unexpected token.
297 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
299 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
305 ## ------------------------------------------- ##
306 ## parse.error=verbose and consistent errors. ##
307 ## ------------------------------------------- ##
309 AT_SETUP([[parse.error=verbose and consistent errors]])
311 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
313 AT_BISON_OPTION_PUSHDEFS([$1])
315 m4_pushdef([AT_YYLEX_PROTOTYPE],
316 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
317 [[int yylex (YYSTYPE *lvalp)]])])
319 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
323 import java.io.IOException;
326 %code {]AT_SKEL_CC_IF([[
327 #include <string>]], [[
330 ]AT_YYERROR_DECLARE])[
331 ]AT_YYLEX_PROTOTYPE[;
335 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
339 %define parse.error verbose
345 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
349 `--------*/]AT_SKEL_JAVA_IF([[
351 public String input = "]$3[";
352 public int index = 0;
355 if (index < input.length ())
356 return input.charAt (index++);
360 public Object getLVal ()
362 return new Integer(1);
367 static char const *input = "]$3[";
383 AT_FULL_COMPILE([[input]])
385 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
386 $5, [a], [[, expecting 'a']],
387 $5, [b], [[, expecting 'b']])])
389 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
390 [AT_PARSER_CHECK([[./input]], [[1]]]),
392 [[syntax error, unexpected ]$4[]AT_EXPECTING[
395 m4_popdef([AT_EXPECTING])
396 m4_popdef([AT_YYLEX_PROTOTYPE])
397 AT_BISON_OPTION_POPDEFS
401 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
404 start: consistent-error-on-a-a 'a' ;
406 consistent-error-on-a-a:
407 'a' default-reduction
408 | 'a' default-reduction 'a'
412 default-reduction: /*empty*/ ;
415 // Provide another context in which all rules are useful so that this
416 // test case looks a little more realistic.
417 start: 'b' consistent-error-on-a-a 'c' ;
420 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
422 # Unfortunately, no expected tokens are reported even though 'b' can be
423 # accepted. Nevertheless, the main point of this test is to make sure
424 # that at least the unexpected token is reported. In a previous version
425 # of Bison, it wasn't reported because the error is detected in a
426 # consistent state with an error action, and that case always triggered
427 # the simple "syntax error" message.
429 # The point isn't to test IELR here, but state merging happens to
430 # complicate this example.
431 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
432 [AT_PREVIOUS_STATE_GRAMMAR],
433 [AT_PREVIOUS_STATE_INPUT],
435 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
437 [AT_PREVIOUS_STATE_GRAMMAR],
438 [AT_PREVIOUS_STATE_INPUT],
440 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
442 [AT_PREVIOUS_STATE_GRAMMAR],
443 [AT_PREVIOUS_STATE_INPUT],
445 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
447 [AT_PREVIOUS_STATE_GRAMMAR],
448 [AT_PREVIOUS_STATE_INPUT],
449 [[end of input]], [[none]])
451 # Even canonical LR doesn't foresee the error for 'a'!
452 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
453 %define lr.default-reduction consistent]],
454 [AT_PREVIOUS_STATE_GRAMMAR],
455 [AT_PREVIOUS_STATE_INPUT],
457 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
458 %define lr.default-reduction accepting]],
459 [AT_PREVIOUS_STATE_GRAMMAR],
460 [AT_PREVIOUS_STATE_INPUT],
462 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
463 [AT_PREVIOUS_STATE_GRAMMAR],
464 [AT_PREVIOUS_STATE_INPUT],
467 # Only LAC gets it right.
468 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
469 %define parse.lac full]],
470 [AT_PREVIOUS_STATE_GRAMMAR],
471 [AT_PREVIOUS_STATE_INPUT],
473 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
474 %define parse.lac full]],
475 [AT_PREVIOUS_STATE_GRAMMAR],
476 [AT_PREVIOUS_STATE_INPUT],
479 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
480 m4_popdef([AT_PREVIOUS_STATE_INPUT])
482 m4_pushdef([AT_USER_ACTION_GRAMMAR],
485 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
486 // incorrectly for the 'b' set in the semantic action below. All 'a'
487 // tokens are returned by yylex, which sets $$ = 1.
490 fprintf (stderr, "Wrong destructor.\n");
493 // Rather than depend on an inconsistent state to induce reading a
494 // lookahead as in the previous grammar, just assign the lookahead in a
495 // semantic action. That lookahead isn't needed before either error
496 // action is encountered. In a previous version of Bison, this was a
497 // problem as it meant yychar was not translated into yytoken before
498 // either error action. The second error action thus invoked a
499 // destructor that it selected according to the incorrect yytoken. The
500 // first error action would have reported an incorrect unexpected token
501 // except that, due to the bug described in the previous grammar, the
502 // unexpected token was not reported at all.
503 start: error-reduce consistent-error 'a' { USE ($][3); } ;
506 'a' 'a' consistent-reduction consistent-error 'a'
507 { USE (($][1, $][2, $][5)); }
512 consistent-reduction: /*empty*/ {
513 assert (yychar == YYEMPTY);
520 | /*empty*/ %prec 'a'
523 // Provide another context in which all rules are useful so that this
524 // test case looks a little more realistic.
525 start: 'b' consistent-error 'b' ;
527 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
529 AT_CONSISTENT_ERRORS_CHECK([[]],
530 [AT_USER_ACTION_GRAMMAR],
531 [AT_USER_ACTION_INPUT],
533 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
534 [AT_USER_ACTION_GRAMMAR],
535 [AT_USER_ACTION_INPUT],
537 # No C++ or Java test because yychar cannot be manipulated by users.
539 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
540 [AT_USER_ACTION_GRAMMAR],
541 [AT_USER_ACTION_INPUT],
544 # Canonical LR doesn't foresee the error for 'a'!
545 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
546 [AT_USER_ACTION_GRAMMAR],
547 [AT_USER_ACTION_INPUT],
549 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
550 [AT_USER_ACTION_GRAMMAR],
551 [AT_USER_ACTION_INPUT],
554 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
555 [AT_USER_ACTION_GRAMMAR],
556 [AT_USER_ACTION_INPUT],
558 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
559 %define lr.default-reduction accepting]],
560 [AT_USER_ACTION_GRAMMAR],
561 [AT_USER_ACTION_INPUT],
564 m4_popdef([AT_USER_ACTION_GRAMMAR])
565 m4_popdef([AT_USER_ACTION_INPUT])
567 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
573 ## ------------------------------------------------------- ##
574 ## LAC: %nonassoc requires splitting canonical LR states. ##
575 ## ------------------------------------------------------- ##
577 # This test case demonstrates that, when %nonassoc is used, canonical
578 # LR(1) parser table construction followed by conflict resolution
579 # without further state splitting is not always sufficient to produce a
580 # parser that can detect all syntax errors as soon as possible on one
581 # token of lookahead. However, LAC solves the problem completely even
582 # with minimal LR parser tables.
584 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
585 AT_BISON_OPTION_PUSHDEFS
586 AT_DATA_GRAMMAR([[input.y]],
599 'a' problem 'a' // First context.
600 | 'b' problem 'b' // Second context.
601 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
610 // For the state reached after shifting the 'a' in these productions,
611 // lookahead sets are the same in both the first and second contexts.
612 // Thus, canonical LR reuses the same state for both contexts. However,
613 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
614 // error action only in the first context. In order to immediately
615 // detect the syntax error on 'a' here for only the first context, this
616 // canonical LR state would have to be split into two states, and the
617 // 'a' lookahead would have to be removed from only one of the states.
619 'a' // Reduction lookahead set is always ['a', 'b'].
621 | 'a' 'c' // 'c' is forgotten as an expected token.
624 reduce-nonassoc: %prec 'a';
628 ]AT_YYLEX_DEFINE(["aaa"])[
631 AT_BISON_OPTION_POPDEFS
633 # Show canonical LR's failure.
634 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
636 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
638 AT_COMPILE([[input]])
639 AT_PARSER_CHECK([[./input]], [[1]], [[]],
640 [[syntax error, unexpected 'a', expecting 'b'
643 # It's corrected by LAC.
644 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
645 -o input.c input.y]], [[0]], [[]],
646 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
648 AT_COMPILE([[input]])
649 AT_PARSER_CHECK([[./input]], [[1]], [[]],
650 [[syntax error, unexpected 'a', expecting 'b' or 'c'
653 # IELR is sufficient when LAC is used.
654 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
656 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
658 AT_COMPILE([[input]])
659 AT_PARSER_CHECK([[./input]], [[1]], [[]],
660 [[syntax error, unexpected 'a', expecting 'b' or 'c'
665 ## ------------------------- ##
666 ## Unresolved SR Conflicts. ##
667 ## ------------------------- ##
669 AT_SETUP([Unresolved SR Conflicts])
671 AT_KEYWORDS([report])
676 exp: exp OP exp | NUM;
679 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
680 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
683 # Check the contents of the report.
684 AT_CHECK([cat input.output], [],
685 [[State 5 conflicts: 1 shift/reduce
696 Terminals, with rules where they appear
704 Nonterminals, with rules where they appear
709 on left: 1 2, on right: 0 1
714 0 $accept: . exp $end
718 NUM shift, and go to state 1
727 $default reduce using rule 2 (exp)
732 0 $accept: exp . $end
735 $end shift, and go to state 3
736 OP shift, and go to state 4
741 0 $accept: exp $end .
752 NUM shift, and go to state 1
760 1 | exp OP exp . [$end, OP]
762 OP shift, and go to state 4
764 OP [reduce using rule 1 (exp)]
765 $default reduce using rule 1 (exp)
772 ## ----------------------- ##
773 ## Resolved SR Conflicts. ##
774 ## ----------------------- ##
776 AT_SETUP([Resolved SR Conflicts])
778 AT_KEYWORDS([report])
784 exp: exp OP exp | NUM;
787 AT_BISON_CHECK([-o input.c --report=all input.y])
789 # Check the contents of the report.
790 AT_CHECK([cat input.output], [],
799 Terminals, with rules where they appear
807 Nonterminals, with rules where they appear
812 on left: 1 2, on right: 0 1
817 0 $accept: . exp $end
821 NUM shift, and go to state 1
830 $default reduce using rule 2 (exp)
835 0 $accept: exp . $end
838 $end shift, and go to state 3
839 OP shift, and go to state 4
844 0 $accept: exp $end .
855 NUM shift, and go to state 1
863 1 | exp OP exp . [$end, OP]
865 $default reduce using rule 1 (exp)
867 Conflict between rule 1 and token OP resolved as reduce (%left OP).
873 ## ---------------------- ##
874 ## %precedence suffices. ##
875 ## ---------------------- ##
877 AT_SETUP([%precedence suffices])
884 "if" cond "then" stmt
885 | "if" cond "then" stmt "else" stmt
894 AT_BISON_CHECK([-o input.c input.y])
899 ## ------------------------------ ##
900 ## %precedence does not suffice. ##
901 ## ------------------------------ ##
903 AT_SETUP([%precedence does not suffice])
910 "if" cond "then" stmt
911 | "if" cond "then" stmt "else" stmt
921 AT_BISON_CHECK([-o input.c input.y], 0, [],
922 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
923 input.y:12.3-18: warning: rule useless in parser due to conflicts [-Wother]
929 ## -------------------------------- ##
930 ## Defaulted Conflicted Reduction. ##
931 ## -------------------------------- ##
933 # When there are RR conflicts, some rules are disabled. Usually it is
934 # simply displayed as:
936 # $end reduce using rule 3 (num)
937 # $end [reduce using rule 4 (id)]
939 # But when `reduce 3' is the default action, we'd produce:
941 # $end [reduce using rule 4 (id)]
942 # $default reduce using rule 3 (num)
944 # In this precise case (a reduction is masked by the default
945 # reduction), we make the `reduce 3' explicit:
947 # $end reduce using rule 3 (num)
948 # $end [reduce using rule 4 (id)]
949 # $default reduce using rule 3 (num)
951 # Maybe that's not the best display, but then, please propose something
954 AT_SETUP([Defaulted Conflicted Reduction])
955 AT_KEYWORDS([report])
965 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
966 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
967 input.y:4.6-8: warning: rule useless in parser due to conflicts [-Wother]
970 # Check the contents of the report.
971 AT_CHECK([cat input.output], [],
972 [[Rules useless in parser due to conflicts
977 State 1 conflicts: 1 reduce/reduce
992 Terminals, with rules where they appear
999 Nonterminals, with rules where they appear
1004 on left: 1 2, on right: 0
1006 on left: 3, on right: 1
1008 on left: 4, on right: 2
1013 0 $accept: . exp $end
1019 '0' shift, and go to state 1
1031 $end reduce using rule 3 (num)
1032 $end [reduce using rule 4 (id)]
1033 $default reduce using rule 3 (num)
1038 0 $accept: exp . $end
1040 $end shift, and go to state 5
1047 $default reduce using rule 1 (exp)
1054 $default reduce using rule 2 (exp)
1059 0 $accept: exp $end .
1069 ## -------------------- ##
1070 ## %expect not enough. ##
1071 ## -------------------- ##
1073 AT_SETUP([%expect not enough])
1079 exp: exp OP exp | NUM;
1082 AT_BISON_CHECK([-o input.c input.y], 1, [],
1083 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1088 ## --------------- ##
1089 ## %expect right. ##
1090 ## --------------- ##
1092 AT_SETUP([%expect right])
1098 exp: exp OP exp | NUM;
1101 AT_BISON_CHECK([-o input.c input.y])
1105 ## ------------------ ##
1106 ## %expect too much. ##
1107 ## ------------------ ##
1109 AT_SETUP([%expect too much])
1115 exp: exp OP exp | NUM;
1118 AT_BISON_CHECK([-o input.c input.y], 1, [],
1119 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1124 ## ------------------------------- ##
1125 ## %expect with reduce conflicts. ##
1126 ## ------------------------------- ##
1128 AT_SETUP([%expect with reduce conflicts])
1133 program: a 'a' | a a;
1137 AT_BISON_CHECK([-o input.c input.y], 1, [],
1138 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1143 ## ------------------------- ##
1144 ## %prec with user strings. ##
1145 ## ------------------------- ##
1147 AT_SETUP([%prec with user string])
1149 AT_DATA([[input.y]],
1156 AT_BISON_CHECK([-o input.c input.y])
1160 ## -------------------------------- ##
1161 ## %no-default-prec without %prec. ##
1162 ## -------------------------------- ##
1164 AT_SETUP([%no-default-prec without %prec])
1166 AT_DATA([[input.y]],
1180 AT_BISON_CHECK([-Wall -o input.c input.y], 0, [],
1181 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1182 input.y:1.7-9: warning: useless precedence and associativity for '+' [-Wprecedence]
1183 input.y:2.7-9: warning: useless precedence and associativity for '*' [-Wprecedence]
1188 ## ----------------------------- ##
1189 ## %no-default-prec with %prec. ##
1190 ## ----------------------------- ##
1192 AT_SETUP([%no-default-prec with %prec])
1194 AT_DATA([[input.y]],
1202 e: e '+' e %prec '+'
1208 AT_BISON_CHECK([-o input.c input.y])
1212 ## --------------- ##
1213 ## %default-prec. ##
1214 ## --------------- ##
1216 AT_SETUP([%default-prec])
1218 AT_DATA([[input.y]],
1232 AT_BISON_CHECK([-o input.c input.y])
1236 ## ---------------------------------------------- ##
1237 ## Unreachable States After Conflict Resolution. ##
1238 ## ---------------------------------------------- ##
1240 AT_SETUP([[Unreachable States After Conflict Resolution]])
1242 # If conflict resolution makes states unreachable, remove those states, report
1243 # rules that are then unused, and don't report conflicts in those states. Test
1244 # what happens when a nonterminal becomes useless as a result of state removal
1245 # since that causes lalr.o's goto map to be rewritten.
1247 AT_DATA([[input.y]],
1253 start: resolved_conflict 'a' reported_conflicts 'a' ;
1255 /* S/R conflict resolved as reduce, so the state with item
1256 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1257 * unreachable, and the associated production is useless. */
1263 /* S/R conflict that need not be reported since it is unreachable because of
1264 * the previous conflict resolution. Nonterminal unreachable1 and all its
1265 * productions are useless. */
1271 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1274 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1275 * when their states are renumbered due to state removal. */
1284 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1285 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1286 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1287 input.y:12.5-20: warning: rule useless in parser due to conflicts [-Wother]
1288 input.y:20.5-20: warning: rule useless in parser due to conflicts [-Wother]
1289 input.y:21.4: warning: rule useless in parser due to conflicts [-Wother]
1290 input.y:25.13: warning: rule useless in parser due to conflicts [-Wother]
1291 input.y:25.16: warning: rule useless in parser due to conflicts [-Wother]
1292 input.y:31.5-7: warning: rule useless in parser due to conflicts [-Wother]
1293 input.y:32.4: warning: rule useless in parser due to conflicts [-Wother]
1296 AT_CHECK([[cat input.output]], 0,
1297 [[Rules useless in parser due to conflicts
1299 2 resolved_conflict: 'a' unreachable1
1301 4 unreachable1: 'a' unreachable2
1304 6 unreachable2: /* empty */
1307 9 reported_conflicts: 'a'
1311 State 4 conflicts: 1 shift/reduce
1312 State 5 conflicts: 1 reduce/reduce
1317 0 $accept: start $end
1319 1 start: resolved_conflict 'a' reported_conflicts 'a'
1321 2 resolved_conflict: 'a' unreachable1
1324 4 unreachable1: 'a' unreachable2
1327 6 unreachable2: /* empty */
1330 8 reported_conflicts: 'a'
1335 Terminals, with rules where they appear
1342 Nonterminals, with rules where they appear
1347 on left: 1, on right: 0
1348 resolved_conflict (6)
1349 on left: 2 3, on right: 1
1351 on left: 4 5, on right: 2
1353 on left: 6 7, on right: 4
1354 reported_conflicts (9)
1355 on left: 8 9 10, on right: 1
1360 0 $accept: . start $end
1361 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1362 2 resolved_conflict: . 'a' unreachable1
1365 $default reduce using rule 3 (resolved_conflict)
1368 resolved_conflict go to state 2
1370 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1375 0 $accept: start . $end
1377 $end shift, and go to state 3
1382 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1384 'a' shift, and go to state 4
1389 0 $accept: start $end .
1396 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1397 8 reported_conflicts: . 'a'
1401 'a' shift, and go to state 5
1403 'a' [reduce using rule 10 (reported_conflicts)]
1405 reported_conflicts go to state 6
1410 8 reported_conflicts: 'a' . ['a']
1413 'a' reduce using rule 8 (reported_conflicts)
1414 'a' [reduce using rule 9 (reported_conflicts)]
1415 $default reduce using rule 8 (reported_conflicts)
1420 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1422 'a' shift, and go to state 7
1427 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1429 $default reduce using rule 1 (start)
1432 AT_DATA([[input-keep.y]],
1433 [[%define lr.keep-unreachable-state
1435 AT_CHECK([[cat input.y >> input-keep.y]])
1437 AT_BISON_CHECK([[input-keep.y]], 0, [],
1438 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1439 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1440 input-keep.y:22.4: warning: rule useless in parser due to conflicts [-Wother]
1441 input-keep.y:26.16: warning: rule useless in parser due to conflicts [-Wother]
1442 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts [-Wother]
1443 input-keep.y:33.4: warning: rule useless in parser due to conflicts [-Wother]
1449 ## ------------------------------------------------------------ ##
1450 ## Solved conflicts report for multiple reductions in a state. ##
1451 ## ------------------------------------------------------------ ##
1453 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1455 # Used to lose earlier solved conflict messages even within a single S/R/R.
1457 AT_DATA([[input.y]],
1473 empty_a: %prec 'a' ;
1474 empty_b: %prec 'b' ;
1475 empty_c1: %prec 'c' ;
1476 empty_c2: %prec 'c' ;
1477 empty_c3: %prec 'd' ;
1479 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1480 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1483 0 $accept: . start $end
1496 13 empty_c3: . ['c']
1498 'b' shift, and go to state 1
1500 'c' reduce using rule 13 (empty_c3)
1501 $default reduce using rule 9 (empty_a)
1504 empty_a go to state 3
1505 empty_b go to state 4
1506 empty_c1 go to state 5
1507 empty_c2 go to state 6
1508 empty_c3 go to state 7
1510 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1511 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1512 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1513 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1514 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1523 ## ------------------------------------------------------------ ##
1524 ## %nonassoc error actions for multiple reductions in a state. ##
1525 ## ------------------------------------------------------------ ##
1527 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1528 # multiple reductions in a state.
1530 # For a %nonassoc error action token, used to print the first remaining
1531 # reduction on that token without brackets.
1533 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1535 AT_DATA([[input.y]],
1536 [[%nonassoc 'a' 'b' 'c'
1548 empty_a: %prec 'a' ;
1549 empty_b: %prec 'b' ;
1550 empty_c1: %prec 'c' ;
1551 empty_c2: %prec 'c' ;
1552 empty_c3: %prec 'c' ;
1555 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1556 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1559 0 $accept: . start $end
1571 12 empty_c2: . ['c']
1572 13 empty_c3: . ['c']
1574 'a' error (nonassociative)
1575 'b' error (nonassociative)
1576 'c' error (nonassociative)
1578 'c' [reduce using rule 12 (empty_c2)]
1579 'c' [reduce using rule 13 (empty_c3)]
1582 empty_a go to state 2
1583 empty_b go to state 3
1584 empty_c1 go to state 4
1585 empty_c2 go to state 5
1586 empty_c3 go to state 6
1588 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1589 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1590 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1598 ## -------------------- ##
1599 ## %expect-rr non GLR. ##
1600 ## -------------------- ##
1602 AT_SETUP([[%expect-rr non GLR]])
1610 AT_BISON_CHECK([[1.y]], [[0]], [],
1611 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1620 AT_BISON_CHECK([[2.y]], [[0]], [],
1621 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1622 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1623 2.y:3.12-14: warning: rule useless in parser due to conflicts [-Wother]
1629 ## ---------------------------------- ##
1630 ## -W versus %expect and %expect-rr. ##
1631 ## ---------------------------------- ##
1633 AT_SETUP([[-W versus %expect and %expect-rr]])
1635 AT_DATA([[sr-rr.y]],
1638 start: 'a' | A 'a' | B 'a' ;
1645 start: 'a' | A 'a' ;
1656 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1657 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1658 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1660 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1661 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1663 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1664 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1668 # This is piece of code is rather complex for a simple task: try every
1669 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1670 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1673 # Number and types of genuine conflicts in the grammar.
1674 for gram in sr-rr sr rr; do
1675 # Number of expected s/r conflicts.
1676 for sr_exp_i in '' 0 1 2; do
1677 # Number of expected r/r conflicts.
1678 for rr_exp_i in '' 0 1 2; do
1679 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1681 # Build grammar file.
1686 if test -n "$sr_exp_i"; then
1688 file=$file-expect-$sr_exp
1689 directives="%expect $sr_exp"
1691 if test -n "$rr_exp_i"; then
1693 file=$file-expect-rr-$rr_exp
1694 directives="$directives %expect-rr $rr_exp"
1697 echo "$directives" > $file
1698 cat $gram.y >> $file
1700 # Number of found conflicts.
1702 (sr) sr_count=1; rr_count=0;;
1703 (rr) sr_count=0; rr_count=1;;
1704 (sr-rr) sr_count=1; rr_count=1;;
1707 # Update number of expected conflicts: if %expect is given then
1708 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1710 case $sr_exp_i:$rr_exp_i in
1716 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1717 ]AT_BISON_CHECK([[-Wnone $file]])[
1718 ]AT_BISON_CHECK([[-Werror $file]])[
1721 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1722 echo "warning: $sr_count shift/reduce conflicts"
1723 elif test "$sr_exp_i" -ne "$sr_count"; then
1724 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1726 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1727 echo "warning: $rr_count reduce/reduce conflicts"
1728 elif test "$rr_exp_i" -ne "$rr_count"; then
1729 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1731 } | sed -e "s/^/$file: /" > experr
1732 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1733 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[