1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2011 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 */
46 ## ------------------- ##
47 ## %nonassoc and eof. ##
48 ## ------------------- ##
50 AT_SETUP([%nonassoc and eof])
52 AT_DATA_GRAMMAR([input.y],
59 #define YYERROR_VERBOSE 1
61 yyerror (const char *msg)
63 fprintf (stderr, "%s\n", msg);
66 /* The current argument. */
67 static const char *input;
73 if (! (toknum <= strlen (input)))
75 return input[toknum++];
89 main (int argc, const char *argv[])
91 input = argc <= 1 ? "" : argv[1];
96 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
97 [AT_BISON_CHECK([$1[ -o input.c input.y]])
100 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
102 AT_PARSER_CHECK([./input '0<0'])
103 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
104 [syntax error, unexpected '<'AT_EXPECTING
107 AT_PARSER_CHECK([./input '0>0'])
108 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
109 [syntax error, unexpected '>'AT_EXPECTING
112 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
113 [syntax error, unexpected '>'AT_EXPECTING
116 m4_popdef([AT_EXPECTING])])
118 # Expected token list is missing.
119 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
121 # We must disable default reductions in inconsistent states in order to
122 # have an explicit list of all expected tokens.
123 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reductions=consistent]],
126 # lr.default-reductions=consistent happens to work for this test case.
127 # However, for other grammars, lookahead sets can be merged for
128 # different left contexts, so it is still possible to have an incorrect
129 # expected list. Canonical LR is almost a general solution (that is, it
130 # can fail only when %nonassoc is used), so make sure it gives the same
132 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
134 # parse.lac=full is a completely general solution that does not require
135 # any of the above sacrifices. Of course, it does not extend the
136 # language-recognition power of LALR to (IE)LR, but it does ensure that
137 # the reported list of expected tokens matches what the given parser
138 # would have accepted in place of the unexpected token.
139 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
141 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
147 ## ------------------------------------------- ##
148 ## parse.error=verbose and consistent errors. ##
149 ## ------------------------------------------- ##
151 AT_SETUP([[parse.error=verbose and consistent errors]])
153 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
155 AT_BISON_OPTION_PUSHDEFS([$1])
157 m4_pushdef([AT_YYLEX_PROTOTYPE],
158 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
159 [[int yylex (YYSTYPE *lvalp)]])])
161 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
165 import java.io.IOException;
168 %code {]AT_SKEL_CC_IF([[
169 #include <string>]], [[
172 void yyerror (char const *msg);]])[
173 ]AT_YYLEX_PROTOTYPE[;
177 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
181 %define parse.error verbose
187 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
191 `--------*/]AT_SKEL_JAVA_IF([[
193 public String input = "]$3[";
194 public int index = 0;
197 if (index < input.length ())
198 return input.charAt (index++);
202 public Object getLVal ()
204 return new Integer(1);
209 static char const *input = "]$3[";
216 `----------*/]AT_SKEL_JAVA_IF([[
218 public void yyerror (String msg)
220 System.err.println (msg);
225 %%]], [AT_SKEL_CC_IF([[
228 yy::parser::error (std::string const &msg)
230 std::cerr << msg << std::endl;
234 yyerror (char const *msg)
236 fprintf (stderr, "%s\n", msg);
241 `-------*/]AT_SKEL_JAVA_IF([[
245 public static void main (String args[]) throws IOException
247 YYParser p = new YYParser ();
250 }]], [AT_SKEL_CC_IF([[
256 return parser.parse ();
266 AT_FULL_COMPILE([[input]])
268 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
269 $5, [a], [[, expecting 'a']],
270 $5, [b], [[, expecting 'b']])])
272 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
273 [AT_PARSER_CHECK([[./input]], [[1]]]),
275 [[syntax error, unexpected ]$4[]AT_EXPECTING[
278 m4_popdef([AT_EXPECTING])
279 m4_popdef([AT_YYLEX_PROTOTYPE])
280 AT_BISON_OPTION_POPDEFS
284 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
287 start: consistent-error-on-a-a 'a' ;
289 consistent-error-on-a-a:
290 'a' default-reduction
291 | 'a' default-reduction 'a'
295 default-reduction: /*empty*/ ;
298 // Provide another context in which all rules are useful so that this
299 // test case looks a little more realistic.
300 start: 'b' consistent-error-on-a-a 'c' ;
303 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
305 # Unfortunately, no expected tokens are reported even though 'b' can be
306 # accepted. Nevertheless, the main point of this test is to make sure
307 # that at least the unexpected token is reported. In a previous version
308 # of Bison, it wasn't reported because the error is detected in a
309 # consistent state with an error action, and that case always triggered
310 # the simple "syntax error" message.
312 # The point isn't to test IELR here, but state merging happens to
313 # complicate this example.
314 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
315 [AT_PREVIOUS_STATE_GRAMMAR],
316 [AT_PREVIOUS_STATE_INPUT],
318 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
320 [AT_PREVIOUS_STATE_GRAMMAR],
321 [AT_PREVIOUS_STATE_INPUT],
323 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
325 [AT_PREVIOUS_STATE_GRAMMAR],
326 [AT_PREVIOUS_STATE_INPUT],
328 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
330 [AT_PREVIOUS_STATE_GRAMMAR],
331 [AT_PREVIOUS_STATE_INPUT],
332 [[end of input]], [[none]])
334 # Even canonical LR doesn't foresee the error for 'a'!
335 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
336 %define lr.default-reductions consistent]],
337 [AT_PREVIOUS_STATE_GRAMMAR],
338 [AT_PREVIOUS_STATE_INPUT],
340 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
341 %define lr.default-reductions accepting]],
342 [AT_PREVIOUS_STATE_GRAMMAR],
343 [AT_PREVIOUS_STATE_INPUT],
345 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
346 [AT_PREVIOUS_STATE_GRAMMAR],
347 [AT_PREVIOUS_STATE_INPUT],
350 # Only LAC gets it right.
351 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
352 %define parse.lac full]],
353 [AT_PREVIOUS_STATE_GRAMMAR],
354 [AT_PREVIOUS_STATE_INPUT],
356 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
357 %define parse.lac full]],
358 [AT_PREVIOUS_STATE_GRAMMAR],
359 [AT_PREVIOUS_STATE_INPUT],
362 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
363 m4_popdef([AT_PREVIOUS_STATE_INPUT])
365 m4_pushdef([AT_USER_ACTION_GRAMMAR],
368 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
369 // incorrectly for the 'b' set in the semantic action below. All 'a'
370 // tokens are returned by yylex, which sets $$ = 1.
373 fprintf (stderr, "Wrong destructor.\n");
376 // Rather than depend on an inconsistent state to induce reading a
377 // lookahead as in the previous grammar, just assign the lookahead in a
378 // semantic action. That lookahead isn't needed before either error
379 // action is encountered. In a previous version of Bison, this was a
380 // problem as it meant yychar was not translated into yytoken before
381 // either error action. The second error action thus invoked a
382 // destructor that it selected according to the incorrect yytoken. The
383 // first error action would have reported an incorrect unexpected token
384 // except that, due to the bug described in the previous grammar, the
385 // unexpected token was not reported at all.
386 start: error-reduce consistent-error 'a' { USE ($][3); } ;
389 'a' 'a' consistent-reduction consistent-error 'a'
390 { USE (($][1, $][2, $][5)); }
395 consistent-reduction: /*empty*/ {
396 assert (yychar == YYEMPTY);
403 | /*empty*/ %prec 'a'
406 // Provide another context in which all rules are useful so that this
407 // test case looks a little more realistic.
408 start: 'b' consistent-error 'b' ;
410 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
412 AT_CONSISTENT_ERRORS_CHECK([[]],
413 [AT_USER_ACTION_GRAMMAR],
414 [AT_USER_ACTION_INPUT],
416 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
417 [AT_USER_ACTION_GRAMMAR],
418 [AT_USER_ACTION_INPUT],
420 # No C++ or Java test because yychar cannot be manipulated by users.
422 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]],
423 [AT_USER_ACTION_GRAMMAR],
424 [AT_USER_ACTION_INPUT],
427 # Canonical LR doesn't foresee the error for 'a'!
428 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions accepting]],
429 [AT_USER_ACTION_GRAMMAR],
430 [AT_USER_ACTION_INPUT],
432 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
433 [AT_USER_ACTION_GRAMMAR],
434 [AT_USER_ACTION_INPUT],
437 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
438 [AT_USER_ACTION_GRAMMAR],
439 [AT_USER_ACTION_INPUT],
441 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
442 %define lr.default-reductions accepting]],
443 [AT_USER_ACTION_GRAMMAR],
444 [AT_USER_ACTION_INPUT],
447 m4_popdef([AT_USER_ACTION_GRAMMAR])
448 m4_popdef([AT_USER_ACTION_INPUT])
450 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
456 ## ------------------------------------------------------- ##
457 ## LAC: %nonassoc requires splitting canonical LR states. ##
458 ## ------------------------------------------------------- ##
460 # This test case demonstrates that, when %nonassoc is used, canonical
461 # LR(1) parser table construction followed by conflict resolution
462 # without further state splitting is not always sufficient to produce a
463 # parser that can detect all syntax errors as soon as possible on one
464 # token of lookahead. However, LAC solves the problem completely even
465 # with minimal LR parser tables.
467 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
469 AT_DATA_GRAMMAR([[input.y]],
472 void yyerror (char const *);
482 'a' problem 'a' // First context.
483 | 'b' problem 'b' // Second context.
484 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
493 // For the state reached after shifting the 'a' in these productions,
494 // lookahead sets are the same in both the first and second contexts.
495 // Thus, canonical LR reuses the same state for both contexts. However,
496 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
497 // error action only in the first context. In order to immediately
498 // detect the syntax error on 'a' here for only the first context, this
499 // canonical LR state would have to be split into two states, and the
500 // 'a' lookahead would have to be removed from only one of the states.
502 'a' // Reduction lookahead set is always ['a', 'b'].
504 | 'a' 'c' // 'c' is forgotten as an expected token.
507 reduce-nonassoc: %prec 'a';
512 yyerror (char const *msg)
514 fprintf (stderr, "%s\n", msg);
520 char const *input = "aaa";
531 # Show canonical LR's failure.
532 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
534 [[input.y: conflicts: 2 shift/reduce
536 AT_COMPILE([[input]])
537 AT_PARSER_CHECK([[./input]], [[1]], [[]],
538 [[syntax error, unexpected 'a', expecting 'b'
541 # It's corrected by LAC.
542 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
543 -o input.c input.y]], [[0]], [[]],
544 [[input.y: conflicts: 2 shift/reduce
546 AT_COMPILE([[input]])
547 AT_PARSER_CHECK([[./input]], [[1]], [[]],
548 [[syntax error, unexpected 'a', expecting 'b' or 'c'
551 # IELR is sufficient when LAC is used.
552 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
554 [[input.y: conflicts: 2 shift/reduce
556 AT_COMPILE([[input]])
557 AT_PARSER_CHECK([[./input]], [[1]], [[]],
558 [[syntax error, unexpected 'a', expecting 'b' or 'c'
563 ## ------------------------- ##
564 ## Unresolved SR Conflicts. ##
565 ## ------------------------- ##
567 AT_SETUP([Unresolved SR Conflicts])
569 AT_KEYWORDS([report])
574 exp: exp OP exp | NUM;
577 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
578 [input.y: conflicts: 1 shift/reduce
581 # Check the contents of the report.
582 AT_CHECK([cat input.output], [],
583 [[State 5 conflicts: 1 shift/reduce
594 Terminals, with rules where they appear
602 Nonterminals, with rules where they appear
607 on left: 1 2, on right: 0 1
612 0 $accept: . exp $end
616 NUM shift, and go to state 1
625 $default reduce using rule 2 (exp)
630 0 $accept: exp . $end
633 $end shift, and go to state 3
634 OP shift, and go to state 4
639 0 $accept: exp $end .
650 NUM shift, and go to state 1
658 1 | exp OP exp . [$end, OP]
660 OP shift, and go to state 4
662 OP [reduce using rule 1 (exp)]
663 $default reduce using rule 1 (exp)
670 ## ----------------------- ##
671 ## Resolved SR Conflicts. ##
672 ## ----------------------- ##
674 AT_SETUP([Resolved SR Conflicts])
676 AT_KEYWORDS([report])
682 exp: exp OP exp | NUM;
685 AT_BISON_CHECK([-o input.c --report=all input.y])
687 # Check the contents of the report.
688 AT_CHECK([cat input.output], [],
697 Terminals, with rules where they appear
705 Nonterminals, with rules where they appear
710 on left: 1 2, on right: 0 1
715 0 $accept: . exp $end
719 NUM shift, and go to state 1
728 $default reduce using rule 2 (exp)
733 0 $accept: exp . $end
736 $end shift, and go to state 3
737 OP shift, and go to state 4
742 0 $accept: exp $end .
753 NUM shift, and go to state 1
761 1 | exp OP exp . [$end, OP]
763 $default reduce using rule 1 (exp)
765 Conflict between rule 1 and token OP resolved as reduce (%left OP).
771 ## ---------------------- ##
772 ## %precedence suffices. ##
773 ## ---------------------- ##
775 AT_SETUP([%precedence suffices])
782 "if" cond "then" stmt
783 | "if" cond "then" stmt "else" stmt
792 AT_BISON_CHECK([-o input.c input.y])
797 ## ------------------------------ ##
798 ## %precedence does not suffice. ##
799 ## ------------------------------ ##
801 AT_SETUP([%precedence does not suffice])
808 "if" cond "then" stmt
809 | "if" cond "then" stmt "else" stmt
819 AT_BISON_CHECK([-o input.c input.y], 0, [],
820 [[input.y: conflicts: 1 shift/reduce
821 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond
827 ## -------------------------------- ##
828 ## Defaulted Conflicted Reduction. ##
829 ## -------------------------------- ##
831 # When there are RR conflicts, some rules are disabled. Usually it is
832 # simply displayed as:
834 # $end reduce using rule 3 (num)
835 # $end [reduce using rule 4 (id)]
837 # But when `reduce 3' is the default action, we'd produce:
839 # $end [reduce using rule 4 (id)]
840 # $default reduce using rule 3 (num)
842 # In this precise case (a reduction is masked by the default
843 # reduction), we make the `reduce 3' explicit:
845 # $end reduce using rule 3 (num)
846 # $end [reduce using rule 4 (id)]
847 # $default reduce using rule 3 (num)
849 # Maybe that's not the best display, but then, please propose something
852 AT_SETUP([Defaulted Conflicted Reduction])
853 AT_KEYWORDS([report])
863 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
864 [[input.y: conflicts: 1 reduce/reduce
865 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
868 # Check the contents of the report.
869 AT_CHECK([cat input.output], [],
870 [[Rules useless in parser due to conflicts
875 State 1 conflicts: 1 reduce/reduce
890 Terminals, with rules where they appear
897 Nonterminals, with rules where they appear
902 on left: 1 2, on right: 0
904 on left: 3, on right: 1
906 on left: 4, on right: 2
911 0 $accept: . exp $end
917 '0' shift, and go to state 1
929 $end reduce using rule 3 (num)
930 $end [reduce using rule 4 (id)]
931 $default reduce using rule 3 (num)
936 0 $accept: exp . $end
938 $end shift, and go to state 5
945 $default reduce using rule 1 (exp)
952 $default reduce using rule 2 (exp)
957 0 $accept: exp $end .
967 ## -------------------- ##
968 ## %expect not enough. ##
969 ## -------------------- ##
971 AT_SETUP([%expect not enough])
977 exp: exp OP exp | NUM;
980 AT_BISON_CHECK([-o input.c input.y], 1, [],
981 [input.y: conflicts: 1 shift/reduce
982 input.y: expected 0 shift/reduce conflicts
987 ## --------------- ##
989 ## --------------- ##
991 AT_SETUP([%expect right])
997 exp: exp OP exp | NUM;
1000 AT_BISON_CHECK([-o input.c input.y])
1004 ## ------------------ ##
1005 ## %expect too much. ##
1006 ## ------------------ ##
1008 AT_SETUP([%expect too much])
1014 exp: exp OP exp | NUM;
1017 AT_BISON_CHECK([-o input.c input.y], 1, [],
1018 [input.y: conflicts: 1 shift/reduce
1019 input.y: expected 2 shift/reduce conflicts
1024 ## ------------------------------- ##
1025 ## %expect with reduce conflicts. ##
1026 ## ------------------------------- ##
1028 AT_SETUP([%expect with reduce conflicts])
1033 program: a 'a' | a a;
1037 AT_BISON_CHECK([-o input.c input.y], 1, [],
1038 [input.y: conflicts: 1 reduce/reduce
1039 input.y: expected 0 reduce/reduce conflicts
1044 ## ------------------------- ##
1045 ## %prec with user strings. ##
1046 ## ------------------------- ##
1048 AT_SETUP([%prec with user string])
1050 AT_DATA([[input.y]],
1057 AT_BISON_CHECK([-o input.c input.y])
1061 ## -------------------------------- ##
1062 ## %no-default-prec without %prec. ##
1063 ## -------------------------------- ##
1065 AT_SETUP([%no-default-prec without %prec])
1067 AT_DATA([[input.y]],
1081 AT_BISON_CHECK([-o input.c input.y], 0, [],
1082 [[input.y: conflicts: 4 shift/reduce
1087 ## ----------------------------- ##
1088 ## %no-default-prec with %prec. ##
1089 ## ----------------------------- ##
1091 AT_SETUP([%no-default-prec with %prec])
1093 AT_DATA([[input.y]],
1101 e: e '+' e %prec '+'
1107 AT_BISON_CHECK([-o input.c input.y])
1111 ## --------------- ##
1112 ## %default-prec. ##
1113 ## --------------- ##
1115 AT_SETUP([%default-prec])
1117 AT_DATA([[input.y]],
1131 AT_BISON_CHECK([-o input.c input.y])
1135 ## ---------------------------------------------- ##
1136 ## Unreachable States After Conflict Resolution. ##
1137 ## ---------------------------------------------- ##
1139 AT_SETUP([[Unreachable States After Conflict Resolution]])
1141 # If conflict resolution makes states unreachable, remove those states, report
1142 # rules that are then unused, and don't report conflicts in those states. Test
1143 # what happens when a nonterminal becomes useless as a result of state removal
1144 # since that causes lalr.o's goto map to be rewritten.
1146 AT_DATA([[input.y]],
1152 start: resolved_conflict 'a' reported_conflicts 'a' ;
1154 /* S/R conflict resolved as reduce, so the state with item
1155 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1156 * unreachable, and the associated production is useless. */
1162 /* S/R conflict that need not be reported since it is unreachable because of
1163 * the previous conflict resolution. Nonterminal unreachable1 and all its
1164 * productions are useless. */
1170 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1173 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1174 * when their states are renumbered due to state removal. */
1183 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1184 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1185 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
1186 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
1187 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1188 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1189 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1190 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1191 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1194 AT_CHECK([[cat input.output]], 0,
1195 [[Rules useless in parser due to conflicts
1197 2 resolved_conflict: 'a' unreachable1
1199 4 unreachable1: 'a' unreachable2
1202 6 unreachable2: /* empty */
1205 9 reported_conflicts: 'a'
1209 State 4 conflicts: 1 shift/reduce
1210 State 5 conflicts: 1 reduce/reduce
1215 0 $accept: start $end
1217 1 start: resolved_conflict 'a' reported_conflicts 'a'
1219 2 resolved_conflict: 'a' unreachable1
1222 4 unreachable1: 'a' unreachable2
1225 6 unreachable2: /* empty */
1228 8 reported_conflicts: 'a'
1233 Terminals, with rules where they appear
1240 Nonterminals, with rules where they appear
1245 on left: 1, on right: 0
1246 resolved_conflict (6)
1247 on left: 2 3, on right: 1
1249 on left: 4 5, on right: 2
1251 on left: 6 7, on right: 4
1252 reported_conflicts (9)
1253 on left: 8 9 10, on right: 1
1258 0 $accept: . start $end
1259 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1260 2 resolved_conflict: . 'a' unreachable1
1263 $default reduce using rule 3 (resolved_conflict)
1266 resolved_conflict go to state 2
1268 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1273 0 $accept: start . $end
1275 $end shift, and go to state 3
1280 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1282 'a' shift, and go to state 4
1287 0 $accept: start $end .
1294 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1295 8 reported_conflicts: . 'a'
1299 'a' shift, and go to state 5
1301 'a' [reduce using rule 10 (reported_conflicts)]
1303 reported_conflicts go to state 6
1308 8 reported_conflicts: 'a' . ['a']
1311 'a' reduce using rule 8 (reported_conflicts)
1312 'a' [reduce using rule 9 (reported_conflicts)]
1313 $default reduce using rule 8 (reported_conflicts)
1318 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1320 'a' shift, and go to state 7
1325 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1327 $default reduce using rule 1 (start)
1330 AT_DATA([[input-keep.y]],
1331 [[%define lr.keep-unreachable-states
1333 AT_CHECK([[cat input.y >> input-keep.y]])
1335 AT_BISON_CHECK([[input-keep.y]], 0, [],
1336 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
1337 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1338 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1339 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1340 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1346 ## ------------------------------------------------------------ ##
1347 ## Solved conflicts report for multiple reductions in a state. ##
1348 ## ------------------------------------------------------------ ##
1350 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1352 # Used to lose earlier solved conflict messages even within a single S/R/R.
1354 AT_DATA([[input.y]],
1370 empty_a: %prec 'a' ;
1371 empty_b: %prec 'b' ;
1372 empty_c1: %prec 'c' ;
1373 empty_c2: %prec 'c' ;
1374 empty_c3: %prec 'd' ;
1376 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1377 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1380 0 $accept: . start $end
1393 13 empty_c3: . ['c']
1395 'b' shift, and go to state 1
1397 'c' reduce using rule 13 (empty_c3)
1398 $default reduce using rule 9 (empty_a)
1401 empty_a go to state 3
1402 empty_b go to state 4
1403 empty_c1 go to state 5
1404 empty_c2 go to state 6
1405 empty_c3 go to state 7
1407 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1408 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1409 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1410 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1411 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1420 ## ------------------------------------------------------------ ##
1421 ## %nonassoc error actions for multiple reductions in a state. ##
1422 ## ------------------------------------------------------------ ##
1424 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1425 # multiple reductions in a state.
1427 # For a %nonassoc error action token, used to print the first remaining
1428 # reduction on that token without brackets.
1430 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1432 AT_DATA([[input.y]],
1433 [[%nonassoc 'a' 'b' 'c'
1445 empty_a: %prec 'a' ;
1446 empty_b: %prec 'b' ;
1447 empty_c1: %prec 'c' ;
1448 empty_c2: %prec 'c' ;
1449 empty_c3: %prec 'c' ;
1452 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1453 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1456 0 $accept: . start $end
1468 12 empty_c2: . ['c']
1469 13 empty_c3: . ['c']
1471 'a' error (nonassociative)
1472 'b' error (nonassociative)
1473 'c' error (nonassociative)
1475 'c' [reduce using rule 12 (empty_c2)]
1476 'c' [reduce using rule 13 (empty_c3)]
1479 empty_a go to state 2
1480 empty_b go to state 3
1481 empty_c1 go to state 4
1482 empty_c2 go to state 5
1483 empty_c3 go to state 6
1485 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1486 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1487 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1495 ## --------------------------------- ##
1496 ## -W versus %expect and %expect-rr ##
1497 ## --------------------------------- ##
1499 AT_SETUP([[-W versus %expect and %expect-rr]])
1501 AT_DATA([[sr-rr.y]],
1504 start: 'a' | A 'a' | B 'a' ;
1511 start: 'a' | A 'a' ;
1522 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1523 [[sr-rr.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1525 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1526 [[sr-rr.y: conflicts: 1 reduce/reduce
1528 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1529 [[sr-rr.y: conflicts: 1 shift/reduce
1532 [for gram in sr-rr sr rr; do
1533 for sr_exp_i in '' 0 1 2; do
1534 for rr_exp_i in '' 0 1 2; do
1535 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1537 # Build grammar file.
1542 if test -n "$sr_exp_i"; then
1544 file=$file-expect-$sr_exp
1545 directives="%expect $sr_exp"
1547 if test -n "$rr_exp_i"; then
1549 file=$file-expect-rr-$rr_exp
1550 directives="$directives %expect-rr $rr_exp"
1553 echo "$directives" > $file
1554 cat $gram.y >> $file
1556 # Count actual conflicts.
1560 if test $gram = sr || test $gram = sr-rr; then
1561 conflicts="1 shift/reduce"
1564 if test $gram = rr || test $gram = sr-rr; then
1565 if test -n "$conflicts"; then
1566 conflicts="$conflicts, "
1568 conflicts="${conflicts}1 reduce/reduce"
1573 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1574 ]AT_BISON_CHECK([[-Wnone $file]])[
1575 ]AT_BISON_CHECK([[-Werror $file]])[
1577 echo "$file: conflicts: $conflicts" > experr
1578 if test $sr_count -ne $sr_exp; then
1579 if test $sr_exp -ne 1; then s=s; else s= ; fi
1580 echo "$file: expected $sr_exp shift/reduce conflict$s" >> experr
1582 if test $rr_count -ne $rr_exp; then
1583 if test $rr_exp -ne 1; then s=s; else s= ; fi
1584 echo "$file: expected $rr_exp reduce/reduce conflict$s" >> experr
1586 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1587 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[