1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2015 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 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
398 AT_SETUP([[parse.error=verbose and consistent errors: $1]])
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
487 ]) dnl AT_CONSISTENT_ERRORS_CHECK
492 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
495 start: consistent-error-on-a-a 'a' ;
497 consistent-error-on-a-a:
498 'a' default-reduction
499 | 'a' default-reduction 'a'
503 default-reduction: /*empty*/ ;
506 // Provide another context in which all rules are useful so that this
507 // test case looks a little more realistic.
508 start: 'b' consistent-error-on-a-a 'c' ;
511 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
513 # Unfortunately, no expected tokens are reported even though 'b' can be
514 # accepted. Nevertheless, the main point of this test is to make sure
515 # that at least the unexpected token is reported. In a previous version
516 # of Bison, it wasn't reported because the error is detected in a
517 # consistent state with an error action, and that case always triggered
518 # the simple "syntax error" message.
520 # The point isn't to test IELR here, but state merging happens to
521 # complicate this example.
522 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],
536 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
538 [AT_PREVIOUS_STATE_GRAMMAR],
539 [AT_PREVIOUS_STATE_INPUT],
540 [[end of input]], [[none]])
542 # Even canonical LR doesn't foresee the error for 'a'!
543 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
544 %define lr.default-reduction consistent]],
545 [AT_PREVIOUS_STATE_GRAMMAR],
546 [AT_PREVIOUS_STATE_INPUT],
548 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
549 %define lr.default-reduction accepting]],
550 [AT_PREVIOUS_STATE_GRAMMAR],
551 [AT_PREVIOUS_STATE_INPUT],
553 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
554 [AT_PREVIOUS_STATE_GRAMMAR],
555 [AT_PREVIOUS_STATE_INPUT],
558 # Only LAC gets it right.
559 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
560 %define parse.lac full]],
561 [AT_PREVIOUS_STATE_GRAMMAR],
562 [AT_PREVIOUS_STATE_INPUT],
564 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
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*/ {
604 assert (yychar == YYEMPTY);
611 | /*empty*/ %prec 'a'
614 // Provide another context in which all rules are useful so that this
615 // test case looks a little more realistic.
616 start: 'b' consistent-error 'b' ;
618 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
620 AT_CONSISTENT_ERRORS_CHECK([[]],
621 [AT_USER_ACTION_GRAMMAR],
622 [AT_USER_ACTION_INPUT],
624 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
625 [AT_USER_ACTION_GRAMMAR],
626 [AT_USER_ACTION_INPUT],
628 # No C++ or Java test because yychar cannot be manipulated by users.
630 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
631 [AT_USER_ACTION_GRAMMAR],
632 [AT_USER_ACTION_INPUT],
635 # Canonical LR doesn't foresee the error for 'a'!
636 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
637 [AT_USER_ACTION_GRAMMAR],
638 [AT_USER_ACTION_INPUT],
640 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
641 [AT_USER_ACTION_GRAMMAR],
642 [AT_USER_ACTION_INPUT],
645 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
646 [AT_USER_ACTION_GRAMMAR],
647 [AT_USER_ACTION_INPUT],
649 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
650 %define lr.default-reduction accepting]],
651 [AT_USER_ACTION_GRAMMAR],
652 [AT_USER_ACTION_INPUT],
655 m4_popdef([AT_USER_ACTION_GRAMMAR])
656 m4_popdef([AT_USER_ACTION_INPUT])
658 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
663 ## ------------------------------------------------------- ##
664 ## LAC: %nonassoc requires splitting canonical LR states. ##
665 ## ------------------------------------------------------- ##
667 # This test case demonstrates that, when %nonassoc is used, canonical
668 # LR(1) parser table construction followed by conflict resolution
669 # without further state splitting is not always sufficient to produce a
670 # parser that can detect all syntax errors as soon as possible on one
671 # token of lookahead. However, LAC solves the problem completely even
672 # with minimal LR parser tables.
674 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
675 AT_BISON_OPTION_PUSHDEFS
676 AT_DATA_GRAMMAR([[input.y]],
689 'a' problem 'a' // First context.
690 | 'b' problem 'b' // Second context.
691 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
700 // For the state reached after shifting the 'a' in these productions,
701 // lookahead sets are the same in both the first and second contexts.
702 // Thus, canonical LR reuses the same state for both contexts. However,
703 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
704 // error action only in the first context. In order to immediately
705 // detect the syntax error on 'a' here for only the first context, this
706 // canonical LR state would have to be split into two states, and the
707 // 'a' lookahead would have to be removed from only one of the states.
709 'a' // Reduction lookahead set is always ['a', 'b'].
711 | 'a' 'c' // 'c' is forgotten as an expected token.
714 reduce-nonassoc: %prec 'a';
718 ]AT_YYLEX_DEFINE(["aaa"])[
721 AT_BISON_OPTION_POPDEFS
723 # Show canonical LR's failure.
724 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
726 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
728 AT_COMPILE([[input]])
729 AT_PARSER_CHECK([[./input]], [[1]], [[]],
730 [[syntax error, unexpected 'a', expecting 'b'
733 # It's corrected by LAC.
734 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
735 -o input.c input.y]], [[0]], [[]],
736 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
738 AT_COMPILE([[input]])
739 AT_PARSER_CHECK([[./input]], [[1]], [[]],
740 [[syntax error, unexpected 'a', expecting 'b' or 'c'
743 # IELR is sufficient when LAC is used.
744 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
746 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
748 AT_COMPILE([[input]])
749 AT_PARSER_CHECK([[./input]], [[1]], [[]],
750 [[syntax error, unexpected 'a', expecting 'b' or 'c'
755 ## ------------------------- ##
756 ## Unresolved SR Conflicts. ##
757 ## ------------------------- ##
759 AT_SETUP([Unresolved SR Conflicts])
761 AT_KEYWORDS([report])
766 exp: exp OP exp | NUM;
769 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
770 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
773 # Check the contents of the report.
774 AT_CHECK([cat input.output], [],
775 [[State 5 conflicts: 1 shift/reduce
786 Terminals, with rules where they appear
794 Nonterminals, with rules where they appear
799 on left: 1 2, on right: 0 1
804 0 $accept: . exp $end
808 NUM shift, and go to state 1
817 $default reduce using rule 2 (exp)
822 0 $accept: exp . $end
825 $end shift, and go to state 3
826 OP shift, and go to state 4
831 0 $accept: exp $end .
842 NUM shift, and go to state 1
850 1 | exp OP exp . [$end, OP]
852 OP shift, and go to state 4
854 OP [reduce using rule 1 (exp)]
855 $default reduce using rule 1 (exp)
862 ## ----------------------- ##
863 ## Resolved SR Conflicts. ##
864 ## ----------------------- ##
866 AT_SETUP([Resolved SR Conflicts])
868 AT_KEYWORDS([report])
874 exp: exp OP exp | NUM;
877 AT_BISON_CHECK([-o input.c --report=all input.y])
879 # Check the contents of the report.
880 AT_CHECK([cat input.output], [],
889 Terminals, with rules where they appear
897 Nonterminals, with rules where they appear
902 on left: 1 2, on right: 0 1
907 0 $accept: . exp $end
911 NUM shift, and go to state 1
920 $default reduce using rule 2 (exp)
925 0 $accept: exp . $end
928 $end shift, and go to state 3
929 OP shift, and go to state 4
934 0 $accept: exp $end .
945 NUM shift, and go to state 1
953 1 | exp OP exp . [$end, OP]
955 $default reduce using rule 1 (exp)
957 Conflict between rule 1 and token OP resolved as reduce (%left OP).
963 ## ---------------------- ##
964 ## %precedence suffices. ##
965 ## ---------------------- ##
967 AT_SETUP([%precedence suffices])
974 "if" cond "then" stmt
975 | "if" cond "then" stmt "else" stmt
984 AT_BISON_CHECK([-o input.c input.y])
989 ## ------------------------------ ##
990 ## %precedence does not suffice. ##
991 ## ------------------------------ ##
993 AT_SETUP([%precedence does not suffice])
1000 "if" cond "then" stmt
1001 | "if" cond "then" stmt "else" stmt
1011 AT_BISON_CHECK([-o input.c input.y], 0, [],
1012 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1013 input.y:12.3-18: warning: rule useless in parser due to conflicts [-Wother]
1019 ## -------------------------------- ##
1020 ## Defaulted Conflicted Reduction. ##
1021 ## -------------------------------- ##
1023 # When there are RR conflicts, some rules are disabled. Usually it is
1024 # simply displayed as:
1026 # $end reduce using rule 3 (num)
1027 # $end [reduce using rule 4 (id)]
1029 # But when 'reduce 3' is the default action, we'd produce:
1031 # $end [reduce using rule 4 (id)]
1032 # $default reduce using rule 3 (num)
1034 # In this precise case (a reduction is masked by the default
1035 # reduction), we make the 'reduce 3' explicit:
1037 # $end reduce using rule 3 (num)
1038 # $end [reduce using rule 4 (id)]
1039 # $default reduce using rule 3 (num)
1041 # Maybe that's not the best display, but then, please propose something
1044 AT_SETUP([Defaulted Conflicted Reduction])
1045 AT_KEYWORDS([report])
1055 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
1056 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1057 input.y:4.6-8: warning: rule useless in parser due to conflicts [-Wother]
1060 # Check the contents of the report.
1061 AT_CHECK([cat input.output], [],
1062 [[Rules useless in parser due to conflicts
1067 State 1 conflicts: 1 reduce/reduce
1082 Terminals, with rules where they appear
1089 Nonterminals, with rules where they appear
1094 on left: 1 2, on right: 0
1096 on left: 3, on right: 1
1098 on left: 4, on right: 2
1103 0 $accept: . exp $end
1109 '0' shift, and go to state 1
1121 $end reduce using rule 3 (num)
1122 $end [reduce using rule 4 (id)]
1123 $default reduce using rule 3 (num)
1128 0 $accept: exp . $end
1130 $end shift, and go to state 5
1137 $default reduce using rule 1 (exp)
1144 $default reduce using rule 2 (exp)
1149 0 $accept: exp $end .
1159 ## -------------------- ##
1160 ## %expect not enough. ##
1161 ## -------------------- ##
1163 AT_SETUP([%expect not enough])
1169 exp: exp OP exp | NUM;
1172 AT_BISON_CHECK([-o input.c input.y], 1, [],
1173 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1178 ## --------------- ##
1179 ## %expect right. ##
1180 ## --------------- ##
1182 AT_SETUP([%expect right])
1188 exp: exp OP exp | NUM;
1191 AT_BISON_CHECK([-o input.c input.y])
1195 ## ------------------ ##
1196 ## %expect too much. ##
1197 ## ------------------ ##
1199 AT_SETUP([%expect too much])
1205 exp: exp OP exp | NUM;
1208 AT_BISON_CHECK([-o input.c input.y], 1, [],
1209 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1214 ## ------------------------------- ##
1215 ## %expect with reduce conflicts. ##
1216 ## ------------------------------- ##
1218 AT_SETUP([%expect with reduce conflicts])
1223 program: a 'a' | a a;
1227 AT_BISON_CHECK([-o input.c input.y], 1, [],
1228 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1233 ## ------------------------- ##
1234 ## %prec with user strings. ##
1235 ## ------------------------- ##
1237 AT_SETUP([%prec with user string])
1239 AT_DATA([[input.y]],
1246 AT_BISON_CHECK([-o input.c input.y])
1250 ## -------------------------------- ##
1251 ## %no-default-prec without %prec. ##
1252 ## -------------------------------- ##
1254 AT_SETUP([%no-default-prec without %prec])
1256 AT_DATA([[input.y]],
1270 AT_BISON_CHECK([-Wall -o input.c input.y], 0, [],
1271 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1272 input.y:1.1-5: warning: useless precedence and associativity for '+' [-Wprecedence]
1273 input.y:2.1-5: warning: useless precedence and associativity for '*' [-Wprecedence]
1278 ## ----------------------------- ##
1279 ## %no-default-prec with %prec. ##
1280 ## ----------------------------- ##
1282 AT_SETUP([%no-default-prec with %prec])
1284 AT_DATA([[input.y]],
1292 e: e '+' e %prec '+'
1298 AT_BISON_CHECK([-o input.c input.y])
1302 ## --------------- ##
1303 ## %default-prec. ##
1304 ## --------------- ##
1306 AT_SETUP([%default-prec])
1308 AT_DATA([[input.y]],
1322 AT_BISON_CHECK([-o input.c input.y])
1326 ## ---------------------------------------------- ##
1327 ## Unreachable States After Conflict Resolution. ##
1328 ## ---------------------------------------------- ##
1330 AT_SETUP([[Unreachable States After Conflict Resolution]])
1332 # If conflict resolution makes states unreachable, remove those states, report
1333 # rules that are then unused, and don't report conflicts in those states. Test
1334 # what happens when a nonterminal becomes useless as a result of state removal
1335 # since that causes lalr.o's goto map to be rewritten.
1337 AT_DATA([[input.y]],
1343 start: resolved_conflict 'a' reported_conflicts 'a' ;
1345 /* S/R conflict resolved as reduce, so the state with item
1346 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1347 * unreachable, and the associated production is useless. */
1353 /* S/R conflict that need not be reported since it is unreachable because of
1354 * the previous conflict resolution. Nonterminal unreachable1 and all its
1355 * productions are useless. */
1361 /* Likewise for a R/R conflict and nonterminal unreachable2. */
1364 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1365 * when their states are renumbered due to state removal. */
1374 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1375 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1376 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1377 input.y:12.5-20: warning: rule useless in parser due to conflicts [-Wother]
1378 input.y:20.5-20: warning: rule useless in parser due to conflicts [-Wother]
1379 input.y:21.4: warning: rule useless in parser due to conflicts [-Wother]
1380 input.y:25.13: warning: rule useless in parser due to conflicts [-Wother]
1381 input.y:25.16: warning: rule useless in parser due to conflicts [-Wother]
1382 input.y:31.5-7: warning: rule useless in parser due to conflicts [-Wother]
1383 input.y:32.4: warning: rule useless in parser due to conflicts [-Wother]
1386 AT_CHECK([[cat input.output]], 0,
1387 [[Rules useless in parser due to conflicts
1389 2 resolved_conflict: 'a' unreachable1
1391 4 unreachable1: 'a' unreachable2
1394 6 unreachable2: %empty
1397 9 reported_conflicts: 'a'
1401 State 4 conflicts: 1 shift/reduce
1402 State 5 conflicts: 1 reduce/reduce
1407 0 $accept: start $end
1409 1 start: resolved_conflict 'a' reported_conflicts 'a'
1411 2 resolved_conflict: 'a' unreachable1
1414 4 unreachable1: 'a' unreachable2
1417 6 unreachable2: %empty
1420 8 reported_conflicts: 'a'
1425 Terminals, with rules where they appear
1432 Nonterminals, with rules where they appear
1437 on left: 1, on right: 0
1438 resolved_conflict (6)
1439 on left: 2 3, on right: 1
1441 on left: 4 5, on right: 2
1443 on left: 6 7, on right: 4
1444 reported_conflicts (9)
1445 on left: 8 9 10, on right: 1
1450 0 $accept: . start $end
1451 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1452 2 resolved_conflict: . 'a' unreachable1
1455 $default reduce using rule 3 (resolved_conflict)
1458 resolved_conflict go to state 2
1460 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1465 0 $accept: start . $end
1467 $end shift, and go to state 3
1472 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1474 'a' shift, and go to state 4
1479 0 $accept: start $end .
1486 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1487 8 reported_conflicts: . 'a'
1491 'a' shift, and go to state 5
1493 'a' [reduce using rule 10 (reported_conflicts)]
1495 reported_conflicts go to state 6
1500 8 reported_conflicts: 'a' . ['a']
1503 'a' reduce using rule 8 (reported_conflicts)
1504 'a' [reduce using rule 9 (reported_conflicts)]
1505 $default reduce using rule 8 (reported_conflicts)
1510 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1512 'a' shift, and go to state 7
1517 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1519 $default reduce using rule 1 (start)
1522 AT_DATA([[input-keep.y]],
1523 [[%define lr.keep-unreachable-state
1525 AT_CHECK([[cat input.y >> input-keep.y]])
1527 AT_BISON_CHECK([[input-keep.y]], 0, [],
1528 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1529 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1530 input-keep.y:22.4: warning: rule useless in parser due to conflicts [-Wother]
1531 input-keep.y:26.16: warning: rule useless in parser due to conflicts [-Wother]
1532 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts [-Wother]
1533 input-keep.y:33.4: warning: rule useless in parser due to conflicts [-Wother]
1539 ## ------------------------------------------------------------ ##
1540 ## Solved conflicts report for multiple reductions in a state. ##
1541 ## ------------------------------------------------------------ ##
1543 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1545 # Used to lose earlier solved conflict messages even within a single S/R/R.
1547 AT_DATA([[input.y]],
1563 empty_a: %prec 'a' ;
1564 empty_b: %prec 'b' ;
1565 empty_c1: %prec 'c' ;
1566 empty_c2: %prec 'c' ;
1567 empty_c3: %prec 'd' ;
1569 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1570 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1573 0 $accept: . start $end
1582 9 empty_a: . %empty ['a']
1583 10 empty_b: . %empty []
1584 11 empty_c1: . %empty []
1585 12 empty_c2: . %empty []
1586 13 empty_c3: . %empty ['c']
1588 'b' shift, and go to state 1
1590 'c' reduce using rule 13 (empty_c3)
1591 $default reduce using rule 9 (empty_a)
1594 empty_a go to state 3
1595 empty_b go to state 4
1596 empty_c1 go to state 5
1597 empty_c2 go to state 6
1598 empty_c3 go to state 7
1600 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1601 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1602 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1603 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1604 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1613 ## ------------------------------------------------------------ ##
1614 ## %nonassoc error actions for multiple reductions in a state. ##
1615 ## ------------------------------------------------------------ ##
1617 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1618 # multiple reductions in a state.
1620 # For a %nonassoc error action token, used to print the first remaining
1621 # reduction on that token without brackets.
1623 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1625 AT_DATA([[input.y]],
1626 [[%nonassoc 'a' 'b' 'c'
1638 empty_a: %prec 'a' ;
1639 empty_b: %prec 'b' ;
1640 empty_c1: %prec 'c' ;
1641 empty_c2: %prec 'c' ;
1642 empty_c3: %prec 'c' ;
1645 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1646 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1649 0 $accept: . start $end
1658 9 empty_a: . %empty []
1659 10 empty_b: . %empty []
1660 11 empty_c1: . %empty []
1661 12 empty_c2: . %empty ['c']
1662 13 empty_c3: . %empty ['c']
1664 'a' error (nonassociative)
1665 'b' error (nonassociative)
1666 'c' error (nonassociative)
1668 'c' [reduce using rule 12 (empty_c2)]
1669 'c' [reduce using rule 13 (empty_c3)]
1672 empty_a go to state 2
1673 empty_b go to state 3
1674 empty_c1 go to state 4
1675 empty_c2 go to state 5
1676 empty_c3 go to state 6
1678 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1679 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1680 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1688 ## -------------------- ##
1689 ## %expect-rr non GLR. ##
1690 ## -------------------- ##
1692 AT_SETUP([[%expect-rr non GLR]])
1700 AT_BISON_CHECK([[1.y]], [[0]], [],
1701 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1710 AT_BISON_CHECK([[2.y]], [[0]], [],
1711 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1712 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1713 2.y:3.12-14: warning: rule useless in parser due to conflicts [-Wother]
1719 ## ---------------------------------- ##
1720 ## -W versus %expect and %expect-rr. ##
1721 ## ---------------------------------- ##
1723 AT_SETUP([[-W versus %expect and %expect-rr]])
1725 AT_DATA([[sr-rr.y]],
1728 start: 'a' | A 'a' | B 'a' ;
1735 start: 'a' | A 'a' ;
1746 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
1747 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1748 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1750 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
1751 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1753 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
1754 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1758 # This is piece of code is rather complex for a simple task: try every
1759 # combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1760 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1763 # Number and types of genuine conflicts in the grammar.
1764 for gram in sr-rr sr rr; do
1765 # Number of expected s/r conflicts.
1766 for sr_exp_i in '' 0 1 2; do
1767 # Number of expected r/r conflicts.
1768 for rr_exp_i in '' 0 1 2; do
1769 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1771 # Build grammar file.
1776 if test -n "$sr_exp_i"; then
1778 file=$file-expect-$sr_exp
1779 directives="%expect $sr_exp"
1781 if test -n "$rr_exp_i"; then
1783 file=$file-expect-rr-$rr_exp
1784 directives="$directives %expect-rr $rr_exp"
1787 echo "$directives" > $file
1788 cat $gram.y >> $file
1790 # Number of found conflicts.
1792 (sr) sr_count=1; rr_count=0;;
1793 (rr) sr_count=0; rr_count=1;;
1794 (sr-rr) sr_count=1; rr_count=1;;
1797 # Update number of expected conflicts: if %expect is given then
1798 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1800 case $sr_exp_i:$rr_exp_i in
1806 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1807 ]AT_BISON_CHECK([[-Wnone $file]])[
1808 ]AT_BISON_CHECK([[-Werror $file]])[
1811 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1812 echo "warning: $sr_count shift/reduce conflicts"
1813 elif test "$sr_exp_i" -ne "$sr_count"; then
1814 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
1816 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1817 echo "warning: $rr_count reduce/reduce conflicts"
1818 elif test "$rr_exp_i" -ne "$rr_count"; then
1819 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
1821 } | sed -e "s/^/$file: /" > experr
1822 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1823 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[