1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2015, 2018-2019 Free Software
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 AT_BANNER([[Conflicts.]])
21 ## ------------------------- ##
22 ## Token declaration order. ##
23 ## ------------------------- ##
25 # This test checks that token are declared left to right when in a precedence
28 AT_SETUP([Token declaration order])
30 AT_BISON_OPTION_PUSHDEFS
32 AT_DATA_GRAMMAR([[input.y]],
81 AT_BISON_CHECK([-o input.c input.y])
84 AT_PARSER_CHECK([input])
86 AT_BISON_OPTION_POPDEFS
91 ## --------------------------------------------------- ##
92 ## Token declaration order: literals vs. identifiers. ##
93 ## --------------------------------------------------- ##
95 # This test checks that when several tokens are declared by the same keyword,
96 # some of them defined as a character ('a'), others as simple textual reference
97 # (A), they are declared correctly left to right.
98 # Previously, the following test would declare the states in the order 'o' 'p'
99 # M N, instead of M N 'o' 'p'.
101 AT_SETUP([Token declaration order: literals vs. identifiers])
103 AT_BISON_OPTION_PUSHDEFS
104 AT_DATA_GRAMMAR([[input.y]],
130 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
131 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
134 0 $accept: . exp $end
152 'a' shift, and go to state 1
153 'b' shift, and go to state 2
154 C shift, and go to state 3
155 D shift, and go to state 4
156 E shift, and go to state 5
157 F shift, and go to state 6
158 'g' shift, and go to state 7
159 'h' shift, and go to state 8
160 'i' shift, and go to state 9
161 'j' shift, and go to state 10
162 K shift, and go to state 11
163 L shift, and go to state 12
164 M shift, and go to state 13
165 N shift, and go to state 14
166 'o' shift, and go to state 15
167 'p' shift, and go to state 16
175 AT_BISON_OPTION_POPDEFS
179 ## ------------------------------- ##
180 ## Useless associativity warning. ##
181 ## ------------------------------- ##
183 AT_SETUP([Useless associativity warning])
186 [[%token EQ "=" PL "+" ST "*" LP "("
205 AT_BISON_CHECK([-Wprecedence input.y], 0, [],
206 [[input.y:2.1-9: warning: useless precedence and associativity for "=" [-Wprecedence]
207 input.y:4.1-5: warning: useless associativity for "*", use %precedence [-Wprecedence]
208 input.y:5.1-11: warning: useless precedence for "(" [-Wprecedence]
214 ## ---------------------------- ##
215 ## Useless precedence warning. ##
216 ## ---------------------------- ##
218 AT_SETUP([Useless precedence warning])
221 [[%token A B U V W X Y Z
248 AT_BISON_CHECK([-Wprecedence -fcaret -o input.c input.y], 0, [],
249 [[input.y:7.1-9: warning: useless precedence and associativity for U [-Wprecedence]
252 input.y:6.1-6: warning: useless precedence and associativity for V [-Wprecedence]
255 input.y:5.1-5: warning: useless precedence and associativity for W [-Wprecedence]
258 input.y:2.1-11: warning: useless precedence for Z [-Wprecedence]
266 ## ---------------- ##
267 ## S/R in initial. ##
268 ## ---------------- ##
270 # I once hacked Bison in such a way that it lost its reductions on the
271 # initial state (because it was confusing it with the last state). It
272 # took me a while to strip down my failures to this simple case. So
273 # make sure it finds the s/r conflict below.
275 AT_SETUP([S/R in initial])
284 AT_BISON_CHECK([-o input.c input.y], 0, [],
285 [[input.y:4.10-15: warning: rule useless in parser due to conflicts [-Wother]
288 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
289 [[input.y:4.10-15: warning: rule useless in parser due to conflicts [-Wother]
297 ## ------------------- ##
298 ## %nonassoc and eof. ##
299 ## ------------------- ##
301 AT_SETUP([%nonassoc and eof])
303 AT_BISON_OPTION_PUSHDEFS
304 AT_DATA_GRAMMAR([input.y],
312 #define YYERROR_VERBOSE 1
314 /* The current argument. */
315 static const char *input;
320 static size_t toknum;
321 assert (toknum <= strlen (input));
322 return input[toknum++];
336 main (int argc, const char *argv[])
338 input = argc <= 1 ? "" : argv[1];
342 AT_BISON_OPTION_POPDEFS
344 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
345 [AT_BISON_CHECK([$1[ -o input.c input.y]])
348 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
350 AT_PARSER_CHECK([input '0<0'])
351 AT_PARSER_CHECK([input '0<0<0'], [1], [],
352 [syntax error, unexpected '<'AT_EXPECTING
355 AT_PARSER_CHECK([input '0>0'])
356 AT_PARSER_CHECK([input '0>0>0'], [1], [],
357 [syntax error, unexpected '>'AT_EXPECTING
360 AT_PARSER_CHECK([input '0<0>0'], [1], [],
361 [syntax error, unexpected '>'AT_EXPECTING
364 m4_popdef([AT_EXPECTING])])
366 # Expected token list is missing.
367 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
369 # We must disable default reductions in inconsistent states in order to
370 # have an explicit list of all expected tokens.
371 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
374 # lr.default-reduction=consistent happens to work for this test case.
375 # However, for other grammars, lookahead sets can be merged for
376 # different left contexts, so it is still possible to have an incorrect
377 # expected list. Canonical LR is almost a general solution (that is, it
378 # can fail only when %nonassoc is used), so make sure it gives the same
380 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
382 # parse.lac=full is a completely general solution that does not require
383 # any of the above sacrifices. Of course, it does not extend the
384 # language-recognition power of LALR to (IE)LR, but it does ensure that
385 # the reported list of expected tokens matches what the given parser
386 # would have accepted in place of the unexpected token.
387 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
389 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
395 ## ------------------------------------------- ##
396 ## parse.error=verbose and consistent errors. ##
397 ## ------------------------------------------- ##
399 AT_CONSISTENT_ERRORS_CHECK([BISON-DIRECTIVE],
402 [UNEXPECTED-TOKEN], [EXPECTED-TOKEN])
403 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
405 AT_SETUP([[parse.error=verbose and consistent errors: ]$1])
407 AT_BISON_OPTION_PUSHDEFS([$1 AT_CXX_IF([[%defines]], [[%define api.pure]])])
409 AT_DATA_GRAMMAR([input.y],
413 import java.io.IOException;
423 ]AT_CXX_IF([[%defines]], [[%define api.pure]])])[
427 #if defined __GNUC__ && 8 <= __GNUC__
428 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
435 %define parse.error verbose
441 ]AT_JAVA_IF([[%code lexer {
442 ]AT_YYLEX_DEFINE([$3], [[return new Integer(1)]])[
448 ]AT_YYLEX_DEFINE(["$3"], [[*lvalp = 1]])[
458 AT_FULL_COMPILE([[input]])
460 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
461 $5, [a], [[, expecting 'a']],
462 $5, [b], [[, expecting 'b']])])
464 AT_PARSER_CHECK([[input]], [[1]],
466 [[syntax error, unexpected ]$4[]AT_EXPECTING[
469 m4_popdef([AT_EXPECTING])
470 AT_BISON_OPTION_POPDEFS
473 ]) dnl AT_CONSISTENT_ERRORS_CHECK
478 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
481 start: consistent-error-on-a-a 'a' ;
483 consistent-error-on-a-a:
484 'a' default-reduction
485 | 'a' default-reduction 'a'
489 default-reduction: %empty ;
492 // Provide another context in which all rules are useful so that this
493 // test case looks a little more realistic.
494 start: 'b' consistent-error-on-a-a 'c' ;
497 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
499 # Unfortunately, no expected tokens are reported even though 'b' can be
500 # accepted. Nevertheless, the main point of this test is to make sure
501 # that at least the unexpected token is reported. In a previous version
502 # of Bison, it wasn't reported because the error is detected in a
503 # consistent state with an error action, and that case always triggered
504 # the simple "syntax error" message.
506 # The point isn't to test IELR here, but state merging happens to
507 # complicate this example.
508 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
509 [AT_PREVIOUS_STATE_GRAMMAR],
510 [AT_PREVIOUS_STATE_INPUT],
512 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
514 [AT_PREVIOUS_STATE_GRAMMAR],
515 [AT_PREVIOUS_STATE_INPUT],
517 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
519 [AT_PREVIOUS_STATE_GRAMMAR],
520 [AT_PREVIOUS_STATE_INPUT],
522 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
524 [AT_PREVIOUS_STATE_GRAMMAR],
525 [AT_PREVIOUS_STATE_INPUT],
526 [[end of input]], [[none]])
528 # Even canonical LR doesn't foresee the error for 'a'!
529 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
530 %define lr.default-reduction consistent]],
531 [AT_PREVIOUS_STATE_GRAMMAR],
532 [AT_PREVIOUS_STATE_INPUT],
534 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
535 %define lr.default-reduction accepting]],
536 [AT_PREVIOUS_STATE_GRAMMAR],
537 [AT_PREVIOUS_STATE_INPUT],
539 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
540 [AT_PREVIOUS_STATE_GRAMMAR],
541 [AT_PREVIOUS_STATE_INPUT],
544 # Only LAC gets it right. In C.
545 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
546 %define parse.lac full]],
547 [AT_PREVIOUS_STATE_GRAMMAR],
548 [AT_PREVIOUS_STATE_INPUT],
550 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
551 %define parse.lac full]],
552 [AT_PREVIOUS_STATE_GRAMMAR],
553 [AT_PREVIOUS_STATE_INPUT],
556 # Only LAC gets it right. In C++.
557 AT_CONSISTENT_ERRORS_CHECK([[%language "c++"
558 %define lr.type canonical-lr
559 %define parse.lac full]],
560 [AT_PREVIOUS_STATE_GRAMMAR],
561 [AT_PREVIOUS_STATE_INPUT],
563 AT_CONSISTENT_ERRORS_CHECK([[%language "c++"
565 %define parse.lac full]],
566 [AT_PREVIOUS_STATE_GRAMMAR],
567 [AT_PREVIOUS_STATE_INPUT],
570 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
571 m4_popdef([AT_PREVIOUS_STATE_INPUT])
573 m4_pushdef([AT_USER_ACTION_GRAMMAR],
576 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
577 // incorrectly for the 'b' set in the semantic action below. All 'a'
578 // tokens are returned by yylex, which sets $$ = 1.
581 fprintf (stderr, "Wrong destructor.\n");
584 // Rather than depend on an inconsistent state to induce reading a
585 // lookahead as in the previous grammar, just assign the lookahead in a
586 // semantic action. That lookahead isn't needed before either error
587 // action is encountered. In a previous version of Bison, this was a
588 // problem as it meant yychar was not translated into yytoken before
589 // either error action. The second error action thus invoked a
590 // destructor that it selected according to the incorrect yytoken. The
591 // first error action would have reported an incorrect unexpected token
592 // except that, due to the bug described in the previous grammar, the
593 // unexpected token was not reported at all.
594 start: error-reduce consistent-error 'a' { USE ($][3); } ;
597 'a' 'a' consistent-reduction consistent-error 'a'
598 { USE (($][1, $][2, $][5)); }
603 consistent-reduction: %empty
605 assert (yychar == YYEMPTY);
615 // Provide another context in which all rules are useful so that this
616 // test case looks a little more realistic.
617 start: 'b' consistent-error 'b' ;
619 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
621 AT_CONSISTENT_ERRORS_CHECK([[]],
622 [AT_USER_ACTION_GRAMMAR],
623 [AT_USER_ACTION_INPUT],
625 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
626 [AT_USER_ACTION_GRAMMAR],
627 [AT_USER_ACTION_INPUT],
629 # No C++ or Java test because yychar cannot be manipulated by users.
631 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
632 [AT_USER_ACTION_GRAMMAR],
633 [AT_USER_ACTION_INPUT],
636 # Canonical LR doesn't foresee the error for 'a'!
637 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
638 [AT_USER_ACTION_GRAMMAR],
639 [AT_USER_ACTION_INPUT],
641 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
642 [AT_USER_ACTION_GRAMMAR],
643 [AT_USER_ACTION_INPUT],
646 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
647 [AT_USER_ACTION_GRAMMAR],
648 [AT_USER_ACTION_INPUT],
650 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
651 %define lr.default-reduction accepting]],
652 [AT_USER_ACTION_GRAMMAR],
653 [AT_USER_ACTION_INPUT],
656 m4_popdef([AT_USER_ACTION_GRAMMAR])
657 m4_popdef([AT_USER_ACTION_INPUT])
659 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
664 ## ------------------------------------------------------- ##
665 ## LAC: %nonassoc requires splitting canonical LR states. ##
666 ## ------------------------------------------------------- ##
668 # This test case demonstrates that, when %nonassoc is used, canonical
669 # LR(1) parser table construction followed by conflict resolution
670 # without further state splitting is not always sufficient to produce a
671 # parser that can detect all syntax errors as soon as possible on one
672 # token of lookahead. However, LAC solves the problem completely even
673 # with minimal LR parser tables.
675 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
676 AT_BISON_OPTION_PUSHDEFS
677 AT_DATA_GRAMMAR([[input.y]],
684 %define parse.error verbose
690 'a' problem 'a' // First context.
691 | 'b' problem 'b' // Second context.
692 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
701 // For the state reached after shifting the 'a' in these productions,
702 // lookahead sets are the same in both the first and second contexts.
703 // Thus, canonical LR reuses the same state for both contexts. However,
704 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
705 // error action only in the first context. In order to immediately
706 // detect the syntax error on 'a' here for only the first context, this
707 // canonical LR state would have to be split into two states, and the
708 // 'a' lookahead would have to be removed from only one of the states.
710 'a' // Reduction lookahead set is always ['a', 'b'].
712 | 'a' 'c' // 'c' is forgotten as an expected token.
715 reduce-nonassoc: %prec 'a';
719 ]AT_YYLEX_DEFINE(["aaa"])[
722 AT_BISON_OPTION_POPDEFS
724 # Show canonical LR's failure.
725 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
727 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
729 AT_COMPILE([[input]])
730 AT_PARSER_CHECK([[input]], [[1]], [[]],
731 [[syntax error, unexpected 'a', expecting 'b'
734 # It's corrected by LAC.
735 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
736 -o input.c input.y]], [[0]], [[]],
737 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
739 AT_COMPILE([[input]])
740 AT_PARSER_CHECK([[input]], [[1]], [[]],
741 [[syntax error, unexpected 'a', expecting 'b' or 'c'
744 # IELR is sufficient when LAC is used.
745 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
747 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
749 AT_COMPILE([[input]])
750 AT_PARSER_CHECK([[input]], [[1]], [[]],
751 [[syntax error, unexpected 'a', expecting 'b' or 'c'
756 ## ------------------------- ##
757 ## Unresolved SR Conflicts. ##
758 ## ------------------------- ##
760 AT_SETUP([Unresolved SR Conflicts])
762 AT_KEYWORDS([report])
767 exp: exp OP exp | NUM;
770 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
771 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
774 # Check the contents of the report.
775 AT_CHECK([cat input.output], [],
776 [[State 5 conflicts: 1 shift/reduce
787 Terminals, with rules where they appear
795 Nonterminals, with rules where they appear
806 0 $accept: . exp $end
810 NUM shift, and go to state 1
819 $default reduce using rule 2 (exp)
824 0 $accept: exp . $end
827 $end shift, and go to state 3
828 OP shift, and go to state 4
833 0 $accept: exp $end .
844 NUM shift, and go to state 1
852 1 | exp OP exp . [$end, OP]
854 OP shift, and go to state 4
856 OP [reduce using rule 1 (exp)]
857 $default reduce using rule 1 (exp)
864 ## ----------------------- ##
865 ## Resolved SR Conflicts. ##
866 ## ----------------------- ##
868 AT_SETUP([Resolved SR Conflicts])
870 AT_KEYWORDS([report])
876 exp: exp OP exp | NUM;
879 AT_BISON_CHECK([-o input.c --report=all input.y])
881 # Check the contents of the report.
882 AT_CHECK([cat input.output], [],
891 Terminals, with rules where they appear
899 Nonterminals, with rules where they appear
910 0 $accept: . exp $end
914 NUM shift, and go to state 1
923 $default reduce using rule 2 (exp)
928 0 $accept: exp . $end
931 $end shift, and go to state 3
932 OP shift, and go to state 4
937 0 $accept: exp $end .
948 NUM shift, and go to state 1
956 1 | exp OP exp . [$end, OP]
958 $default reduce using rule 1 (exp)
960 Conflict between rule 1 and token OP resolved as reduce (%left OP).
966 ## ---------------------- ##
967 ## %precedence suffices. ##
968 ## ---------------------- ##
970 AT_SETUP([%precedence suffices])
977 "if" cond "then" stmt
978 | "if" cond "then" stmt "else" stmt
987 AT_BISON_CHECK([-o input.c input.y])
992 ## ------------------------------ ##
993 ## %precedence does not suffice. ##
994 ## ------------------------------ ##
996 AT_SETUP([%precedence does not suffice])
1003 "if" cond "then" stmt
1004 | "if" cond "then" stmt "else" stmt
1014 AT_BISON_CHECK([-o input.c input.y], 0, [],
1015 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1016 input.y:12.3-18: warning: rule useless in parser due to conflicts [-Wother]
1022 ## -------------------------------- ##
1023 ## Defaulted Conflicted Reduction. ##
1024 ## -------------------------------- ##
1026 # When there are RR conflicts, some rules are disabled. Usually it is
1027 # simply displayed as:
1029 # $end reduce using rule 3 (num)
1030 # $end [reduce using rule 4 (id)]
1032 # But when 'reduce 3' is the default action, we'd produce:
1034 # $end [reduce using rule 4 (id)]
1035 # $default reduce using rule 3 (num)
1037 # In this precise case (a reduction is masked by the default
1038 # reduction), we make the 'reduce 3' explicit:
1040 # $end reduce using rule 3 (num)
1041 # $end [reduce using rule 4 (id)]
1042 # $default reduce using rule 3 (num)
1044 # Maybe that's not the best display, but then, please propose something
1047 AT_SETUP([Defaulted Conflicted Reduction])
1048 AT_KEYWORDS([report])
1058 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
1059 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1060 input.y:4.6-8: warning: rule useless in parser due to conflicts [-Wother]
1063 # Check the contents of the report.
1064 AT_CHECK([cat input.output], [],
1065 [[Rules useless in parser due to conflicts
1070 State 1 conflicts: 1 reduce/reduce
1085 Terminals, with rules where they appear
1092 Nonterminals, with rules where they appear
1109 0 $accept: . exp $end
1115 '0' shift, and go to state 1
1127 $end reduce using rule 3 (num)
1128 $end [reduce using rule 4 (id)]
1129 $default reduce using rule 3 (num)
1134 0 $accept: exp . $end
1136 $end shift, and go to state 5
1143 $default reduce using rule 1 (exp)
1150 $default reduce using rule 2 (exp)
1155 0 $accept: exp $end .
1165 ## -------------------- ##
1166 ## %expect not enough. ##
1167 ## -------------------- ##
1169 AT_SETUP([%expect not enough])
1175 exp: exp OP exp | NUM;
1178 AT_BISON_CHECK([-o input.c input.y], 1, [],
1179 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1184 ## --------------- ##
1185 ## %expect right. ##
1186 ## --------------- ##
1188 AT_SETUP([%expect right])
1194 exp: exp OP exp | NUM;
1197 AT_BISON_CHECK([-o input.c input.y])
1201 ## ------------------ ##
1202 ## %expect too much. ##
1203 ## ------------------ ##
1205 AT_SETUP([%expect too much])
1211 exp: exp OP exp | NUM;
1214 AT_BISON_CHECK([-o input.c input.y], 1, [],
1215 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1220 ## ------------------------------- ##
1221 ## %expect with reduce conflicts. ##
1222 ## ------------------------------- ##
1224 AT_SETUP([%expect with reduce conflicts])
1229 program: a 'a' | a a;
1233 AT_BISON_CHECK([-o input.c input.y], 1, [],
1234 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1239 ## ------------------------------------ ##
1240 ## %expect in grammar rule not enough. ##
1241 ## ------------------------------------ ##
1243 AT_SETUP([%expect in grammar rule not enough])
1249 exp: exp OP exp %expect 0 | NUM;
1252 AT_BISON_CHECK([-o input.c input.y], 1, [],
1253 [[input.y:4.6-25: error: shift/reduce conflicts for rule 1: 1 found, 0 expected
1258 ## ------------------------------- ##
1259 ## %expect in grammar rule right. ##
1260 ## ------------------------------- ##
1262 AT_SETUP([%expect in grammar rule right])
1268 exp: exp OP exp %expect 1 | NUM;
1271 AT_BISON_CHECK([-o input.c input.y])
1275 ## -------------------------- ##
1276 ## %expect in grammar rules. ##
1277 ## -------------------------- ##
1279 AT_SETUP([%expect in grammar rules])
1286 | exp "+" exp %expect 2
1287 | exp "*" exp %expect 2
1290 AT_BISON_CHECK([-o input.c -rall input.y])
1294 ## ---------------------------------- ##
1295 ## %expect in grammar rule too much. ##
1296 ## ---------------------------------- ##
1298 AT_SETUP([%expect in grammar rule too much])
1304 exp: exp OP exp | NUM %expect 1;
1307 AT_BISON_CHECK([-o input.c input.y], 1, [],
1308 [[input.y:4.19-31: error: shift/reduce conflicts for rule 2: 0 found, 1 expected
1313 ## ---------------------------- ##
1314 ## %expect-rr in grammar rule. ##
1315 ## ---------------------------- ##
1317 AT_SETUP([%expect-rr in grammar rule])
1334 AT_BISON_CHECK([-o input.c input.y])
1338 ## ------------------------------------- ##
1339 ## %expect-rr too much in grammar rule. ##
1340 ## ------------------------------------- ##
1342 AT_SETUP([%expect-rr too much in grammar rule])
1359 AT_BISON_CHECK([-fcaret -o input.c input.y], 1, [],
1360 [[input.y:12.4-15: error: reduce/reduce conflicts for rule 8: 3 found, 4 expected
1361 12 | b: %expect-rr 4
1367 ## --------------------------------------- ##
1368 ## %expect-rr not enough in grammar rule. ##
1369 ## --------------------------------------- ##
1371 AT_SETUP([%expect-rr not enough in grammar rule])
1388 AT_BISON_CHECK([-fcaret -o input.c input.y], 1, [],
1389 [[input.y:12.4-15: error: reduce/reduce conflicts for rule 8: 3 found, 2 expected
1390 12 | b: %expect-rr 2
1396 ## ------------------------- ##
1397 ## %prec with user strings. ##
1398 ## ------------------------- ##
1400 AT_SETUP([%prec with user string])
1402 AT_DATA([[input.y]],
1409 AT_BISON_CHECK([-o input.c input.y])
1413 ## -------------------------------- ##
1414 ## %no-default-prec without %prec. ##
1415 ## -------------------------------- ##
1417 AT_SETUP([%no-default-prec without %prec])
1419 AT_DATA([[input.y]],
1433 AT_BISON_CHECK([-Wall -o input.c input.y], 0, [],
1434 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1435 input.y:1.1-5: warning: useless precedence and associativity for '+' [-Wprecedence]
1436 input.y:2.1-5: warning: useless precedence and associativity for '*' [-Wprecedence]
1441 ## ----------------------------- ##
1442 ## %no-default-prec with %prec. ##
1443 ## ----------------------------- ##
1445 AT_SETUP([%no-default-prec with %prec])
1447 AT_DATA([[input.y]],
1455 e: e '+' e %prec '+'
1461 AT_BISON_CHECK([-o input.c input.y])
1465 ## --------------- ##
1466 ## %default-prec. ##
1467 ## --------------- ##
1469 AT_SETUP([%default-prec])
1471 AT_DATA([[input.y]],
1485 AT_BISON_CHECK([-o input.c input.y])
1489 ## ---------------------------------------------- ##
1490 ## Unreachable States After Conflict Resolution. ##
1491 ## ---------------------------------------------- ##
1493 AT_SETUP([[Unreachable States After Conflict Resolution]])
1495 # If conflict resolution makes states unreachable, remove those states, report
1496 # rules that are then unused, and don't report conflicts in those states. Test
1497 # what happens when a nonterminal becomes useless as a result of state removal
1498 # since that causes lalr.o's goto map to be rewritten.
1500 AT_DATA([[input.y]],
1506 start: resolved_conflict 'a' reported_conflicts 'a' ;
1508 /* S/R conflict resolved as reduce, so the state with item
1509 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1510 * unreachable, and the associated production is useless. */
1516 /* S/R conflict that need not be reported since it is unreachable because of
1517 * the previous conflict resolution. Nonterminal unreachable1 and all its
1518 * productions are useless. */
1524 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1527 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1528 * when their states are renumbered due to state removal. */
1537 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1538 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1539 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1540 input.y:12.5-20: warning: rule useless in parser due to conflicts [-Wother]
1541 input.y:20.5-20: warning: rule useless in parser due to conflicts [-Wother]
1542 input.y:21.4: warning: rule useless in parser due to conflicts [-Wother]
1543 input.y:25.14: warning: rule useless in parser due to conflicts [-Wother]
1544 input.y:25.16: warning: rule useless in parser due to conflicts [-Wother]
1545 input.y:31.5-7: warning: rule useless in parser due to conflicts [-Wother]
1546 input.y:32.4: warning: rule useless in parser due to conflicts [-Wother]
1549 AT_CHECK([[cat input.output]], 0,
1550 [[Rules useless in parser due to conflicts
1552 2 resolved_conflict: 'a' unreachable1
1554 4 unreachable1: 'a' unreachable2
1557 6 unreachable2: %empty
1560 9 reported_conflicts: 'a'
1564 State 4 conflicts: 1 shift/reduce
1565 State 5 conflicts: 1 reduce/reduce
1570 0 $accept: start $end
1572 1 start: resolved_conflict 'a' reported_conflicts 'a'
1574 2 resolved_conflict: 'a' unreachable1
1577 4 unreachable1: 'a' unreachable2
1580 6 unreachable2: %empty
1583 8 reported_conflicts: 'a'
1588 Terminals, with rules where they appear
1595 Nonterminals, with rules where they appear
1602 resolved_conflict (6)
1611 reported_conflicts (9)
1618 0 $accept: . start $end
1619 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1620 2 resolved_conflict: . 'a' unreachable1
1623 $default reduce using rule 3 (resolved_conflict)
1626 resolved_conflict go to state 2
1628 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1633 0 $accept: start . $end
1635 $end shift, and go to state 3
1640 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1642 'a' shift, and go to state 4
1647 0 $accept: start $end .
1654 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1655 8 reported_conflicts: . 'a'
1659 'a' shift, and go to state 5
1661 'a' [reduce using rule 10 (reported_conflicts)]
1663 reported_conflicts go to state 6
1668 8 reported_conflicts: 'a' . ['a']
1671 'a' reduce using rule 8 (reported_conflicts)
1672 'a' [reduce using rule 9 (reported_conflicts)]
1673 $default reduce using rule 8 (reported_conflicts)
1678 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1680 'a' shift, and go to state 7
1685 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1687 $default reduce using rule 1 (start)
1690 AT_DATA([[input-keep.y]],
1691 [[%define lr.keep-unreachable-state
1693 AT_CHECK([[cat input.y >> input-keep.y]])
1695 AT_BISON_CHECK([[input-keep.y]], 0, [],
1696 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1697 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1698 input-keep.y:22.4: warning: rule useless in parser due to conflicts [-Wother]
1699 input-keep.y:26.16: warning: rule useless in parser due to conflicts [-Wother]
1700 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts [-Wother]
1701 input-keep.y:33.4: warning: rule useless in parser due to conflicts [-Wother]
1707 ## ------------------------------------------------------------ ##
1708 ## Solved conflicts report for multiple reductions in a state. ##
1709 ## ------------------------------------------------------------ ##
1711 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1713 # Used to lose earlier solved conflict messages even within a single S/R/R.
1715 AT_DATA([[input.y]],
1731 empty_a: %empty %prec 'a' ;
1732 empty_b: %empty %prec 'b' ;
1733 empty_c1: %empty %prec 'c' ;
1734 empty_c2: %empty %prec 'c' ;
1735 empty_c3: %empty %prec 'd' ;
1737 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1738 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1741 0 $accept: . start $end
1750 9 empty_a: . %empty ['a']
1751 10 empty_b: . %empty []
1752 11 empty_c1: . %empty []
1753 12 empty_c2: . %empty []
1754 13 empty_c3: . %empty ['c']
1756 'b' shift, and go to state 1
1758 'c' reduce using rule 13 (empty_c3)
1759 $default reduce using rule 9 (empty_a)
1762 empty_a go to state 3
1763 empty_b go to state 4
1764 empty_c1 go to state 5
1765 empty_c2 go to state 6
1766 empty_c3 go to state 7
1768 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1769 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1770 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1771 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1772 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1781 ## ------------------------------------------------------------ ##
1782 ## %nonassoc error actions for multiple reductions in a state. ##
1783 ## ------------------------------------------------------------ ##
1785 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1786 # multiple reductions in a state.
1788 # For a %nonassoc error action token, used to print the first remaining
1789 # reduction on that token without brackets.
1791 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1793 AT_DATA([[input.y]],
1794 [[%nonassoc 'a' 'b' 'c'
1806 empty_a: %prec 'a' ;
1807 empty_b: %prec 'b' ;
1808 empty_c1: %prec 'c' ;
1809 empty_c2: %prec 'c' ;
1810 empty_c3: %prec 'c' ;
1813 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1814 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1817 0 $accept: . start $end
1826 9 empty_a: . %empty []
1827 10 empty_b: . %empty []
1828 11 empty_c1: . %empty []
1829 12 empty_c2: . %empty ['c']
1830 13 empty_c3: . %empty ['c']
1832 'a' error (nonassociative)
1833 'b' error (nonassociative)
1834 'c' error (nonassociative)
1836 'c' [reduce using rule 12 (empty_c2)]
1837 'c' [reduce using rule 13 (empty_c3)]
1840 empty_a go to state 2
1841 empty_b go to state 3
1842 empty_c1 go to state 4
1843 empty_c2 go to state 5
1844 empty_c3 go to state 6
1846 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1847 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1848 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1856 ## -------------------- ##
1857 ## %expect-rr non GLR. ##
1858 ## -------------------- ##
1860 AT_SETUP([[%expect-rr non GLR]])
1868 AT_BISON_CHECK([[1.y]], [[0]], [],
1869 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1878 AT_BISON_CHECK([[2.y]], [[0]], [],
1879 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1880 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1881 2.y:3.12-14: warning: rule useless in parser due to conflicts [-Wother]
1887 ## ---------------------------------- ##
1888 ## -W versus %expect and %expect-rr. ##
1889 ## ---------------------------------- ##
1891 AT_SETUP([[-W versus %expect and %expect-rr]])
1893 AT_DATA([[sr-rr.y]],
1896 start: 'a' | A 'a' | B 'a' ;
1903 start: 'a' | A 'a' ;
1914 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1915 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1916 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1918 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1919 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1921 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1922 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1926 # This is piece of code is rather complex for a simple task: try every
1927 # combination of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1928 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1931 # Number and types of genuine conflicts in the grammar.
1932 for gram in sr-rr sr rr; do
1933 # Number of expected s/r conflicts.
1934 for sr_exp_i in '' 0 1 2; do
1935 # Number of expected r/r conflicts.
1936 for rr_exp_i in '' 0 1 2; do
1937 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1939 # Build grammar file.
1944 if test -n "$sr_exp_i"; then
1946 file=$file-expect-$sr_exp
1947 directives="%expect $sr_exp"
1949 if test -n "$rr_exp_i"; then
1951 file=$file-expect-rr-$rr_exp
1952 directives="$directives %expect-rr $rr_exp"
1955 echo "$directives" > $file
1956 cat $gram.y >> $file
1958 # Number of found conflicts.
1960 (sr) sr_count=1; rr_count=0;;
1961 (rr) sr_count=0; rr_count=1;;
1962 (sr-rr) sr_count=1; rr_count=1;;
1965 # Update number of expected conflicts: if %expect is given then
1966 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1968 case $sr_exp_i:$rr_exp_i in
1974 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1975 ]AT_BISON_CHECK([[-Wnone $file]])[
1976 ]AT_BISON_CHECK([[-Werror $file]])[
1979 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1980 echo "warning: $sr_count shift/reduce conflicts"
1981 elif test "$sr_exp_i" -ne "$sr_count"; then
1982 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1984 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1985 echo "warning: $rr_count reduce/reduce conflicts"
1986 elif test "$rr_exp_i" -ne "$rr_count"; then
1987 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1989 } | sed -e "s/^/$file: /" > experr
1990 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1991 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[