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 ## Token declaration order. ##
22 ## ------------------------- ##
24 # This test checks that token are declared left to right when in a precedence
27 AT_SETUP([Token declaration order])
29 AT_BISON_OPTION_PUSHDEFS
31 AT_DATA_GRAMMAR([[input.y]],
80 AT_BISON_CHECK([-o input.c input.y])
83 AT_PARSER_CHECK([./input])
85 AT_BISON_OPTION_POPDEFS
90 ## --------------------------------------------------- ##
91 ## Token declaration order: literals vs. identifiers. ##
92 ## --------------------------------------------------- ##
94 # This test checks that when several tokens are declared by the same keyword,
95 # some of them defined as a character ('a'), others as simple textual reference
96 # (A), they are declared correctly left to right.
97 # Previously, the following test would declare the states in the order 'o' 'p'
98 # M N, instead of M N 'o' 'p'.
100 AT_SETUP([Token declaration order: literals vs. identifiers])
102 AT_DATA_GRAMMAR([[input.y]],
128 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
129 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
132 0 $accept: . exp $end
150 'a' shift, and go to state 1
151 'b' shift, and go to state 2
152 C shift, and go to state 3
153 D shift, and go to state 4
154 E shift, and go to state 5
155 F shift, and go to state 6
156 'g' shift, and go to state 7
157 'h' shift, and go to state 8
158 'i' shift, and go to state 9
159 'j' shift, and go to state 10
160 K shift, and go to state 11
161 L shift, and go to state 12
162 M shift, and go to state 13
163 N shift, and go to state 14
164 'o' shift, and go to state 15
165 'p' shift, and go to state 16
176 ## ------------------------------- ##
177 ## Useless associativity warning. ##
178 ## ------------------------------- ##
180 AT_SETUP([Useless associativity warning])
201 AT_BISON_CHECK([-Wprecedence input.y], 0, [],
202 [[input.y:1.11-13: warning: useless precedence and associativity for "=" [-Wprecedence]
203 input.y:3.7-9: warning: useless associativity for "*", use %precedence [-Wprecedence]
204 input.y:4.13-15: warning: useless precedence for "(" [-Wprecedence]
210 ## ---------------------------- ##
211 ## Useless precedence warning. ##
212 ## ---------------------------- ##
214 AT_SETUP([Useless precedence warning])
244 AT_BISON_CHECK([-Wprecedence -fcaret -o input.c input.y], 0, [],
245 [[input.y:2.13: warning: useless precedence for Z [-Wprecedence]
248 input.y:5.7: warning: useless precedence and associativity for W [-Wprecedence]
251 input.y:6.8: warning: useless precedence and associativity for V [-Wprecedence]
254 input.y:7.11: warning: useless precedence and associativity for U [-Wprecedence]
262 ## ---------------- ##
263 ## S/R in initial. ##
264 ## ---------------- ##
266 # I once hacked Bison in such a way that it lost its reductions on the
267 # initial state (because it was confusing it with the last state). It
268 # took me a while to strip down my failures to this simple case. So
269 # make sure it finds the s/r conflict below.
271 AT_SETUP([S/R in initial])
277 e: 'e' | /* Nothing. */;
280 AT_BISON_CHECK([-o input.c input.y], 0, [],
281 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
284 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
285 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
286 e: 'e' | /* Nothing. */;
293 ## ------------------- ##
294 ## %nonassoc and eof. ##
295 ## ------------------- ##
297 AT_SETUP([%nonassoc and eof])
299 AT_BISON_OPTION_PUSHDEFS
300 AT_DATA_GRAMMAR([input.y],
308 #define YYERROR_VERBOSE 1
310 /* The current argument. */
311 static const char *input;
316 static size_t toknum;
317 assert (toknum <= strlen (input));
318 return input[toknum++];
332 main (int argc, const char *argv[])
334 input = argc <= 1 ? "" : argv[1];
338 AT_BISON_OPTION_POPDEFS
340 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
341 [AT_BISON_CHECK([$1[ -o input.c input.y]])
344 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
346 AT_PARSER_CHECK([./input '0<0'])
347 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
348 [syntax error, unexpected '<'AT_EXPECTING
351 AT_PARSER_CHECK([./input '0>0'])
352 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
353 [syntax error, unexpected '>'AT_EXPECTING
356 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
357 [syntax error, unexpected '>'AT_EXPECTING
360 m4_popdef([AT_EXPECTING])])
362 # Expected token list is missing.
363 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
365 # We must disable default reductions in inconsistent states in order to
366 # have an explicit list of all expected tokens.
367 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
370 # lr.default-reduction=consistent happens to work for this test case.
371 # However, for other grammars, lookahead sets can be merged for
372 # different left contexts, so it is still possible to have an incorrect
373 # expected list. Canonical LR is almost a general solution (that is, it
374 # can fail only when %nonassoc is used), so make sure it gives the same
376 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
378 # parse.lac=full is a completely general solution that does not require
379 # any of the above sacrifices. Of course, it does not extend the
380 # language-recognition power of LALR to (IE)LR, but it does ensure that
381 # the reported list of expected tokens matches what the given parser
382 # would have accepted in place of the unexpected token.
383 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
385 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
391 ## ------------------------------------------- ##
392 ## parse.error=verbose and consistent errors. ##
393 ## ------------------------------------------- ##
395 AT_SETUP([[parse.error=verbose and consistent errors]])
397 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
399 AT_BISON_OPTION_PUSHDEFS([$1])
401 m4_pushdef([AT_YYLEX_PROTOTYPE],
402 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
403 [[int yylex (YYSTYPE *lvalp)]])])
405 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
409 import java.io.IOException;
412 %code {]AT_SKEL_CC_IF([[
413 #include <string>]], [[
416 ]AT_YYERROR_DECLARE])[
417 ]AT_YYLEX_PROTOTYPE[;
421 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
425 %define parse.error verbose
431 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
435 `--------*/]AT_SKEL_JAVA_IF([[
437 public String input = "]$3[";
438 public int index = 0;
441 if (index < input.length ())
442 return input.charAt (index++);
446 public Object getLVal ()
448 return new Integer(1);
453 static char const *input = "]$3[";
469 AT_FULL_COMPILE([[input]])
471 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
472 $5, [a], [[, expecting 'a']],
473 $5, [b], [[, expecting 'b']])])
475 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
476 [AT_PARSER_CHECK([[./input]], [[1]]]),
478 [[syntax error, unexpected ]$4[]AT_EXPECTING[
481 m4_popdef([AT_EXPECTING])
482 m4_popdef([AT_YYLEX_PROTOTYPE])
483 AT_BISON_OPTION_POPDEFS
487 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
490 start: consistent-error-on-a-a 'a' ;
492 consistent-error-on-a-a:
493 'a' default-reduction
494 | 'a' default-reduction 'a'
498 default-reduction: /*empty*/ ;
501 // Provide another context in which all rules are useful so that this
502 // test case looks a little more realistic.
503 start: 'b' consistent-error-on-a-a 'c' ;
506 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
508 # Unfortunately, no expected tokens are reported even though 'b' can be
509 # accepted. Nevertheless, the main point of this test is to make sure
510 # that at least the unexpected token is reported. In a previous version
511 # of Bison, it wasn't reported because the error is detected in a
512 # consistent state with an error action, and that case always triggered
513 # the simple "syntax error" message.
515 # The point isn't to test IELR here, but state merging happens to
516 # complicate this example.
517 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
518 [AT_PREVIOUS_STATE_GRAMMAR],
519 [AT_PREVIOUS_STATE_INPUT],
521 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
523 [AT_PREVIOUS_STATE_GRAMMAR],
524 [AT_PREVIOUS_STATE_INPUT],
526 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
528 [AT_PREVIOUS_STATE_GRAMMAR],
529 [AT_PREVIOUS_STATE_INPUT],
531 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
533 [AT_PREVIOUS_STATE_GRAMMAR],
534 [AT_PREVIOUS_STATE_INPUT],
535 [[end of input]], [[none]])
537 # Even canonical LR doesn't foresee the error for 'a'!
538 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
539 %define lr.default-reduction consistent]],
540 [AT_PREVIOUS_STATE_GRAMMAR],
541 [AT_PREVIOUS_STATE_INPUT],
543 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
544 %define lr.default-reduction accepting]],
545 [AT_PREVIOUS_STATE_GRAMMAR],
546 [AT_PREVIOUS_STATE_INPUT],
548 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
549 [AT_PREVIOUS_STATE_GRAMMAR],
550 [AT_PREVIOUS_STATE_INPUT],
553 # Only LAC gets it right.
554 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
555 %define parse.lac full]],
556 [AT_PREVIOUS_STATE_GRAMMAR],
557 [AT_PREVIOUS_STATE_INPUT],
559 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
560 %define parse.lac full]],
561 [AT_PREVIOUS_STATE_GRAMMAR],
562 [AT_PREVIOUS_STATE_INPUT],
565 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
566 m4_popdef([AT_PREVIOUS_STATE_INPUT])
568 m4_pushdef([AT_USER_ACTION_GRAMMAR],
571 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
572 // incorrectly for the 'b' set in the semantic action below. All 'a'
573 // tokens are returned by yylex, which sets $$ = 1.
576 fprintf (stderr, "Wrong destructor.\n");
579 // Rather than depend on an inconsistent state to induce reading a
580 // lookahead as in the previous grammar, just assign the lookahead in a
581 // semantic action. That lookahead isn't needed before either error
582 // action is encountered. In a previous version of Bison, this was a
583 // problem as it meant yychar was not translated into yytoken before
584 // either error action. The second error action thus invoked a
585 // destructor that it selected according to the incorrect yytoken. The
586 // first error action would have reported an incorrect unexpected token
587 // except that, due to the bug described in the previous grammar, the
588 // unexpected token was not reported at all.
589 start: error-reduce consistent-error 'a' { USE ($][3); } ;
592 'a' 'a' consistent-reduction consistent-error 'a'
593 { USE (($][1, $][2, $][5)); }
598 consistent-reduction: /*empty*/ {
599 assert (yychar == YYEMPTY);
606 | /*empty*/ %prec 'a'
609 // Provide another context in which all rules are useful so that this
610 // test case looks a little more realistic.
611 start: 'b' consistent-error 'b' ;
613 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
615 AT_CONSISTENT_ERRORS_CHECK([[]],
616 [AT_USER_ACTION_GRAMMAR],
617 [AT_USER_ACTION_INPUT],
619 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
620 [AT_USER_ACTION_GRAMMAR],
621 [AT_USER_ACTION_INPUT],
623 # No C++ or Java test because yychar cannot be manipulated by users.
625 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
626 [AT_USER_ACTION_GRAMMAR],
627 [AT_USER_ACTION_INPUT],
630 # Canonical LR doesn't foresee the error for 'a'!
631 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
632 [AT_USER_ACTION_GRAMMAR],
633 [AT_USER_ACTION_INPUT],
635 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
636 [AT_USER_ACTION_GRAMMAR],
637 [AT_USER_ACTION_INPUT],
640 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
641 [AT_USER_ACTION_GRAMMAR],
642 [AT_USER_ACTION_INPUT],
644 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
645 %define lr.default-reduction accepting]],
646 [AT_USER_ACTION_GRAMMAR],
647 [AT_USER_ACTION_INPUT],
650 m4_popdef([AT_USER_ACTION_GRAMMAR])
651 m4_popdef([AT_USER_ACTION_INPUT])
653 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
659 ## ------------------------------------------------------- ##
660 ## LAC: %nonassoc requires splitting canonical LR states. ##
661 ## ------------------------------------------------------- ##
663 # This test case demonstrates that, when %nonassoc is used, canonical
664 # LR(1) parser table construction followed by conflict resolution
665 # without further state splitting is not always sufficient to produce a
666 # parser that can detect all syntax errors as soon as possible on one
667 # token of lookahead. However, LAC solves the problem completely even
668 # with minimal LR parser tables.
670 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
671 AT_BISON_OPTION_PUSHDEFS
672 AT_DATA_GRAMMAR([[input.y]],
685 'a' problem 'a' // First context.
686 | 'b' problem 'b' // Second context.
687 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
696 // For the state reached after shifting the 'a' in these productions,
697 // lookahead sets are the same in both the first and second contexts.
698 // Thus, canonical LR reuses the same state for both contexts. However,
699 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
700 // error action only in the first context. In order to immediately
701 // detect the syntax error on 'a' here for only the first context, this
702 // canonical LR state would have to be split into two states, and the
703 // 'a' lookahead would have to be removed from only one of the states.
705 'a' // Reduction lookahead set is always ['a', 'b'].
707 | 'a' 'c' // 'c' is forgotten as an expected token.
710 reduce-nonassoc: %prec 'a';
714 ]AT_YYLEX_DEFINE(["aaa"])[
717 AT_BISON_OPTION_POPDEFS
719 # Show canonical LR's failure.
720 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
722 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
724 AT_COMPILE([[input]])
725 AT_PARSER_CHECK([[./input]], [[1]], [[]],
726 [[syntax error, unexpected 'a', expecting 'b'
729 # It's corrected by LAC.
730 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
731 -o input.c input.y]], [[0]], [[]],
732 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
734 AT_COMPILE([[input]])
735 AT_PARSER_CHECK([[./input]], [[1]], [[]],
736 [[syntax error, unexpected 'a', expecting 'b' or 'c'
739 # IELR is sufficient when LAC is used.
740 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
742 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
744 AT_COMPILE([[input]])
745 AT_PARSER_CHECK([[./input]], [[1]], [[]],
746 [[syntax error, unexpected 'a', expecting 'b' or 'c'
751 ## ------------------------- ##
752 ## Unresolved SR Conflicts. ##
753 ## ------------------------- ##
755 AT_SETUP([Unresolved SR Conflicts])
757 AT_KEYWORDS([report])
762 exp: exp OP exp | NUM;
765 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
766 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
769 # Check the contents of the report.
770 AT_CHECK([cat input.output], [],
771 [[State 5 conflicts: 1 shift/reduce
782 Terminals, with rules where they appear
790 Nonterminals, with rules where they appear
795 on left: 1 2, on right: 0 1
800 0 $accept: . exp $end
804 NUM shift, and go to state 1
813 $default reduce using rule 2 (exp)
818 0 $accept: exp . $end
821 $end shift, and go to state 3
822 OP shift, and go to state 4
827 0 $accept: exp $end .
838 NUM shift, and go to state 1
846 1 | exp OP exp . [$end, OP]
848 OP shift, and go to state 4
850 OP [reduce using rule 1 (exp)]
851 $default reduce using rule 1 (exp)
858 ## ----------------------- ##
859 ## Resolved SR Conflicts. ##
860 ## ----------------------- ##
862 AT_SETUP([Resolved SR Conflicts])
864 AT_KEYWORDS([report])
870 exp: exp OP exp | NUM;
873 AT_BISON_CHECK([-o input.c --report=all input.y])
875 # Check the contents of the report.
876 AT_CHECK([cat input.output], [],
885 Terminals, with rules where they appear
893 Nonterminals, with rules where they appear
898 on left: 1 2, on right: 0 1
903 0 $accept: . exp $end
907 NUM shift, and go to state 1
916 $default reduce using rule 2 (exp)
921 0 $accept: exp . $end
924 $end shift, and go to state 3
925 OP shift, and go to state 4
930 0 $accept: exp $end .
941 NUM shift, and go to state 1
949 1 | exp OP exp . [$end, OP]
951 $default reduce using rule 1 (exp)
953 Conflict between rule 1 and token OP resolved as reduce (%left OP).
959 ## ---------------------- ##
960 ## %precedence suffices. ##
961 ## ---------------------- ##
963 AT_SETUP([%precedence suffices])
970 "if" cond "then" stmt
971 | "if" cond "then" stmt "else" stmt
980 AT_BISON_CHECK([-o input.c input.y])
985 ## ------------------------------ ##
986 ## %precedence does not suffice. ##
987 ## ------------------------------ ##
989 AT_SETUP([%precedence does not suffice])
996 "if" cond "then" stmt
997 | "if" cond "then" stmt "else" stmt
1007 AT_BISON_CHECK([-o input.c input.y], 0, [],
1008 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1009 input.y:12.3-18: warning: rule useless in parser due to conflicts [-Wother]
1015 ## -------------------------------- ##
1016 ## Defaulted Conflicted Reduction. ##
1017 ## -------------------------------- ##
1019 # When there are RR conflicts, some rules are disabled. Usually it is
1020 # simply displayed as:
1022 # $end reduce using rule 3 (num)
1023 # $end [reduce using rule 4 (id)]
1025 # But when 'reduce 3' is the default action, we'd produce:
1027 # $end [reduce using rule 4 (id)]
1028 # $default reduce using rule 3 (num)
1030 # In this precise case (a reduction is masked by the default
1031 # reduction), we make the 'reduce 3' explicit:
1033 # $end reduce using rule 3 (num)
1034 # $end [reduce using rule 4 (id)]
1035 # $default reduce using rule 3 (num)
1037 # Maybe that's not the best display, but then, please propose something
1040 AT_SETUP([Defaulted Conflicted Reduction])
1041 AT_KEYWORDS([report])
1051 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
1052 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1053 input.y:4.6-8: warning: rule useless in parser due to conflicts [-Wother]
1056 # Check the contents of the report.
1057 AT_CHECK([cat input.output], [],
1058 [[Rules useless in parser due to conflicts
1063 State 1 conflicts: 1 reduce/reduce
1078 Terminals, with rules where they appear
1085 Nonterminals, with rules where they appear
1090 on left: 1 2, on right: 0
1092 on left: 3, on right: 1
1094 on left: 4, on right: 2
1099 0 $accept: . exp $end
1105 '0' shift, and go to state 1
1117 $end reduce using rule 3 (num)
1118 $end [reduce using rule 4 (id)]
1119 $default reduce using rule 3 (num)
1124 0 $accept: exp . $end
1126 $end shift, and go to state 5
1133 $default reduce using rule 1 (exp)
1140 $default reduce using rule 2 (exp)
1145 0 $accept: exp $end .
1155 ## -------------------- ##
1156 ## %expect not enough. ##
1157 ## -------------------- ##
1159 AT_SETUP([%expect not enough])
1165 exp: exp OP exp | NUM;
1168 AT_BISON_CHECK([-o input.c input.y], 1, [],
1169 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1174 ## --------------- ##
1175 ## %expect right. ##
1176 ## --------------- ##
1178 AT_SETUP([%expect right])
1184 exp: exp OP exp | NUM;
1187 AT_BISON_CHECK([-o input.c input.y])
1191 ## ------------------ ##
1192 ## %expect too much. ##
1193 ## ------------------ ##
1195 AT_SETUP([%expect too much])
1201 exp: exp OP exp | NUM;
1204 AT_BISON_CHECK([-o input.c input.y], 1, [],
1205 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1210 ## ------------------------------- ##
1211 ## %expect with reduce conflicts. ##
1212 ## ------------------------------- ##
1214 AT_SETUP([%expect with reduce conflicts])
1219 program: a 'a' | a a;
1223 AT_BISON_CHECK([-o input.c input.y], 1, [],
1224 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1229 ## ------------------------- ##
1230 ## %prec with user strings. ##
1231 ## ------------------------- ##
1233 AT_SETUP([%prec with user string])
1235 AT_DATA([[input.y]],
1242 AT_BISON_CHECK([-o input.c input.y])
1246 ## -------------------------------- ##
1247 ## %no-default-prec without %prec. ##
1248 ## -------------------------------- ##
1250 AT_SETUP([%no-default-prec without %prec])
1252 AT_DATA([[input.y]],
1266 AT_BISON_CHECK([-Wall -o input.c input.y], 0, [],
1267 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1268 input.y:1.7-9: warning: useless precedence and associativity for '+' [-Wprecedence]
1269 input.y:2.7-9: warning: useless precedence and associativity for '*' [-Wprecedence]
1274 ## ----------------------------- ##
1275 ## %no-default-prec with %prec. ##
1276 ## ----------------------------- ##
1278 AT_SETUP([%no-default-prec with %prec])
1280 AT_DATA([[input.y]],
1288 e: e '+' e %prec '+'
1294 AT_BISON_CHECK([-o input.c input.y])
1298 ## --------------- ##
1299 ## %default-prec. ##
1300 ## --------------- ##
1302 AT_SETUP([%default-prec])
1304 AT_DATA([[input.y]],
1318 AT_BISON_CHECK([-o input.c input.y])
1322 ## ---------------------------------------------- ##
1323 ## Unreachable States After Conflict Resolution. ##
1324 ## ---------------------------------------------- ##
1326 AT_SETUP([[Unreachable States After Conflict Resolution]])
1328 # If conflict resolution makes states unreachable, remove those states, report
1329 # rules that are then unused, and don't report conflicts in those states. Test
1330 # what happens when a nonterminal becomes useless as a result of state removal
1331 # since that causes lalr.o's goto map to be rewritten.
1333 AT_DATA([[input.y]],
1339 start: resolved_conflict 'a' reported_conflicts 'a' ;
1341 /* S/R conflict resolved as reduce, so the state with item
1342 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1343 * unreachable, and the associated production is useless. */
1349 /* S/R conflict that need not be reported since it is unreachable because of
1350 * the previous conflict resolution. Nonterminal unreachable1 and all its
1351 * productions are useless. */
1357 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1360 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1361 * when their states are renumbered due to state removal. */
1370 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1371 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1372 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1373 input.y:12.5-20: warning: rule useless in parser due to conflicts [-Wother]
1374 input.y:20.5-20: warning: rule useless in parser due to conflicts [-Wother]
1375 input.y:21.4: warning: rule useless in parser due to conflicts [-Wother]
1376 input.y:25.13: warning: rule useless in parser due to conflicts [-Wother]
1377 input.y:25.16: warning: rule useless in parser due to conflicts [-Wother]
1378 input.y:31.5-7: warning: rule useless in parser due to conflicts [-Wother]
1379 input.y:32.4: warning: rule useless in parser due to conflicts [-Wother]
1382 AT_CHECK([[cat input.output]], 0,
1383 [[Rules useless in parser due to conflicts
1385 2 resolved_conflict: 'a' unreachable1
1387 4 unreachable1: 'a' unreachable2
1390 6 unreachable2: %empty
1393 9 reported_conflicts: 'a'
1397 State 4 conflicts: 1 shift/reduce
1398 State 5 conflicts: 1 reduce/reduce
1403 0 $accept: start $end
1405 1 start: resolved_conflict 'a' reported_conflicts 'a'
1407 2 resolved_conflict: 'a' unreachable1
1410 4 unreachable1: 'a' unreachable2
1413 6 unreachable2: %empty
1416 8 reported_conflicts: 'a'
1421 Terminals, with rules where they appear
1428 Nonterminals, with rules where they appear
1433 on left: 1, on right: 0
1434 resolved_conflict (6)
1435 on left: 2 3, on right: 1
1437 on left: 4 5, on right: 2
1439 on left: 6 7, on right: 4
1440 reported_conflicts (9)
1441 on left: 8 9 10, on right: 1
1446 0 $accept: . start $end
1447 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1448 2 resolved_conflict: . 'a' unreachable1
1451 $default reduce using rule 3 (resolved_conflict)
1454 resolved_conflict go to state 2
1456 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1461 0 $accept: start . $end
1463 $end shift, and go to state 3
1468 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1470 'a' shift, and go to state 4
1475 0 $accept: start $end .
1482 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1483 8 reported_conflicts: . 'a'
1487 'a' shift, and go to state 5
1489 'a' [reduce using rule 10 (reported_conflicts)]
1491 reported_conflicts go to state 6
1496 8 reported_conflicts: 'a' . ['a']
1499 'a' reduce using rule 8 (reported_conflicts)
1500 'a' [reduce using rule 9 (reported_conflicts)]
1501 $default reduce using rule 8 (reported_conflicts)
1506 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1508 'a' shift, and go to state 7
1513 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1515 $default reduce using rule 1 (start)
1518 AT_DATA([[input-keep.y]],
1519 [[%define lr.keep-unreachable-state
1521 AT_CHECK([[cat input.y >> input-keep.y]])
1523 AT_BISON_CHECK([[input-keep.y]], 0, [],
1524 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1525 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1526 input-keep.y:22.4: warning: rule useless in parser due to conflicts [-Wother]
1527 input-keep.y:26.16: warning: rule useless in parser due to conflicts [-Wother]
1528 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts [-Wother]
1529 input-keep.y:33.4: warning: rule useless in parser due to conflicts [-Wother]
1535 ## ------------------------------------------------------------ ##
1536 ## Solved conflicts report for multiple reductions in a state. ##
1537 ## ------------------------------------------------------------ ##
1539 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1541 # Used to lose earlier solved conflict messages even within a single S/R/R.
1543 AT_DATA([[input.y]],
1559 empty_a: %prec 'a' ;
1560 empty_b: %prec 'b' ;
1561 empty_c1: %prec 'c' ;
1562 empty_c2: %prec 'c' ;
1563 empty_c3: %prec 'd' ;
1565 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1566 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1569 0 $accept: . start $end
1582 13 empty_c3: . ['c']
1584 'b' shift, and go to state 1
1586 'c' reduce using rule 13 (empty_c3)
1587 $default reduce using rule 9 (empty_a)
1590 empty_a go to state 3
1591 empty_b go to state 4
1592 empty_c1 go to state 5
1593 empty_c2 go to state 6
1594 empty_c3 go to state 7
1596 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1597 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1598 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1599 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1600 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1609 ## ------------------------------------------------------------ ##
1610 ## %nonassoc error actions for multiple reductions in a state. ##
1611 ## ------------------------------------------------------------ ##
1613 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1614 # multiple reductions in a state.
1616 # For a %nonassoc error action token, used to print the first remaining
1617 # reduction on that token without brackets.
1619 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1621 AT_DATA([[input.y]],
1622 [[%nonassoc 'a' 'b' 'c'
1634 empty_a: %prec 'a' ;
1635 empty_b: %prec 'b' ;
1636 empty_c1: %prec 'c' ;
1637 empty_c2: %prec 'c' ;
1638 empty_c3: %prec 'c' ;
1641 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1642 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1645 0 $accept: . start $end
1657 12 empty_c2: . ['c']
1658 13 empty_c3: . ['c']
1660 'a' error (nonassociative)
1661 'b' error (nonassociative)
1662 'c' error (nonassociative)
1664 'c' [reduce using rule 12 (empty_c2)]
1665 'c' [reduce using rule 13 (empty_c3)]
1668 empty_a go to state 2
1669 empty_b go to state 3
1670 empty_c1 go to state 4
1671 empty_c2 go to state 5
1672 empty_c3 go to state 6
1674 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1675 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1676 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1684 ## -------------------- ##
1685 ## %expect-rr non GLR. ##
1686 ## -------------------- ##
1688 AT_SETUP([[%expect-rr non GLR]])
1696 AT_BISON_CHECK([[1.y]], [[0]], [],
1697 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1706 AT_BISON_CHECK([[2.y]], [[0]], [],
1707 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1708 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1709 2.y:3.12-14: warning: rule useless in parser due to conflicts [-Wother]
1715 ## ---------------------------------- ##
1716 ## -W versus %expect and %expect-rr. ##
1717 ## ---------------------------------- ##
1719 AT_SETUP([[-W versus %expect and %expect-rr]])
1721 AT_DATA([[sr-rr.y]],
1724 start: 'a' | A 'a' | B 'a' ;
1731 start: 'a' | A 'a' ;
1742 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1743 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1744 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1746 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1747 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1749 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1750 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1754 # This is piece of code is rather complex for a simple task: try every
1755 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1756 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1759 # Number and types of genuine conflicts in the grammar.
1760 for gram in sr-rr sr rr; do
1761 # Number of expected s/r conflicts.
1762 for sr_exp_i in '' 0 1 2; do
1763 # Number of expected r/r conflicts.
1764 for rr_exp_i in '' 0 1 2; do
1765 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1767 # Build grammar file.
1772 if test -n "$sr_exp_i"; then
1774 file=$file-expect-$sr_exp
1775 directives="%expect $sr_exp"
1777 if test -n "$rr_exp_i"; then
1779 file=$file-expect-rr-$rr_exp
1780 directives="$directives %expect-rr $rr_exp"
1783 echo "$directives" > $file
1784 cat $gram.y >> $file
1786 # Number of found conflicts.
1788 (sr) sr_count=1; rr_count=0;;
1789 (rr) sr_count=0; rr_count=1;;
1790 (sr-rr) sr_count=1; rr_count=1;;
1793 # Update number of expected conflicts: if %expect is given then
1794 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1796 case $sr_exp_i:$rr_exp_i in
1802 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1803 ]AT_BISON_CHECK([[-Wnone $file]])[
1804 ]AT_BISON_CHECK([[-Werror $file]])[
1807 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1808 echo "warning: $sr_count shift/reduce conflicts"
1809 elif test "$sr_exp_i" -ne "$sr_count"; then
1810 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1812 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1813 echo "warning: $rr_count reduce/reduce conflicts"
1814 elif test "$rr_exp_i" -ne "$rr_count"; then
1815 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1817 } | sed -e "s/^/$file: /" > experr
1818 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1819 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[