1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2013 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 AT_BANNER([[Conflicts.]])
20 ## ------------------------------ ##
21 ## Useless associativity warning. ##
22 ## ------------------------------ ##
24 AT_SETUP([Useless associativity warning])
42 AT_BISON_CHECK([input.y], 0, [],
43 [[input.y:5.13: warning: useless precedence for E [-Wother]
44 input.y:2.7: warning: useless associativity for A [-Wother]
45 input.y:2.9: warning: useless associativity for B [-Wother]
46 input.y:3.8: warning: useless associativity for C [-Wother]
47 input.y:4.11: warning: useless associativity for D [-Wother]
53 ## ------------------------ ##
54 ## Token declaration order. ##
55 ## ------------------------ ##
57 # This test checks that token are declared left to right when in a precedence
60 AT_SETUP([Token declaration order])
62 AT_BISON_OPTION_PUSHDEFS
64 AT_DATA_GRAMMAR([[input.y]],
113 AT_BISON_CHECK([-o input.c input.y], [], [],
114 [[input.y:24.13: warning: useless precedence for R [-Wother]
115 input.y:24.15: warning: useless precedence for S [-Wother]
116 input.y:24.17: warning: useless precedence for T [-Wother]
117 input.y:24.19: warning: useless precedence for U [-Wother]
118 input.y:25.13: warning: useless precedence for V [-Wother]
119 input.y:25.15: warning: useless precedence for W [-Wother]
120 input.y:18.8: warning: useless associativity for E [-Wother]
121 input.y:18.10: warning: useless associativity for F [-Wother]
122 input.y:18.12: warning: useless associativity for G [-Wother]
123 input.y:19.8: warning: useless associativity for H [-Wother]
124 input.y:19.10: warning: useless associativity for I [-Wother]
125 input.y:20.8: warning: useless associativity for J [-Wother]
126 input.y:21.8: warning: useless associativity for K [-Wother]
127 input.y:22.8: warning: useless associativity for L [-Wother]
128 input.y:22.10: warning: useless associativity for M [-Wother]
129 input.y:22.12: warning: useless associativity for N [-Wother]
130 input.y:23.11: warning: useless associativity for O [-Wother]
131 input.y:23.13: warning: useless associativity for P [-Wother]
132 input.y:23.15: warning: useless associativity for Q [-Wother]
136 AT_PARSER_CHECK([./input])
138 AT_BISON_OPTION_POPDEFS
143 ## ---------------------------- ##
144 ## Useless precedence warning. ##
145 ## ---------------------------- ##
147 AT_SETUP([Useless precedence warning])
177 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
178 [[input.y:2.13: warning: useless precedence for Z [-Wother]
181 input.y:5.7: warning: useless associativity for W [-Wother]
184 input.y:6.8: warning: useless associativity for V [-Wother]
187 input.y:7.11: warning: useless associativity for U [-Wother]
195 ## ---------------- ##
196 ## S/R in initial. ##
197 ## ---------------- ##
199 # I once hacked Bison in such a way that it lost its reductions on the
200 # initial state (because it was confusing it with the last state). It
201 # took me a while to strip down my failures to this simple case. So
202 # make sure it finds the s/r conflict below.
204 AT_SETUP([S/R in initial])
210 e: 'e' | /* Nothing. */;
213 AT_BISON_CHECK([-o input.c input.y], 0, [],
214 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */ [-Wother]
217 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
218 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
219 e: 'e' | /* Nothing. */;
226 ## ------------------- ##
227 ## %nonassoc and eof. ##
228 ## ------------------- ##
230 AT_SETUP([%nonassoc and eof])
232 AT_BISON_OPTION_PUSHDEFS
233 AT_DATA_GRAMMAR([input.y],
241 #define YYERROR_VERBOSE 1
243 /* The current argument. */
244 static const char *input;
249 static size_t toknum;
250 assert (toknum <= strlen (input));
251 return input[toknum++];
265 main (int argc, const char *argv[])
267 input = argc <= 1 ? "" : argv[1];
271 AT_BISON_OPTION_POPDEFS
273 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
274 [AT_BISON_CHECK([$1[ -o input.c input.y]])
277 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
279 AT_PARSER_CHECK([./input '0<0'])
280 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
281 [syntax error, unexpected '<'AT_EXPECTING
284 AT_PARSER_CHECK([./input '0>0'])
285 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
286 [syntax error, unexpected '>'AT_EXPECTING
289 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
290 [syntax error, unexpected '>'AT_EXPECTING
293 m4_popdef([AT_EXPECTING])])
295 # Expected token list is missing.
296 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
298 # We must disable default reductions in inconsistent states in order to
299 # have an explicit list of all expected tokens.
300 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
303 # lr.default-reduction=consistent happens to work for this test case.
304 # However, for other grammars, lookahead sets can be merged for
305 # different left contexts, so it is still possible to have an incorrect
306 # expected list. Canonical LR is almost a general solution (that is, it
307 # can fail only when %nonassoc is used), so make sure it gives the same
309 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
311 # parse.lac=full is a completely general solution that does not require
312 # any of the above sacrifices. Of course, it does not extend the
313 # language-recognition power of LALR to (IE)LR, but it does ensure that
314 # the reported list of expected tokens matches what the given parser
315 # would have accepted in place of the unexpected token.
316 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
318 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
324 ## ------------------------------------------- ##
325 ## parse.error=verbose and consistent errors. ##
326 ## ------------------------------------------- ##
328 AT_SETUP([[parse.error=verbose and consistent errors]])
330 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
332 AT_BISON_OPTION_PUSHDEFS([$1])
334 m4_pushdef([AT_YYLEX_PROTOTYPE],
335 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
336 [[int yylex (YYSTYPE *lvalp)]])])
338 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
342 import java.io.IOException;
345 %code {]AT_SKEL_CC_IF([[
346 #include <string>]], [[
349 ]AT_YYERROR_DECLARE])[
350 ]AT_YYLEX_PROTOTYPE[;
354 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
358 %define parse.error verbose
364 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
368 `--------*/]AT_SKEL_JAVA_IF([[
370 public String input = "]$3[";
371 public int index = 0;
374 if (index < input.length ())
375 return input.charAt (index++);
379 public Object getLVal ()
381 return new Integer(1);
386 static char const *input = "]$3[";
402 AT_FULL_COMPILE([[input]])
404 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
405 $5, [a], [[, expecting 'a']],
406 $5, [b], [[, expecting 'b']])])
408 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
409 [AT_PARSER_CHECK([[./input]], [[1]]]),
411 [[syntax error, unexpected ]$4[]AT_EXPECTING[
414 m4_popdef([AT_EXPECTING])
415 m4_popdef([AT_YYLEX_PROTOTYPE])
416 AT_BISON_OPTION_POPDEFS
420 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
423 start: consistent-error-on-a-a 'a' ;
425 consistent-error-on-a-a:
426 'a' default-reduction
427 | 'a' default-reduction 'a'
431 default-reduction: /*empty*/ ;
434 // Provide another context in which all rules are useful so that this
435 // test case looks a little more realistic.
436 start: 'b' consistent-error-on-a-a 'c' ;
439 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
441 # Unfortunately, no expected tokens are reported even though 'b' can be
442 # accepted. Nevertheless, the main point of this test is to make sure
443 # that at least the unexpected token is reported. In a previous version
444 # of Bison, it wasn't reported because the error is detected in a
445 # consistent state with an error action, and that case always triggered
446 # the simple "syntax error" message.
448 # The point isn't to test IELR here, but state merging happens to
449 # complicate this example.
450 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
451 [AT_PREVIOUS_STATE_GRAMMAR],
452 [AT_PREVIOUS_STATE_INPUT],
454 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
456 [AT_PREVIOUS_STATE_GRAMMAR],
457 [AT_PREVIOUS_STATE_INPUT],
459 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
461 [AT_PREVIOUS_STATE_GRAMMAR],
462 [AT_PREVIOUS_STATE_INPUT],
464 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
466 [AT_PREVIOUS_STATE_GRAMMAR],
467 [AT_PREVIOUS_STATE_INPUT],
468 [[end of input]], [[none]])
470 # Even canonical LR doesn't foresee the error for 'a'!
471 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
472 %define lr.default-reduction consistent]],
473 [AT_PREVIOUS_STATE_GRAMMAR],
474 [AT_PREVIOUS_STATE_INPUT],
476 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
477 %define lr.default-reduction accepting]],
478 [AT_PREVIOUS_STATE_GRAMMAR],
479 [AT_PREVIOUS_STATE_INPUT],
481 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
482 [AT_PREVIOUS_STATE_GRAMMAR],
483 [AT_PREVIOUS_STATE_INPUT],
486 # Only LAC gets it right.
487 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
488 %define parse.lac full]],
489 [AT_PREVIOUS_STATE_GRAMMAR],
490 [AT_PREVIOUS_STATE_INPUT],
492 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
493 %define parse.lac full]],
494 [AT_PREVIOUS_STATE_GRAMMAR],
495 [AT_PREVIOUS_STATE_INPUT],
498 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
499 m4_popdef([AT_PREVIOUS_STATE_INPUT])
501 m4_pushdef([AT_USER_ACTION_GRAMMAR],
504 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
505 // incorrectly for the 'b' set in the semantic action below. All 'a'
506 // tokens are returned by yylex, which sets $$ = 1.
509 fprintf (stderr, "Wrong destructor.\n");
512 // Rather than depend on an inconsistent state to induce reading a
513 // lookahead as in the previous grammar, just assign the lookahead in a
514 // semantic action. That lookahead isn't needed before either error
515 // action is encountered. In a previous version of Bison, this was a
516 // problem as it meant yychar was not translated into yytoken before
517 // either error action. The second error action thus invoked a
518 // destructor that it selected according to the incorrect yytoken. The
519 // first error action would have reported an incorrect unexpected token
520 // except that, due to the bug described in the previous grammar, the
521 // unexpected token was not reported at all.
522 start: error-reduce consistent-error 'a' { USE ($][3); } ;
525 'a' 'a' consistent-reduction consistent-error 'a'
526 { USE (($][1, $][2, $][5)); }
531 consistent-reduction: /*empty*/ {
532 assert (yychar == YYEMPTY);
539 | /*empty*/ %prec 'a'
542 // Provide another context in which all rules are useful so that this
543 // test case looks a little more realistic.
544 start: 'b' consistent-error 'b' ;
546 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
548 AT_CONSISTENT_ERRORS_CHECK([[]],
549 [AT_USER_ACTION_GRAMMAR],
550 [AT_USER_ACTION_INPUT],
552 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
553 [AT_USER_ACTION_GRAMMAR],
554 [AT_USER_ACTION_INPUT],
556 # No C++ or Java test because yychar cannot be manipulated by users.
558 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
559 [AT_USER_ACTION_GRAMMAR],
560 [AT_USER_ACTION_INPUT],
563 # Canonical LR doesn't foresee the error for 'a'!
564 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
565 [AT_USER_ACTION_GRAMMAR],
566 [AT_USER_ACTION_INPUT],
568 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
569 [AT_USER_ACTION_GRAMMAR],
570 [AT_USER_ACTION_INPUT],
573 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
574 [AT_USER_ACTION_GRAMMAR],
575 [AT_USER_ACTION_INPUT],
577 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
578 %define lr.default-reduction accepting]],
579 [AT_USER_ACTION_GRAMMAR],
580 [AT_USER_ACTION_INPUT],
583 m4_popdef([AT_USER_ACTION_GRAMMAR])
584 m4_popdef([AT_USER_ACTION_INPUT])
586 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
592 ## ------------------------------------------------------- ##
593 ## LAC: %nonassoc requires splitting canonical LR states. ##
594 ## ------------------------------------------------------- ##
596 # This test case demonstrates that, when %nonassoc is used, canonical
597 # LR(1) parser table construction followed by conflict resolution
598 # without further state splitting is not always sufficient to produce a
599 # parser that can detect all syntax errors as soon as possible on one
600 # token of lookahead. However, LAC solves the problem completely even
601 # with minimal LR parser tables.
603 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
604 AT_BISON_OPTION_PUSHDEFS
605 AT_DATA_GRAMMAR([[input.y]],
618 'a' problem 'a' // First context.
619 | 'b' problem 'b' // Second context.
620 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
629 // For the state reached after shifting the 'a' in these productions,
630 // lookahead sets are the same in both the first and second contexts.
631 // Thus, canonical LR reuses the same state for both contexts. However,
632 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
633 // error action only in the first context. In order to immediately
634 // detect the syntax error on 'a' here for only the first context, this
635 // canonical LR state would have to be split into two states, and the
636 // 'a' lookahead would have to be removed from only one of the states.
638 'a' // Reduction lookahead set is always ['a', 'b'].
640 | 'a' 'c' // 'c' is forgotten as an expected token.
643 reduce-nonassoc: %prec 'a';
647 ]AT_YYLEX_DEFINE(["aaa"])[
650 AT_BISON_OPTION_POPDEFS
652 # Show canonical LR's failure.
653 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
655 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
657 AT_COMPILE([[input]])
658 AT_PARSER_CHECK([[./input]], [[1]], [[]],
659 [[syntax error, unexpected 'a', expecting 'b'
662 # It's corrected by LAC.
663 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
664 -o input.c input.y]], [[0]], [[]],
665 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
667 AT_COMPILE([[input]])
668 AT_PARSER_CHECK([[./input]], [[1]], [[]],
669 [[syntax error, unexpected 'a', expecting 'b' or 'c'
672 # IELR is sufficient when LAC is used.
673 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
675 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
677 AT_COMPILE([[input]])
678 AT_PARSER_CHECK([[./input]], [[1]], [[]],
679 [[syntax error, unexpected 'a', expecting 'b' or 'c'
684 ## ------------------------- ##
685 ## Unresolved SR Conflicts. ##
686 ## ------------------------- ##
688 AT_SETUP([Unresolved SR Conflicts])
690 AT_KEYWORDS([report])
695 exp: exp OP exp | NUM;
698 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
699 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
702 # Check the contents of the report.
703 AT_CHECK([cat input.output], [],
704 [[State 5 conflicts: 1 shift/reduce
715 Terminals, with rules where they appear
723 Nonterminals, with rules where they appear
728 on left: 1 2, on right: 0 1
733 0 $accept: . exp $end
737 NUM shift, and go to state 1
746 $default reduce using rule 2 (exp)
751 0 $accept: exp . $end
754 $end shift, and go to state 3
755 OP shift, and go to state 4
760 0 $accept: exp $end .
771 NUM shift, and go to state 1
779 1 | exp OP exp . [$end, OP]
781 OP shift, and go to state 4
783 OP [reduce using rule 1 (exp)]
784 $default reduce using rule 1 (exp)
791 ## ----------------------- ##
792 ## Resolved SR Conflicts. ##
793 ## ----------------------- ##
795 AT_SETUP([Resolved SR Conflicts])
797 AT_KEYWORDS([report])
803 exp: exp OP exp | NUM;
806 AT_BISON_CHECK([-o input.c --report=all input.y])
808 # Check the contents of the report.
809 AT_CHECK([cat input.output], [],
818 Terminals, with rules where they appear
826 Nonterminals, with rules where they appear
831 on left: 1 2, on right: 0 1
836 0 $accept: . exp $end
840 NUM shift, and go to state 1
849 $default reduce using rule 2 (exp)
854 0 $accept: exp . $end
857 $end shift, and go to state 3
858 OP shift, and go to state 4
863 0 $accept: exp $end .
874 NUM shift, and go to state 1
882 1 | exp OP exp . [$end, OP]
884 $default reduce using rule 1 (exp)
886 Conflict between rule 1 and token OP resolved as reduce (%left OP).
892 ## ---------------------- ##
893 ## %precedence suffices. ##
894 ## ---------------------- ##
896 AT_SETUP([%precedence suffices])
903 "if" cond "then" stmt
904 | "if" cond "then" stmt "else" stmt
913 AT_BISON_CHECK([-o input.c input.y])
918 ## ------------------------------ ##
919 ## %precedence does not suffice. ##
920 ## ------------------------------ ##
922 AT_SETUP([%precedence does not suffice])
929 "if" cond "then" stmt
930 | "if" cond "then" stmt "else" stmt
940 AT_BISON_CHECK([-o input.c input.y], 0, [],
941 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
942 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother]
948 ## -------------------------------- ##
949 ## Defaulted Conflicted Reduction. ##
950 ## -------------------------------- ##
952 # When there are RR conflicts, some rules are disabled. Usually it is
953 # simply displayed as:
955 # $end reduce using rule 3 (num)
956 # $end [reduce using rule 4 (id)]
958 # But when `reduce 3' is the default action, we'd produce:
960 # $end [reduce using rule 4 (id)]
961 # $default reduce using rule 3 (num)
963 # In this precise case (a reduction is masked by the default
964 # reduction), we make the `reduce 3' explicit:
966 # $end reduce using rule 3 (num)
967 # $end [reduce using rule 4 (id)]
968 # $default reduce using rule 3 (num)
970 # Maybe that's not the best display, but then, please propose something
973 AT_SETUP([Defaulted Conflicted Reduction])
974 AT_KEYWORDS([report])
984 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
985 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
986 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother]
989 # Check the contents of the report.
990 AT_CHECK([cat input.output], [],
991 [[Rules useless in parser due to conflicts
996 State 1 conflicts: 1 reduce/reduce
1011 Terminals, with rules where they appear
1018 Nonterminals, with rules where they appear
1023 on left: 1 2, on right: 0
1025 on left: 3, on right: 1
1027 on left: 4, on right: 2
1032 0 $accept: . exp $end
1038 '0' shift, and go to state 1
1050 $end reduce using rule 3 (num)
1051 $end [reduce using rule 4 (id)]
1052 $default reduce using rule 3 (num)
1057 0 $accept: exp . $end
1059 $end shift, and go to state 5
1066 $default reduce using rule 1 (exp)
1073 $default reduce using rule 2 (exp)
1078 0 $accept: exp $end .
1088 ## -------------------- ##
1089 ## %expect not enough. ##
1090 ## -------------------- ##
1092 AT_SETUP([%expect not enough])
1098 exp: exp OP exp | NUM;
1101 AT_BISON_CHECK([-o input.c input.y], 1, [],
1102 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1107 ## --------------- ##
1108 ## %expect right. ##
1109 ## --------------- ##
1111 AT_SETUP([%expect right])
1117 exp: exp OP exp | NUM;
1120 AT_BISON_CHECK([-o input.c input.y])
1124 ## ------------------ ##
1125 ## %expect too much. ##
1126 ## ------------------ ##
1128 AT_SETUP([%expect too much])
1134 exp: exp OP exp | NUM;
1137 AT_BISON_CHECK([-o input.c input.y], 1, [],
1138 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1143 ## ------------------------------- ##
1144 ## %expect with reduce conflicts. ##
1145 ## ------------------------------- ##
1147 AT_SETUP([%expect with reduce conflicts])
1152 program: a 'a' | a a;
1156 AT_BISON_CHECK([-o input.c input.y], 1, [],
1157 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1162 ## ------------------------- ##
1163 ## %prec with user strings. ##
1164 ## ------------------------- ##
1166 AT_SETUP([%prec with user string])
1168 AT_DATA([[input.y]],
1175 AT_BISON_CHECK([-o input.c input.y])
1179 ## -------------------------------- ##
1180 ## %no-default-prec without %prec. ##
1181 ## -------------------------------- ##
1183 AT_SETUP([%no-default-prec without %prec])
1185 AT_DATA([[input.y]],
1199 AT_BISON_CHECK([-o input.c input.y], 0, [],
1200 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1201 input.y:1.7-9: warning: useless associativity for '+' [-Wother]
1202 input.y:2.7-9: warning: useless associativity for '*' [-Wother]
1207 ## ----------------------------- ##
1208 ## %no-default-prec with %prec. ##
1209 ## ----------------------------- ##
1211 AT_SETUP([%no-default-prec with %prec])
1213 AT_DATA([[input.y]],
1221 e: e '+' e %prec '+'
1227 AT_BISON_CHECK([-o input.c input.y])
1231 ## --------------- ##
1232 ## %default-prec. ##
1233 ## --------------- ##
1235 AT_SETUP([%default-prec])
1237 AT_DATA([[input.y]],
1251 AT_BISON_CHECK([-o input.c input.y])
1255 ## ---------------------------------------------- ##
1256 ## Unreachable States After Conflict Resolution. ##
1257 ## ---------------------------------------------- ##
1259 AT_SETUP([[Unreachable States After Conflict Resolution]])
1261 # If conflict resolution makes states unreachable, remove those states, report
1262 # rules that are then unused, and don't report conflicts in those states. Test
1263 # what happens when a nonterminal becomes useless as a result of state removal
1264 # since that causes lalr.o's goto map to be rewritten.
1266 AT_DATA([[input.y]],
1272 start: resolved_conflict 'a' reported_conflicts 'a' ;
1274 /* S/R conflict resolved as reduce, so the state with item
1275 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1276 * unreachable, and the associated production is useless. */
1282 /* S/R conflict that need not be reported since it is unreachable because of
1283 * the previous conflict resolution. Nonterminal unreachable1 and all its
1284 * productions are useless. */
1290 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1293 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1294 * when their states are renumbered due to state removal. */
1303 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1304 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1305 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1306 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother]
1307 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother]
1308 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1309 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1310 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1311 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1312 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1315 AT_CHECK([[cat input.output]], 0,
1316 [[Rules useless in parser due to conflicts
1318 2 resolved_conflict: 'a' unreachable1
1320 4 unreachable1: 'a' unreachable2
1323 6 unreachable2: /* empty */
1326 9 reported_conflicts: 'a'
1330 State 4 conflicts: 1 shift/reduce
1331 State 5 conflicts: 1 reduce/reduce
1336 0 $accept: start $end
1338 1 start: resolved_conflict 'a' reported_conflicts 'a'
1340 2 resolved_conflict: 'a' unreachable1
1343 4 unreachable1: 'a' unreachable2
1346 6 unreachable2: /* empty */
1349 8 reported_conflicts: 'a'
1354 Terminals, with rules where they appear
1361 Nonterminals, with rules where they appear
1366 on left: 1, on right: 0
1367 resolved_conflict (6)
1368 on left: 2 3, on right: 1
1370 on left: 4 5, on right: 2
1372 on left: 6 7, on right: 4
1373 reported_conflicts (9)
1374 on left: 8 9 10, on right: 1
1379 0 $accept: . start $end
1380 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1381 2 resolved_conflict: . 'a' unreachable1
1384 $default reduce using rule 3 (resolved_conflict)
1387 resolved_conflict go to state 2
1389 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1394 0 $accept: start . $end
1396 $end shift, and go to state 3
1401 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1403 'a' shift, and go to state 4
1408 0 $accept: start $end .
1415 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1416 8 reported_conflicts: . 'a'
1420 'a' shift, and go to state 5
1422 'a' [reduce using rule 10 (reported_conflicts)]
1424 reported_conflicts go to state 6
1429 8 reported_conflicts: 'a' . ['a']
1432 'a' reduce using rule 8 (reported_conflicts)
1433 'a' [reduce using rule 9 (reported_conflicts)]
1434 $default reduce using rule 8 (reported_conflicts)
1439 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1441 'a' shift, and go to state 7
1446 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1448 $default reduce using rule 1 (start)
1451 AT_DATA([[input-keep.y]],
1452 [[%define lr.keep-unreachable-state
1454 AT_CHECK([[cat input.y >> input-keep.y]])
1456 AT_BISON_CHECK([[input-keep.y]], 0, [],
1457 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1458 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1459 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1460 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1461 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1462 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
1468 ## ------------------------------------------------------------ ##
1469 ## Solved conflicts report for multiple reductions in a state. ##
1470 ## ------------------------------------------------------------ ##
1472 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1474 # Used to lose earlier solved conflict messages even within a single S/R/R.
1476 AT_DATA([[input.y]],
1492 empty_a: %prec 'a' ;
1493 empty_b: %prec 'b' ;
1494 empty_c1: %prec 'c' ;
1495 empty_c2: %prec 'c' ;
1496 empty_c3: %prec 'd' ;
1498 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1499 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1502 0 $accept: . start $end
1515 13 empty_c3: . ['c']
1517 'b' shift, and go to state 1
1519 'c' reduce using rule 13 (empty_c3)
1520 $default reduce using rule 9 (empty_a)
1523 empty_a go to state 3
1524 empty_b go to state 4
1525 empty_c1 go to state 5
1526 empty_c2 go to state 6
1527 empty_c3 go to state 7
1529 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1530 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1531 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1532 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1533 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1542 ## ------------------------------------------------------------ ##
1543 ## %nonassoc error actions for multiple reductions in a state. ##
1544 ## ------------------------------------------------------------ ##
1546 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1547 # multiple reductions in a state.
1549 # For a %nonassoc error action token, used to print the first remaining
1550 # reduction on that token without brackets.
1552 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1554 AT_DATA([[input.y]],
1555 [[%nonassoc 'a' 'b' 'c'
1567 empty_a: %prec 'a' ;
1568 empty_b: %prec 'b' ;
1569 empty_c1: %prec 'c' ;
1570 empty_c2: %prec 'c' ;
1571 empty_c3: %prec 'c' ;
1574 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1575 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1578 0 $accept: . start $end
1590 12 empty_c2: . ['c']
1591 13 empty_c3: . ['c']
1593 'a' error (nonassociative)
1594 'b' error (nonassociative)
1595 'c' error (nonassociative)
1597 'c' [reduce using rule 12 (empty_c2)]
1598 'c' [reduce using rule 13 (empty_c3)]
1601 empty_a go to state 2
1602 empty_b go to state 3
1603 empty_c1 go to state 4
1604 empty_c2 go to state 5
1605 empty_c3 go to state 6
1607 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1608 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1609 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1617 ## -------------------- ##
1618 ## %expect-rr non GLR. ##
1619 ## -------------------- ##
1621 AT_SETUP([[%expect-rr non GLR]])
1629 AT_BISON_CHECK([[1.y]], [[0]], [],
1630 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1639 AT_BISON_CHECK([[2.y]], [[0]], [],
1640 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1641 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1642 2.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother]
1648 ## ---------------------------------- ##
1649 ## -W versus %expect and %expect-rr. ##
1650 ## ---------------------------------- ##
1652 AT_SETUP([[-W versus %expect and %expect-rr]])
1654 AT_DATA([[sr-rr.y]],
1657 start: 'a' | A 'a' | B 'a' ;
1664 start: 'a' | A 'a' ;
1675 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1676 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1677 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1679 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1680 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1682 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1683 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1687 # This is piece of code is rather complex for a simple task: try every
1688 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1689 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1692 # Number and types of genuine conflicts in the grammar.
1693 for gram in sr-rr sr rr; do
1694 # Number of expected s/r conflicts.
1695 for sr_exp_i in '' 0 1 2; do
1696 # Number of expected r/r conflicts.
1697 for rr_exp_i in '' 0 1 2; do
1698 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1700 # Build grammar file.
1705 if test -n "$sr_exp_i"; then
1707 file=$file-expect-$sr_exp
1708 directives="%expect $sr_exp"
1710 if test -n "$rr_exp_i"; then
1712 file=$file-expect-rr-$rr_exp
1713 directives="$directives %expect-rr $rr_exp"
1716 echo "$directives" > $file
1717 cat $gram.y >> $file
1719 # Number of found conflicts.
1721 (sr) sr_count=1; rr_count=0;;
1722 (rr) sr_count=0; rr_count=1;;
1723 (sr-rr) sr_count=1; rr_count=1;;
1726 # Update number of expected conflicts: if %expect is given then
1727 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1729 case $sr_exp_i:$rr_exp_i in
1735 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1736 ]AT_BISON_CHECK([[-Wnone $file]])[
1737 ]AT_BISON_CHECK([[-Werror $file]])[
1740 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1741 echo "warning: $sr_count shift/reduce conflicts"
1742 elif test "$sr_exp_i" -ne "$sr_count"; then
1743 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1745 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1746 echo "warning: $rr_count reduce/reduce conflicts"
1747 elif test "$rr_exp_i" -ne "$rr_count"; then
1748 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1750 } | sed -e "s/^/$file: /" > experr
1751 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1752 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[