style: rename stmtMerge as stmt_merge
[bison.git] / tests / conflicts.at
blob4e5b395859d2eaefb15057d71756999f31048a9a
1 # Exercising Bison on conflicts.                         -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2015, 2018-2021 Free Software
4 # Foundation, Inc.
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 <https://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
26 # statement.
28 AT_SETUP([Token declaration order])
30 AT_BISON_OPTION_PUSHDEFS
32 AT_DATA_GRAMMAR([[input.y]],
33 [[%code {
34   #include <stdio.h>
35   ]AT_YYERROR_DECLARE[
36   ]AT_YYLEX_DECLARE[
38 %token A B C
39 %token D
40 %right E F G
41 %right H I
42 %right J
43 %left  K
44 %left  L M N
45 %nonassoc O P Q
46 %precedence R S T U
47 %precedence V W
49 exp: A
51 ]AT_YYERROR_DEFINE[
52 ]AT_YYLEX_DEFINE[
53 int main (void)
55   assert (A < B);
56   assert (B < C);
57   assert (C < D);
58   assert (D < E);
59   assert (E < F);
60   assert (F < G);
61   assert (G < H);
62   assert (H < I);
63   assert (I < J);
64   assert (J < K);
65   assert (K < L);
66   assert (L < M);
67   assert (M < N);
68   assert (N < O);
69   assert (O < P);
70   assert (P < Q);
71   assert (Q < R);
72   assert (R < S);
73   assert (S < T);
74   assert (T < U);
75   assert (U < V);
76   assert (V < W);
77   return 0;
79 ]])
81 AT_BISON_CHECK([-o input.c input.y])
82 AT_COMPILE([input])
84 AT_PARSER_CHECK([input])
86 AT_BISON_OPTION_POPDEFS
88 AT_CLEANUP
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]],
105 [[%token 'a' 'b' C D
106 %token E F 'g' 'h'
107 %right 'i' 'j' K L
108 %right M N 'o' 'p'
110 exp: 'a'
111    | 'b'
112    | C
113    | D
114    | E
115    | F
116    | 'g'
117    | 'h'
118    | 'i'
119    | 'j'
120    | K
121    | L
122    | M
123    | N
124    | 'o'
125    | 'p'
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,
132 [[State 0
134     0 $accept: . exp $end
135     1 exp: . 'a'
136     2    | . 'b'
137     3    | . C
138     4    | . D
139     5    | . E
140     6    | . F
141     7    | . 'g'
142     8    | . 'h'
143     9    | . 'i'
144    10    | . 'j'
145    11    | . K
146    12    | . L
147    13    | . M
148    14    | . N
149    15    | . 'o'
150    16    | . 'p'
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
169     exp  go to state 17
172 State 1
175 AT_BISON_OPTION_POPDEFS
176 AT_CLEANUP
179 ## ------------------------------- ##
180 ## Useless associativity warning.  ##
181 ## ------------------------------- ##
183 AT_SETUP([Useless associativity warning])
185 AT_DATA([[input.y]],
186 [[%token EQ "=" PL "+" ST "*"  LP "("
187 %nonassoc "="
188 %left "+"
189 %left "*"
190 %precedence "("
192 stmt:
193   exp
194 | "var" "=" exp
197 exp:
198   exp "+" exp
199 | exp "*" "num"
200 | "(" exp ")"
201 | "num"
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]
211 AT_CLEANUP
214 ## ---------------------------- ##
215 ## Useless precedence warning.  ##
216 ## ---------------------------- ##
218 AT_SETUP([Useless precedence warning])
220 AT_DATA([[input.y]],
221 [[%token A B U V W X Y Z
222 %precedence Z
223 %left X
224 %precedence Y
225 %left W
226 %right V
227 %nonassoc U
229 a: b
230  | a U b
231  | f
233 b: c
234  | b V c
236 c: d
237  | c W d
239 d: A
240  | d X d
241  | d Y A
243 f: B
244  | f Z B
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]
250     7 | %nonassoc U
251       | ^~~~~~~~~
252 input.y:6.1-6: warning: useless precedence and associativity for V [-Wprecedence]
253     6 | %right V
254       | ^~~~~~
255 input.y:5.1-5: warning: useless precedence and associativity for W [-Wprecedence]
256     5 | %left W
257       | ^~~~~
258 input.y:2.1-11: warning: useless precedence for Z [-Wprecedence]
259     2 | %precedence Z
260       | ^~~~~~~~~~~
263 AT_CLEANUP
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])
277 AT_DATA([[input.y]],
278 [[%expect 1
280 exp: e 'e';
281 e: 'e' | %empty;
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]
290     4 | e: 'e' | %empty;
291       |          ^~~~~~
294 AT_CLEANUP
297 ## ------------------- ##
298 ## %nonassoc and eof.  ##
299 ## ------------------- ##
301 AT_SETUP([%nonassoc and eof])
303 AT_BISON_OPTION_PUSHDEFS
304 AT_DATA_GRAMMAR([input.y],
307 #include <stdio.h>
308 #include <stdlib.h>
309 #include <string.h>
310 #include <assert.h>
312 ]AT_YYERROR_DEFINE[
313 /* The current argument. */
314 static const char *input;
316 static int
317 yylex (void)
319   static size_t toknum;
320   assert (toknum <= strlen (input));
321   return input[toknum++];
326 %define parse.error verbose
328 %nonassoc '<' '>'
331 expr: expr '<' expr
332     | expr '>' expr
333     | '0'
334     ;
337 main (int argc, const char *argv[])
339   input = argc <= 1 ? "" : argv[1];
340   return yyparse ();
343 AT_BISON_OPTION_POPDEFS
345 m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
346 [AT_BISON_CHECK([$1[ -o input.c input.y]])
347 AT_COMPILE([input])
349 m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting end of file]])])
351 AT_PARSER_CHECK([input '0<0'])
352 AT_PARSER_CHECK([input '0<0<0'], [1], [],
353          [syntax error, unexpected '<'AT_EXPECTING
356 AT_PARSER_CHECK([input '0>0'])
357 AT_PARSER_CHECK([input '0>0>0'], [1], [],
358          [syntax error, unexpected '>'AT_EXPECTING
361 AT_PARSER_CHECK([input '0<0>0'], [1], [],
362          [syntax error, unexpected '>'AT_EXPECTING
365 m4_popdef([AT_EXPECTING])])
367 # Expected token list is missing.
368 AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
370 # We must disable default reductions in inconsistent states in order to
371 # have an explicit list of all expected tokens.
372 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
373                           [[correct]])
375 # lr.default-reduction=consistent happens to work for this test case.
376 # However, for other grammars, lookahead sets can be merged for
377 # different left contexts, so it is still possible to have an incorrect
378 # expected list.  Canonical LR is almost a general solution (that is, it
379 # can fail only when %nonassoc is used), so make sure it gives the same
380 # result as above.
381 AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
383 # parse.lac=full is a completely general solution that does not require
384 # any of the above sacrifices.  Of course, it does not extend the
385 # language-recognition power of LALR to (IE)LR, but it does ensure that
386 # the reported list of expected tokens matches what the given parser
387 # would have accepted in place of the unexpected token.
388 AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
390 m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
392 AT_CLEANUP
396 ## ------------------------------------------- ##
397 ## parse.error=verbose and consistent errors.  ##
398 ## ------------------------------------------- ##
400 AT_CONSISTENT_ERRORS_CHECK([BISON-DIRECTIVE],
401                            [GRAMMAR],
402                            [INPUT],
403                            [UNEXPECTED-TOKEN], [EXPECTED-TOKEN])
404 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
406 AT_SETUP([[parse.error=verbose and consistent errors: ]$1])
408 AT_BISON_OPTION_PUSHDEFS([$1 AT_CXX_IF([[%header]], [[%define api.pure]])])
410 AT_DATA_GRAMMAR([input.y],
411 [AT_JAVA_IF([[
413 %code imports {
414   import java.io.IOException;
415 }]], [[
417 %code {
418   #include <assert.h>
419   ]AT_YYERROR_DECLARE[
420   ]AT_YYLEX_DECLARE[
421   #define USE(Var)
424 ]AT_CXX_IF([[%header]], [[%define api.pure]])])[
426 ]AT_YACC_C_IF([[
427 %code {
428   #if defined __GNUC__ && 8 <= __GNUC__
429   # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
430   #endif
432 ]])[
434 ]$1[
436 %define parse.error verbose
440 ]$2[
442 ]AT_JAVA_IF([[%code lexer {
443   ]AT_YYLEX_DEFINE(["$3"], [[return new Integer(1)]])[
444   ]AT_YYERROR_DEFINE[
447 ]], [[
449 ]AT_YYLEX_DEFINE(["$3"], [[*lvalp = 1]])[
450 ]AT_YYERROR_DEFINE[
451 ]])[
453 /*-------.
454 | main.  |
455 `-------*/
456 ]AT_MAIN_DEFINE
459 AT_FULL_COMPILE([[input]])
461 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
462                                   $5, [a],  [[, expecting 'a']],
463                                   $5, [b],  [[, expecting 'b']])])
465 AT_PARSER_CHECK([[input]], [[1]],
466 [[]],
467 [[syntax error, unexpected ]$4[]AT_EXPECTING[
470 m4_popdef([AT_EXPECTING])
471 AT_BISON_OPTION_POPDEFS
473 AT_CLEANUP
474 ]) dnl AT_CONSISTENT_ERRORS_CHECK
479 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
480 [[%nonassoc 'a';
482 start: consistent-error-on-a-a 'a' ;
484 consistent-error-on-a-a:
485     'a' default-reduction
486   | 'a' default-reduction 'a'
487   | 'a' shift
488   ;
490 default-reduction: %empty ;
491 shift: 'b' ;
493 // Provide another context in which all rules are useful so that this
494 // test case looks a little more realistic.
495 start: 'b' consistent-error-on-a-a 'c' ;
498 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
500 # Unfortunately, no expected tokens are reported even though 'b' can be
501 # accepted.  Nevertheless, the main point of this test is to make sure
502 # that at least the unexpected token is reported.  In a previous version
503 # of Bison, it wasn't reported because the error is detected in a
504 # consistent state with an error action, and that case always triggered
505 # the simple "syntax error" message.
507 # The point isn't to test IELR here, but state merging happens to
508 # complicate this example.
509 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
510                            [AT_PREVIOUS_STATE_GRAMMAR],
511                            [AT_PREVIOUS_STATE_INPUT],
512                            [[end of file]], [[none]])
513 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
514                              %glr-parser]],
515                            [AT_PREVIOUS_STATE_GRAMMAR],
516                            [AT_PREVIOUS_STATE_INPUT],
517                            [[end of file]], [[none]])
518 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
519                              %language "c++"]],
520                            [AT_PREVIOUS_STATE_GRAMMAR],
521                            [AT_PREVIOUS_STATE_INPUT],
522                            [[end of file]], [[none]])
523 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
524                              %language "java"]],
525                            [AT_PREVIOUS_STATE_GRAMMAR],
526                            [AT_PREVIOUS_STATE_INPUT],
527                            [[end of file]], [[none]])
529 # Even canonical LR doesn't foresee the error for 'a'!
530 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
531                              %define lr.default-reduction consistent]],
532                            [AT_PREVIOUS_STATE_GRAMMAR],
533                            [AT_PREVIOUS_STATE_INPUT],
534                            [[end of file]], [[ab]])
535 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
536                              %define lr.default-reduction accepting]],
537                            [AT_PREVIOUS_STATE_GRAMMAR],
538                            [AT_PREVIOUS_STATE_INPUT],
539                            [[end of file]], [[ab]])
540 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
541                            [AT_PREVIOUS_STATE_GRAMMAR],
542                            [AT_PREVIOUS_STATE_INPUT],
543                            [[end of file]], [[ab]])
545 # Only LAC gets it right.  In C.
546 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
547                              %define parse.lac full]],
548                            [AT_PREVIOUS_STATE_GRAMMAR],
549                            [AT_PREVIOUS_STATE_INPUT],
550                            [[end of file]], [[b]])
551 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
552                              %define parse.lac full]],
553                            [AT_PREVIOUS_STATE_GRAMMAR],
554                            [AT_PREVIOUS_STATE_INPUT],
555                            [[end of file]], [[b]])
557 # Only LAC gets it right.  In C++.
558 AT_CONSISTENT_ERRORS_CHECK([[%language "c++"
559                              %define lr.type canonical-lr
560                              %define parse.lac full]],
561                            [AT_PREVIOUS_STATE_GRAMMAR],
562                            [AT_PREVIOUS_STATE_INPUT],
563                            [[end of file]], [[b]])
564 AT_CONSISTENT_ERRORS_CHECK([[%language "c++"
565                              %define lr.type ielr
566                              %define parse.lac full]],
567                            [AT_PREVIOUS_STATE_GRAMMAR],
568                            [AT_PREVIOUS_STATE_INPUT],
569                            [[end of file]], [[b]])
571 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
572 m4_popdef([AT_PREVIOUS_STATE_INPUT])
574 m4_pushdef([AT_USER_ACTION_GRAMMAR],
575 [[%nonassoc 'a';
577 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
578 // incorrectly for the 'b' set in the semantic action below.  All 'a'
579 // tokens are returned by yylex, which sets $$ = 1.
580 %destructor {
581   if (!$$)
582     fprintf (stderr, "Wrong destructor.\n");
583 } 'a';
585 // Rather than depend on an inconsistent state to induce reading a
586 // lookahead as in the previous grammar, just assign the lookahead in a
587 // semantic action.  That lookahead isn't needed before either error
588 // action is encountered.  In a previous version of Bison, this was a
589 // problem as it meant yychar was not translated into yytoken before
590 // either error action.  The second error action thus invoked a
591 // destructor that it selected according to the incorrect yytoken.  The
592 // first error action would have reported an incorrect unexpected token
593 // except that, due to the bug described in the previous grammar, the
594 // unexpected token was not reported at all.
595 start: error-reduce consistent-error 'a' { USE ($][3); } ;
597 error-reduce:
598   'a' 'a' consistent-reduction consistent-error 'a'
599   { USE (($][1, $][2, $][5)); }
600 | 'a' error
601   { USE ($][1); }
604 consistent-reduction: %empty
606   assert (yychar == YYEMPTY);
607   yylval = 0;
608   yychar = 'b';
609 } ;
611 consistent-error:
612   'a' { USE ($][1); }
613 | %empty %prec 'a'
616 // Provide another context in which all rules are useful so that this
617 // test case looks a little more realistic.
618 start: 'b' consistent-error 'b' ;
620 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
622 AT_CONSISTENT_ERRORS_CHECK([[]],
623                            [AT_USER_ACTION_GRAMMAR],
624                            [AT_USER_ACTION_INPUT],
625                            [['b']], [[none]])
626 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
627                            [AT_USER_ACTION_GRAMMAR],
628                            [AT_USER_ACTION_INPUT],
629                            [['b']], [[none]])
630 # No C++ or Java test because yychar cannot be manipulated by users.
632 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
633                            [AT_USER_ACTION_GRAMMAR],
634                            [AT_USER_ACTION_INPUT],
635                            [['b']], [[none]])
637 # Canonical LR doesn't foresee the error for 'a'!
638 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
639                            [AT_USER_ACTION_GRAMMAR],
640                            [AT_USER_ACTION_INPUT],
641                            [[end of file]], [[a]])
642 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
643                            [AT_USER_ACTION_GRAMMAR],
644                            [AT_USER_ACTION_INPUT],
645                            [[end of file]], [[a]])
647 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
648                            [AT_USER_ACTION_GRAMMAR],
649                            [AT_USER_ACTION_INPUT],
650                            [['b']], [[none]])
651 AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
652                              %define lr.default-reduction accepting]],
653                            [AT_USER_ACTION_GRAMMAR],
654                            [AT_USER_ACTION_INPUT],
655                            [[end of file]], [[none]])
657 m4_popdef([AT_USER_ACTION_GRAMMAR])
658 m4_popdef([AT_USER_ACTION_INPUT])
660 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
665 ## ------------------------------------------------------- ##
666 ## LAC: %nonassoc requires splitting canonical LR states.  ##
667 ## ------------------------------------------------------- ##
669 # This test case demonstrates that, when %nonassoc is used, canonical
670 # LR(1) parser table construction followed by conflict resolution
671 # without further state splitting is not always sufficient to produce a
672 # parser that can detect all syntax errors as soon as possible on one
673 # token of lookahead.  However, LAC solves the problem completely even
674 # with minimal LR parser tables.
676 AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
677 AT_BISON_OPTION_PUSHDEFS
678 AT_DATA_GRAMMAR([[input.y]],
679 [[%code {
680   #include <stdio.h>
681   ]AT_YYERROR_DECLARE[
682   ]AT_YYLEX_DECLARE[
685 %define parse.error verbose
686 %nonassoc 'a'
690 start:
691   'a' problem 'a' // First context.
692 | 'b' problem 'b' // Second context.
693 | 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
696 problem:
697   look reduce-nonassoc
698 | look 'a'
699 | look 'b'
702 // For the state reached after shifting the 'a' in these productions,
703 // lookahead sets are the same in both the first and second contexts.
704 // Thus, canonical LR reuses the same state for both contexts.  However,
705 // the lookahead 'a' for the reduction "look: 'a'" later becomes an
706 // error action only in the first context.  In order to immediately
707 // detect the syntax error on 'a' here for only the first context, this
708 // canonical LR state would have to be split into two states, and the
709 // 'a' lookahead would have to be removed from only one of the states.
710 look:
711   'a' // Reduction lookahead set is always ['a', 'b'].
712 | 'a' 'b'
713 | 'a' 'c' // 'c' is forgotten as an expected token.
716 reduce-nonassoc: %prec 'a';
719 ]AT_YYERROR_DEFINE[
720 ]AT_YYLEX_DEFINE(["aaa"])[
721 ]AT_MAIN_DEFINE
723 AT_BISON_OPTION_POPDEFS
725 # Show canonical LR's failure.
726 AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
727                [[0]], [[]],
728 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
729 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
731 AT_COMPILE([[input]])
732 AT_PARSER_CHECK([[input]], [[1]], [[]],
733 [[syntax error, unexpected 'a', expecting 'b'
736 # It's corrected by LAC.
737 AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
738                  -o input.c input.y]], [[0]], [[]],
739 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
740 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
742 AT_COMPILE([[input]])
743 AT_PARSER_CHECK([[input]], [[1]], [[]],
744 [[syntax error, unexpected 'a', expecting 'b' or 'c'
747 # IELR is sufficient when LAC is used.
748 AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
749                [[0]], [[]],
750 [[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
751 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
753 AT_COMPILE([[input]])
754 AT_PARSER_CHECK([[input]], [[1]], [[]],
755 [[syntax error, unexpected 'a', expecting 'b' or 'c'
758 AT_CLEANUP
760 ## ------------------------- ##
761 ## Unresolved SR Conflicts.  ##
762 ## ------------------------- ##
764 AT_SETUP([Unresolved SR Conflicts])
766 AT_KEYWORDS([cex report])
768 AT_DATA([input.y],
769 [[%token NUM OP
771 exp: exp OP exp | NUM;
774 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
775 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
776 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
779 # Check the contents of the report.
780 AT_CHECK([cat input.output], [],
781 [[State 5 conflicts: 1 shift/reduce
784 Grammar
786     0 $accept: exp $end
788     1 exp: exp OP exp
789     2    | NUM
792 Terminals, with rules where they appear
794     $end (0) 0
795     error (256)
796     NUM (258) 2
797     OP (259) 1
800 Nonterminals, with rules where they appear
802     $accept (5)
803         on left: 0
804     exp (6)
805         on left: 1 2
806         on right: 0 1
809 State 0
811     0 $accept: . exp $end
812     1 exp: . exp OP exp
813     2    | . NUM
815     NUM  shift, and go to state 1
817     exp  go to state 2
820 State 1
822     2 exp: NUM .
824     $default  reduce using rule 2 (exp)
827 State 2
829     0 $accept: exp . $end
830     1 exp: exp . OP exp
832     $end  shift, and go to state 3
833     OP    shift, and go to state 4
836 State 3
838     0 $accept: exp $end .
840     $default  accept
843 State 4
845     1 exp: . exp OP exp
846     1    | exp OP . exp
847     2    | . NUM
849     NUM  shift, and go to state 1
851     exp  go to state 5
854 State 5
856     1 exp: exp . OP exp
857     1    | exp OP exp .  [$end, OP]
859     OP  shift, and go to state 4
861     OP        [reduce using rule 1 (exp)]
862     $default  reduce using rule 1 (exp)
864     shift/reduce conflict on token OP:
865         1 exp: exp OP exp .
866         1 exp: exp . OP exp
867       Example: exp OP exp . OP exp
868       Shift derivation
869         exp
870         `-> 1: exp OP exp
871                       `-> 1: exp . OP exp
872       Reduce derivation
873         exp
874         `-> 1: exp                 OP exp
875                `-> 1: exp OP exp .
879 AT_CLEANUP
883 ## ----------------------- ##
884 ## Resolved SR Conflicts.  ##
885 ## ----------------------- ##
887 AT_SETUP([Resolved SR Conflicts])
889 AT_KEYWORDS([report])
891 AT_DATA([input.y],
892 [[%token NUM OP
893 %left OP
895 exp: exp OP exp | NUM;
898 AT_BISON_CHECK([-o input.c --report=all input.y])
900 # Check the contents of the report.
901 AT_CHECK([cat input.output], [],
902 [[Grammar
904     0 $accept: exp $end
906     1 exp: exp OP exp
907     2    | NUM
910 Terminals, with rules where they appear
912     $end (0) 0
913     error (256)
914     NUM (258) 2
915     OP (259) 1
918 Nonterminals, with rules where they appear
920     $accept (5)
921         on left: 0
922     exp (6)
923         on left: 1 2
924         on right: 0 1
927 State 0
929     0 $accept: . exp $end
930     1 exp: . exp OP exp
931     2    | . NUM
933     NUM  shift, and go to state 1
935     exp  go to state 2
938 State 1
940     2 exp: NUM .
942     $default  reduce using rule 2 (exp)
945 State 2
947     0 $accept: exp . $end
948     1 exp: exp . OP exp
950     $end  shift, and go to state 3
951     OP    shift, and go to state 4
954 State 3
956     0 $accept: exp $end .
958     $default  accept
961 State 4
963     1 exp: . exp OP exp
964     1    | exp OP . exp
965     2    | . NUM
967     NUM  shift, and go to state 1
969     exp  go to state 5
972 State 5
974     1 exp: exp . OP exp
975     1    | exp OP exp .  [$end, OP]
977     $default  reduce using rule 1 (exp)
979     Conflict between rule 1 and token OP resolved as reduce (%left OP).
982 AT_CLEANUP
985 ## ---------------------- ##
986 ## %precedence suffices.  ##
987 ## ---------------------- ##
989 AT_SETUP([%precedence suffices])
991 AT_DATA([input.y],
992 [[%precedence "then"
993 %precedence "else"
995 stmt:
996   "if" cond "then" stmt
997 | "if" cond "then" stmt "else" stmt
998 | "stmt"
1001 cond:
1002   "exp"
1006 AT_BISON_CHECK([-o input.c input.y])
1008 AT_CLEANUP
1011 ## ------------------------------ ##
1012 ## %precedence does not suffice.  ##
1013 ## ------------------------------ ##
1015 AT_SETUP([%precedence does not suffice])
1017 AT_DATA([input.y],
1018 [[%precedence "then"
1019 %precedence "else"
1021 stmt:
1022   "if" cond "then" stmt
1023 | "if" cond "then" stmt "else" stmt
1024 | "stmt"
1027 cond:
1028   "exp"
1029 | cond "then" cond
1033 AT_BISON_CHECK([-o input.c input.y], 0, [],
1034 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1035 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1036 input.y:12.3-18: warning: rule useless in parser due to conflicts [-Wother]
1039 AT_CLEANUP
1042 ## ---------------------------------------- ##
1043 ## Syntax error in consistent error state.  ##
1044 ## ---------------------------------------- ##
1046 # AT_TEST(SKELETON-NAME)
1047 # ----------------------
1048 # Make sure yysyntax_error does nothing silly when called on yytoken
1049 # == YYEMPTY.
1051 m4_pushdef([AT_TEST],
1052 [AT_SETUP([Syntax error in consistent error state: $1])
1054 AT_BISON_OPTION_PUSHDEFS([%skeleton "$1"])
1056 AT_DATA_GRAMMAR([input.y],
1057 [[%define parse.error verbose
1058 %skeleton "$1"
1060 %nonassoc 'a';
1062 start: 'a' consistent-error-on-a-a 'a';
1064 consistent-error-on-a-a:
1065     'a' default-reduction
1066   | 'a' default-reduction 'a'
1067   ;
1069 default-reduction: %empty;
1071 %code {
1072   #include <stdio.h>
1073   ]AT_YYERROR_DECLARE[
1074   ]AT_YYLEX_DECLARE[
1077 ]AT_YYERROR_DEFINE[
1078 ]AT_YYLEX_DEFINE("aa")[
1079 ]AT_MAIN_DEFINE[
1082 AT_BISON_CHECK([-o input.AT_LANG_EXT input.y], 0, [],
1083 [[input.y:17.5-25: warning: rule useless in parser due to conflicts [-Wother]
1084 input.y:18.5-29: warning: rule useless in parser due to conflicts [-Wother]
1086 AT_LANG_COMPILE([input])
1087 AT_PARSER_CHECK([[input]], 1, [],
1088 [[syntax error
1091 AT_BISON_OPTION_POPDEFS
1092 AT_CLEANUP
1095 ## FIXME: test Java and D.
1096 m4_map_args([AT_TEST], [yacc.c], [glr.c], [lalr1.cc], [glr.cc], [glr2.cc])
1098 m4_popdef([AT_TEST])
1102 ## -------------------------------- ##
1103 ## Defaulted Conflicted Reduction.  ##
1104 ## -------------------------------- ##
1106 # When there are RR conflicts, some rules are disabled.  Usually it is
1107 # simply displayed as:
1109 #    $end           reduce using rule 3 (num)
1110 #    $end           [reduce using rule 4 (id)]
1112 # But when 'reduce 3' is the default action, we'd produce:
1114 #    $end           [reduce using rule 4 (id)]
1115 #    $default    reduce using rule 3 (num)
1117 # In this precise case (a reduction is masked by the default
1118 # reduction), we make the 'reduce 3' explicit:
1120 #    $end           reduce using rule 3 (num)
1121 #    $end           [reduce using rule 4 (id)]
1122 #    $default    reduce using rule 3 (num)
1124 # Maybe that's not the best display, but then, please propose something
1125 # else.
1127 AT_SETUP([Defaulted Conflicted Reduction])
1128 AT_KEYWORDS([cex report])
1130 AT_DATA([input.y],
1131 [[%%
1132 exp: num | id;
1133 num: '0';
1134 id : '0';
1138 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
1139 [[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1140 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1141 input.y:4.6-8: warning: rule useless in parser due to conflicts [-Wother]
1144 # Check the contents of the report.
1145 AT_CHECK([cat input.output], [],
1146 [[Rules useless in parser due to conflicts
1148     4 id: '0'
1151 State 1 conflicts: 1 reduce/reduce
1154 Grammar
1156     0 $accept: exp $end
1158     1 exp: num
1159     2    | id
1161     3 num: '0'
1163     4 id: '0'
1166 Terminals, with rules where they appear
1168     $end (0) 0
1169     '0' (48) 3 4
1170     error (256)
1173 Nonterminals, with rules where they appear
1175     $accept (4)
1176         on left: 0
1177     exp (5)
1178         on left: 1 2
1179         on right: 0
1180     num (6)
1181         on left: 3
1182         on right: 1
1183     id (7)
1184         on left: 4
1185         on right: 2
1188 State 0
1190     0 $accept: . exp $end
1191     1 exp: . num
1192     2    | . id
1193     3 num: . '0'
1194     4 id: . '0'
1196     '0'  shift, and go to state 1
1198     exp  go to state 2
1199     num  go to state 3
1200     id   go to state 4
1203 State 1
1205     3 num: '0' .  [$end]
1206     4 id: '0' .  [$end]
1208     $end      reduce using rule 3 (num)
1209     $end      [reduce using rule 4 (id)]
1210     $default  reduce using rule 3 (num)
1212     reduce/reduce conflict on token $end:
1213         3 num: '0' .
1214         4 id: '0' .
1215       Example: '0' .
1216       First reduce derivation
1217         exp
1218         `-> 1: num
1219                `-> 3: '0' .
1220       Second reduce derivation
1221         exp
1222         `-> 2: id
1223                `-> 4: '0' .
1227 State 2
1229     0 $accept: exp . $end
1231     $end  shift, and go to state 5
1234 State 3
1236     1 exp: num .
1238     $default  reduce using rule 1 (exp)
1241 State 4
1243     2 exp: id .
1245     $default  reduce using rule 2 (exp)
1248 State 5
1250     0 $accept: exp $end .
1252     $default  accept
1255 AT_CLEANUP
1260 ## -------------------- ##
1261 ## %expect not enough.  ##
1262 ## -------------------- ##
1264 AT_SETUP([%expect not enough])
1266 AT_DATA([input.y],
1267 [[%token NUM OP
1268 %expect 0
1270 exp: exp OP exp | NUM;
1273 AT_BISON_CHECK([-o input.c input.y], 1, [],
1274 [[input.y: error: shift/reduce conflicts: 1 found, 0 expected
1275 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1277 AT_CLEANUP
1280 ## --------------- ##
1281 ## %expect right.  ##
1282 ## --------------- ##
1284 AT_SETUP([%expect right])
1286 AT_DATA([input.y],
1287 [[%token NUM OP
1288 %expect 1
1290 exp: exp OP exp | NUM;
1293 AT_BISON_CHECK([-o input.c input.y])
1294 AT_CLEANUP
1297 ## ------------------ ##
1298 ## %expect too much.  ##
1299 ## ------------------ ##
1301 AT_SETUP([%expect too much])
1303 AT_DATA([input.y],
1304 [[%token NUM OP
1305 %expect 2
1307 exp: exp OP exp | NUM;
1310 AT_BISON_CHECK([-o input.c input.y], 1, [],
1311 [[input.y: error: shift/reduce conflicts: 1 found, 2 expected
1312 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1314 AT_CLEANUP
1317 ## ------------------------------- ##
1318 ## %expect with reduce conflicts.  ##
1319 ## ------------------------------- ##
1321 AT_SETUP([%expect with reduce conflicts])
1323 AT_DATA([input.y],
1324 [[%expect 0
1326 program: a 'a' | a a;
1327 a: 'a';
1330 AT_BISON_CHECK([-o input.c input.y], 1, [],
1331 [[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
1332 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1334 AT_CLEANUP
1337 ## ------------------------------------ ##
1338 ## %expect in grammar rule not enough.  ##
1339 ## ------------------------------------ ##
1341 AT_SETUP([%expect in grammar rule not enough])
1343 AT_DATA([input.y],
1344 [[%token NUM OP
1345 %expect 1
1347 exp: exp OP exp %expect 0 | NUM;
1350 AT_BISON_CHECK([-o input.c input.y], 1, [],
1351 [[input.y:4.6-25: error: shift/reduce conflicts for rule 1: 1 found, 0 expected
1353 AT_CLEANUP
1356 ## ------------------------------- ##
1357 ## %expect in grammar rule right.  ##
1358 ## ------------------------------- ##
1360 AT_SETUP([%expect in grammar rule right])
1362 AT_DATA([input.y],
1363 [[%token NUM OP
1364 %expect 1
1366 exp: exp OP exp %expect 1 | NUM;
1369 AT_BISON_CHECK([-o input.c input.y])
1370 AT_CLEANUP
1373 ## -------------------------- ##
1374 ## %expect in grammar rules.  ##
1375 ## -------------------------- ##
1377 AT_SETUP([%expect in grammar rules])
1379 AT_DATA([input.y],
1380 [[%expect 4
1382 exp:
1383   "number"
1384 | exp "+" exp  %expect 2
1385 | exp "*" exp  %expect 2
1388 AT_BISON_CHECK([-o input.c -rall input.y])
1389 AT_CLEANUP
1392 ## ---------------------------------- ##
1393 ## %expect in grammar rule too much.  ##
1394 ## ---------------------------------- ##
1396 AT_SETUP([%expect in grammar rule too much])
1398 AT_DATA([input.y],
1399 [[%token NUM OP
1400 %expect 1
1402 exp: exp OP exp | NUM %expect 1;
1405 AT_BISON_CHECK([-o input.c input.y], 1, [],
1406 [[input.y:4.19-31: error: shift/reduce conflicts for rule 2: 0 found, 1 expected
1408 AT_CLEANUP
1411 ## ---------------------------- ##
1412 ## %expect-rr in grammar rule.  ##
1413 ## ---------------------------- ##
1415 AT_SETUP([%expect-rr in grammar rule])
1417 AT_DATA([input.y],
1418 [[%glr-parser
1419 %expect-rr 3
1422 : a '1'
1423 | a '2'
1424 | a '3'
1425 | b '1'
1426 | b '2'
1427 | b '3'
1429 b: %expect-rr 3
1432 AT_BISON_CHECK([-o input.c input.y])
1433 AT_CLEANUP
1436 ## ------------------------------------- ##
1437 ## %expect-rr too much in grammar rule.  ##
1438 ## ------------------------------------- ##
1440 AT_SETUP([%expect-rr too much in grammar rule])
1442 AT_DATA([input.y],
1443 [[%glr-parser
1444 %expect-rr 3
1447 : a '1'
1448 | a '2'
1449 | a '3'
1450 | b '1'
1451 | b '2'
1452 | b '3'
1454 b: %expect-rr 4
1457 AT_BISON_CHECK([-fcaret -o input.c input.y], 1, [],
1458 [[input.y:12.4-15: error: reduce/reduce conflicts for rule 8: 3 found, 4 expected
1459    12 | b: %expect-rr 4
1460       |    ^~~~~~~~~~~~
1462 AT_CLEANUP
1465 ## --------------------------------------- ##
1466 ## %expect-rr not enough in grammar rule.  ##
1467 ## --------------------------------------- ##
1469 AT_SETUP([%expect-rr not enough in grammar rule])
1471 AT_DATA([input.y],
1472 [[%glr-parser
1473 %expect-rr 3
1476 : a '1'
1477 | a '2'
1478 | a '3'
1479 | b '1'
1480 | b '2'
1481 | b '3'
1483 b: %expect-rr 2
1486 AT_BISON_CHECK([-fcaret -o input.c input.y], 1, [],
1487 [[input.y:12.4-15: error: reduce/reduce conflicts for rule 8: 3 found, 2 expected
1488    12 | b: %expect-rr 2
1489       |    ^~~~~~~~~~~~
1491 AT_CLEANUP
1494 ## ------------------------- ##
1495 ## %prec with user strings.  ##
1496 ## ------------------------- ##
1498 AT_SETUP([%prec with user string])
1500 AT_DATA([[input.y]],
1501 [[%%
1502 exp:
1503   "foo" %prec "foo"
1507 AT_BISON_CHECK([-o input.c input.y])
1508 AT_CLEANUP
1511 ## -------------------------------- ##
1512 ## %no-default-prec without %prec.  ##
1513 ## -------------------------------- ##
1515 AT_SETUP([%no-default-prec without %prec])
1517 AT_DATA([[input.y]],
1518 [[%left '+'
1519 %left '*'
1523 %no-default-prec;
1525 e:   e '+' e
1526    | e '*' e
1527    | '0'
1528    ;
1531 AT_BISON_CHECK([-Wall -o input.c input.y], 0, [],
1532 [[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
1533 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1534 input.y:1.1-5: warning: useless precedence and associativity for '+' [-Wprecedence]
1535 input.y:2.1-5: warning: useless precedence and associativity for '*' [-Wprecedence]
1537 AT_CLEANUP
1540 ## ----------------------------- ##
1541 ## %no-default-prec with %prec.  ##
1542 ## ----------------------------- ##
1544 AT_SETUP([%no-default-prec with %prec])
1546 AT_DATA([[input.y]],
1547 [[%left '+'
1548 %left '*'
1552 %no-default-prec;
1554 e:   e '+' e %prec '+'
1555    | e '*' e %prec '*'
1556    | '0'
1557    ;
1560 AT_BISON_CHECK([-o input.c input.y])
1561 AT_CLEANUP
1564 ## --------------- ##
1565 ## %default-prec.  ##
1566 ## --------------- ##
1568 AT_SETUP([%default-prec])
1570 AT_DATA([[input.y]],
1571 [[%left '+'
1572 %left '*'
1576 %default-prec;
1578 e:   e '+' e
1579    | e '*' e
1580    | '0'
1581    ;
1584 AT_BISON_CHECK([-o input.c input.y])
1585 AT_CLEANUP
1588 ## ---------------------------------------------- ##
1589 ## Unreachable States After Conflict Resolution.  ##
1590 ## ---------------------------------------------- ##
1592 AT_SETUP([[Unreachable States After Conflict Resolution]])
1594 AT_KEYWORDS([cex report])
1596 # If conflict resolution makes states unreachable, remove those states, report
1597 # rules that are then unused, and don't report conflicts in those states.  Test
1598 # what happens when a nonterminal becomes useless as a result of state removal
1599 # since that causes lalr.o's goto map to be rewritten.
1601 AT_DATA([[input.y]],
1602 [[%output "input.c"
1603 %left 'a'
1607 start: resolved_conflict 'a' reported_conflicts 'a' ;
1609 /* S/R conflict resolved as reduce, so the state with item
1610  * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1611  * unreachable, and the associated production is useless.  */
1612 resolved_conflict:
1613     'a' unreachable1
1614   | %prec 'a'
1615   ;
1617 /* S/R conflict that need not be reported since it is unreachable because of
1618  * the previous conflict resolution.  Nonterminal unreachable1 and all its
1619  * productions are useless.  */
1620 unreachable1:
1621     'a' unreachable2
1622   |
1623   ;
1625 /* Likewise for a R/R conflict and nonterminal unreachable2.  */
1626 unreachable2: | ;
1628 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
1629  * when their states are renumbered due to state removal.  */
1630 reported_conflicts:
1631     'a'
1632   | 'a'
1633   |
1634   ;
1638 AT_BISON_CHECK([[--report=all input.y]], 0, [],
1639 [[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1640 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
1641 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1642 input.y:12.5-20: warning: rule useless in parser due to conflicts [-Wother]
1643 input.y:20.5-20: warning: rule useless in parser due to conflicts [-Wother]
1644 input.y:21.4: warning: rule useless in parser due to conflicts [-Wother]
1645 input.y:25.14: warning: rule useless in parser due to conflicts [-Wother]
1646 input.y:25.16: warning: rule useless in parser due to conflicts [-Wother]
1647 input.y:31.5-7: warning: rule useless in parser due to conflicts [-Wother]
1648 input.y:32.4: warning: rule useless in parser due to conflicts [-Wother]
1651 AT_CHECK([[cat input.output]], 0,
1652 [[Rules useless in parser due to conflicts
1654     2 resolved_conflict: 'a' unreachable1
1656     4 unreachable1: 'a' unreachable2
1657     5             | %empty
1659     6 unreachable2: %empty
1660     7             | %empty
1662     9 reported_conflicts: 'a'
1663    10                   | %empty
1666 State 4 conflicts: 1 shift/reduce
1667 State 5 conflicts: 1 reduce/reduce
1670 Grammar
1672     0 $accept: start $end
1674     1 start: resolved_conflict 'a' reported_conflicts 'a'
1676     2 resolved_conflict: 'a' unreachable1
1677     3                  | %empty
1679     4 unreachable1: 'a' unreachable2
1680     5             | %empty
1682     6 unreachable2: %empty
1683     7             | %empty
1685     8 reported_conflicts: 'a'
1686     9                   | 'a'
1687    10                   | %empty
1690 Terminals, with rules where they appear
1692     $end (0) 0
1693     'a' (97) 1 2 4 8 9
1694     error (256)
1697 Nonterminals, with rules where they appear
1699     $accept (4)
1700         on left: 0
1701     start (5)
1702         on left: 1
1703         on right: 0
1704     resolved_conflict (6)
1705         on left: 2 3
1706         on right: 1
1707     unreachable1 (7)
1708         on left: 4 5
1709         on right: 2
1710     unreachable2 (8)
1711         on left: 6 7
1712         on right: 4
1713     reported_conflicts (9)
1714         on left: 8 9 10
1715         on right: 1
1718 State 0
1720     0 $accept: . start $end
1721     1 start: . resolved_conflict 'a' reported_conflicts 'a'
1722     2 resolved_conflict: . 'a' unreachable1
1723     3                  | %empty .  ['a']
1725     $default  reduce using rule 3 (resolved_conflict)
1727     start              go to state 1
1728     resolved_conflict  go to state 2
1730     Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1733 State 1
1735     0 $accept: start . $end
1737     $end  shift, and go to state 3
1740 State 2
1742     1 start: resolved_conflict . 'a' reported_conflicts 'a'
1744     'a'  shift, and go to state 4
1747 State 3
1749     0 $accept: start $end .
1751     $default  accept
1754 State 4
1756     1 start: resolved_conflict 'a' . reported_conflicts 'a'
1757     8 reported_conflicts: . 'a'
1758     9                   | . 'a'
1759    10                   | %empty .  ['a']
1761     'a'  shift, and go to state 5
1763     'a'  [reduce using rule 10 (reported_conflicts)]
1765     reported_conflicts  go to state 6
1767     shift/reduce conflict on token 'a':
1768        10 reported_conflicts: %empty .
1769         8 reported_conflicts: . 'a'
1770       First example: resolved_conflict . 'a' 'a'
1771       Shift derivation
1772         start
1773         `-> 1: resolved_conflict reported_conflicts 'a'
1774                                  `-> 8: . 'a'
1775       Second example: resolved_conflict . 'a'
1776       Reduce derivation
1777         start
1778         `-> 1: resolved_conflict reported_conflicts 'a'
1779                                  `-> 10: %empty .
1781     shift/reduce conflict on token 'a':
1782        10 reported_conflicts: %empty .
1783         9 reported_conflicts: . 'a'
1784       First example: resolved_conflict . 'a' 'a'
1785       Shift derivation
1786         start
1787         `-> 1: resolved_conflict reported_conflicts 'a'
1788                                  `-> 9: . 'a'
1789       Second example: resolved_conflict . 'a'
1790       Reduce derivation
1791         start
1792         `-> 1: resolved_conflict reported_conflicts 'a'
1793                                  `-> 10: %empty .
1797 State 5
1799     8 reported_conflicts: 'a' .  ['a']
1800     9                   | 'a' .  ['a']
1802     'a'       reduce using rule 8 (reported_conflicts)
1803     'a'       [reduce using rule 9 (reported_conflicts)]
1804     $default  reduce using rule 8 (reported_conflicts)
1806     reduce/reduce conflict on token 'a':
1807         8 reported_conflicts: 'a' .
1808         9 reported_conflicts: 'a' .
1809       Example: 'a' .
1810       First reduce derivation
1811         reported_conflicts
1812         `-> 8: 'a' .
1813       Second reduce derivation
1814         reported_conflicts
1815         `-> 9: 'a' .
1819 State 6
1821     1 start: resolved_conflict 'a' reported_conflicts . 'a'
1823     'a'  shift, and go to state 7
1826 State 7
1828     1 start: resolved_conflict 'a' reported_conflicts 'a' .
1830     $default  reduce using rule 1 (start)
1833 AT_DATA([[input-keep.y]],
1834 [[%define lr.keep-unreachable-state
1836 AT_CHECK([[cat input.y >> input-keep.y]])
1838 AT_BISON_CHECK([[input-keep.y]], 0, [],
1839 [[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1840 input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
1841 input-keep.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
1842 input-keep.y:22.4: warning: rule useless in parser due to conflicts [-Wother]
1843 input-keep.y:26.16: warning: rule useless in parser due to conflicts [-Wother]
1844 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts [-Wother]
1845 input-keep.y:33.4: warning: rule useless in parser due to conflicts [-Wother]
1848 AT_CLEANUP
1851 ## ------------------------------------------------------------ ##
1852 ## Solved conflicts report for multiple reductions in a state.  ##
1853 ## ------------------------------------------------------------ ##
1855 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1857 # Used to lose earlier solved conflict messages even within a single S/R/R.
1859 AT_DATA([[input.y]],
1860 [[%left 'a'
1861 %right 'b'
1862 %right 'c'
1863 %right 'd'
1865 start:
1866     'a'
1867   | empty_a 'a'
1868   | 'b'
1869   | empty_b 'b'
1870   | 'c'
1871   | empty_c1 'c'
1872   | empty_c2 'c'
1873   | empty_c3 'c'
1874   ;
1875 empty_a:  %empty %prec 'a' ;
1876 empty_b:  %empty %prec 'b' ;
1877 empty_c1: %empty %prec 'c' ;
1878 empty_c2: %empty %prec 'c' ;
1879 empty_c3: %empty %prec 'd' ;
1881 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1882 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1883 [[State 0
1885     0 $accept: . start $end
1886     1 start: . 'a'
1887     2      | . empty_a 'a'
1888     3      | . 'b'
1889     4      | . empty_b 'b'
1890     5      | . 'c'
1891     6      | . empty_c1 'c'
1892     7      | . empty_c2 'c'
1893     8      | . empty_c3 'c'
1894     9 empty_a: %empty .  ['a']
1895    10 empty_b: %empty .  []
1896    11 empty_c1: %empty .  []
1897    12 empty_c2: %empty .  []
1898    13 empty_c3: %empty .  ['c']
1900     'b'  shift, and go to state 1
1902     'c'       reduce using rule 13 (empty_c3)
1903     $default  reduce using rule 9 (empty_a)
1905     start     go to state 2
1906     empty_a   go to state 3
1907     empty_b   go to state 4
1908     empty_c1  go to state 5
1909     empty_c2  go to state 6
1910     empty_c3  go to state 7
1912     Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1913     Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1914     Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1915     Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1916     Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1919 State 1
1922 AT_CLEANUP
1925 ## ------------------------------------------------------------ ##
1926 ## %nonassoc error actions for multiple reductions in a state.  ##
1927 ## ------------------------------------------------------------ ##
1929 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1930 # multiple reductions in a state.
1932 # For a %nonassoc error action token, used to print the first remaining
1933 # reduction on that token without brackets.
1935 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1937 AT_KEYWORDS([cex report])
1939 AT_DATA([[input.y]],
1940 [[%nonassoc 'a' 'b' 'c'
1942 start:
1943     'a'
1944   | empty_a 'a'
1945   | 'b'
1946   | empty_b 'b'
1947   | 'c'
1948   | empty_c1 'c'
1949   | empty_c2 'c'
1950   | empty_c3 'c'
1951   ;
1952 empty_a: %prec 'a' ;
1953 empty_b: %prec 'b' ;
1954 empty_c1: %prec 'c' ;
1955 empty_c2: %prec 'c' ;
1956 empty_c3: %prec 'c' ;
1959 AT_BISON_CHECK([[--trace=cex -fcaret --report=all -o input.c input.y]], 0, [],
1960 [[bison (GNU Bison) ]AT_PACKAGE_VERSION[
1961 init: 0.000000
1962 # state items: 26
1963 State 0:
1964     0 $accept: . start $end
1965     ->     0 $accept: start . $end
1966     =>     2 start: . empty_a 'a'
1967     =>     4 start: . empty_b 'b'
1968     =>     6 start: . empty_c1 'c'
1969     =>     7 start: . empty_c2 'c'
1970     =>     8 start: . empty_c3 'c'
1972     1 start: . 'a'  DISABLED
1974     2 start: . empty_a 'a'
1975     ->     2 start: empty_a . 'a'
1976     =>     9 empty_a: %empty .
1977     <-     0 $accept: . start $end
1979     3 start: . 'b'  DISABLED
1981     4 start: . empty_b 'b'
1982     ->     4 start: empty_b . 'b'
1983     =>    10 empty_b: %empty .
1984     <-     0 $accept: . start $end
1986     5 start: . 'c'  DISABLED
1988     6 start: . empty_c1 'c'
1989     ->     6 start: empty_c1 . 'c'
1990     =>    11 empty_c1: %empty .
1991     <-     0 $accept: . start $end
1993     7 start: . empty_c2 'c'
1994     ->     7 start: empty_c2 . 'c'
1995     =>    12 empty_c2: %empty .
1996     <-     0 $accept: . start $end
1998     8 start: . empty_c3 'c'
1999     ->     8 start: empty_c3 . 'c'
2000     =>    13 empty_c3: %empty .
2001     <-     0 $accept: . start $end
2003     9 empty_a: %empty .
2004     <-     2 start: . empty_a 'a'
2006    10 empty_b: %empty .
2007     <-     4 start: . empty_b 'b'
2009    11 empty_c1: %empty .
2010     <-     6 start: . empty_c1 'c'
2012    12 empty_c2: %empty .
2013     <-     7 start: . empty_c2 'c'
2015    13 empty_c3: %empty .
2016     <-     8 start: . empty_c3 'c'
2018 State 1:
2019     0 $accept: start . $end
2020     ->     0 $accept: start $end .
2021     <-     0 $accept: . start $end
2023 State 2:
2024     2 start: empty_a . 'a'
2025     ->     2 start: empty_a 'a' .
2026     <-     2 start: . empty_a 'a'
2028 State 3:
2029     4 start: empty_b . 'b'
2030     ->     4 start: empty_b 'b' .
2031     <-     4 start: . empty_b 'b'
2033 State 4:
2034     6 start: empty_c1 . 'c'
2035     ->     6 start: empty_c1 'c' .
2036     <-     6 start: . empty_c1 'c'
2038 State 5:
2039     7 start: empty_c2 . 'c'
2040     ->     7 start: empty_c2 'c' .
2041     <-     7 start: . empty_c2 'c'
2043 State 6:
2044     8 start: empty_c3 . 'c'
2045     ->     8 start: empty_c3 'c' .
2046     <-     8 start: . empty_c3 'c'
2048 State 7:
2049     0 $accept: start $end .
2050     <-     0 $accept: start . $end
2052 State 8:
2053     2 start: empty_a 'a' .
2054     <-     2 start: empty_a . 'a'
2056 State 9:
2057     4 start: empty_b 'b' .
2058     <-     4 start: empty_b . 'b'
2060 State 10:
2061     6 start: empty_c1 'c' .
2062     <-     6 start: empty_c1 . 'c'
2064 State 11:
2065     7 start: empty_c2 'c' .
2066     <-     7 start: empty_c2 . 'c'
2068 State 12:
2069     8 start: empty_c3 'c' .
2070     <-     8 start: empty_c3 . 'c'
2072 FIRSTS
2073   $accept firsts
2074     'a'
2075     'b'
2076     'c'
2077   start firsts
2078     'a'
2079     'b'
2080     'c'
2081   empty_a firsts
2082   empty_b firsts
2083   empty_c1 firsts
2084   empty_c2 firsts
2085   empty_c3 firsts
2088 input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
2089 input.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
2090 input.y:4.5-7: warning: rule useless in parser due to conflicts [-Wother]
2091     4 |     'a'
2092       |     ^~~
2093 input.y:6.5-7: warning: rule useless in parser due to conflicts [-Wother]
2094     6 |   | 'b'
2095       |     ^~~
2096 input.y:8.5-7: warning: rule useless in parser due to conflicts [-Wother]
2097     8 |   | 'c'
2098       |     ^~~
2099 input.y:13.10-18: warning: rule useless in parser due to conflicts [-Wother]
2100    13 | empty_a: %prec 'a' ;
2101       |          ^~~~~~~~~
2102 input.y:14.10-18: warning: rule useless in parser due to conflicts [-Wother]
2103    14 | empty_b: %prec 'b' ;
2104       |          ^~~~~~~~~
2105 input.y:15.11-19: warning: rule useless in parser due to conflicts [-Wother]
2106    15 | empty_c1: %prec 'c' ;
2107       |           ^~~~~~~~~
2108 input.y:16.11-19: warning: rule useless in parser due to conflicts [-Wother]
2109    16 | empty_c2: %prec 'c' ;
2110       |           ^~~~~~~~~
2111 input.y:17.11-19: warning: rule useless in parser due to conflicts [-Wother]
2112    17 | empty_c3: %prec 'c' ;
2113       |           ^~~~~~~~~
2114 REDUCE ITEM PATH:
2115     0 $accept: . start $end
2116     7 start: . empty_c2 'c'
2117    12 empty_c2: %empty .
2118 CONFLICT 1 (size 1 depth 0 rc 2)
2119    12 empty_c2: %empty .
2120    12 empty_c2: %empty .
2122     .
2124 CONFLICT 2 (size 1 depth 0 rc 2)
2125    13 empty_c3: %empty .
2126    13 empty_c3: %empty .
2128     .
2131 CONFLICT 1 (size 2 depth -1 rc 4)
2132     7 start: . empty_c2 'c'
2133     7 start: empty_c2 . 'c'
2135     empty_c2
2136     `-> 12: %empty .
2138 CONFLICT 2 (size 1 depth 0 rc 3)
2139    13 empty_c3: %empty .
2140    13 empty_c3: %empty .
2142     .
2145 CONFLICT 1 (size 1 depth 0 rc 3)
2146    12 empty_c2: %empty .
2147    12 empty_c2: %empty .
2149     .
2151 CONFLICT 2 (size 2 depth -1 rc 2)
2152     8 start: . empty_c3 'c'
2153     8 start: empty_c3 . 'c'
2155     empty_c3
2156     `-> 13: %empty .
2159 CONFLICT 1 (size 2 depth -1 rc 3)
2160     7 start: . empty_c2 'c'
2161     7 start: empty_c2 . 'c'
2163     empty_c2
2164     `-> 12: %empty .
2166 CONFLICT 2 (size 2 depth -1 rc 2)
2167     8 start: . empty_c3 'c'
2168     8 start: empty_c3 . 'c'
2170     empty_c3
2171     `-> 13: %empty .
2174 CONFLICT 1 (size 3 depth -1 rc 2)
2175     7 start: . empty_c2 'c'
2176     7 start: empty_c2 'c' .
2178     empty_c2
2179     `-> 12: %empty .
2181 CONFLICT 2 (size 3 depth -1 rc 2)
2182     8 start: . empty_c3 'c'
2183     8 start: empty_c3 'c' .
2185     empty_c3
2186     `-> 13: %empty .
2189 CONFLICT 1 (size 2 depth -1 rc 4)
2190     0 $accept: . start $end
2191     0 $accept: start . $end
2193     start
2194     `-> 7: empty_c2         'c'
2195            `-> 12: %empty .
2197 CONFLICT 2 (size 3 depth -1 rc 3)
2198     8 start: . empty_c3 'c'
2199     8 start: empty_c3 'c' .
2201     empty_c3
2202     `-> 13: %empty .
2205 CONFLICT 1 (size 3 depth -1 rc 3)
2206     7 start: . empty_c2 'c'
2207     7 start: empty_c2 'c' .
2209     empty_c2
2210     `-> 12: %empty .
2212 CONFLICT 2 (size 2 depth -1 rc 2)
2213     0 $accept: . start $end
2214     0 $accept: start . $end
2216     start
2217     `-> 8: empty_c3         'c'
2218            `-> 13: %empty .
2221 CONFLICT 1 (size 2 depth -1 rc 3)
2222     0 $accept: . start $end
2223     0 $accept: start . $end
2225     start
2226     `-> 7: empty_c2         'c'
2227            `-> 12: %empty .
2229 CONFLICT 2 (size 2 depth -1 rc 2)
2230     0 $accept: . start $end
2231     0 $accept: start . $end
2233     start
2234     `-> 8: empty_c3         'c'
2235            `-> 13: %empty .
2239 AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
2240 [[State 0
2242     0 $accept: . start $end
2243     1 start: . 'a'
2244     2      | . empty_a 'a'
2245     3      | . 'b'
2246     4      | . empty_b 'b'
2247     5      | . 'c'
2248     6      | . empty_c1 'c'
2249     7      | . empty_c2 'c'
2250     8      | . empty_c3 'c'
2251     9 empty_a: %empty .  []
2252    10 empty_b: %empty .  []
2253    11 empty_c1: %empty .  []
2254    12 empty_c2: %empty .  ['c']
2255    13 empty_c3: %empty .  ['c']
2257     'a'  error (nonassociative)
2258     'b'  error (nonassociative)
2259     'c'  error (nonassociative)
2261     'c'  [reduce using rule 12 (empty_c2)]
2262     'c'  [reduce using rule 13 (empty_c3)]
2264     start     go to state 1
2265     empty_a   go to state 2
2266     empty_b   go to state 3
2267     empty_c1  go to state 4
2268     empty_c2  go to state 5
2269     empty_c3  go to state 6
2271     Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
2272     Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
2273     Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
2275     reduce/reduce conflict on token 'c':
2276        12 empty_c2: %empty .
2277        13 empty_c3: %empty .
2278       Example: . 'c'
2279       First reduce derivation
2280         start
2281         `-> 7: empty_c2         'c'
2282                `-> 12: %empty .
2283       Second reduce derivation
2284         start
2285         `-> 8: empty_c3         'c'
2286                `-> 13: %empty .
2290 State 1
2292 AT_CLEANUP
2295 ## -------------------- ##
2296 ## %expect-rr non GLR.  ##
2297 ## -------------------- ##
2299 AT_SETUP([[%expect-rr non GLR]])
2301 AT_DATA([[1.y]],
2302 [[%expect-rr 0
2304 exp: 'a'
2307 AT_BISON_CHECK([[1.y]], [[0]], [],
2308 [[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
2311 AT_DATA([[2.y]],
2312 [[%expect-rr 1
2314 exp: 'a' | 'a';
2317 AT_BISON_CHECK([[2.y]], [[0]], [],
2318 [[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
2319 2.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
2320 2.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
2321 2.y:3.12-14: warning: rule useless in parser due to conflicts [-Wother]
2324 AT_CLEANUP
2327 ## ---------------------------------- ##
2328 ## -W versus %expect and %expect-rr.  ##
2329 ## ---------------------------------- ##
2331 AT_SETUP([[-W versus %expect and %expect-rr]])
2333 AT_DATA([[sr-rr.y]],
2334 [[%glr-parser
2336 start: 'a' | A 'a' | B 'a' ;
2337 A: ;
2338 B: ;
2340 AT_DATA([[sr.y]],
2341 [[%glr-parser
2343 start: 'a' | A 'a' ;
2344 A: ;
2346 AT_DATA([[rr.y]],
2347 [[%glr-parser
2349 start: A | B ;
2350 A: ;
2351 B: ;
2354 AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
2355 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
2356 sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
2357 sr-rr.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
2359 AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
2360 [[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
2361 sr-rr.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples
2363 AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
2364 [[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
2368 # This is piece of code is rather complex for a simple task: try every
2369 # combination of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
2370 # or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
2371 # RR).
2373 # Number and types of genuine conflicts in the grammar.
2374 for gram in sr-rr sr rr; do
2375   # Number of expected s/r conflicts.
2376   for sr_exp_i in '' 0 1 2; do
2377     # Number of expected r/r conflicts.
2378     for rr_exp_i in '' 0 1 2; do
2379       test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
2381       # Build grammar file.
2382       sr_exp=0
2383       rr_exp=0
2384       file=$gram
2385       directives=
2386       if test -n "$sr_exp_i"; then
2387         sr_exp=$sr_exp_i
2388         file=$file-expect-$sr_exp
2389         directives="%expect $sr_exp"
2390       fi
2391       if test -n "$rr_exp_i"; then
2392         rr_exp=$rr_exp_i
2393         file=$file-expect-rr-$rr_exp
2394         directives="$directives %expect-rr $rr_exp"
2395       fi
2396       file=$file.y
2397       echo "$directives" > $file
2398       cat $gram.y >> $file
2400       # Number of found conflicts.
2401       case $gram in
2402         (sr)    sr_count=1; rr_count=0;;
2403         (rr)    sr_count=0; rr_count=1;;
2404         (sr-rr) sr_count=1; rr_count=1;;
2405       esac
2407       # Update number of expected conflicts: if %expect is given then
2408       # %expect-rr defaults to 0, and vice-versa.  Leave empty if
2409       # nothing expected.
2410       case $sr_exp_i:$rr_exp_i in
2411         ?:) rr_exp_i=0;;
2412         :?) sr_exp_i=0;;
2413       esac
2415       # Run tests.
2416       if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
2417         ]AT_BISON_CHECK([[-Wnone $file]])[
2418         ]AT_BISON_CHECK([[-Werror $file]])[
2419       else
2420         {
2421           issue_note=false
2422           if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
2423             echo "warning: $sr_count shift/reduce conflicts"
2424             issue_note=true
2425           elif test "$sr_exp_i" -ne "$sr_count"; then
2426             echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
2427             if test "$sr_count" -ne 0; then
2428               issue_note=true
2429             fi
2430           fi
2431           if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
2432             echo "warning: $rr_count reduce/reduce conflicts"
2433             issue_note=true
2434           elif test "$rr_exp_i" -ne "$rr_count"; then
2435             echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
2436             if test "$rr_count" -ne 0; then
2437               issue_note=true
2438             fi
2439           fi
2440           if $issue_note; then
2441             echo "note: rerun with option '-Wcounterexamples' to generate conflict counterexamples"
2442           fi
2443         } | sed -e "s/^/$file: /" > experr
2444         ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
2445         ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[
2446       fi
2447     done
2448   done
2449 done]
2451 AT_CLEANUP