1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2014 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])
183 [[%token EQ "=" PL "+" ST "*" LP "("
202 AT_BISON_CHECK([-Wprecedence input.y], 0, [],
203 [[input.y:2.1-9: warning: useless precedence and associativity for "=" [-Wprecedence]
204 input.y:4.1-5: warning: useless associativity for "*", use %precedence [-Wprecedence]
205 input.y:5.1-11: warning: useless precedence for "(" [-Wprecedence]
211 ## ---------------------------- ##
212 ## Useless precedence warning. ##
213 ## ---------------------------- ##
215 AT_SETUP([Useless precedence warning])
218 [[%token A B U V W X Y Z
245 AT_BISON_CHECK([-Wprecedence -fcaret -o input.c input.y], 0, [],
246 [[input.y:7.1-9: warning: useless precedence and associativity for U [-Wprecedence]
249 input.y:6.1-6: warning: useless precedence and associativity for V [-Wprecedence]
252 input.y:5.1-5: warning: useless precedence and associativity for W [-Wprecedence]
255 input.y:2.1-11: warning: useless precedence for Z [-Wprecedence]
263 ## ---------------- ##
264 ## S/R in initial. ##
265 ## ---------------- ##
267 # I once hacked Bison in such a way that it lost its reductions on the
268 # initial state (because it was confusing it with the last state). It
269 # took me a while to strip down my failures to this simple case. So
270 # make sure it finds the s/r conflict below.
272 AT_SETUP([S/R in initial])
278 e: 'e' | /* Nothing. */;
281 AT_BISON_CHECK([-o input.c input.y], 0, [],
282 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
285 AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
286 [[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
287 e: 'e' | /* Nothing. */;
294 ## ------------------- ##
295 ## %nonassoc and eof. ##
296 ## ------------------- ##
298 AT_SETUP([%nonassoc and eof])
300 AT_BISON_OPTION_PUSHDEFS
301 AT_DATA_GRAMMAR([input.y],
309 #define YYERROR_VERBOSE 1
311 /* The current argument. */
312 static const char *input;
317 static size_t toknum;
318 assert (toknum <= strlen (input));
319 return input[toknum++];
333 main (int argc, const char *argv[])
335 input = argc <= 1 ? "" : argv[1];
339 AT_BISON_OPTION_POPDEFS
341 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
342 [AT_BISON_CHECK([$1[ -o input.c input.y]])
345 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
347 AT_PARSER_CHECK([./input '0<0'])
348 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
349 [syntax error, unexpected '<'AT_EXPECTING
352 AT_PARSER_CHECK([./input '0>0'])
353 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
354 [syntax error, unexpected '>'AT_EXPECTING
357 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
358 [syntax error, unexpected '>'AT_EXPECTING
361 m4_popdef([AT_EXPECTING])])
363 # Expected token list is missing.
364 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
366 # We must disable default reductions in inconsistent states in order to
367 # have an explicit list of all expected tokens.
368 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
371 # lr.default-reduction=consistent happens to work for this test case.
372 # However, for other grammars, lookahead sets can be merged for
373 # different left contexts, so it is still possible to have an incorrect
374 # expected list. Canonical LR is almost a general solution (that is, it
375 # can fail only when %nonassoc is used), so make sure it gives the same
377 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
379 # parse.lac=full is a completely general solution that does not require
380 # any of the above sacrifices. Of course, it does not extend the
381 # language-recognition power of LALR to (IE)LR, but it does ensure that
382 # the reported list of expected tokens matches what the given parser
383 # would have accepted in place of the unexpected token.
384 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
386 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
392 ## ------------------------------------------- ##
393 ## parse.error=verbose and consistent errors. ##
394 ## ------------------------------------------- ##
396 AT_SETUP([[parse.error=verbose and consistent errors]])
398 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
400 AT_BISON_OPTION_PUSHDEFS([$1])
402 m4_pushdef([AT_YYLEX_PROTOTYPE],
403 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
404 [[int yylex (YYSTYPE *lvalp)]])])
406 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
410 import java.io.IOException;
413 %code {]AT_SKEL_CC_IF([[
414 #include <string>]], [[
417 ]AT_YYERROR_DECLARE])[
418 ]AT_YYLEX_PROTOTYPE[;
422 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
426 %define parse.error verbose
432 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
436 `--------*/]AT_SKEL_JAVA_IF([[
438 public String input = "]$3[";
439 public int index = 0;
442 if (index < input.length ())
443 return input.charAt (index++);
447 public Object getLVal ()
449 return new Integer(1);
454 static char const *input = "]$3[";
470 AT_FULL_COMPILE([[input]])
472 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
473 $5, [a], [[, expecting 'a']],
474 $5, [b], [[, expecting 'b']])])
476 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
477 [AT_PARSER_CHECK([[./input]], [[1]]]),
479 [[syntax error, unexpected ]$4[]AT_EXPECTING[
482 m4_popdef([AT_EXPECTING])
483 m4_popdef([AT_YYLEX_PROTOTYPE])
484 AT_BISON_OPTION_POPDEFS
488 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
491 start: consistent-error-on-a-a 'a' ;
493 consistent-error-on-a-a:
494 'a' default-reduction
495 | 'a' default-reduction 'a'
499 default-reduction: /*empty*/ ;
502 // Provide another context in which all rules are useful so that this
503 // test case looks a little more realistic.
504 start: 'b' consistent-error-on-a-a 'c' ;
507 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
509 # Unfortunately, no expected tokens are reported even though 'b' can be
510 # accepted. Nevertheless, the main point of this test is to make sure
511 # that at least the unexpected token is reported. In a previous version
512 # of Bison, it wasn't reported because the error is detected in a
513 # consistent state with an error action, and that case always triggered
514 # the simple "syntax error" message.
516 # The point isn't to test IELR here, but state merging happens to
517 # complicate this example.
518 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],
527 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
529 [AT_PREVIOUS_STATE_GRAMMAR],
530 [AT_PREVIOUS_STATE_INPUT],
532 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
534 [AT_PREVIOUS_STATE_GRAMMAR],
535 [AT_PREVIOUS_STATE_INPUT],
536 [[end of input]], [[none]])
538 # Even canonical LR doesn't foresee the error for 'a'!
539 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
540 %define lr.default-reduction consistent]],
541 [AT_PREVIOUS_STATE_GRAMMAR],
542 [AT_PREVIOUS_STATE_INPUT],
544 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
545 %define lr.default-reduction accepting]],
546 [AT_PREVIOUS_STATE_GRAMMAR],
547 [AT_PREVIOUS_STATE_INPUT],
549 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
550 [AT_PREVIOUS_STATE_GRAMMAR],
551 [AT_PREVIOUS_STATE_INPUT],
554 # Only LAC gets it right.
555 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
556 %define parse.lac full]],
557 [AT_PREVIOUS_STATE_GRAMMAR],
558 [AT_PREVIOUS_STATE_INPUT],
560 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
561 %define parse.lac full]],
562 [AT_PREVIOUS_STATE_GRAMMAR],
563 [AT_PREVIOUS_STATE_INPUT],
566 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
567 m4_popdef([AT_PREVIOUS_STATE_INPUT])
569 m4_pushdef([AT_USER_ACTION_GRAMMAR],
572 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
573 // incorrectly for the 'b' set in the semantic action below. All 'a'
574 // tokens are returned by yylex, which sets $$ = 1.
577 fprintf (stderr, "Wrong destructor.\n");
580 // Rather than depend on an inconsistent state to induce reading a
581 // lookahead as in the previous grammar, just assign the lookahead in a
582 // semantic action. That lookahead isn't needed before either error
583 // action is encountered. In a previous version of Bison, this was a
584 // problem as it meant yychar was not translated into yytoken before
585 // either error action. The second error action thus invoked a
586 // destructor that it selected according to the incorrect yytoken. The
587 // first error action would have reported an incorrect unexpected token
588 // except that, due to the bug described in the previous grammar, the
589 // unexpected token was not reported at all.
590 start: error-reduce consistent-error 'a' { USE ($][3); } ;
593 'a' 'a' consistent-reduction consistent-error 'a'
594 { USE (($][1, $][2, $][5)); }
599 consistent-reduction: /*empty*/ {
600 assert (yychar == YYEMPTY);
607 | /*empty*/ %prec 'a'
610 // Provide another context in which all rules are useful so that this
611 // test case looks a little more realistic.
612 start: 'b' consistent-error 'b' ;
614 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
616 AT_CONSISTENT_ERRORS_CHECK([[]],
617 [AT_USER_ACTION_GRAMMAR],
618 [AT_USER_ACTION_INPUT],
620 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
621 [AT_USER_ACTION_GRAMMAR],
622 [AT_USER_ACTION_INPUT],
624 # No C++ or Java test because yychar cannot be manipulated by users.
626 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
627 [AT_USER_ACTION_GRAMMAR],
628 [AT_USER_ACTION_INPUT],
631 # Canonical LR doesn't foresee the error for 'a'!
632 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
633 [AT_USER_ACTION_GRAMMAR],
634 [AT_USER_ACTION_INPUT],
636 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
637 [AT_USER_ACTION_GRAMMAR],
638 [AT_USER_ACTION_INPUT],
641 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
642 [AT_USER_ACTION_GRAMMAR],
643 [AT_USER_ACTION_INPUT],
645 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
646 %define lr.default-reduction accepting]],
647 [AT_USER_ACTION_GRAMMAR],
648 [AT_USER_ACTION_INPUT],
651 m4_popdef([AT_USER_ACTION_GRAMMAR])
652 m4_popdef([AT_USER_ACTION_INPUT])
654 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
660 ## ------------------------------------------------------- ##
661 ## LAC: %nonassoc requires splitting canonical LR states. ##
662 ## ------------------------------------------------------- ##
664 # This test case demonstrates that, when %nonassoc is used, canonical
665 # LR(1) parser table construction followed by conflict resolution
666 # without further state splitting is not always sufficient to produce a
667 # parser that can detect all syntax errors as soon as possible on one
668 # token of lookahead. However, LAC solves the problem completely even
669 # with minimal LR parser tables.
671 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
672 AT_BISON_OPTION_PUSHDEFS
673 AT_DATA_GRAMMAR([[input.y]],
686 'a' problem 'a' // First context.
687 | 'b' problem 'b' // Second context.
688 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
697 // For the state reached after shifting the 'a' in these productions,
698 // lookahead sets are the same in both the first and second contexts.
699 // Thus, canonical LR reuses the same state for both contexts. However,
700 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
701 // error action only in the first context. In order to immediately
702 // detect the syntax error on 'a' here for only the first context, this
703 // canonical LR state would have to be split into two states, and the
704 // 'a' lookahead would have to be removed from only one of the states.
706 'a' // Reduction lookahead set is always ['a', 'b'].
708 | 'a' 'c' // 'c' is forgotten as an expected token.
711 reduce-nonassoc: %prec 'a';
715 ]AT_YYLEX_DEFINE(["aaa"])[
718 AT_BISON_OPTION_POPDEFS
720 # Show canonical LR's failure.
721 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
723 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
725 AT_COMPILE([[input]])
726 AT_PARSER_CHECK([[./input]], [[1]], [[]],
727 [[syntax error, unexpected 'a', expecting 'b'
730 # It's corrected by LAC.
731 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
732 -o input.c input.y]], [[0]], [[]],
733 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
735 AT_COMPILE([[input]])
736 AT_PARSER_CHECK([[./input]], [[1]], [[]],
737 [[syntax error, unexpected 'a', expecting 'b' or 'c'
740 # IELR is sufficient when LAC is used.
741 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
743 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
745 AT_COMPILE([[input]])
746 AT_PARSER_CHECK([[./input]], [[1]], [[]],
747 [[syntax error, unexpected 'a', expecting 'b' or 'c'
752 ## ------------------------- ##
753 ## Unresolved SR Conflicts. ##
754 ## ------------------------- ##
756 AT_SETUP([Unresolved SR Conflicts])
758 AT_KEYWORDS([report])
763 exp: exp OP exp | NUM;
766 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
767 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
770 # Check the contents of the report.
771 AT_CHECK([cat input.output], [],
772 [[State 5 conflicts: 1 shift/reduce
783 Terminals, with rules where they appear
791 Nonterminals, with rules where they appear
796 on left: 1 2, on right: 0 1
801 0 $accept: . exp $end
805 NUM shift, and go to state 1
814 $default reduce using rule 2 (exp)
819 0 $accept: exp . $end
822 $end shift, and go to state 3
823 OP shift, and go to state 4
828 0 $accept: exp $end .
839 NUM shift, and go to state 1
847 1 | exp OP exp . [$end, OP]
849 OP shift, and go to state 4
851 OP [reduce using rule 1 (exp)]
852 $default reduce using rule 1 (exp)
859 ## ----------------------- ##
860 ## Resolved SR Conflicts. ##
861 ## ----------------------- ##
863 AT_SETUP([Resolved SR Conflicts])
865 AT_KEYWORDS([report])
871 exp: exp OP exp | NUM;
874 AT_BISON_CHECK([-o input.c --report=all input.y])
876 # Check the contents of the report.
877 AT_CHECK([cat input.output], [],
886 Terminals, with rules where they appear
894 Nonterminals, with rules where they appear
899 on left: 1 2, on right: 0 1
904 0 $accept: . exp $end
908 NUM shift, and go to state 1
917 $default reduce using rule 2 (exp)
922 0 $accept: exp . $end
925 $end shift, and go to state 3
926 OP shift, and go to state 4
931 0 $accept: exp $end .
942 NUM shift, and go to state 1
950 1 | exp OP exp . [$end, OP]
952 $default reduce using rule 1 (exp)
954 Conflict between rule 1 and token OP resolved as reduce (%left OP).
960 ## ---------------------- ##
961 ## %precedence suffices. ##
962 ## ---------------------- ##
964 AT_SETUP([%precedence suffices])
971 "if" cond "then" stmt
972 | "if" cond "then" stmt "else" stmt
981 AT_BISON_CHECK([-o input.c input.y])
986 ## ------------------------------ ##
987 ## %precedence does not suffice. ##
988 ## ------------------------------ ##
990 AT_SETUP([%precedence does not suffice])
997 "if" cond "then" stmt
998 | "if" cond "then" stmt "else" stmt
1008 AT_BISON_CHECK([-o input.c input.y], 0, [],
1009 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1010 input.y:12.3-18: warning: rule useless in parser due to conflicts [-Wother]
1016 ## -------------------------------- ##
1017 ## Defaulted Conflicted Reduction. ##
1018 ## -------------------------------- ##
1020 # When there are RR conflicts, some rules are disabled. Usually it is
1021 # simply displayed as:
1023 # $end reduce using rule 3 (num)
1024 # $end [reduce using rule 4 (id)]
1026 # But when 'reduce 3' is the default action, we'd produce:
1028 # $end [reduce using rule 4 (id)]
1029 # $default reduce using rule 3 (num)
1031 # In this precise case (a reduction is masked by the default
1032 # reduction), we make the 'reduce 3' explicit:
1034 # $end reduce using rule 3 (num)
1035 # $end [reduce using rule 4 (id)]
1036 # $default reduce using rule 3 (num)
1038 # Maybe that's not the best display, but then, please propose something
1041 AT_SETUP([Defaulted Conflicted Reduction])
1042 AT_KEYWORDS([report])
1052 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
1053 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1054 input.y:4.6-8: warning: rule useless in parser due to conflicts [-Wother]
1057 # Check the contents of the report.
1058 AT_CHECK([cat input.output], [],
1059 [[Rules useless in parser due to conflicts
1064 State 1 conflicts: 1 reduce/reduce
1079 Terminals, with rules where they appear
1086 Nonterminals, with rules where they appear
1091 on left: 1 2, on right: 0
1093 on left: 3, on right: 1
1095 on left: 4, on right: 2
1100 0 $accept: . exp $end
1106 '0' shift, and go to state 1
1118 $end reduce using rule 3 (num)
1119 $end [reduce using rule 4 (id)]
1120 $default reduce using rule 3 (num)
1125 0 $accept: exp . $end
1127 $end shift, and go to state 5
1134 $default reduce using rule 1 (exp)
1141 $default reduce using rule 2 (exp)
1146 0 $accept: exp $end .
1156 ## -------------------- ##
1157 ## %expect not enough. ##
1158 ## -------------------- ##
1160 AT_SETUP([%expect not enough])
1166 exp: exp OP exp | NUM;
1169 AT_BISON_CHECK([-o input.c input.y], 1, [],
1170 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1175 ## --------------- ##
1176 ## %expect right. ##
1177 ## --------------- ##
1179 AT_SETUP([%expect right])
1185 exp: exp OP exp | NUM;
1188 AT_BISON_CHECK([-o input.c input.y])
1192 ## ------------------ ##
1193 ## %expect too much. ##
1194 ## ------------------ ##
1196 AT_SETUP([%expect too much])
1202 exp: exp OP exp | NUM;
1205 AT_BISON_CHECK([-o input.c input.y], 1, [],
1206 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1211 ## ------------------------------- ##
1212 ## %expect with reduce conflicts. ##
1213 ## ------------------------------- ##
1215 AT_SETUP([%expect with reduce conflicts])
1220 program: a 'a' | a a;
1224 AT_BISON_CHECK([-o input.c input.y], 1, [],
1225 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1230 ## ------------------------- ##
1231 ## %prec with user strings. ##
1232 ## ------------------------- ##
1234 AT_SETUP([%prec with user string])
1236 AT_DATA([[input.y]],
1243 AT_BISON_CHECK([-o input.c input.y])
1247 ## -------------------------------- ##
1248 ## %no-default-prec without %prec. ##
1249 ## -------------------------------- ##
1251 AT_SETUP([%no-default-prec without %prec])
1253 AT_DATA([[input.y]],
1267 AT_BISON_CHECK([-Wall -o input.c input.y], 0, [],
1268 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1269 input.y:1.1-5: warning: useless precedence and associativity for '+' [-Wprecedence]
1270 input.y:2.1-5: warning: useless precedence and associativity for '*' [-Wprecedence]
1275 ## ----------------------------- ##
1276 ## %no-default-prec with %prec. ##
1277 ## ----------------------------- ##
1279 AT_SETUP([%no-default-prec with %prec])
1281 AT_DATA([[input.y]],
1289 e: e '+' e %prec '+'
1295 AT_BISON_CHECK([-o input.c input.y])
1299 ## --------------- ##
1300 ## %default-prec. ##
1301 ## --------------- ##
1303 AT_SETUP([%default-prec])
1305 AT_DATA([[input.y]],
1319 AT_BISON_CHECK([-o input.c input.y])
1323 ## ---------------------------------------------- ##
1324 ## Unreachable States After Conflict Resolution. ##
1325 ## ---------------------------------------------- ##
1327 AT_SETUP([[Unreachable States After Conflict Resolution]])
1329 # If conflict resolution makes states unreachable, remove those states, report
1330 # rules that are then unused, and don't report conflicts in those states. Test
1331 # what happens when a nonterminal becomes useless as a result of state removal
1332 # since that causes lalr.o's goto map to be rewritten.
1334 AT_DATA([[input.y]],
1340 start: resolved_conflict 'a' reported_conflicts 'a' ;
1342 /* S/R conflict resolved as reduce, so the state with item
1343 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1344 * unreachable, and the associated production is useless. */
1350 /* S/R conflict that need not be reported since it is unreachable because of
1351 * the previous conflict resolution. Nonterminal unreachable1 and all its
1352 * productions are useless. */
1358 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1361 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1362 * when their states are renumbered due to state removal. */
1371 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1372 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1373 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1374 input.y:12.5-20: warning: rule useless in parser due to conflicts [-Wother]
1375 input.y:20.5-20: warning: rule useless in parser due to conflicts [-Wother]
1376 input.y:21.4: warning: rule useless in parser due to conflicts [-Wother]
1377 input.y:25.13: warning: rule useless in parser due to conflicts [-Wother]
1378 input.y:25.16: warning: rule useless in parser due to conflicts [-Wother]
1379 input.y:31.5-7: warning: rule useless in parser due to conflicts [-Wother]
1380 input.y:32.4: warning: rule useless in parser due to conflicts [-Wother]
1383 AT_CHECK([[cat input.output]], 0,
1384 [[Rules useless in parser due to conflicts
1386 2 resolved_conflict: 'a' unreachable1
1388 4 unreachable1: 'a' unreachable2
1391 6 unreachable2: %empty
1394 9 reported_conflicts: 'a'
1398 State 4 conflicts: 1 shift/reduce
1399 State 5 conflicts: 1 reduce/reduce
1404 0 $accept: start $end
1406 1 start: resolved_conflict 'a' reported_conflicts 'a'
1408 2 resolved_conflict: 'a' unreachable1
1411 4 unreachable1: 'a' unreachable2
1414 6 unreachable2: %empty
1417 8 reported_conflicts: 'a'
1422 Terminals, with rules where they appear
1429 Nonterminals, with rules where they appear
1434 on left: 1, on right: 0
1435 resolved_conflict (6)
1436 on left: 2 3, on right: 1
1438 on left: 4 5, on right: 2
1440 on left: 6 7, on right: 4
1441 reported_conflicts (9)
1442 on left: 8 9 10, on right: 1
1447 0 $accept: . start $end
1448 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1449 2 resolved_conflict: . 'a' unreachable1
1452 $default reduce using rule 3 (resolved_conflict)
1455 resolved_conflict go to state 2
1457 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1462 0 $accept: start . $end
1464 $end shift, and go to state 3
1469 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1471 'a' shift, and go to state 4
1476 0 $accept: start $end .
1483 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1484 8 reported_conflicts: . 'a'
1488 'a' shift, and go to state 5
1490 'a' [reduce using rule 10 (reported_conflicts)]
1492 reported_conflicts go to state 6
1497 8 reported_conflicts: 'a' . ['a']
1500 'a' reduce using rule 8 (reported_conflicts)
1501 'a' [reduce using rule 9 (reported_conflicts)]
1502 $default reduce using rule 8 (reported_conflicts)
1507 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1509 'a' shift, and go to state 7
1514 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1516 $default reduce using rule 1 (start)
1519 AT_DATA([[input-keep.y]],
1520 [[%define lr.keep-unreachable-state
1522 AT_CHECK([[cat input.y >> input-keep.y]])
1524 AT_BISON_CHECK([[input-keep.y]], 0, [],
1525 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1526 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1527 input-keep.y:22.4: warning: rule useless in parser due to conflicts [-Wother]
1528 input-keep.y:26.16: warning: rule useless in parser due to conflicts [-Wother]
1529 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts [-Wother]
1530 input-keep.y:33.4: warning: rule useless in parser due to conflicts [-Wother]
1536 ## ------------------------------------------------------------ ##
1537 ## Solved conflicts report for multiple reductions in a state. ##
1538 ## ------------------------------------------------------------ ##
1540 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1542 # Used to lose earlier solved conflict messages even within a single S/R/R.
1544 AT_DATA([[input.y]],
1560 empty_a: %prec 'a' ;
1561 empty_b: %prec 'b' ;
1562 empty_c1: %prec 'c' ;
1563 empty_c2: %prec 'c' ;
1564 empty_c3: %prec 'd' ;
1566 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1567 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1570 0 $accept: . start $end
1579 9 empty_a: . %empty ['a']
1580 10 empty_b: . %empty []
1581 11 empty_c1: . %empty []
1582 12 empty_c2: . %empty []
1583 13 empty_c3: . %empty ['c']
1585 'b' shift, and go to state 1
1587 'c' reduce using rule 13 (empty_c3)
1588 $default reduce using rule 9 (empty_a)
1591 empty_a go to state 3
1592 empty_b go to state 4
1593 empty_c1 go to state 5
1594 empty_c2 go to state 6
1595 empty_c3 go to state 7
1597 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1598 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1599 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1600 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1601 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1610 ## ------------------------------------------------------------ ##
1611 ## %nonassoc error actions for multiple reductions in a state. ##
1612 ## ------------------------------------------------------------ ##
1614 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1615 # multiple reductions in a state.
1617 # For a %nonassoc error action token, used to print the first remaining
1618 # reduction on that token without brackets.
1620 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1622 AT_DATA([[input.y]],
1623 [[%nonassoc 'a' 'b' 'c'
1635 empty_a: %prec 'a' ;
1636 empty_b: %prec 'b' ;
1637 empty_c1: %prec 'c' ;
1638 empty_c2: %prec 'c' ;
1639 empty_c3: %prec 'c' ;
1642 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1643 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1646 0 $accept: . start $end
1655 9 empty_a: . %empty []
1656 10 empty_b: . %empty []
1657 11 empty_c1: . %empty []
1658 12 empty_c2: . %empty ['c']
1659 13 empty_c3: . %empty ['c']
1661 'a' error (nonassociative)
1662 'b' error (nonassociative)
1663 'c' error (nonassociative)
1665 'c' [reduce using rule 12 (empty_c2)]
1666 'c' [reduce using rule 13 (empty_c3)]
1669 empty_a go to state 2
1670 empty_b go to state 3
1671 empty_c1 go to state 4
1672 empty_c2 go to state 5
1673 empty_c3 go to state 6
1675 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1676 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1677 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1685 ## -------------------- ##
1686 ## %expect-rr non GLR. ##
1687 ## -------------------- ##
1689 AT_SETUP([[%expect-rr non GLR]])
1697 AT_BISON_CHECK([[1.y]], [[0]], [],
1698 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1707 AT_BISON_CHECK([[2.y]], [[0]], [],
1708 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1709 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1710 2.y:3.12-14: warning: rule useless in parser due to conflicts [-Wother]
1716 ## ---------------------------------- ##
1717 ## -W versus %expect and %expect-rr. ##
1718 ## ---------------------------------- ##
1720 AT_SETUP([[-W versus %expect and %expect-rr]])
1722 AT_DATA([[sr-rr.y]],
1725 start: 'a' | A 'a' | B 'a' ;
1732 start: 'a' | A 'a' ;
1743 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1744 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1745 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1747 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1748 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1750 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1751 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1755 # This is piece of code is rather complex for a simple task: try every
1756 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1757 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1760 # Number and types of genuine conflicts in the grammar.
1761 for gram in sr-rr sr rr; do
1762 # Number of expected s/r conflicts.
1763 for sr_exp_i in '' 0 1 2; do
1764 # Number of expected r/r conflicts.
1765 for rr_exp_i in '' 0 1 2; do
1766 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1768 # Build grammar file.
1773 if test -n "$sr_exp_i"; then
1775 file=$file-expect-$sr_exp
1776 directives="%expect $sr_exp"
1778 if test -n "$rr_exp_i"; then
1780 file=$file-expect-rr-$rr_exp
1781 directives="$directives %expect-rr $rr_exp"
1784 echo "$directives" > $file
1785 cat $gram.y >> $file
1787 # Number of found conflicts.
1789 (sr) sr_count=1; rr_count=0;;
1790 (rr) sr_count=0; rr_count=1;;
1791 (sr-rr) sr_count=1; rr_count=1;;
1794 # Update number of expected conflicts: if %expect is given then
1795 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1797 case $sr_exp_i:$rr_exp_i in
1803 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1804 ]AT_BISON_CHECK([[-Wnone $file]])[
1805 ]AT_BISON_CHECK([[-Werror $file]])[
1808 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1809 echo "warning: $sr_count shift/reduce conflicts"
1810 elif test "$sr_exp_i" -ne "$sr_count"; then
1811 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1813 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1814 echo "warning: $rr_count reduce/reduce conflicts"
1815 elif test "$rr_exp_i" -ne "$rr_count"; then
1816 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1818 } | sed -e "s/^/$file: /" > experr
1819 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1820 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[