1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2012 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 AT_BANNER([[Conflicts.]])
21 ## ---------------- ##
23 ## ---------------- ##
25 # I once hacked Bison in such a way that it lost its reductions on the
26 # initial state (because it was confusing it with the last state). It
27 # took me a while to strip down my failures to this simple case. So
28 # make sure it finds the s/r conflict below.
30 AT_SETUP([S/R in initial])
36 e: 'e' | /* Nothing. */;
39 AT_BISON_CHECK([-o input.c input.y], 0, [],
40 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */ [-Wother]
46 ## ------------------- ##
47 ## %nonassoc and eof. ##
48 ## ------------------- ##
50 AT_SETUP([%nonassoc and eof])
52 AT_BISON_OPTION_PUSHDEFS
53 AT_DATA_GRAMMAR([input.y],
61 #define YYERROR_VERBOSE 1
63 /* The current argument. */
64 static const char *input;
70 assert (toknum <= strlen (input));
71 return input[toknum++];
85 main (int argc, const char *argv[])
87 input = argc <= 1 ? "" : argv[1];
91 AT_BISON_OPTION_POPDEFS
93 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
94 [AT_BISON_CHECK([$1[ -o input.c input.y]])
97 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
99 AT_PARSER_CHECK([./input '0<0'])
100 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
101 [syntax error, unexpected '<'AT_EXPECTING
104 AT_PARSER_CHECK([./input '0>0'])
105 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
106 [syntax error, unexpected '>'AT_EXPECTING
109 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
110 [syntax error, unexpected '>'AT_EXPECTING
113 m4_popdef([AT_EXPECTING])])
115 # Expected token list is missing.
116 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
118 # We must disable default reductions in inconsistent states in order to
119 # have an explicit list of all expected tokens.
120 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reductions=consistent]],
123 # lr.default-reductions=consistent happens to work for this test case.
124 # However, for other grammars, lookahead sets can be merged for
125 # different left contexts, so it is still possible to have an incorrect
126 # expected list. Canonical LR is almost a general solution (that is, it
127 # can fail only when %nonassoc is used), so make sure it gives the same
129 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
131 # parse.lac=full is a completely general solution that does not require
132 # any of the above sacrifices. Of course, it does not extend the
133 # language-recognition power of LALR to (IE)LR, but it does ensure that
134 # the reported list of expected tokens matches what the given parser
135 # would have accepted in place of the unexpected token.
136 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
138 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
144 ## ------------------------------------------- ##
145 ## parse.error=verbose and consistent errors. ##
146 ## ------------------------------------------- ##
148 AT_SETUP([[parse.error=verbose and consistent errors]])
150 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
152 AT_BISON_OPTION_PUSHDEFS([$1])
154 m4_pushdef([AT_YYLEX_PROTOTYPE],
155 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
156 [[int yylex (YYSTYPE *lvalp)]])])
158 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
162 import java.io.IOException;
165 %code {]AT_SKEL_CC_IF([[
166 #include <string>]], [[
169 ]AT_YYERROR_DECLARE])[
170 ]AT_YYLEX_PROTOTYPE[;
174 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
178 %define parse.error verbose
184 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
188 `--------*/]AT_SKEL_JAVA_IF([[
190 public String input = "]$3[";
191 public int index = 0;
194 if (index < input.length ())
195 return input.charAt (index++);
199 public Object getLVal ()
201 return new Integer(1);
206 static char const *input = "]$3[";
218 `-------*/]AT_SKEL_JAVA_IF([[
222 public static void main (String args[]) throws IOException
224 YYParser p = new YYParser ();
227 }]], [AT_SKEL_CC_IF([[
233 return parser.parse ();
243 AT_FULL_COMPILE([[input]])
245 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
246 $5, [a], [[, expecting 'a']],
247 $5, [b], [[, expecting 'b']])])
249 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
250 [AT_PARSER_CHECK([[./input]], [[1]]]),
252 [[syntax error, unexpected ]$4[]AT_EXPECTING[
255 m4_popdef([AT_EXPECTING])
256 m4_popdef([AT_YYLEX_PROTOTYPE])
257 AT_BISON_OPTION_POPDEFS
261 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
264 start: consistent-error-on-a-a 'a' ;
266 consistent-error-on-a-a:
267 'a' default-reduction
268 | 'a' default-reduction 'a'
272 default-reduction: /*empty*/ ;
275 // Provide another context in which all rules are useful so that this
276 // test case looks a little more realistic.
277 start: 'b' consistent-error-on-a-a 'c' ;
280 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
282 # Unfortunately, no expected tokens are reported even though 'b' can be
283 # accepted. Nevertheless, the main point of this test is to make sure
284 # that at least the unexpected token is reported. In a previous version
285 # of Bison, it wasn't reported because the error is detected in a
286 # consistent state with an error action, and that case always triggered
287 # the simple "syntax error" message.
289 # The point isn't to test IELR here, but state merging happens to
290 # complicate this example.
291 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
292 [AT_PREVIOUS_STATE_GRAMMAR],
293 [AT_PREVIOUS_STATE_INPUT],
295 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
297 [AT_PREVIOUS_STATE_GRAMMAR],
298 [AT_PREVIOUS_STATE_INPUT],
300 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
302 [AT_PREVIOUS_STATE_GRAMMAR],
303 [AT_PREVIOUS_STATE_INPUT],
305 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
307 [AT_PREVIOUS_STATE_GRAMMAR],
308 [AT_PREVIOUS_STATE_INPUT],
309 [[end of input]], [[none]])
311 # Even canonical LR doesn't foresee the error for 'a'!
312 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
313 %define lr.default-reductions consistent]],
314 [AT_PREVIOUS_STATE_GRAMMAR],
315 [AT_PREVIOUS_STATE_INPUT],
317 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
318 %define lr.default-reductions accepting]],
319 [AT_PREVIOUS_STATE_GRAMMAR],
320 [AT_PREVIOUS_STATE_INPUT],
322 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
323 [AT_PREVIOUS_STATE_GRAMMAR],
324 [AT_PREVIOUS_STATE_INPUT],
327 # Only LAC gets it right.
328 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
329 %define parse.lac full]],
330 [AT_PREVIOUS_STATE_GRAMMAR],
331 [AT_PREVIOUS_STATE_INPUT],
333 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
334 %define parse.lac full]],
335 [AT_PREVIOUS_STATE_GRAMMAR],
336 [AT_PREVIOUS_STATE_INPUT],
339 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
340 m4_popdef([AT_PREVIOUS_STATE_INPUT])
342 m4_pushdef([AT_USER_ACTION_GRAMMAR],
345 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
346 // incorrectly for the 'b' set in the semantic action below. All 'a'
347 // tokens are returned by yylex, which sets $$ = 1.
350 fprintf (stderr, "Wrong destructor.\n");
353 // Rather than depend on an inconsistent state to induce reading a
354 // lookahead as in the previous grammar, just assign the lookahead in a
355 // semantic action. That lookahead isn't needed before either error
356 // action is encountered. In a previous version of Bison, this was a
357 // problem as it meant yychar was not translated into yytoken before
358 // either error action. The second error action thus invoked a
359 // destructor that it selected according to the incorrect yytoken. The
360 // first error action would have reported an incorrect unexpected token
361 // except that, due to the bug described in the previous grammar, the
362 // unexpected token was not reported at all.
363 start: error-reduce consistent-error 'a' { USE ($][3); } ;
366 'a' 'a' consistent-reduction consistent-error 'a'
367 { USE (($][1, $][2, $][5)); }
372 consistent-reduction: /*empty*/ {
373 assert (yychar == YYEMPTY);
380 | /*empty*/ %prec 'a'
383 // Provide another context in which all rules are useful so that this
384 // test case looks a little more realistic.
385 start: 'b' consistent-error 'b' ;
387 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
389 AT_CONSISTENT_ERRORS_CHECK([[]],
390 [AT_USER_ACTION_GRAMMAR],
391 [AT_USER_ACTION_INPUT],
393 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
394 [AT_USER_ACTION_GRAMMAR],
395 [AT_USER_ACTION_INPUT],
397 # No C++ or Java test because yychar cannot be manipulated by users.
399 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]],
400 [AT_USER_ACTION_GRAMMAR],
401 [AT_USER_ACTION_INPUT],
404 # Canonical LR doesn't foresee the error for 'a'!
405 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions accepting]],
406 [AT_USER_ACTION_GRAMMAR],
407 [AT_USER_ACTION_INPUT],
409 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
410 [AT_USER_ACTION_GRAMMAR],
411 [AT_USER_ACTION_INPUT],
414 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
415 [AT_USER_ACTION_GRAMMAR],
416 [AT_USER_ACTION_INPUT],
418 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
419 %define lr.default-reductions accepting]],
420 [AT_USER_ACTION_GRAMMAR],
421 [AT_USER_ACTION_INPUT],
424 m4_popdef([AT_USER_ACTION_GRAMMAR])
425 m4_popdef([AT_USER_ACTION_INPUT])
427 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
433 ## ------------------------------------------------------- ##
434 ## LAC: %nonassoc requires splitting canonical LR states. ##
435 ## ------------------------------------------------------- ##
437 # This test case demonstrates that, when %nonassoc is used, canonical
438 # LR(1) parser table construction followed by conflict resolution
439 # without further state splitting is not always sufficient to produce a
440 # parser that can detect all syntax errors as soon as possible on one
441 # token of lookahead. However, LAC solves the problem completely even
442 # with minimal LR parser tables.
444 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
445 AT_BISON_OPTION_PUSHDEFS
446 AT_DATA_GRAMMAR([[input.y]],
459 'a' problem 'a' // First context.
460 | 'b' problem 'b' // Second context.
461 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
470 // For the state reached after shifting the 'a' in these productions,
471 // lookahead sets are the same in both the first and second contexts.
472 // Thus, canonical LR reuses the same state for both contexts. However,
473 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
474 // error action only in the first context. In order to immediately
475 // detect the syntax error on 'a' here for only the first context, this
476 // canonical LR state would have to be split into two states, and the
477 // 'a' lookahead would have to be removed from only one of the states.
479 'a' // Reduction lookahead set is always ['a', 'b'].
481 | 'a' 'c' // 'c' is forgotten as an expected token.
484 reduce-nonassoc: %prec 'a';
488 ]AT_YYLEX_DEFINE(["aaa"])[
496 AT_BISON_OPTION_POPDEFS
498 # Show canonical LR's failure.
499 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
501 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
503 AT_COMPILE([[input]])
504 AT_PARSER_CHECK([[./input]], [[1]], [[]],
505 [[syntax error, unexpected 'a', expecting 'b'
508 # It's corrected by LAC.
509 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
510 -o input.c input.y]], [[0]], [[]],
511 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
513 AT_COMPILE([[input]])
514 AT_PARSER_CHECK([[./input]], [[1]], [[]],
515 [[syntax error, unexpected 'a', expecting 'b' or 'c'
518 # IELR is sufficient when LAC is used.
519 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
521 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
523 AT_COMPILE([[input]])
524 AT_PARSER_CHECK([[./input]], [[1]], [[]],
525 [[syntax error, unexpected 'a', expecting 'b' or 'c'
530 ## ------------------------- ##
531 ## Unresolved SR Conflicts. ##
532 ## ------------------------- ##
534 AT_SETUP([Unresolved SR Conflicts])
536 AT_KEYWORDS([report])
541 exp: exp OP exp | NUM;
544 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
545 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
548 # Check the contents of the report.
549 AT_CHECK([cat input.output], [],
550 [[State 5 conflicts: 1 shift/reduce
561 Terminals, with rules where they appear
569 Nonterminals, with rules where they appear
574 on left: 1 2, on right: 0 1
579 0 $accept: . exp $end
583 NUM shift, and go to state 1
592 $default reduce using rule 2 (exp)
597 0 $accept: exp . $end
600 $end shift, and go to state 3
601 OP shift, and go to state 4
606 0 $accept: exp $end .
617 NUM shift, and go to state 1
625 1 | exp OP exp . [$end, OP]
627 OP shift, and go to state 4
629 OP [reduce using rule 1 (exp)]
630 $default reduce using rule 1 (exp)
637 ## ----------------------- ##
638 ## Resolved SR Conflicts. ##
639 ## ----------------------- ##
641 AT_SETUP([Resolved SR Conflicts])
643 AT_KEYWORDS([report])
649 exp: exp OP exp | NUM;
652 AT_BISON_CHECK([-o input.c --report=all input.y])
654 # Check the contents of the report.
655 AT_CHECK([cat input.output], [],
664 Terminals, with rules where they appear
672 Nonterminals, with rules where they appear
677 on left: 1 2, on right: 0 1
682 0 $accept: . exp $end
686 NUM shift, and go to state 1
695 $default reduce using rule 2 (exp)
700 0 $accept: exp . $end
703 $end shift, and go to state 3
704 OP shift, and go to state 4
709 0 $accept: exp $end .
720 NUM shift, and go to state 1
728 1 | exp OP exp . [$end, OP]
730 $default reduce using rule 1 (exp)
732 Conflict between rule 1 and token OP resolved as reduce (%left OP).
738 ## ---------------------- ##
739 ## %precedence suffices. ##
740 ## ---------------------- ##
742 AT_SETUP([%precedence suffices])
749 "if" cond "then" stmt
750 | "if" cond "then" stmt "else" stmt
759 AT_BISON_CHECK([-o input.c input.y])
764 ## ------------------------------ ##
765 ## %precedence does not suffice. ##
766 ## ------------------------------ ##
768 AT_SETUP([%precedence does not suffice])
775 "if" cond "then" stmt
776 | "if" cond "then" stmt "else" stmt
786 AT_BISON_CHECK([-o input.c input.y], 0, [],
787 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
788 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother]
794 ## -------------------------------- ##
795 ## Defaulted Conflicted Reduction. ##
796 ## -------------------------------- ##
798 # When there are RR conflicts, some rules are disabled. Usually it is
799 # simply displayed as:
801 # $end reduce using rule 3 (num)
802 # $end [reduce using rule 4 (id)]
804 # But when `reduce 3' is the default action, we'd produce:
806 # $end [reduce using rule 4 (id)]
807 # $default reduce using rule 3 (num)
809 # In this precise case (a reduction is masked by the default
810 # reduction), we make the `reduce 3' explicit:
812 # $end reduce using rule 3 (num)
813 # $end [reduce using rule 4 (id)]
814 # $default reduce using rule 3 (num)
816 # Maybe that's not the best display, but then, please propose something
819 AT_SETUP([Defaulted Conflicted Reduction])
820 AT_KEYWORDS([report])
830 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
831 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
832 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother]
835 # Check the contents of the report.
836 AT_CHECK([cat input.output], [],
837 [[Rules useless in parser due to conflicts
842 State 1 conflicts: 1 reduce/reduce
857 Terminals, with rules where they appear
864 Nonterminals, with rules where they appear
869 on left: 1 2, on right: 0
871 on left: 3, on right: 1
873 on left: 4, on right: 2
878 0 $accept: . exp $end
884 '0' shift, and go to state 1
896 $end reduce using rule 3 (num)
897 $end [reduce using rule 4 (id)]
898 $default reduce using rule 3 (num)
903 0 $accept: exp . $end
905 $end shift, and go to state 5
912 $default reduce using rule 1 (exp)
919 $default reduce using rule 2 (exp)
924 0 $accept: exp $end .
934 ## -------------------- ##
935 ## %expect not enough. ##
936 ## -------------------- ##
938 AT_SETUP([%expect not enough])
944 exp: exp OP exp | NUM;
947 AT_BISON_CHECK([-o input.c input.y], 1, [],
948 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
953 ## --------------- ##
955 ## --------------- ##
957 AT_SETUP([%expect right])
963 exp: exp OP exp | NUM;
966 AT_BISON_CHECK([-o input.c input.y])
970 ## ------------------ ##
971 ## %expect too much. ##
972 ## ------------------ ##
974 AT_SETUP([%expect too much])
980 exp: exp OP exp | NUM;
983 AT_BISON_CHECK([-o input.c input.y], 1, [],
984 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
989 ## ------------------------------- ##
990 ## %expect with reduce conflicts. ##
991 ## ------------------------------- ##
993 AT_SETUP([%expect with reduce conflicts])
998 program: a 'a' | a a;
1002 AT_BISON_CHECK([-o input.c input.y], 1, [],
1003 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1008 ## ------------------------- ##
1009 ## %prec with user strings. ##
1010 ## ------------------------- ##
1012 AT_SETUP([%prec with user string])
1014 AT_DATA([[input.y]],
1021 AT_BISON_CHECK([-o input.c input.y])
1025 ## -------------------------------- ##
1026 ## %no-default-prec without %prec. ##
1027 ## -------------------------------- ##
1029 AT_SETUP([%no-default-prec without %prec])
1031 AT_DATA([[input.y]],
1045 AT_BISON_CHECK([-o input.c input.y], 0, [],
1046 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1051 ## ----------------------------- ##
1052 ## %no-default-prec with %prec. ##
1053 ## ----------------------------- ##
1055 AT_SETUP([%no-default-prec with %prec])
1057 AT_DATA([[input.y]],
1065 e: e '+' e %prec '+'
1071 AT_BISON_CHECK([-o input.c input.y])
1075 ## --------------- ##
1076 ## %default-prec. ##
1077 ## --------------- ##
1079 AT_SETUP([%default-prec])
1081 AT_DATA([[input.y]],
1095 AT_BISON_CHECK([-o input.c input.y])
1099 ## ---------------------------------------------- ##
1100 ## Unreachable States After Conflict Resolution. ##
1101 ## ---------------------------------------------- ##
1103 AT_SETUP([[Unreachable States After Conflict Resolution]])
1105 # If conflict resolution makes states unreachable, remove those states, report
1106 # rules that are then unused, and don't report conflicts in those states. Test
1107 # what happens when a nonterminal becomes useless as a result of state removal
1108 # since that causes lalr.o's goto map to be rewritten.
1110 AT_DATA([[input.y]],
1116 start: resolved_conflict 'a' reported_conflicts 'a' ;
1118 /* S/R conflict resolved as reduce, so the state with item
1119 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1120 * unreachable, and the associated production is useless. */
1126 /* S/R conflict that need not be reported since it is unreachable because of
1127 * the previous conflict resolution. Nonterminal unreachable1 and all its
1128 * productions are useless. */
1134 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1137 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1138 * when their states are renumbered due to state removal. */
1147 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1148 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1149 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1150 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother]
1151 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother]
1152 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1153 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1154 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1155 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1156 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1159 AT_CHECK([[cat input.output]], 0,
1160 [[Rules useless in parser due to conflicts
1162 2 resolved_conflict: 'a' unreachable1
1164 4 unreachable1: 'a' unreachable2
1167 6 unreachable2: /* empty */
1170 9 reported_conflicts: 'a'
1174 State 4 conflicts: 1 shift/reduce
1175 State 5 conflicts: 1 reduce/reduce
1180 0 $accept: start $end
1182 1 start: resolved_conflict 'a' reported_conflicts 'a'
1184 2 resolved_conflict: 'a' unreachable1
1187 4 unreachable1: 'a' unreachable2
1190 6 unreachable2: /* empty */
1193 8 reported_conflicts: 'a'
1198 Terminals, with rules where they appear
1205 Nonterminals, with rules where they appear
1210 on left: 1, on right: 0
1211 resolved_conflict (6)
1212 on left: 2 3, on right: 1
1214 on left: 4 5, on right: 2
1216 on left: 6 7, on right: 4
1217 reported_conflicts (9)
1218 on left: 8 9 10, on right: 1
1223 0 $accept: . start $end
1224 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1225 2 resolved_conflict: . 'a' unreachable1
1228 $default reduce using rule 3 (resolved_conflict)
1231 resolved_conflict go to state 2
1233 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1238 0 $accept: start . $end
1240 $end shift, and go to state 3
1245 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1247 'a' shift, and go to state 4
1252 0 $accept: start $end .
1259 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1260 8 reported_conflicts: . 'a'
1264 'a' shift, and go to state 5
1266 'a' [reduce using rule 10 (reported_conflicts)]
1268 reported_conflicts go to state 6
1273 8 reported_conflicts: 'a' . ['a']
1276 'a' reduce using rule 8 (reported_conflicts)
1277 'a' [reduce using rule 9 (reported_conflicts)]
1278 $default reduce using rule 8 (reported_conflicts)
1283 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1285 'a' shift, and go to state 7
1290 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1292 $default reduce using rule 1 (start)
1295 AT_DATA([[input-keep.y]],
1296 [[%define lr.keep-unreachable-states
1298 AT_CHECK([[cat input.y >> input-keep.y]])
1300 AT_BISON_CHECK([[input-keep.y]], 0, [],
1301 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1302 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1303 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1304 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1305 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1306 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1312 ## ------------------------------------------------------------ ##
1313 ## Solved conflicts report for multiple reductions in a state. ##
1314 ## ------------------------------------------------------------ ##
1316 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1318 # Used to lose earlier solved conflict messages even within a single S/R/R.
1320 AT_DATA([[input.y]],
1336 empty_a: %prec 'a' ;
1337 empty_b: %prec 'b' ;
1338 empty_c1: %prec 'c' ;
1339 empty_c2: %prec 'c' ;
1340 empty_c3: %prec 'd' ;
1342 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1343 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1346 0 $accept: . start $end
1359 13 empty_c3: . ['c']
1361 'b' shift, and go to state 1
1363 'c' reduce using rule 13 (empty_c3)
1364 $default reduce using rule 9 (empty_a)
1367 empty_a go to state 3
1368 empty_b go to state 4
1369 empty_c1 go to state 5
1370 empty_c2 go to state 6
1371 empty_c3 go to state 7
1373 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1374 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1375 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1376 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1377 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1386 ## ------------------------------------------------------------ ##
1387 ## %nonassoc error actions for multiple reductions in a state. ##
1388 ## ------------------------------------------------------------ ##
1390 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1391 # multiple reductions in a state.
1393 # For a %nonassoc error action token, used to print the first remaining
1394 # reduction on that token without brackets.
1396 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1398 AT_DATA([[input.y]],
1399 [[%nonassoc 'a' 'b' 'c'
1411 empty_a: %prec 'a' ;
1412 empty_b: %prec 'b' ;
1413 empty_c1: %prec 'c' ;
1414 empty_c2: %prec 'c' ;
1415 empty_c3: %prec 'c' ;
1418 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1419 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1422 0 $accept: . start $end
1434 12 empty_c2: . ['c']
1435 13 empty_c3: . ['c']
1437 'a' error (nonassociative)
1438 'b' error (nonassociative)
1439 'c' error (nonassociative)
1441 'c' [reduce using rule 12 (empty_c2)]
1442 'c' [reduce using rule 13 (empty_c3)]
1445 empty_a go to state 2
1446 empty_b go to state 3
1447 empty_c1 go to state 4
1448 empty_c2 go to state 5
1449 empty_c3 go to state 6
1451 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1452 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1453 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1461 ## -------------------- ##
1462 ## %expect-rr non GLR. ##
1463 ## -------------------- ##
1465 AT_SETUP([[%expect-rr non GLR]])
1473 AT_BISON_CHECK([[1.y]], [[0]], [],
1474 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1483 AT_BISON_CHECK([[2.y]], [[0]], [],
1484 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1485 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1486 2.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother]
1492 ## ---------------------------------- ##
1493 ## -W versus %expect and %expect-rr. ##
1494 ## ---------------------------------- ##
1496 AT_SETUP([[-W versus %expect and %expect-rr]])
1498 AT_DATA([[sr-rr.y]],
1501 start: 'a' | A 'a' | B 'a' ;
1508 start: 'a' | A 'a' ;
1519 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1520 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1521 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1523 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1524 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1526 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1527 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1531 # This is piece of code is rather complex for a simple task: try every
1532 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1533 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1536 # Number and types of genuine conflicts in the grammar.
1537 for gram in sr-rr sr rr; do
1538 # Number of expected s/r conflicts.
1539 for sr_exp_i in '' 0 1 2; do
1540 # Number of expected r/r conflicts.
1541 for rr_exp_i in '' 0 1 2; do
1542 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1544 # Build grammar file.
1549 if test -n "$sr_exp_i"; then
1551 file=$file-expect-$sr_exp
1552 directives="%expect $sr_exp"
1554 if test -n "$rr_exp_i"; then
1556 file=$file-expect-rr-$rr_exp
1557 directives="$directives %expect-rr $rr_exp"
1560 echo "$directives" > $file
1561 cat $gram.y >> $file
1563 # Number of found conflicts.
1565 (sr) sr_count=1; rr_count=0;;
1566 (rr) sr_count=0; rr_count=1;;
1567 (sr-rr) sr_count=1; rr_count=1;;
1570 # Update number of expected conflicts: if %expect is given then
1571 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1573 case $sr_exp_i:$rr_exp_i in
1579 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1580 ]AT_BISON_CHECK([[-Wnone $file]])[
1581 ]AT_BISON_CHECK([[-Werror $file]])[
1584 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1585 echo "warning: $sr_count shift/reduce conflicts"
1586 elif test "$sr_exp_i" -ne "$sr_count"; then
1587 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1589 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1590 echo "warning: $rr_count reduce/reduce conflicts"
1591 elif test "$rr_exp_i" -ne "$rr_count"; then
1592 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1594 } | sed -e "s/^/$file: /" > experr
1595 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1596 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[