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 ## ---------------- ##
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]
43 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
44 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
45 e: 'e' | /* Nothing. */;
52 ## ------------------- ##
53 ## %nonassoc and eof. ##
54 ## ------------------- ##
56 AT_SETUP([%nonassoc and eof])
58 AT_BISON_OPTION_PUSHDEFS
59 AT_DATA_GRAMMAR([input.y],
67 #define YYERROR_VERBOSE 1
69 /* The current argument. */
70 static const char *input;
76 assert (toknum <= strlen (input));
77 return input[toknum++];
91 main (int argc, const char *argv[])
93 input = argc <= 1 ? "" : argv[1];
97 AT_BISON_OPTION_POPDEFS
99 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
100 [AT_BISON_CHECK([$1[ -o input.c input.y]])
103 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
105 AT_PARSER_CHECK([./input '0<0'])
106 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
107 [syntax error, unexpected '<'AT_EXPECTING
110 AT_PARSER_CHECK([./input '0>0'])
111 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
112 [syntax error, unexpected '>'AT_EXPECTING
115 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
116 [syntax error, unexpected '>'AT_EXPECTING
119 m4_popdef([AT_EXPECTING])])
121 # Expected token list is missing.
122 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
124 # We must disable default reductions in inconsistent states in order to
125 # have an explicit list of all expected tokens.
126 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
129 # lr.default-reduction=consistent happens to work for this test case.
130 # However, for other grammars, lookahead sets can be merged for
131 # different left contexts, so it is still possible to have an incorrect
132 # expected list. Canonical LR is almost a general solution (that is, it
133 # can fail only when %nonassoc is used), so make sure it gives the same
135 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
137 # parse.lac=full is a completely general solution that does not require
138 # any of the above sacrifices. Of course, it does not extend the
139 # language-recognition power of LALR to (IE)LR, but it does ensure that
140 # the reported list of expected tokens matches what the given parser
141 # would have accepted in place of the unexpected token.
142 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
144 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
150 ## ------------------------------------------- ##
151 ## parse.error=verbose and consistent errors. ##
152 ## ------------------------------------------- ##
154 AT_SETUP([[parse.error=verbose and consistent errors]])
156 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
158 AT_BISON_OPTION_PUSHDEFS([$1])
160 m4_pushdef([AT_YYLEX_PROTOTYPE],
161 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
162 [[int yylex (YYSTYPE *lvalp)]])])
164 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
168 import java.io.IOException;
171 %code {]AT_SKEL_CC_IF([[
172 #include <string>]], [[
175 ]AT_YYERROR_DECLARE])[
176 ]AT_YYLEX_PROTOTYPE[;
180 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
184 %define parse.error verbose
190 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
194 `--------*/]AT_SKEL_JAVA_IF([[
196 public String input = "]$3[";
197 public int index = 0;
200 if (index < input.length ())
201 return input.charAt (index++);
205 public Object getLVal ()
207 return new Integer(1);
212 static char const *input = "]$3[";
228 AT_FULL_COMPILE([[input]])
230 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
231 $5, [a], [[, expecting 'a']],
232 $5, [b], [[, expecting 'b']])])
234 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
235 [AT_PARSER_CHECK([[./input]], [[1]]]),
237 [[syntax error, unexpected ]$4[]AT_EXPECTING[
240 m4_popdef([AT_EXPECTING])
241 m4_popdef([AT_YYLEX_PROTOTYPE])
242 AT_BISON_OPTION_POPDEFS
246 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
249 start: consistent-error-on-a-a 'a' ;
251 consistent-error-on-a-a:
252 'a' default-reduction
253 | 'a' default-reduction 'a'
257 default-reduction: /*empty*/ ;
260 // Provide another context in which all rules are useful so that this
261 // test case looks a little more realistic.
262 start: 'b' consistent-error-on-a-a 'c' ;
265 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
267 # Unfortunately, no expected tokens are reported even though 'b' can be
268 # accepted. Nevertheless, the main point of this test is to make sure
269 # that at least the unexpected token is reported. In a previous version
270 # of Bison, it wasn't reported because the error is detected in a
271 # consistent state with an error action, and that case always triggered
272 # the simple "syntax error" message.
274 # The point isn't to test IELR here, but state merging happens to
275 # complicate this example.
276 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
277 [AT_PREVIOUS_STATE_GRAMMAR],
278 [AT_PREVIOUS_STATE_INPUT],
280 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
282 [AT_PREVIOUS_STATE_GRAMMAR],
283 [AT_PREVIOUS_STATE_INPUT],
285 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
287 [AT_PREVIOUS_STATE_GRAMMAR],
288 [AT_PREVIOUS_STATE_INPUT],
290 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
292 [AT_PREVIOUS_STATE_GRAMMAR],
293 [AT_PREVIOUS_STATE_INPUT],
294 [[end of input]], [[none]])
296 # Even canonical LR doesn't foresee the error for 'a'!
297 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
298 %define lr.default-reduction consistent]],
299 [AT_PREVIOUS_STATE_GRAMMAR],
300 [AT_PREVIOUS_STATE_INPUT],
302 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
303 %define lr.default-reduction accepting]],
304 [AT_PREVIOUS_STATE_GRAMMAR],
305 [AT_PREVIOUS_STATE_INPUT],
307 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
308 [AT_PREVIOUS_STATE_GRAMMAR],
309 [AT_PREVIOUS_STATE_INPUT],
312 # Only LAC gets it right.
313 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
314 %define parse.lac full]],
315 [AT_PREVIOUS_STATE_GRAMMAR],
316 [AT_PREVIOUS_STATE_INPUT],
318 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
319 %define parse.lac full]],
320 [AT_PREVIOUS_STATE_GRAMMAR],
321 [AT_PREVIOUS_STATE_INPUT],
324 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
325 m4_popdef([AT_PREVIOUS_STATE_INPUT])
327 m4_pushdef([AT_USER_ACTION_GRAMMAR],
330 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
331 // incorrectly for the 'b' set in the semantic action below. All 'a'
332 // tokens are returned by yylex, which sets $$ = 1.
335 fprintf (stderr, "Wrong destructor.\n");
338 // Rather than depend on an inconsistent state to induce reading a
339 // lookahead as in the previous grammar, just assign the lookahead in a
340 // semantic action. That lookahead isn't needed before either error
341 // action is encountered. In a previous version of Bison, this was a
342 // problem as it meant yychar was not translated into yytoken before
343 // either error action. The second error action thus invoked a
344 // destructor that it selected according to the incorrect yytoken. The
345 // first error action would have reported an incorrect unexpected token
346 // except that, due to the bug described in the previous grammar, the
347 // unexpected token was not reported at all.
348 start: error-reduce consistent-error 'a' { USE ($][3); } ;
351 'a' 'a' consistent-reduction consistent-error 'a'
352 { USE (($][1, $][2, $][5)); }
357 consistent-reduction: /*empty*/ {
358 assert (yychar == YYEMPTY);
365 | /*empty*/ %prec 'a'
368 // Provide another context in which all rules are useful so that this
369 // test case looks a little more realistic.
370 start: 'b' consistent-error 'b' ;
372 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
374 AT_CONSISTENT_ERRORS_CHECK([[]],
375 [AT_USER_ACTION_GRAMMAR],
376 [AT_USER_ACTION_INPUT],
378 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
379 [AT_USER_ACTION_GRAMMAR],
380 [AT_USER_ACTION_INPUT],
382 # No C++ or Java test because yychar cannot be manipulated by users.
384 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
385 [AT_USER_ACTION_GRAMMAR],
386 [AT_USER_ACTION_INPUT],
389 # Canonical LR doesn't foresee the error for 'a'!
390 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
391 [AT_USER_ACTION_GRAMMAR],
392 [AT_USER_ACTION_INPUT],
394 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
395 [AT_USER_ACTION_GRAMMAR],
396 [AT_USER_ACTION_INPUT],
399 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
400 [AT_USER_ACTION_GRAMMAR],
401 [AT_USER_ACTION_INPUT],
403 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
404 %define lr.default-reduction accepting]],
405 [AT_USER_ACTION_GRAMMAR],
406 [AT_USER_ACTION_INPUT],
409 m4_popdef([AT_USER_ACTION_GRAMMAR])
410 m4_popdef([AT_USER_ACTION_INPUT])
412 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
418 ## ------------------------------------------------------- ##
419 ## LAC: %nonassoc requires splitting canonical LR states. ##
420 ## ------------------------------------------------------- ##
422 # This test case demonstrates that, when %nonassoc is used, canonical
423 # LR(1) parser table construction followed by conflict resolution
424 # without further state splitting is not always sufficient to produce a
425 # parser that can detect all syntax errors as soon as possible on one
426 # token of lookahead. However, LAC solves the problem completely even
427 # with minimal LR parser tables.
429 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
430 AT_BISON_OPTION_PUSHDEFS
431 AT_DATA_GRAMMAR([[input.y]],
444 'a' problem 'a' // First context.
445 | 'b' problem 'b' // Second context.
446 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
455 // For the state reached after shifting the 'a' in these productions,
456 // lookahead sets are the same in both the first and second contexts.
457 // Thus, canonical LR reuses the same state for both contexts. However,
458 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
459 // error action only in the first context. In order to immediately
460 // detect the syntax error on 'a' here for only the first context, this
461 // canonical LR state would have to be split into two states, and the
462 // 'a' lookahead would have to be removed from only one of the states.
464 'a' // Reduction lookahead set is always ['a', 'b'].
466 | 'a' 'c' // 'c' is forgotten as an expected token.
469 reduce-nonassoc: %prec 'a';
473 ]AT_YYLEX_DEFINE(["aaa"])[
476 AT_BISON_OPTION_POPDEFS
478 # Show canonical LR's failure.
479 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
481 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
483 AT_COMPILE([[input]])
484 AT_PARSER_CHECK([[./input]], [[1]], [[]],
485 [[syntax error, unexpected 'a', expecting 'b'
488 # It's corrected by LAC.
489 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
490 -o input.c input.y]], [[0]], [[]],
491 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
493 AT_COMPILE([[input]])
494 AT_PARSER_CHECK([[./input]], [[1]], [[]],
495 [[syntax error, unexpected 'a', expecting 'b' or 'c'
498 # IELR is sufficient when LAC is used.
499 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -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' or 'c'
510 ## ------------------------- ##
511 ## Unresolved SR Conflicts. ##
512 ## ------------------------- ##
514 AT_SETUP([Unresolved SR Conflicts])
516 AT_KEYWORDS([report])
521 exp: exp OP exp | NUM;
524 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
525 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
528 # Check the contents of the report.
529 AT_CHECK([cat input.output], [],
530 [[State 5 conflicts: 1 shift/reduce
541 Terminals, with rules where they appear
549 Nonterminals, with rules where they appear
554 on left: 1 2, on right: 0 1
559 0 $accept: . exp $end
563 NUM shift, and go to state 1
572 $default reduce using rule 2 (exp)
577 0 $accept: exp . $end
580 $end shift, and go to state 3
581 OP shift, and go to state 4
586 0 $accept: exp $end .
597 NUM shift, and go to state 1
605 1 | exp OP exp . [$end, OP]
607 OP shift, and go to state 4
609 OP [reduce using rule 1 (exp)]
610 $default reduce using rule 1 (exp)
617 ## ----------------------- ##
618 ## Resolved SR Conflicts. ##
619 ## ----------------------- ##
621 AT_SETUP([Resolved SR Conflicts])
623 AT_KEYWORDS([report])
629 exp: exp OP exp | NUM;
632 AT_BISON_CHECK([-o input.c --report=all input.y])
634 # Check the contents of the report.
635 AT_CHECK([cat input.output], [],
644 Terminals, with rules where they appear
652 Nonterminals, with rules where they appear
657 on left: 1 2, on right: 0 1
662 0 $accept: . exp $end
666 NUM shift, and go to state 1
675 $default reduce using rule 2 (exp)
680 0 $accept: exp . $end
683 $end shift, and go to state 3
684 OP shift, and go to state 4
689 0 $accept: exp $end .
700 NUM shift, and go to state 1
708 1 | exp OP exp . [$end, OP]
710 $default reduce using rule 1 (exp)
712 Conflict between rule 1 and token OP resolved as reduce (%left OP).
718 ## ---------------------- ##
719 ## %precedence suffices. ##
720 ## ---------------------- ##
722 AT_SETUP([%precedence suffices])
729 "if" cond "then" stmt
730 | "if" cond "then" stmt "else" stmt
739 AT_BISON_CHECK([-o input.c input.y])
744 ## ------------------------------ ##
745 ## %precedence does not suffice. ##
746 ## ------------------------------ ##
748 AT_SETUP([%precedence does not suffice])
755 "if" cond "then" stmt
756 | "if" cond "then" stmt "else" stmt
766 AT_BISON_CHECK([-o input.c input.y], 0, [],
767 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
768 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother]
774 ## -------------------------------- ##
775 ## Defaulted Conflicted Reduction. ##
776 ## -------------------------------- ##
778 # When there are RR conflicts, some rules are disabled. Usually it is
779 # simply displayed as:
781 # $end reduce using rule 3 (num)
782 # $end [reduce using rule 4 (id)]
784 # But when `reduce 3' is the default action, we'd produce:
786 # $end [reduce using rule 4 (id)]
787 # $default reduce using rule 3 (num)
789 # In this precise case (a reduction is masked by the default
790 # reduction), we make the `reduce 3' explicit:
792 # $end reduce using rule 3 (num)
793 # $end [reduce using rule 4 (id)]
794 # $default reduce using rule 3 (num)
796 # Maybe that's not the best display, but then, please propose something
799 AT_SETUP([Defaulted Conflicted Reduction])
800 AT_KEYWORDS([report])
810 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
811 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
812 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother]
815 # Check the contents of the report.
816 AT_CHECK([cat input.output], [],
817 [[Rules useless in parser due to conflicts
822 State 1 conflicts: 1 reduce/reduce
837 Terminals, with rules where they appear
844 Nonterminals, with rules where they appear
849 on left: 1 2, on right: 0
851 on left: 3, on right: 1
853 on left: 4, on right: 2
858 0 $accept: . exp $end
864 '0' shift, and go to state 1
876 $end reduce using rule 3 (num)
877 $end [reduce using rule 4 (id)]
878 $default reduce using rule 3 (num)
883 0 $accept: exp . $end
885 $end shift, and go to state 5
892 $default reduce using rule 1 (exp)
899 $default reduce using rule 2 (exp)
904 0 $accept: exp $end .
914 ## -------------------- ##
915 ## %expect not enough. ##
916 ## -------------------- ##
918 AT_SETUP([%expect not enough])
924 exp: exp OP exp | NUM;
927 AT_BISON_CHECK([-o input.c input.y], 1, [],
928 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
933 ## --------------- ##
935 ## --------------- ##
937 AT_SETUP([%expect right])
943 exp: exp OP exp | NUM;
946 AT_BISON_CHECK([-o input.c input.y])
950 ## ------------------ ##
951 ## %expect too much. ##
952 ## ------------------ ##
954 AT_SETUP([%expect too much])
960 exp: exp OP exp | NUM;
963 AT_BISON_CHECK([-o input.c input.y], 1, [],
964 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
969 ## ------------------------------- ##
970 ## %expect with reduce conflicts. ##
971 ## ------------------------------- ##
973 AT_SETUP([%expect with reduce conflicts])
978 program: a 'a' | a a;
982 AT_BISON_CHECK([-o input.c input.y], 1, [],
983 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
988 ## ------------------------- ##
989 ## %prec with user strings. ##
990 ## ------------------------- ##
992 AT_SETUP([%prec with user string])
1001 AT_BISON_CHECK([-o input.c input.y])
1005 ## -------------------------------- ##
1006 ## %no-default-prec without %prec. ##
1007 ## -------------------------------- ##
1009 AT_SETUP([%no-default-prec without %prec])
1011 AT_DATA([[input.y]],
1025 AT_BISON_CHECK([-o input.c input.y], 0, [],
1026 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1031 ## ----------------------------- ##
1032 ## %no-default-prec with %prec. ##
1033 ## ----------------------------- ##
1035 AT_SETUP([%no-default-prec with %prec])
1037 AT_DATA([[input.y]],
1045 e: e '+' e %prec '+'
1051 AT_BISON_CHECK([-o input.c input.y])
1055 ## --------------- ##
1056 ## %default-prec. ##
1057 ## --------------- ##
1059 AT_SETUP([%default-prec])
1061 AT_DATA([[input.y]],
1075 AT_BISON_CHECK([-o input.c input.y])
1079 ## ---------------------------------------------- ##
1080 ## Unreachable States After Conflict Resolution. ##
1081 ## ---------------------------------------------- ##
1083 AT_SETUP([[Unreachable States After Conflict Resolution]])
1085 # If conflict resolution makes states unreachable, remove those states, report
1086 # rules that are then unused, and don't report conflicts in those states. Test
1087 # what happens when a nonterminal becomes useless as a result of state removal
1088 # since that causes lalr.o's goto map to be rewritten.
1090 AT_DATA([[input.y]],
1096 start: resolved_conflict 'a' reported_conflicts 'a' ;
1098 /* S/R conflict resolved as reduce, so the state with item
1099 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1100 * unreachable, and the associated production is useless. */
1106 /* S/R conflict that need not be reported since it is unreachable because of
1107 * the previous conflict resolution. Nonterminal unreachable1 and all its
1108 * productions are useless. */
1114 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1117 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1118 * when their states are renumbered due to state removal. */
1127 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1128 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1129 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1130 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother]
1131 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother]
1132 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1133 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1134 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1135 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1136 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1139 AT_CHECK([[cat input.output]], 0,
1140 [[Rules useless in parser due to conflicts
1142 2 resolved_conflict: 'a' unreachable1
1144 4 unreachable1: 'a' unreachable2
1147 6 unreachable2: /* empty */
1150 9 reported_conflicts: 'a'
1154 State 4 conflicts: 1 shift/reduce
1155 State 5 conflicts: 1 reduce/reduce
1160 0 $accept: start $end
1162 1 start: resolved_conflict 'a' reported_conflicts 'a'
1164 2 resolved_conflict: 'a' unreachable1
1167 4 unreachable1: 'a' unreachable2
1170 6 unreachable2: /* empty */
1173 8 reported_conflicts: 'a'
1178 Terminals, with rules where they appear
1185 Nonterminals, with rules where they appear
1190 on left: 1, on right: 0
1191 resolved_conflict (6)
1192 on left: 2 3, on right: 1
1194 on left: 4 5, on right: 2
1196 on left: 6 7, on right: 4
1197 reported_conflicts (9)
1198 on left: 8 9 10, on right: 1
1203 0 $accept: . start $end
1204 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1205 2 resolved_conflict: . 'a' unreachable1
1208 $default reduce using rule 3 (resolved_conflict)
1211 resolved_conflict go to state 2
1213 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1218 0 $accept: start . $end
1220 $end shift, and go to state 3
1225 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1227 'a' shift, and go to state 4
1232 0 $accept: start $end .
1239 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1240 8 reported_conflicts: . 'a'
1244 'a' shift, and go to state 5
1246 'a' [reduce using rule 10 (reported_conflicts)]
1248 reported_conflicts go to state 6
1253 8 reported_conflicts: 'a' . ['a']
1256 'a' reduce using rule 8 (reported_conflicts)
1257 'a' [reduce using rule 9 (reported_conflicts)]
1258 $default reduce using rule 8 (reported_conflicts)
1263 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1265 'a' shift, and go to state 7
1270 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1272 $default reduce using rule 1 (start)
1275 AT_DATA([[input-keep.y]],
1276 [[%define lr.keep-unreachable-state
1278 AT_CHECK([[cat input.y >> input-keep.y]])
1280 AT_BISON_CHECK([[input-keep.y]], 0, [],
1281 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1282 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1283 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1284 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1285 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1286 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1292 ## ------------------------------------------------------------ ##
1293 ## Solved conflicts report for multiple reductions in a state. ##
1294 ## ------------------------------------------------------------ ##
1296 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1298 # Used to lose earlier solved conflict messages even within a single S/R/R.
1300 AT_DATA([[input.y]],
1316 empty_a: %prec 'a' ;
1317 empty_b: %prec 'b' ;
1318 empty_c1: %prec 'c' ;
1319 empty_c2: %prec 'c' ;
1320 empty_c3: %prec 'd' ;
1322 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1323 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1326 0 $accept: . start $end
1339 13 empty_c3: . ['c']
1341 'b' shift, and go to state 1
1343 'c' reduce using rule 13 (empty_c3)
1344 $default reduce using rule 9 (empty_a)
1347 empty_a go to state 3
1348 empty_b go to state 4
1349 empty_c1 go to state 5
1350 empty_c2 go to state 6
1351 empty_c3 go to state 7
1353 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1354 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1355 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1356 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1357 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1366 ## ------------------------------------------------------------ ##
1367 ## %nonassoc error actions for multiple reductions in a state. ##
1368 ## ------------------------------------------------------------ ##
1370 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1371 # multiple reductions in a state.
1373 # For a %nonassoc error action token, used to print the first remaining
1374 # reduction on that token without brackets.
1376 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1378 AT_DATA([[input.y]],
1379 [[%nonassoc 'a' 'b' 'c'
1391 empty_a: %prec 'a' ;
1392 empty_b: %prec 'b' ;
1393 empty_c1: %prec 'c' ;
1394 empty_c2: %prec 'c' ;
1395 empty_c3: %prec 'c' ;
1398 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1399 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1402 0 $accept: . start $end
1414 12 empty_c2: . ['c']
1415 13 empty_c3: . ['c']
1417 'a' error (nonassociative)
1418 'b' error (nonassociative)
1419 'c' error (nonassociative)
1421 'c' [reduce using rule 12 (empty_c2)]
1422 'c' [reduce using rule 13 (empty_c3)]
1425 empty_a go to state 2
1426 empty_b go to state 3
1427 empty_c1 go to state 4
1428 empty_c2 go to state 5
1429 empty_c3 go to state 6
1431 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1432 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1433 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1441 ## -------------------- ##
1442 ## %expect-rr non GLR. ##
1443 ## -------------------- ##
1445 AT_SETUP([[%expect-rr non GLR]])
1453 AT_BISON_CHECK([[1.y]], [[0]], [],
1454 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1463 AT_BISON_CHECK([[2.y]], [[0]], [],
1464 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1465 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1466 2.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother]
1472 ## ---------------------------------- ##
1473 ## -W versus %expect and %expect-rr. ##
1474 ## ---------------------------------- ##
1476 AT_SETUP([[-W versus %expect and %expect-rr]])
1478 AT_DATA([[sr-rr.y]],
1481 start: 'a' | A 'a' | B 'a' ;
1488 start: 'a' | A 'a' ;
1499 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1500 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1501 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1503 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1504 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1506 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1507 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1511 # This is piece of code is rather complex for a simple task: try every
1512 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1513 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1516 # Number and types of genuine conflicts in the grammar.
1517 for gram in sr-rr sr rr; do
1518 # Number of expected s/r conflicts.
1519 for sr_exp_i in '' 0 1 2; do
1520 # Number of expected r/r conflicts.
1521 for rr_exp_i in '' 0 1 2; do
1522 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1524 # Build grammar file.
1529 if test -n "$sr_exp_i"; then
1531 file=$file-expect-$sr_exp
1532 directives="%expect $sr_exp"
1534 if test -n "$rr_exp_i"; then
1536 file=$file-expect-rr-$rr_exp
1537 directives="$directives %expect-rr $rr_exp"
1540 echo "$directives" > $file
1541 cat $gram.y >> $file
1543 # Number of found conflicts.
1545 (sr) sr_count=1; rr_count=0;;
1546 (rr) sr_count=0; rr_count=1;;
1547 (sr-rr) sr_count=1; rr_count=1;;
1550 # Update number of expected conflicts: if %expect is given then
1551 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1553 case $sr_exp_i:$rr_exp_i in
1559 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1560 ]AT_BISON_CHECK([[-Wnone $file]])[
1561 ]AT_BISON_CHECK([[-Werror $file]])[
1564 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1565 echo "warning: $sr_count shift/reduce conflicts"
1566 elif test "$sr_exp_i" -ne "$sr_count"; then
1567 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1569 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1570 echo "warning: $rr_count reduce/reduce conflicts"
1571 elif test "$rr_exp_i" -ne "$rr_count"; then
1572 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1574 } | sed -e "s/^/$file: /" > experr
1575 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1576 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[