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_BISON_CHECK([-o input.c input.y], [], [],
82 [[input.y:24.13: warning: useless precedence for R [-Wother]
83 input.y:24.15: warning: useless precedence for S [-Wother]
84 input.y:24.17: warning: useless precedence for T [-Wother]
85 input.y:24.19: warning: useless precedence for U [-Wother]
86 input.y:25.13: warning: useless precedence for V [-Wother]
87 input.y:25.15: warning: useless precedence for W [-Wother]
91 AT_PARSER_CHECK([./input])
93 AT_BISON_OPTION_POPDEFS
98 ## ---------------------------- ##
99 ## Useless precedence warning. ##
100 ## ---------------------------- ##
102 AT_SETUP([Useless precedence warning])
132 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
133 [[input.y:2.13: warning: useless precedence for Z [-Wother]
141 ## ---------------- ##
142 ## S/R in initial. ##
143 ## ---------------- ##
145 # I once hacked Bison in such a way that it lost its reductions on the
146 # initial state (because it was confusing it with the last state). It
147 # took me a while to strip down my failures to this simple case. So
148 # make sure it finds the s/r conflict below.
150 AT_SETUP([S/R in initial])
156 e: 'e' | /* Nothing. */;
159 AT_BISON_CHECK([-o input.c input.y], 0, [],
160 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */ [-Wother]
163 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
164 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
165 e: 'e' | /* Nothing. */;
172 ## ------------------- ##
173 ## %nonassoc and eof. ##
174 ## ------------------- ##
176 AT_SETUP([%nonassoc and eof])
178 AT_BISON_OPTION_PUSHDEFS
179 AT_DATA_GRAMMAR([input.y],
187 #define YYERROR_VERBOSE 1
189 /* The current argument. */
190 static const char *input;
195 static size_t toknum;
196 assert (toknum <= strlen (input));
197 return input[toknum++];
211 main (int argc, const char *argv[])
213 input = argc <= 1 ? "" : argv[1];
217 AT_BISON_OPTION_POPDEFS
219 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
220 [AT_BISON_CHECK([$1[ -o input.c input.y]])
223 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
225 AT_PARSER_CHECK([./input '0<0'])
226 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
227 [syntax error, unexpected '<'AT_EXPECTING
230 AT_PARSER_CHECK([./input '0>0'])
231 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
232 [syntax error, unexpected '>'AT_EXPECTING
235 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
236 [syntax error, unexpected '>'AT_EXPECTING
239 m4_popdef([AT_EXPECTING])])
241 # Expected token list is missing.
242 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
244 # We must disable default reductions in inconsistent states in order to
245 # have an explicit list of all expected tokens.
246 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
249 # lr.default-reduction=consistent happens to work for this test case.
250 # However, for other grammars, lookahead sets can be merged for
251 # different left contexts, so it is still possible to have an incorrect
252 # expected list. Canonical LR is almost a general solution (that is, it
253 # can fail only when %nonassoc is used), so make sure it gives the same
255 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
257 # parse.lac=full is a completely general solution that does not require
258 # any of the above sacrifices. Of course, it does not extend the
259 # language-recognition power of LALR to (IE)LR, but it does ensure that
260 # the reported list of expected tokens matches what the given parser
261 # would have accepted in place of the unexpected token.
262 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
264 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
270 ## ------------------------------------------- ##
271 ## parse.error=verbose and consistent errors. ##
272 ## ------------------------------------------- ##
274 AT_SETUP([[parse.error=verbose and consistent errors]])
276 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
278 AT_BISON_OPTION_PUSHDEFS([$1])
280 m4_pushdef([AT_YYLEX_PROTOTYPE],
281 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
282 [[int yylex (YYSTYPE *lvalp)]])])
284 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
288 import java.io.IOException;
291 %code {]AT_SKEL_CC_IF([[
292 #include <string>]], [[
295 ]AT_YYERROR_DECLARE])[
296 ]AT_YYLEX_PROTOTYPE[;
300 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
304 %define parse.error verbose
310 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
314 `--------*/]AT_SKEL_JAVA_IF([[
316 public String input = "]$3[";
317 public int index = 0;
320 if (index < input.length ())
321 return input.charAt (index++);
325 public Object getLVal ()
327 return new Integer(1);
332 static char const *input = "]$3[";
348 AT_FULL_COMPILE([[input]])
350 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
351 $5, [a], [[, expecting 'a']],
352 $5, [b], [[, expecting 'b']])])
354 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
355 [AT_PARSER_CHECK([[./input]], [[1]]]),
357 [[syntax error, unexpected ]$4[]AT_EXPECTING[
360 m4_popdef([AT_EXPECTING])
361 m4_popdef([AT_YYLEX_PROTOTYPE])
362 AT_BISON_OPTION_POPDEFS
366 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
369 start: consistent-error-on-a-a 'a' ;
371 consistent-error-on-a-a:
372 'a' default-reduction
373 | 'a' default-reduction 'a'
377 default-reduction: /*empty*/ ;
380 // Provide another context in which all rules are useful so that this
381 // test case looks a little more realistic.
382 start: 'b' consistent-error-on-a-a 'c' ;
385 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
387 # Unfortunately, no expected tokens are reported even though 'b' can be
388 # accepted. Nevertheless, the main point of this test is to make sure
389 # that at least the unexpected token is reported. In a previous version
390 # of Bison, it wasn't reported because the error is detected in a
391 # consistent state with an error action, and that case always triggered
392 # the simple "syntax error" message.
394 # The point isn't to test IELR here, but state merging happens to
395 # complicate this example.
396 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
397 [AT_PREVIOUS_STATE_GRAMMAR],
398 [AT_PREVIOUS_STATE_INPUT],
400 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
402 [AT_PREVIOUS_STATE_GRAMMAR],
403 [AT_PREVIOUS_STATE_INPUT],
405 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
407 [AT_PREVIOUS_STATE_GRAMMAR],
408 [AT_PREVIOUS_STATE_INPUT],
410 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
412 [AT_PREVIOUS_STATE_GRAMMAR],
413 [AT_PREVIOUS_STATE_INPUT],
414 [[end of input]], [[none]])
416 # Even canonical LR doesn't foresee the error for 'a'!
417 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
418 %define lr.default-reduction consistent]],
419 [AT_PREVIOUS_STATE_GRAMMAR],
420 [AT_PREVIOUS_STATE_INPUT],
422 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
423 %define lr.default-reduction accepting]],
424 [AT_PREVIOUS_STATE_GRAMMAR],
425 [AT_PREVIOUS_STATE_INPUT],
427 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
428 [AT_PREVIOUS_STATE_GRAMMAR],
429 [AT_PREVIOUS_STATE_INPUT],
432 # Only LAC gets it right.
433 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
434 %define parse.lac full]],
435 [AT_PREVIOUS_STATE_GRAMMAR],
436 [AT_PREVIOUS_STATE_INPUT],
438 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
439 %define parse.lac full]],
440 [AT_PREVIOUS_STATE_GRAMMAR],
441 [AT_PREVIOUS_STATE_INPUT],
444 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
445 m4_popdef([AT_PREVIOUS_STATE_INPUT])
447 m4_pushdef([AT_USER_ACTION_GRAMMAR],
450 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
451 // incorrectly for the 'b' set in the semantic action below. All 'a'
452 // tokens are returned by yylex, which sets $$ = 1.
455 fprintf (stderr, "Wrong destructor.\n");
458 // Rather than depend on an inconsistent state to induce reading a
459 // lookahead as in the previous grammar, just assign the lookahead in a
460 // semantic action. That lookahead isn't needed before either error
461 // action is encountered. In a previous version of Bison, this was a
462 // problem as it meant yychar was not translated into yytoken before
463 // either error action. The second error action thus invoked a
464 // destructor that it selected according to the incorrect yytoken. The
465 // first error action would have reported an incorrect unexpected token
466 // except that, due to the bug described in the previous grammar, the
467 // unexpected token was not reported at all.
468 start: error-reduce consistent-error 'a' { USE ($][3); } ;
471 'a' 'a' consistent-reduction consistent-error 'a'
472 { USE (($][1, $][2, $][5)); }
477 consistent-reduction: /*empty*/ {
478 assert (yychar == YYEMPTY);
485 | /*empty*/ %prec 'a'
488 // Provide another context in which all rules are useful so that this
489 // test case looks a little more realistic.
490 start: 'b' consistent-error 'b' ;
492 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
494 AT_CONSISTENT_ERRORS_CHECK([[]],
495 [AT_USER_ACTION_GRAMMAR],
496 [AT_USER_ACTION_INPUT],
498 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
499 [AT_USER_ACTION_GRAMMAR],
500 [AT_USER_ACTION_INPUT],
502 # No C++ or Java test because yychar cannot be manipulated by users.
504 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
505 [AT_USER_ACTION_GRAMMAR],
506 [AT_USER_ACTION_INPUT],
509 # Canonical LR doesn't foresee the error for 'a'!
510 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
511 [AT_USER_ACTION_GRAMMAR],
512 [AT_USER_ACTION_INPUT],
514 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
515 [AT_USER_ACTION_GRAMMAR],
516 [AT_USER_ACTION_INPUT],
519 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
520 [AT_USER_ACTION_GRAMMAR],
521 [AT_USER_ACTION_INPUT],
523 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
524 %define lr.default-reduction accepting]],
525 [AT_USER_ACTION_GRAMMAR],
526 [AT_USER_ACTION_INPUT],
529 m4_popdef([AT_USER_ACTION_GRAMMAR])
530 m4_popdef([AT_USER_ACTION_INPUT])
532 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
538 ## ------------------------------------------------------- ##
539 ## LAC: %nonassoc requires splitting canonical LR states. ##
540 ## ------------------------------------------------------- ##
542 # This test case demonstrates that, when %nonassoc is used, canonical
543 # LR(1) parser table construction followed by conflict resolution
544 # without further state splitting is not always sufficient to produce a
545 # parser that can detect all syntax errors as soon as possible on one
546 # token of lookahead. However, LAC solves the problem completely even
547 # with minimal LR parser tables.
549 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
550 AT_BISON_OPTION_PUSHDEFS
551 AT_DATA_GRAMMAR([[input.y]],
564 'a' problem 'a' // First context.
565 | 'b' problem 'b' // Second context.
566 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
575 // For the state reached after shifting the 'a' in these productions,
576 // lookahead sets are the same in both the first and second contexts.
577 // Thus, canonical LR reuses the same state for both contexts. However,
578 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
579 // error action only in the first context. In order to immediately
580 // detect the syntax error on 'a' here for only the first context, this
581 // canonical LR state would have to be split into two states, and the
582 // 'a' lookahead would have to be removed from only one of the states.
584 'a' // Reduction lookahead set is always ['a', 'b'].
586 | 'a' 'c' // 'c' is forgotten as an expected token.
589 reduce-nonassoc: %prec 'a';
593 ]AT_YYLEX_DEFINE(["aaa"])[
596 AT_BISON_OPTION_POPDEFS
598 # Show canonical LR's failure.
599 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
601 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
603 AT_COMPILE([[input]])
604 AT_PARSER_CHECK([[./input]], [[1]], [[]],
605 [[syntax error, unexpected 'a', expecting 'b'
608 # It's corrected by LAC.
609 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
610 -o input.c input.y]], [[0]], [[]],
611 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
613 AT_COMPILE([[input]])
614 AT_PARSER_CHECK([[./input]], [[1]], [[]],
615 [[syntax error, unexpected 'a', expecting 'b' or 'c'
618 # IELR is sufficient when LAC is used.
619 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
621 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
623 AT_COMPILE([[input]])
624 AT_PARSER_CHECK([[./input]], [[1]], [[]],
625 [[syntax error, unexpected 'a', expecting 'b' or 'c'
630 ## ------------------------- ##
631 ## Unresolved SR Conflicts. ##
632 ## ------------------------- ##
634 AT_SETUP([Unresolved SR Conflicts])
636 AT_KEYWORDS([report])
641 exp: exp OP exp | NUM;
644 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
645 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
648 # Check the contents of the report.
649 AT_CHECK([cat input.output], [],
650 [[State 5 conflicts: 1 shift/reduce
661 Terminals, with rules where they appear
669 Nonterminals, with rules where they appear
674 on left: 1 2, on right: 0 1
679 0 $accept: . exp $end
683 NUM shift, and go to state 1
692 $default reduce using rule 2 (exp)
697 0 $accept: exp . $end
700 $end shift, and go to state 3
701 OP shift, and go to state 4
706 0 $accept: exp $end .
717 NUM shift, and go to state 1
725 1 | exp OP exp . [$end, OP]
727 OP shift, and go to state 4
729 OP [reduce using rule 1 (exp)]
730 $default reduce using rule 1 (exp)
737 ## ----------------------- ##
738 ## Resolved SR Conflicts. ##
739 ## ----------------------- ##
741 AT_SETUP([Resolved SR Conflicts])
743 AT_KEYWORDS([report])
749 exp: exp OP exp | NUM;
752 AT_BISON_CHECK([-o input.c --report=all input.y])
754 # Check the contents of the report.
755 AT_CHECK([cat input.output], [],
764 Terminals, with rules where they appear
772 Nonterminals, with rules where they appear
777 on left: 1 2, on right: 0 1
782 0 $accept: . exp $end
786 NUM shift, and go to state 1
795 $default reduce using rule 2 (exp)
800 0 $accept: exp . $end
803 $end shift, and go to state 3
804 OP shift, and go to state 4
809 0 $accept: exp $end .
820 NUM shift, and go to state 1
828 1 | exp OP exp . [$end, OP]
830 $default reduce using rule 1 (exp)
832 Conflict between rule 1 and token OP resolved as reduce (%left OP).
838 ## ---------------------- ##
839 ## %precedence suffices. ##
840 ## ---------------------- ##
842 AT_SETUP([%precedence suffices])
849 "if" cond "then" stmt
850 | "if" cond "then" stmt "else" stmt
859 AT_BISON_CHECK([-o input.c input.y])
864 ## ------------------------------ ##
865 ## %precedence does not suffice. ##
866 ## ------------------------------ ##
868 AT_SETUP([%precedence does not suffice])
875 "if" cond "then" stmt
876 | "if" cond "then" stmt "else" stmt
886 AT_BISON_CHECK([-o input.c input.y], 0, [],
887 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
888 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother]
894 ## -------------------------------- ##
895 ## Defaulted Conflicted Reduction. ##
896 ## -------------------------------- ##
898 # When there are RR conflicts, some rules are disabled. Usually it is
899 # simply displayed as:
901 # $end reduce using rule 3 (num)
902 # $end [reduce using rule 4 (id)]
904 # But when `reduce 3' is the default action, we'd produce:
906 # $end [reduce using rule 4 (id)]
907 # $default reduce using rule 3 (num)
909 # In this precise case (a reduction is masked by the default
910 # reduction), we make the `reduce 3' explicit:
912 # $end reduce using rule 3 (num)
913 # $end [reduce using rule 4 (id)]
914 # $default reduce using rule 3 (num)
916 # Maybe that's not the best display, but then, please propose something
919 AT_SETUP([Defaulted Conflicted Reduction])
920 AT_KEYWORDS([report])
930 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
931 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
932 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother]
935 # Check the contents of the report.
936 AT_CHECK([cat input.output], [],
937 [[Rules useless in parser due to conflicts
942 State 1 conflicts: 1 reduce/reduce
957 Terminals, with rules where they appear
964 Nonterminals, with rules where they appear
969 on left: 1 2, on right: 0
971 on left: 3, on right: 1
973 on left: 4, on right: 2
978 0 $accept: . exp $end
984 '0' shift, and go to state 1
996 $end reduce using rule 3 (num)
997 $end [reduce using rule 4 (id)]
998 $default reduce using rule 3 (num)
1003 0 $accept: exp . $end
1005 $end shift, and go to state 5
1012 $default reduce using rule 1 (exp)
1019 $default reduce using rule 2 (exp)
1024 0 $accept: exp $end .
1034 ## -------------------- ##
1035 ## %expect not enough. ##
1036 ## -------------------- ##
1038 AT_SETUP([%expect not enough])
1044 exp: exp OP exp | NUM;
1047 AT_BISON_CHECK([-o input.c input.y], 1, [],
1048 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1053 ## --------------- ##
1054 ## %expect right. ##
1055 ## --------------- ##
1057 AT_SETUP([%expect right])
1063 exp: exp OP exp | NUM;
1066 AT_BISON_CHECK([-o input.c input.y])
1070 ## ------------------ ##
1071 ## %expect too much. ##
1072 ## ------------------ ##
1074 AT_SETUP([%expect too much])
1080 exp: exp OP exp | NUM;
1083 AT_BISON_CHECK([-o input.c input.y], 1, [],
1084 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1089 ## ------------------------------- ##
1090 ## %expect with reduce conflicts. ##
1091 ## ------------------------------- ##
1093 AT_SETUP([%expect with reduce conflicts])
1098 program: a 'a' | a a;
1102 AT_BISON_CHECK([-o input.c input.y], 1, [],
1103 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1108 ## ------------------------- ##
1109 ## %prec with user strings. ##
1110 ## ------------------------- ##
1112 AT_SETUP([%prec with user string])
1114 AT_DATA([[input.y]],
1121 AT_BISON_CHECK([-o input.c input.y])
1125 ## -------------------------------- ##
1126 ## %no-default-prec without %prec. ##
1127 ## -------------------------------- ##
1129 AT_SETUP([%no-default-prec without %prec])
1131 AT_DATA([[input.y]],
1145 AT_BISON_CHECK([-o input.c input.y], 0, [],
1146 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1151 ## ----------------------------- ##
1152 ## %no-default-prec with %prec. ##
1153 ## ----------------------------- ##
1155 AT_SETUP([%no-default-prec with %prec])
1157 AT_DATA([[input.y]],
1165 e: e '+' e %prec '+'
1171 AT_BISON_CHECK([-o input.c input.y])
1175 ## --------------- ##
1176 ## %default-prec. ##
1177 ## --------------- ##
1179 AT_SETUP([%default-prec])
1181 AT_DATA([[input.y]],
1195 AT_BISON_CHECK([-o input.c input.y])
1199 ## ---------------------------------------------- ##
1200 ## Unreachable States After Conflict Resolution. ##
1201 ## ---------------------------------------------- ##
1203 AT_SETUP([[Unreachable States After Conflict Resolution]])
1205 # If conflict resolution makes states unreachable, remove those states, report
1206 # rules that are then unused, and don't report conflicts in those states. Test
1207 # what happens when a nonterminal becomes useless as a result of state removal
1208 # since that causes lalr.o's goto map to be rewritten.
1210 AT_DATA([[input.y]],
1216 start: resolved_conflict 'a' reported_conflicts 'a' ;
1218 /* S/R conflict resolved as reduce, so the state with item
1219 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1220 * unreachable, and the associated production is useless. */
1226 /* S/R conflict that need not be reported since it is unreachable because of
1227 * the previous conflict resolution. Nonterminal unreachable1 and all its
1228 * productions are useless. */
1234 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1237 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1238 * when their states are renumbered due to state removal. */
1247 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1248 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1249 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1250 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother]
1251 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother]
1252 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1253 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1254 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1255 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1256 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1259 AT_CHECK([[cat input.output]], 0,
1260 [[Rules useless in parser due to conflicts
1262 2 resolved_conflict: 'a' unreachable1
1264 4 unreachable1: 'a' unreachable2
1267 6 unreachable2: /* empty */
1270 9 reported_conflicts: 'a'
1274 State 4 conflicts: 1 shift/reduce
1275 State 5 conflicts: 1 reduce/reduce
1280 0 $accept: start $end
1282 1 start: resolved_conflict 'a' reported_conflicts 'a'
1284 2 resolved_conflict: 'a' unreachable1
1287 4 unreachable1: 'a' unreachable2
1290 6 unreachable2: /* empty */
1293 8 reported_conflicts: 'a'
1298 Terminals, with rules where they appear
1305 Nonterminals, with rules where they appear
1310 on left: 1, on right: 0
1311 resolved_conflict (6)
1312 on left: 2 3, on right: 1
1314 on left: 4 5, on right: 2
1316 on left: 6 7, on right: 4
1317 reported_conflicts (9)
1318 on left: 8 9 10, on right: 1
1323 0 $accept: . start $end
1324 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1325 2 resolved_conflict: . 'a' unreachable1
1328 $default reduce using rule 3 (resolved_conflict)
1331 resolved_conflict go to state 2
1333 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1338 0 $accept: start . $end
1340 $end shift, and go to state 3
1345 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1347 'a' shift, and go to state 4
1352 0 $accept: start $end .
1359 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1360 8 reported_conflicts: . 'a'
1364 'a' shift, and go to state 5
1366 'a' [reduce using rule 10 (reported_conflicts)]
1368 reported_conflicts go to state 6
1373 8 reported_conflicts: 'a' . ['a']
1376 'a' reduce using rule 8 (reported_conflicts)
1377 'a' [reduce using rule 9 (reported_conflicts)]
1378 $default reduce using rule 8 (reported_conflicts)
1383 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1385 'a' shift, and go to state 7
1390 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1392 $default reduce using rule 1 (start)
1395 AT_DATA([[input-keep.y]],
1396 [[%define lr.keep-unreachable-state
1398 AT_CHECK([[cat input.y >> input-keep.y]])
1400 AT_BISON_CHECK([[input-keep.y]], 0, [],
1401 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1402 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1403 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1404 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1405 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1406 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1412 ## ------------------------------------------------------------ ##
1413 ## Solved conflicts report for multiple reductions in a state. ##
1414 ## ------------------------------------------------------------ ##
1416 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1418 # Used to lose earlier solved conflict messages even within a single S/R/R.
1420 AT_DATA([[input.y]],
1436 empty_a: %prec 'a' ;
1437 empty_b: %prec 'b' ;
1438 empty_c1: %prec 'c' ;
1439 empty_c2: %prec 'c' ;
1440 empty_c3: %prec 'd' ;
1442 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1443 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1446 0 $accept: . start $end
1459 13 empty_c3: . ['c']
1461 'b' shift, and go to state 1
1463 'c' reduce using rule 13 (empty_c3)
1464 $default reduce using rule 9 (empty_a)
1467 empty_a go to state 3
1468 empty_b go to state 4
1469 empty_c1 go to state 5
1470 empty_c2 go to state 6
1471 empty_c3 go to state 7
1473 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1474 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1475 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1476 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1477 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1486 ## ------------------------------------------------------------ ##
1487 ## %nonassoc error actions for multiple reductions in a state. ##
1488 ## ------------------------------------------------------------ ##
1490 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1491 # multiple reductions in a state.
1493 # For a %nonassoc error action token, used to print the first remaining
1494 # reduction on that token without brackets.
1496 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1498 AT_DATA([[input.y]],
1499 [[%nonassoc 'a' 'b' 'c'
1511 empty_a: %prec 'a' ;
1512 empty_b: %prec 'b' ;
1513 empty_c1: %prec 'c' ;
1514 empty_c2: %prec 'c' ;
1515 empty_c3: %prec 'c' ;
1518 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1519 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1522 0 $accept: . start $end
1534 12 empty_c2: . ['c']
1535 13 empty_c3: . ['c']
1537 'a' error (nonassociative)
1538 'b' error (nonassociative)
1539 'c' error (nonassociative)
1541 'c' [reduce using rule 12 (empty_c2)]
1542 'c' [reduce using rule 13 (empty_c3)]
1545 empty_a go to state 2
1546 empty_b go to state 3
1547 empty_c1 go to state 4
1548 empty_c2 go to state 5
1549 empty_c3 go to state 6
1551 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1552 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1553 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1561 ## -------------------- ##
1562 ## %expect-rr non GLR. ##
1563 ## -------------------- ##
1565 AT_SETUP([[%expect-rr non GLR]])
1573 AT_BISON_CHECK([[1.y]], [[0]], [],
1574 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1583 AT_BISON_CHECK([[2.y]], [[0]], [],
1584 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1585 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1586 2.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother]
1592 ## ---------------------------------- ##
1593 ## -W versus %expect and %expect-rr. ##
1594 ## ---------------------------------- ##
1596 AT_SETUP([[-W versus %expect and %expect-rr]])
1598 AT_DATA([[sr-rr.y]],
1601 start: 'a' | A 'a' | B 'a' ;
1608 start: 'a' | A 'a' ;
1619 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1620 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1621 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1623 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1624 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1626 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1627 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1631 # This is piece of code is rather complex for a simple task: try every
1632 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1633 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1636 # Number and types of genuine conflicts in the grammar.
1637 for gram in sr-rr sr rr; do
1638 # Number of expected s/r conflicts.
1639 for sr_exp_i in '' 0 1 2; do
1640 # Number of expected r/r conflicts.
1641 for rr_exp_i in '' 0 1 2; do
1642 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1644 # Build grammar file.
1649 if test -n "$sr_exp_i"; then
1651 file=$file-expect-$sr_exp
1652 directives="%expect $sr_exp"
1654 if test -n "$rr_exp_i"; then
1656 file=$file-expect-rr-$rr_exp
1657 directives="$directives %expect-rr $rr_exp"
1660 echo "$directives" > $file
1661 cat $gram.y >> $file
1663 # Number of found conflicts.
1665 (sr) sr_count=1; rr_count=0;;
1666 (rr) sr_count=0; rr_count=1;;
1667 (sr-rr) sr_count=1; rr_count=1;;
1670 # Update number of expected conflicts: if %expect is given then
1671 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1673 case $sr_exp_i:$rr_exp_i in
1679 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1680 ]AT_BISON_CHECK([[-Wnone $file]])[
1681 ]AT_BISON_CHECK([[-Werror $file]])[
1684 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1685 echo "warning: $sr_count shift/reduce conflicts"
1686 elif test "$sr_exp_i" -ne "$sr_count"; then
1687 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1689 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1690 echo "warning: $rr_count reduce/reduce conflicts"
1691 elif test "$rr_exp_i" -ne "$rr_count"; then
1692 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1694 } | sed -e "s/^/$file: /" > experr
1695 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1696 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[