]> git.saurik.com Git - bison.git/blame - tests/conflicts.at
build: please Clang++ 3.2+ on Flex scanners
[bison.git] / tests / conflicts.at
CommitLineData
3c31a486 1# Exercising Bison on conflicts. -*- Autotest -*-
69363a9e 2
7d6bad19 3# Copyright (C) 2002-2005, 2007-2013 Free Software Foundation, Inc.
3c31a486 4
f16b0819 5# This program is free software: you can redistribute it and/or modify
3c31a486 6# it under the terms of the GNU General Public License as published by
f16b0819
PE
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
3c31a486
AD
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.
f16b0819 14#
3c31a486 15# You should have received a copy of the GNU General Public License
f16b0819 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
3c31a486
AD
17
18AT_BANNER([[Conflicts.]])
19
e8f7155d
VT
20## ------------------------------ ##
21## Useless associativity warning. ##
22## ------------------------------ ##
23
24AT_SETUP([Useless associativity warning])
25
26AT_DATA([[input.y]],
27[[%token T
28%left A B
29%right C
30%nonassoc D
31%precedence E
32
33%%
34e: T A T
35 | T B T
36 | T C T
37 | T D T
38 | T E T
39;
40]])
41
42AT_BISON_CHECK([input.y], 0, [],
43[[input.y:5.13: warning: useless precedence for E [-Wother]
44input.y:2.7: warning: useless associativity for A [-Wother]
45input.y:2.9: warning: useless associativity for B [-Wother]
46input.y:3.8: warning: useless associativity for C [-Wother]
47input.y:4.11: warning: useless associativity for D [-Wother]
48]])
49
50AT_CLEANUP
51
3c31a486 52
9e62f1a6
VT
53## ------------------------ ##
54## Token declaration order. ##
55## ------------------------ ##
56
57# This test checks that token are declared left to right when in a precedence
58# statement.
59
60AT_SETUP([Token declaration order])
61
62AT_BISON_OPTION_PUSHDEFS
63
64AT_DATA_GRAMMAR([[input.y]],
65[[%code {
66 #include <stdio.h>
67 ]AT_YYERROR_DECLARE[
68 ]AT_YYLEX_DECLARE[
69}
70%token A B C
71%token D
72%right E F G
73%right H I
74%right J
75%left K
76%left L M N
77%nonassoc O P Q
78%precedence R S T U
79%precedence V W
80%%
81exp: A
82%%
83]AT_YYERROR_DEFINE[
84]AT_YYLEX_DEFINE[
85int main (void)
86{
87 assert (A < B);
88 assert (B < C);
89 assert (C < D);
90 assert (D < E);
91 assert (E < F);
92 assert (F < G);
93 assert (G < H);
94 assert (H < I);
95 assert (I < J);
96 assert (J < K);
97 assert (K < L);
98 assert (L < M);
99 assert (M < N);
100 assert (N < O);
101 assert (O < P);
102 assert (P < Q);
103 assert (Q < R);
104 assert (R < S);
105 assert (S < T);
106 assert (T < U);
107 assert (U < V);
108 assert (V < W);
312c0cff 109 return 0;
9e62f1a6
VT
110}
111]])
112
284bc49c
VT
113AT_BISON_CHECK([-o input.c input.y], [], [],
114[[input.y:24.13: warning: useless precedence for R [-Wother]
115input.y:24.15: warning: useless precedence for S [-Wother]
116input.y:24.17: warning: useless precedence for T [-Wother]
117input.y:24.19: warning: useless precedence for U [-Wother]
118input.y:25.13: warning: useless precedence for V [-Wother]
119input.y:25.15: warning: useless precedence for W [-Wother]
e8f7155d
VT
120input.y:18.8: warning: useless associativity for E [-Wother]
121input.y:18.10: warning: useless associativity for F [-Wother]
122input.y:18.12: warning: useless associativity for G [-Wother]
123input.y:19.8: warning: useless associativity for H [-Wother]
124input.y:19.10: warning: useless associativity for I [-Wother]
125input.y:20.8: warning: useless associativity for J [-Wother]
126input.y:21.8: warning: useless associativity for K [-Wother]
127input.y:22.8: warning: useless associativity for L [-Wother]
128input.y:22.10: warning: useless associativity for M [-Wother]
129input.y:22.12: warning: useless associativity for N [-Wother]
130input.y:23.11: warning: useless associativity for O [-Wother]
131input.y:23.13: warning: useless associativity for P [-Wother]
132input.y:23.15: warning: useless associativity for Q [-Wother]
284bc49c
VT
133]])
134AT_COMPILE([input])
9e62f1a6 135
bb1c50d8 136AT_PARSER_CHECK([./input])
9e62f1a6
VT
137
138AT_BISON_OPTION_POPDEFS
139
140AT_CLEANUP
141
142
284bc49c
VT
143## ---------------------------- ##
144## Useless precedence warning. ##
145## ---------------------------- ##
146
147AT_SETUP([Useless precedence warning])
148
149AT_DATA([[input.y]],
150[[%token A B
151%precedence Z
152%left X
153%precedence Y
154%left W
155%right V
156%nonassoc U
157%%
158a: b
159 | a U b
160 | f
161;
162b: c
163 | b V c
164;
165c: d
166 | c W d
167;
168d: A
169 | d X d
170 | d Y A
171;
172f: B
173 | f Z B
174;
175]])
176
177AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
178[[input.y:2.13: warning: useless precedence for Z [-Wother]
179 %precedence Z
180 ^
e8f7155d
VT
181input.y:5.7: warning: useless associativity for W [-Wother]
182 %left W
183 ^
184input.y:6.8: warning: useless associativity for V [-Wother]
185 %right V
186 ^
187input.y:7.11: warning: useless associativity for U [-Wother]
188 %nonassoc U
189 ^
284bc49c
VT
190]])
191
192AT_CLEANUP
193
194
643a5994
AD
195## ---------------- ##
196## S/R in initial. ##
197## ---------------- ##
198
199# I once hacked Bison in such a way that it lost its reductions on the
200# initial state (because it was confusing it with the last state). It
201# took me a while to strip down my failures to this simple case. So
202# make sure it finds the s/r conflict below.
203
204AT_SETUP([S/R in initial])
205
206AT_DATA([[input.y]],
207[[%expect 1
208%%
209exp: e 'e';
210e: 'e' | /* Nothing. */;
211]])
212
da730230 213AT_BISON_CHECK([-o input.c input.y], 0, [],
73370a9d 214[[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */ [-Wother]
e8832397 215]])
643a5994 216
505ece51 217AT_BISON_CHECK([-fcaret -o input.c input.y], 0, [],
f3ead217 218[[input.y:4.9: warning: rule useless in parser due to conflicts [-Wother]
505ece51
TR
219 e: 'e' | /* Nothing. */;
220 ^
221]])
222
643a5994
AD
223AT_CLEANUP
224
bc933ef1 225
3c31a486
AD
226## ------------------- ##
227## %nonassoc and eof. ##
228## ------------------- ##
229
230AT_SETUP([%nonassoc and eof])
231
55f48c48 232AT_BISON_OPTION_PUSHDEFS
9501dc6e 233AT_DATA_GRAMMAR([input.y],
3c31a486
AD
234[[
235%{
236#include <stdio.h>
6e26ca8c 237#include <stdlib.h>
cf806753 238#include <string.h>
77519a7d 239#include <assert.h>
1207eeac 240
3c31a486 241#define YYERROR_VERBOSE 1
55f48c48 242]AT_YYERROR_DEFINE[
3c31a486 243/* The current argument. */
cf806753 244static const char *input;
3c31a486
AD
245
246static int
247yylex (void)
248{
cf806753 249 static size_t toknum;
77519a7d 250 assert (toknum <= strlen (input));
cf806753 251 return input[toknum++];
3c31a486
AD
252}
253
254%}
255
256%nonassoc '<' '>'
257
258%%
259expr: expr '<' expr
260 | expr '>' expr
261 | '0'
262 ;
263%%
264int
265main (int argc, const char *argv[])
266{
9d774aff 267 input = argc <= 1 ? "" : argv[1];
3c31a486
AD
268 return yyparse ();
269}
270]])
55f48c48 271AT_BISON_OPTION_POPDEFS
3c31a486 272
bf35c71c
JD
273m4_pushdef([AT_NONASSOC_AND_EOF_CHECK],
274[AT_BISON_CHECK([$1[ -o input.c input.y]])
1154cced 275AT_COMPILE([input])
3c31a486 276
bf35c71c
JD
277m4_pushdef([AT_EXPECTING], [m4_if($2, [correct], [[, expecting $end]])])
278
1154cced 279AT_PARSER_CHECK([./input '0<0'])
1154cced 280AT_PARSER_CHECK([./input '0<0<0'], [1], [],
bf35c71c 281 [syntax error, unexpected '<'AT_EXPECTING
3c31a486
AD
282])
283
1154cced
AD
284AT_PARSER_CHECK([./input '0>0'])
285AT_PARSER_CHECK([./input '0>0>0'], [1], [],
bf35c71c 286 [syntax error, unexpected '>'AT_EXPECTING
3c31a486
AD
287])
288
1154cced 289AT_PARSER_CHECK([./input '0<0>0'], [1], [],
bf35c71c 290 [syntax error, unexpected '>'AT_EXPECTING
3c31a486
AD
291])
292
bf35c71c 293m4_popdef([AT_EXPECTING])])
d1cc31c5 294
bf35c71c
JD
295# Expected token list is missing.
296AT_NONASSOC_AND_EOF_CHECK([], [[incorrect]])
d1cc31c5 297
bf35c71c
JD
298# We must disable default reductions in inconsistent states in order to
299# have an explicit list of all expected tokens.
f3bc3386 300AT_NONASSOC_AND_EOF_CHECK([[-Dlr.default-reduction=consistent]],
bf35c71c
JD
301 [[correct]])
302
f3bc3386 303# lr.default-reduction=consistent happens to work for this test case.
bf35c71c
JD
304# However, for other grammars, lookahead sets can be merged for
305# different left contexts, so it is still possible to have an incorrect
306# expected list. Canonical LR is almost a general solution (that is, it
307# can fail only when %nonassoc is used), so make sure it gives the same
308# result as above.
309AT_NONASSOC_AND_EOF_CHECK([[-Dlr.type=canonical-lr]], [[correct]])
310
311# parse.lac=full is a completely general solution that does not require
312# any of the above sacrifices. Of course, it does not extend the
313# language-recognition power of LALR to (IE)LR, but it does ensure that
314# the reported list of expected tokens matches what the given parser
315# would have accepted in place of the unexpected token.
316AT_NONASSOC_AND_EOF_CHECK([[-Dparse.lac=full]], [[correct]])
317
318m4_popdef([AT_NONASSOC_AND_EOF_CHECK])
d1cc31c5 319
3c31a486
AD
320AT_CLEANUP
321
322
323
df222dfa
JD
324## ------------------------------------------- ##
325## parse.error=verbose and consistent errors. ##
326## ------------------------------------------- ##
327
328AT_SETUP([[parse.error=verbose and consistent errors]])
329
330m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
331
d2060f06
JD
332AT_BISON_OPTION_PUSHDEFS([$1])
333
334m4_pushdef([AT_YYLEX_PROTOTYPE],
335[AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
336 [[int yylex (YYSTYPE *lvalp)]])])
337
338AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
339[AT_SKEL_JAVA_IF([[
340
341%code imports {
342 import java.io.IOException;
343}]], [[
344
345%code {]AT_SKEL_CC_IF([[
346 #include <string>]], [[
df222dfa
JD
347 #include <assert.h>
348 #include <stdio.h>
55f48c48 349 ]AT_YYERROR_DECLARE])[
d2060f06 350 ]AT_YYLEX_PROTOTYPE[;
df222dfa
JD
351 #define USE(Var)
352}
353
d2060f06
JD
354]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
355
25a648d8
JD
356]$1[
357
df222dfa
JD
358%define parse.error verbose
359
25a648d8
JD
360%%
361
362]$2[
363
d2060f06 364]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
25a648d8 365
d2060f06
JD
366/*--------.
367| yylex. |
368`--------*/]AT_SKEL_JAVA_IF([[
369
370public String input = "]$3[";
371public int index = 0;
372public int yylex ()
373{
374 if (index < input.length ())
375 return input.charAt (index++);
376 else
377 return 0;
378}
379public Object getLVal ()
380{
381 return new Integer(1);
382}]], [[
383
384]AT_YYLEX_PROTOTYPE[
25a648d8
JD
385{
386 static char const *input = "]$3[";
d2060f06 387 *lvalp = 1;
25a648d8 388 return *input++;
d2060f06 389}]])[
55f48c48
AD
390]AT_YYERROR_DEFINE[
391]AT_SKEL_JAVA_IF([[
d2060f06
JD
392};
393
55f48c48 394%%]])[
d2060f06
JD
395
396/*-------.
397| main. |
3ef9fa8f
AD
398`-------*/
399]AT_MAIN_DEFINE
400])
d2060f06
JD
401
402AT_FULL_COMPILE([[input]])
25a648d8
JD
403
404m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
405 $5, [a], [[, expecting 'a']],
406 $5, [b], [[, expecting 'b']])])
df222dfa 407
d2060f06
JD
408AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
409 [AT_PARSER_CHECK([[./input]], [[1]]]),
410[[]],
25a648d8
JD
411[[syntax error, unexpected ]$4[]AT_EXPECTING[
412]])
413
414m4_popdef([AT_EXPECTING])
d2060f06
JD
415m4_popdef([AT_YYLEX_PROTOTYPE])
416AT_BISON_OPTION_POPDEFS
25a648d8
JD
417
418])
419
d2060f06
JD
420m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
421[[%nonassoc 'a';
422
423start: consistent-error-on-a-a 'a' ;
424
425consistent-error-on-a-a:
426 'a' default-reduction
427 | 'a' default-reduction 'a'
428 | 'a' shift
429 ;
430
431default-reduction: /*empty*/ ;
432shift: 'b' ;
433
434// Provide another context in which all rules are useful so that this
435// test case looks a little more realistic.
436start: 'b' consistent-error-on-a-a 'c' ;
437]])
438
439m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
440
441# Unfortunately, no expected tokens are reported even though 'b' can be
442# accepted. Nevertheless, the main point of this test is to make sure
443# that at least the unexpected token is reported. In a previous version
444# of Bison, it wasn't reported because the error is detected in a
445# consistent state with an error action, and that case always triggered
446# the simple "syntax error" message.
447#
448# The point isn't to test IELR here, but state merging happens to
449# complicate this example.
450AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
451 [AT_PREVIOUS_STATE_GRAMMAR],
452 [AT_PREVIOUS_STATE_INPUT],
453 [[$end]], [[none]])
454AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
455 %glr-parser]],
456 [AT_PREVIOUS_STATE_GRAMMAR],
457 [AT_PREVIOUS_STATE_INPUT],
458 [[$end]], [[none]])
459AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
460 %language "c++"]],
461 [AT_PREVIOUS_STATE_GRAMMAR],
462 [AT_PREVIOUS_STATE_INPUT],
463 [[$end]], [[none]])
464AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
465 %language "java"]],
466 [AT_PREVIOUS_STATE_GRAMMAR],
467 [AT_PREVIOUS_STATE_INPUT],
468 [[end of input]], [[none]])
469
470# Even canonical LR doesn't foresee the error for 'a'!
471AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
f3bc3386 472 %define lr.default-reduction consistent]],
d2060f06
JD
473 [AT_PREVIOUS_STATE_GRAMMAR],
474 [AT_PREVIOUS_STATE_INPUT],
475 [[$end]], [[ab]])
476AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
f3bc3386 477 %define lr.default-reduction accepting]],
d2060f06
JD
478 [AT_PREVIOUS_STATE_GRAMMAR],
479 [AT_PREVIOUS_STATE_INPUT],
480 [[$end]], [[ab]])
481AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
482 [AT_PREVIOUS_STATE_GRAMMAR],
483 [AT_PREVIOUS_STATE_INPUT],
484 [[$end]], [[ab]])
485
bf35c71c
JD
486# Only LAC gets it right.
487AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr
488 %define parse.lac full]],
489 [AT_PREVIOUS_STATE_GRAMMAR],
490 [AT_PREVIOUS_STATE_INPUT],
491 [[$end]], [[b]])
492AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
493 %define parse.lac full]],
494 [AT_PREVIOUS_STATE_GRAMMAR],
495 [AT_PREVIOUS_STATE_INPUT],
496 [[$end]], [[b]])
497
d2060f06
JD
498m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
499m4_popdef([AT_PREVIOUS_STATE_INPUT])
500
25a648d8
JD
501m4_pushdef([AT_USER_ACTION_GRAMMAR],
502[[%nonassoc 'a';
df222dfa 503
d2060f06
JD
504// If $$ = 0 here, then we know that the 'a' destructor is being invoked
505// incorrectly for the 'b' set in the semantic action below. All 'a'
506// tokens are returned by yylex, which sets $$ = 1.
df222dfa
JD
507%destructor {
508 if (!$$)
509 fprintf (stderr, "Wrong destructor.\n");
25a648d8 510} 'a';
df222dfa 511
d2060f06
JD
512// Rather than depend on an inconsistent state to induce reading a
513// lookahead as in the previous grammar, just assign the lookahead in a
514// semantic action. That lookahead isn't needed before either error
515// action is encountered. In a previous version of Bison, this was a
516// problem as it meant yychar was not translated into yytoken before
517// either error action. The second error action thus invoked a
df222dfa
JD
518// destructor that it selected according to the incorrect yytoken. The
519// first error action would have reported an incorrect unexpected token
d2060f06
JD
520// except that, due to the bug described in the previous grammar, the
521// unexpected token was not reported at all.
25a648d8 522start: error-reduce consistent-error 'a' { USE ($][3); } ;
df222dfa
JD
523
524error-reduce:
525 'a' 'a' consistent-reduction consistent-error 'a'
25a648d8 526 { USE (($][1, $][2, $][5)); }
df222dfa 527| 'a' error
25a648d8 528 { USE ($][1); }
df222dfa
JD
529;
530
531consistent-reduction: /*empty*/ {
532 assert (yychar == YYEMPTY);
533 yylval = 0;
534 yychar = 'b';
535} ;
536
537consistent-error:
25a648d8 538 'a' { USE ($][1); }
df222dfa
JD
539| /*empty*/ %prec 'a'
540;
541
542// Provide another context in which all rules are useful so that this
543// test case looks a little more realistic.
544start: 'b' consistent-error 'b' ;
df222dfa 545]])
25a648d8 546m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
df222dfa 547
25a648d8
JD
548AT_CONSISTENT_ERRORS_CHECK([[]],
549 [AT_USER_ACTION_GRAMMAR],
550 [AT_USER_ACTION_INPUT],
551 [['b']], [[none]])
d2060f06
JD
552AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
553 [AT_USER_ACTION_GRAMMAR],
554 [AT_USER_ACTION_INPUT],
555 [['b']], [[none]])
556# No C++ or Java test because yychar cannot be manipulated by users.
557
f3bc3386 558AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction consistent]],
25a648d8
JD
559 [AT_USER_ACTION_GRAMMAR],
560 [AT_USER_ACTION_INPUT],
df222dfa
JD
561 [['b']], [[none]])
562
563# Canonical LR doesn't foresee the error for 'a'!
f3bc3386 564AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reduction accepting]],
25a648d8
JD
565 [AT_USER_ACTION_GRAMMAR],
566 [AT_USER_ACTION_INPUT],
df222dfa 567 [[$end]], [[a]])
25a648d8
JD
568AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
569 [AT_USER_ACTION_GRAMMAR],
570 [AT_USER_ACTION_INPUT],
571 [[$end]], [[a]])
572
bf35c71c
JD
573AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full]],
574 [AT_USER_ACTION_GRAMMAR],
575 [AT_USER_ACTION_INPUT],
576 [['b']], [[none]])
577AT_CONSISTENT_ERRORS_CHECK([[%define parse.lac full
f3bc3386 578 %define lr.default-reduction accepting]],
bf35c71c
JD
579 [AT_USER_ACTION_GRAMMAR],
580 [AT_USER_ACTION_INPUT],
581 [[$end]], [[none]])
582
25a648d8
JD
583m4_popdef([AT_USER_ACTION_GRAMMAR])
584m4_popdef([AT_USER_ACTION_INPUT])
df222dfa
JD
585
586m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
587
588AT_CLEANUP
589
590
591
bf35c71c
JD
592## ------------------------------------------------------- ##
593## LAC: %nonassoc requires splitting canonical LR states. ##
594## ------------------------------------------------------- ##
595
596# This test case demonstrates that, when %nonassoc is used, canonical
597# LR(1) parser table construction followed by conflict resolution
598# without further state splitting is not always sufficient to produce a
599# parser that can detect all syntax errors as soon as possible on one
600# token of lookahead. However, LAC solves the problem completely even
601# with minimal LR parser tables.
602
603AT_SETUP([[LAC: %nonassoc requires splitting canonical LR states]])
55f48c48 604AT_BISON_OPTION_PUSHDEFS
bf35c71c
JD
605AT_DATA_GRAMMAR([[input.y]],
606[[%code {
607 #include <stdio.h>
55f48c48
AD
608 ]AT_YYERROR_DECLARE[
609 ]AT_YYLEX_DECLARE[
bf35c71c
JD
610}
611
612%error-verbose
613%nonassoc 'a'
614
615%%
616
617start:
618 'a' problem 'a' // First context.
619| 'b' problem 'b' // Second context.
620| 'c' reduce-nonassoc // Just makes reduce-nonassoc useful.
621;
622
623problem:
624 look reduce-nonassoc
625| look 'a'
626| look 'b'
627;
628
629// For the state reached after shifting the 'a' in these productions,
630// lookahead sets are the same in both the first and second contexts.
631// Thus, canonical LR reuses the same state for both contexts. However,
632// the lookahead 'a' for the reduction "look: 'a'" later becomes an
633// error action only in the first context. In order to immediately
634// detect the syntax error on 'a' here for only the first context, this
635// canonical LR state would have to be split into two states, and the
636// 'a' lookahead would have to be removed from only one of the states.
637look:
638 'a' // Reduction lookahead set is always ['a', 'b'].
639| 'a' 'b'
640| 'a' 'c' // 'c' is forgotten as an expected token.
641;
642
643reduce-nonassoc: %prec 'a';
644
645%%
55f48c48 646]AT_YYERROR_DEFINE[
95361618 647]AT_YYLEX_DEFINE(["aaa"])[
3ef9fa8f
AD
648]AT_MAIN_DEFINE
649])
55f48c48 650AT_BISON_OPTION_POPDEFS
bf35c71c
JD
651
652# Show canonical LR's failure.
653AT_BISON_CHECK([[-Dlr.type=canonical-lr -o input.c input.y]],
654 [[0]], [[]],
d87ea54c 655[[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
bf35c71c
JD
656]])
657AT_COMPILE([[input]])
658AT_PARSER_CHECK([[./input]], [[1]], [[]],
659[[syntax error, unexpected 'a', expecting 'b'
660]])
661
662# It's corrected by LAC.
663AT_BISON_CHECK([[-Dlr.type=canonical-lr -Dparse.lac=full \
664 -o input.c input.y]], [[0]], [[]],
d87ea54c 665[[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
bf35c71c
JD
666]])
667AT_COMPILE([[input]])
668AT_PARSER_CHECK([[./input]], [[1]], [[]],
669[[syntax error, unexpected 'a', expecting 'b' or 'c'
670]])
671
672# IELR is sufficient when LAC is used.
673AT_BISON_CHECK([[-Dlr.type=ielr -Dparse.lac=full -o input.c input.y]],
674 [[0]], [[]],
d87ea54c 675[[input.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
bf35c71c
JD
676]])
677AT_COMPILE([[input]])
678AT_PARSER_CHECK([[./input]], [[1]], [[]],
679[[syntax error, unexpected 'a', expecting 'b' or 'c'
680]])
681
682AT_CLEANUP
683
3c31a486
AD
684## ------------------------- ##
685## Unresolved SR Conflicts. ##
686## ------------------------- ##
687
688AT_SETUP([Unresolved SR Conflicts])
689
6b98e4b5
AD
690AT_KEYWORDS([report])
691
3c31a486
AD
692AT_DATA([input.y],
693[[%token NUM OP
694%%
695exp: exp OP exp | NUM;
696]])
697
da730230 698AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
d87ea54c
AD
699[[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
700]])
3c31a486
AD
701
702# Check the contents of the report.
703AT_CHECK([cat input.output], [],
2c8ba4cd 704[[State 5 conflicts: 1 shift/reduce
3c31a486
AD
705
706
707Grammar
708
88bce5a2 709 0 $accept: exp $end
6b98e4b5
AD
710
711 1 exp: exp OP exp
712 2 | NUM
3c31a486
AD
713
714
715Terminals, with rules where they appear
716
88bce5a2 717$end (0) 0
3c31a486 718error (256)
007a50a4
AD
719NUM (258) 2
720OP (259) 1
3c31a486
AD
721
722
723Nonterminals, with rules where they appear
724
88bce5a2 725$accept (5)
3c31a486
AD
726 on left: 0
727exp (6)
728 on left: 1 2, on right: 0 1
729
730
d42fe46e 731State 0
3c31a486 732
88bce5a2 733 0 $accept: . exp $end
ce4ccb4b
AD
734 1 exp: . exp OP exp
735 2 | . NUM
643a5994 736
87675353 737 NUM shift, and go to state 1
3c31a486 738
87675353 739 exp go to state 2
3c31a486
AD
740
741
d42fe46e 742State 1
3c31a486 743
ce4ccb4b 744 2 exp: NUM .
3c31a486 745
87675353 746 $default reduce using rule 2 (exp)
3c31a486
AD
747
748
d42fe46e 749State 2
3c31a486 750
88bce5a2 751 0 $accept: exp . $end
ce4ccb4b 752 1 exp: exp . OP exp
3c31a486 753
88bce5a2
AD
754 $end shift, and go to state 3
755 OP shift, and go to state 4
3c31a486
AD
756
757
d42fe46e 758State 3
3c31a486 759
88bce5a2 760 0 $accept: exp $end .
3c31a486 761
e8832397 762 $default accept
3c31a486
AD
763
764
d42fe46e 765State 4
3c31a486 766
ce4ccb4b
AD
767 1 exp: . exp OP exp
768 1 | exp OP . exp
769 2 | . NUM
3c31a486 770
87675353 771 NUM shift, and go to state 1
3c31a486 772
87675353 773 exp go to state 5
3c31a486
AD
774
775
d42fe46e 776State 5
3c31a486 777
a0de5091 778 1 exp: exp . OP exp
88bce5a2 779 1 | exp OP exp . [$end, OP]
3c31a486 780
87675353 781 OP shift, and go to state 4
3c31a486 782
87675353
AD
783 OP [reduce using rule 1 (exp)]
784 $default reduce using rule 1 (exp)
3c31a486
AD
785]])
786
787AT_CLEANUP
788
789
3c31a486 790
ce4ccb4b
AD
791## ----------------------- ##
792## Resolved SR Conflicts. ##
793## ----------------------- ##
794
795AT_SETUP([Resolved SR Conflicts])
3c31a486 796
6b98e4b5
AD
797AT_KEYWORDS([report])
798
3c31a486
AD
799AT_DATA([input.y],
800[[%token NUM OP
ce4ccb4b 801%left OP
3c31a486
AD
802%%
803exp: exp OP exp | NUM;
804]])
805
da730230 806AT_BISON_CHECK([-o input.c --report=all input.y])
3c31a486
AD
807
808# Check the contents of the report.
809AT_CHECK([cat input.output], [],
ce4ccb4b 810[[Grammar
3c31a486 811
88bce5a2 812 0 $accept: exp $end
6b98e4b5
AD
813
814 1 exp: exp OP exp
815 2 | NUM
3c31a486
AD
816
817
818Terminals, with rules where they appear
819
88bce5a2 820$end (0) 0
3c31a486 821error (256)
007a50a4
AD
822NUM (258) 2
823OP (259) 1
3c31a486
AD
824
825
826Nonterminals, with rules where they appear
827
88bce5a2 828$accept (5)
3c31a486
AD
829 on left: 0
830exp (6)
831 on left: 1 2, on right: 0 1
832
833
d42fe46e 834State 0
3c31a486 835
88bce5a2 836 0 $accept: . exp $end
ce4ccb4b
AD
837 1 exp: . exp OP exp
838 2 | . NUM
643a5994 839
87675353 840 NUM shift, and go to state 1
3c31a486 841
87675353 842 exp go to state 2
3c31a486
AD
843
844
d42fe46e 845State 1
3c31a486 846
ce4ccb4b 847 2 exp: NUM .
3c31a486 848
87675353 849 $default reduce using rule 2 (exp)
3c31a486
AD
850
851
d42fe46e 852State 2
3c31a486 853
88bce5a2 854 0 $accept: exp . $end
ce4ccb4b 855 1 exp: exp . OP exp
3c31a486 856
88bce5a2
AD
857 $end shift, and go to state 3
858 OP shift, and go to state 4
3c31a486
AD
859
860
d42fe46e 861State 3
3c31a486 862
88bce5a2 863 0 $accept: exp $end .
3c31a486 864
e8832397 865 $default accept
3c31a486
AD
866
867
d42fe46e 868State 4
3c31a486 869
ce4ccb4b
AD
870 1 exp: . exp OP exp
871 1 | exp OP . exp
872 2 | . NUM
3c31a486 873
87675353 874 NUM shift, and go to state 1
3c31a486 875
87675353 876 exp go to state 5
3c31a486
AD
877
878
d42fe46e 879State 5
3c31a486 880
a0de5091 881 1 exp: exp . OP exp
88bce5a2 882 1 | exp OP exp . [$end, OP]
3c31a486 883
87675353 884 $default reduce using rule 1 (exp)
7ea9a33f 885
4b3d3a8e 886 Conflict between rule 1 and token OP resolved as reduce (%left OP).
bc933ef1
AD
887]])
888
889AT_CLEANUP
890
891
d78f0ac9
AD
892## ---------------------- ##
893## %precedence suffices. ##
894## ---------------------- ##
895
896AT_SETUP([%precedence suffices])
897
898AT_DATA([input.y],
899[[%precedence "then"
900%precedence "else"
901%%
902stmt:
903 "if" cond "then" stmt
904| "if" cond "then" stmt "else" stmt
905| "stmt"
906;
907
908cond:
909 "exp"
910;
911]])
912
913AT_BISON_CHECK([-o input.c input.y])
914
915AT_CLEANUP
916
917
918## ------------------------------ ##
919## %precedence does not suffice. ##
920## ------------------------------ ##
921
922AT_SETUP([%precedence does not suffice])
923
924AT_DATA([input.y],
925[[%precedence "then"
926%precedence "else"
927%%
928stmt:
929 "if" cond "then" stmt
930| "if" cond "then" stmt "else" stmt
931| "stmt"
932;
933
934cond:
935 "exp"
936| cond "then" cond
937;
938]])
939
940AT_BISON_CHECK([-o input.c input.y], 0, [],
d87ea54c 941[[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
73370a9d 942input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond [-Wother]
d78f0ac9
AD
943]])
944
945AT_CLEANUP
946
947
bc933ef1
AD
948## -------------------------------- ##
949## Defaulted Conflicted Reduction. ##
950## -------------------------------- ##
951
952# When there are RR conflicts, some rules are disabled. Usually it is
953# simply displayed as:
954#
88bce5a2
AD
955# $end reduce using rule 3 (num)
956# $end [reduce using rule 4 (id)]
bc933ef1
AD
957#
958# But when `reduce 3' is the default action, we'd produce:
959#
88bce5a2 960# $end [reduce using rule 4 (id)]
bc933ef1
AD
961# $default reduce using rule 3 (num)
962#
963# In this precise case (a reduction is masked by the default
964# reduction), we make the `reduce 3' explicit:
965#
88bce5a2
AD
966# $end reduce using rule 3 (num)
967# $end [reduce using rule 4 (id)]
bc933ef1
AD
968# $default reduce using rule 3 (num)
969#
970# Maybe that's not the best display, but then, please propose something
971# else.
972
973AT_SETUP([Defaulted Conflicted Reduction])
974AT_KEYWORDS([report])
975
976AT_DATA([input.y],
977[[%%
978exp: num | id;
979num: '0';
980id : '0';
981%%
982]])
983
da730230 984AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
d87ea54c 985[[input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
73370a9d 986input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0' [-Wother]
e8832397 987]])
bc933ef1
AD
988
989# Check the contents of the report.
990AT_CHECK([cat input.output], [],
cff03fb2 991[[Rules useless in parser due to conflicts
c8f002c7
AD
992
993 4 id: '0'
994
995
2c8ba4cd 996State 1 conflicts: 1 reduce/reduce
bc933ef1
AD
997
998
999Grammar
1000
88bce5a2 1001 0 $accept: exp $end
bc933ef1
AD
1002
1003 1 exp: num
1004 2 | id
1005
1006 3 num: '0'
1007
1008 4 id: '0'
1009
1010
1011Terminals, with rules where they appear
1012
88bce5a2 1013$end (0) 0
bc933ef1
AD
1014'0' (48) 3 4
1015error (256)
1016
1017
1018Nonterminals, with rules where they appear
1019
88bce5a2 1020$accept (4)
bc933ef1
AD
1021 on left: 0
1022exp (5)
1023 on left: 1 2, on right: 0
1024num (6)
1025 on left: 3, on right: 1
1026id (7)
1027 on left: 4, on right: 2
1028
1029
d42fe46e 1030State 0
bc933ef1 1031
88bce5a2 1032 0 $accept: . exp $end
ce4ccb4b
AD
1033 1 exp: . num
1034 2 | . id
1035 3 num: . '0'
1036 4 id: . '0'
bc933ef1 1037
87675353 1038 '0' shift, and go to state 1
bc933ef1 1039
87675353
AD
1040 exp go to state 2
1041 num go to state 3
1042 id go to state 4
bc933ef1
AD
1043
1044
d42fe46e 1045State 1
bc933ef1 1046
88bce5a2
AD
1047 3 num: '0' . [$end]
1048 4 id: '0' . [$end]
bc933ef1 1049
88bce5a2
AD
1050 $end reduce using rule 3 (num)
1051 $end [reduce using rule 4 (id)]
87675353 1052 $default reduce using rule 3 (num)
bc933ef1
AD
1053
1054
d42fe46e 1055State 2
bc933ef1 1056
88bce5a2 1057 0 $accept: exp . $end
bc933ef1 1058
88bce5a2 1059 $end shift, and go to state 5
bc933ef1
AD
1060
1061
d42fe46e 1062State 3
bc933ef1 1063
ce4ccb4b 1064 1 exp: num .
bc933ef1 1065
87675353 1066 $default reduce using rule 1 (exp)
bc933ef1
AD
1067
1068
d42fe46e 1069State 4
bc933ef1 1070
ce4ccb4b 1071 2 exp: id .
bc933ef1 1072
87675353 1073 $default reduce using rule 2 (exp)
bc933ef1
AD
1074
1075
d42fe46e 1076State 5
bc933ef1 1077
88bce5a2 1078 0 $accept: exp $end .
bc933ef1 1079
e8832397 1080 $default accept
3c31a486
AD
1081]])
1082
1083AT_CLEANUP
1084
1085
1086
1087
1088## -------------------- ##
1089## %expect not enough. ##
1090## -------------------- ##
1091
1092AT_SETUP([%expect not enough])
1093
1094AT_DATA([input.y],
1095[[%token NUM OP
1096%expect 0
1097%%
1098exp: exp OP exp | NUM;
1099]])
1100
da730230 1101AT_BISON_CHECK([-o input.c input.y], 1, [],
11b19212 1102[[input.y: error: shift/reduce conflicts: 1 found, 0 expected
d87ea54c 1103]])
3c31a486
AD
1104AT_CLEANUP
1105
1106
1107## --------------- ##
1108## %expect right. ##
1109## --------------- ##
1110
1111AT_SETUP([%expect right])
1112
1113AT_DATA([input.y],
1114[[%token NUM OP
1115%expect 1
1116%%
1117exp: exp OP exp | NUM;
1118]])
1119
da730230 1120AT_BISON_CHECK([-o input.c input.y])
3c31a486
AD
1121AT_CLEANUP
1122
1123
1124## ------------------ ##
1125## %expect too much. ##
1126## ------------------ ##
1127
1128AT_SETUP([%expect too much])
1129
1130AT_DATA([input.y],
1131[[%token NUM OP
1132%expect 2
1133%%
1134exp: exp OP exp | NUM;
1135]])
1136
da730230 1137AT_BISON_CHECK([-o input.c input.y], 1, [],
11b19212 1138[[input.y: error: shift/reduce conflicts: 1 found, 2 expected
d87ea54c 1139]])
3c31a486 1140AT_CLEANUP
6876ecd3
PE
1141
1142
41976786
AD
1143## ------------------------------- ##
1144## %expect with reduce conflicts. ##
1145## ------------------------------- ##
6876ecd3
PE
1146
1147AT_SETUP([%expect with reduce conflicts])
1148
1149AT_DATA([input.y],
1150[[%expect 0
1151%%
1152program: a 'a' | a a;
1153a: 'a';
1154]])
1155
da730230 1156AT_BISON_CHECK([-o input.c input.y], 1, [],
11b19212 1157[[input.y: error: reduce/reduce conflicts: 1 found, 0 expected
d87ea54c 1158]])
6876ecd3 1159AT_CLEANUP
39a06c25
PE
1160
1161
44bb9084
AD
1162## ------------------------- ##
1163## %prec with user strings. ##
1164## ------------------------- ##
1165
1166AT_SETUP([%prec with user string])
1167
1168AT_DATA([[input.y]],
1169[[%%
1170exp:
1171 "foo" %prec "foo"
1172;
1173]])
1174
1175AT_BISON_CHECK([-o input.c input.y])
1176AT_CLEANUP
1177
1178
1179## -------------------------------- ##
1180## %no-default-prec without %prec. ##
1181## -------------------------------- ##
39a06c25 1182
22fccf95 1183AT_SETUP([%no-default-prec without %prec])
39a06c25
PE
1184
1185AT_DATA([[input.y]],
1186[[%left '+'
1187%left '*'
1188
1189%%
1190
22fccf95 1191%no-default-prec;
39a06c25
PE
1192
1193e: e '+' e
1194 | e '*' e
1195 | '0'
1196 ;
1197]])
1198
da730230 1199AT_BISON_CHECK([-o input.c input.y], 0, [],
d87ea54c 1200[[input.y: warning: 4 shift/reduce conflicts [-Wconflicts-sr]
e8f7155d
VT
1201input.y:1.7-9: warning: useless associativity for '+' [-Wother]
1202input.y:2.7-9: warning: useless associativity for '*' [-Wother]
39a06c25
PE
1203]])
1204AT_CLEANUP
1205
1206
41976786
AD
1207## ----------------------------- ##
1208## %no-default-prec with %prec. ##
1209## ----------------------------- ##
39a06c25 1210
22fccf95 1211AT_SETUP([%no-default-prec with %prec])
39a06c25
PE
1212
1213AT_DATA([[input.y]],
1214[[%left '+'
1215%left '*'
1216
1217%%
1218
22fccf95 1219%no-default-prec;
39a06c25
PE
1220
1221e: e '+' e %prec '+'
1222 | e '*' e %prec '*'
1223 | '0'
1224 ;
1225]])
1226
da730230 1227AT_BISON_CHECK([-o input.c input.y])
39a06c25
PE
1228AT_CLEANUP
1229
1230
41976786
AD
1231## --------------- ##
1232## %default-prec. ##
1233## --------------- ##
39a06c25 1234
22fccf95 1235AT_SETUP([%default-prec])
39a06c25
PE
1236
1237AT_DATA([[input.y]],
1238[[%left '+'
1239%left '*'
1240
1241%%
1242
22fccf95 1243%default-prec;
39a06c25
PE
1244
1245e: e '+' e
1246 | e '*' e
1247 | '0'
1248 ;
1249]])
1250
da730230 1251AT_BISON_CHECK([-o input.c input.y])
39a06c25 1252AT_CLEANUP
5967f0cf
JD
1253
1254
1255## ---------------------------------------------- ##
1256## Unreachable States After Conflict Resolution. ##
1257## ---------------------------------------------- ##
1258
1259AT_SETUP([[Unreachable States After Conflict Resolution]])
1260
1261# If conflict resolution makes states unreachable, remove those states, report
1262# rules that are then unused, and don't report conflicts in those states. Test
1263# what happens when a nonterminal becomes useless as a result of state removal
1264# since that causes lalr.o's goto map to be rewritten.
1265
1266AT_DATA([[input.y]],
1267[[%output "input.c"
1268%left 'a'
1269
1270%%
1271
1272start: resolved_conflict 'a' reported_conflicts 'a' ;
1273
31984206 1274/* S/R conflict resolved as reduce, so the state with item
5967f0cf
JD
1275 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
1276 * unreachable, and the associated production is useless. */
1277resolved_conflict:
1278 'a' unreachable1
1279 | %prec 'a'
1280 ;
1281
1282/* S/R conflict that need not be reported since it is unreachable because of
1283 * the previous conflict resolution. Nonterminal unreachable1 and all its
1284 * productions are useless. */
1285unreachable1:
1286 'a' unreachable2
1287 |
1288 ;
1289
1290/* Likewise for a R/R conflict and nonterminal unreachable2. */
1291unreachable2: | ;
1292
1293/* Make sure remaining S/R and R/R conflicts are still reported correctly even
1294 * when their states are renumbered due to state removal. */
1295reported_conflicts:
1296 'a'
1297 | 'a'
1298 |
1299 ;
1300
1301]])
1302
da730230 1303AT_BISON_CHECK([[--report=all input.y]], 0, [],
d87ea54c
AD
1304[[input.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1305input.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
73370a9d
VS
1306input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1 [-Wother]
1307input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2 [-Wother]
1308input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1309input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1310input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1311input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1312input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
5967f0cf
JD
1313]])
1314
1315AT_CHECK([[cat input.output]], 0,
cff03fb2 1316[[Rules useless in parser due to conflicts
5967f0cf
JD
1317
1318 2 resolved_conflict: 'a' unreachable1
1319
1320 4 unreachable1: 'a' unreachable2
1321 5 | /* empty */
1322
1323 6 unreachable2: /* empty */
1324 7 | /* empty */
1325
1326 9 reported_conflicts: 'a'
1327 10 | /* empty */
1328
1329
1330State 4 conflicts: 1 shift/reduce
1331State 5 conflicts: 1 reduce/reduce
1332
1333
1334Grammar
1335
1336 0 $accept: start $end
1337
1338 1 start: resolved_conflict 'a' reported_conflicts 'a'
1339
1340 2 resolved_conflict: 'a' unreachable1
1341 3 | /* empty */
1342
1343 4 unreachable1: 'a' unreachable2
1344 5 | /* empty */
1345
1346 6 unreachable2: /* empty */
1347 7 | /* empty */
1348
1349 8 reported_conflicts: 'a'
1350 9 | 'a'
1351 10 | /* empty */
1352
1353
1354Terminals, with rules where they appear
1355
1356$end (0) 0
1357'a' (97) 1 2 4 8 9
1358error (256)
1359
1360
1361Nonterminals, with rules where they appear
1362
1363$accept (4)
1364 on left: 0
1365start (5)
1366 on left: 1, on right: 0
1367resolved_conflict (6)
1368 on left: 2 3, on right: 1
1369unreachable1 (7)
1370 on left: 4 5, on right: 2
1371unreachable2 (8)
1372 on left: 6 7, on right: 4
1373reported_conflicts (9)
1374 on left: 8 9 10, on right: 1
1375
1376
d42fe46e 1377State 0
5967f0cf
JD
1378
1379 0 $accept: . start $end
1380 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1381 2 resolved_conflict: . 'a' unreachable1
1382 3 | . ['a']
1383
1384 $default reduce using rule 3 (resolved_conflict)
1385
1386 start go to state 1
1387 resolved_conflict go to state 2
1388
1389 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1390
1391
d42fe46e 1392State 1
5967f0cf
JD
1393
1394 0 $accept: start . $end
1395
1396 $end shift, and go to state 3
1397
1398
d42fe46e 1399State 2
5967f0cf
JD
1400
1401 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1402
1403 'a' shift, and go to state 4
1404
1405
d42fe46e 1406State 3
5967f0cf
JD
1407
1408 0 $accept: start $end .
1409
1410 $default accept
1411
1412
d42fe46e 1413State 4
5967f0cf
JD
1414
1415 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1416 8 reported_conflicts: . 'a'
1417 9 | . 'a'
1418 10 | . ['a']
1419
1420 'a' shift, and go to state 5
1421
1422 'a' [reduce using rule 10 (reported_conflicts)]
1423
1424 reported_conflicts go to state 6
1425
1426
d42fe46e 1427State 5
5967f0cf
JD
1428
1429 8 reported_conflicts: 'a' . ['a']
1430 9 | 'a' . ['a']
1431
1432 'a' reduce using rule 8 (reported_conflicts)
1433 'a' [reduce using rule 9 (reported_conflicts)]
1434 $default reduce using rule 8 (reported_conflicts)
1435
1436
d42fe46e 1437State 6
5967f0cf
JD
1438
1439 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1440
1441 'a' shift, and go to state 7
1442
1443
d42fe46e 1444State 7
5967f0cf
JD
1445
1446 1 start: resolved_conflict 'a' reported_conflicts 'a' .
9d774aff 1447
5967f0cf
JD
1448 $default reduce using rule 1 (start)
1449]])
1450
31984206 1451AT_DATA([[input-keep.y]],
f3bc3386 1452[[%define lr.keep-unreachable-state
31984206
JD
1453]])
1454AT_CHECK([[cat input.y >> input-keep.y]])
1455
da730230 1456AT_BISON_CHECK([[input-keep.y]], 0, [],
d87ea54c
AD
1457[[input-keep.y: warning: 2 shift/reduce conflicts [-Wconflicts-sr]
1458input-keep.y: warning: 2 reduce/reduce conflicts [-Wconflicts-rr]
73370a9d
VS
1459input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */ [-Wother]
1460input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */ [-Wother]
1461input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a' [-Wother]
1462input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */ [-Wother]
31984206
JD
1463]])
1464
5967f0cf 1465AT_CLEANUP
9d774aff
JD
1466
1467
1468## ------------------------------------------------------------ ##
1469## Solved conflicts report for multiple reductions in a state. ##
1470## ------------------------------------------------------------ ##
1471
1472AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1473
1474# Used to lose earlier solved conflict messages even within a single S/R/R.
1475
1476AT_DATA([[input.y]],
1477[[%left 'a'
1478%right 'b'
1479%right 'c'
1480%right 'd'
1481%%
1482start:
1483 'a'
1484 | empty_a 'a'
1485 | 'b'
1486 | empty_b 'b'
1487 | 'c'
1488 | empty_c1 'c'
1489 | empty_c2 'c'
1490 | empty_c3 'c'
1491 ;
1492empty_a: %prec 'a' ;
1493empty_b: %prec 'b' ;
1494empty_c1: %prec 'c' ;
1495empty_c2: %prec 'c' ;
1496empty_c3: %prec 'd' ;
1497]])
da730230 1498AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
d42fe46e
TR
1499AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1500[[State 0
9d774aff
JD
1501
1502 0 $accept: . start $end
1503 1 start: . 'a'
1504 2 | . empty_a 'a'
1505 3 | . 'b'
1506 4 | . empty_b 'b'
1507 5 | . 'c'
1508 6 | . empty_c1 'c'
1509 7 | . empty_c2 'c'
1510 8 | . empty_c3 'c'
1511 9 empty_a: . ['a']
1512 10 empty_b: . []
1513 11 empty_c1: . []
1514 12 empty_c2: . []
1515 13 empty_c3: . ['c']
1516
1517 'b' shift, and go to state 1
d78f0ac9 1518
9d774aff
JD
1519 'c' reduce using rule 13 (empty_c3)
1520 $default reduce using rule 9 (empty_a)
1521
1522 start go to state 2
1523 empty_a go to state 3
1524 empty_b go to state 4
1525 empty_c1 go to state 5
1526 empty_c2 go to state 6
1527 empty_c3 go to state 7
1528
1529 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1530 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1531 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1532 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1533 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1534
1535
d42fe46e 1536State 1
9d774aff
JD
1537]])
1538
1539AT_CLEANUP
1540
1541
1542## ------------------------------------------------------------ ##
1543## %nonassoc error actions for multiple reductions in a state. ##
1544## ------------------------------------------------------------ ##
1545
1546# Used to abort when trying to resolve conflicts as %nonassoc error actions for
1547# multiple reductions in a state.
1548
1549# For a %nonassoc error action token, used to print the first remaining
1550# reduction on that token without brackets.
1551
1552AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1553
1554AT_DATA([[input.y]],
1555[[%nonassoc 'a' 'b' 'c'
1556%%
1557start:
1558 'a'
1559 | empty_a 'a'
1560 | 'b'
1561 | empty_b 'b'
1562 | 'c'
1563 | empty_c1 'c'
1564 | empty_c2 'c'
1565 | empty_c3 'c'
1566 ;
1567empty_a: %prec 'a' ;
1568empty_b: %prec 'b' ;
1569empty_c1: %prec 'c' ;
1570empty_c2: %prec 'c' ;
1571empty_c3: %prec 'c' ;
1572]])
1573
da730230 1574AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
d42fe46e
TR
1575AT_CHECK([[cat input.output | sed -n '/^State 0$/,/^State 1$/p']], 0,
1576[[State 0
9d774aff
JD
1577
1578 0 $accept: . start $end
1579 1 start: . 'a'
1580 2 | . empty_a 'a'
1581 3 | . 'b'
1582 4 | . empty_b 'b'
1583 5 | . 'c'
1584 6 | . empty_c1 'c'
1585 7 | . empty_c2 'c'
1586 8 | . empty_c3 'c'
1587 9 empty_a: . []
1588 10 empty_b: . []
1589 11 empty_c1: . []
1590 12 empty_c2: . ['c']
1591 13 empty_c3: . ['c']
1592
1593 'a' error (nonassociative)
1594 'b' error (nonassociative)
1595 'c' error (nonassociative)
1596
1597 'c' [reduce using rule 12 (empty_c2)]
1598 'c' [reduce using rule 13 (empty_c3)]
1599
1600 start go to state 1
1601 empty_a go to state 2
1602 empty_b go to state 3
1603 empty_c1 go to state 4
1604 empty_c2 go to state 5
1605 empty_c3 go to state 6
1606
1607 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1608 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1609 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1610
1611
d42fe46e 1612State 1
9d774aff
JD
1613]])
1614AT_CLEANUP
786743d5
JD
1615
1616
d1400569
AD
1617## -------------------- ##
1618## %expect-rr non GLR. ##
1619## -------------------- ##
1620
1621AT_SETUP([[%expect-rr non GLR]])
1622
1623AT_DATA([[1.y]],
1624[[%expect-rr 0
1625%%
1626exp: 'a'
1627]])
1628
1629AT_BISON_CHECK([[1.y]], [[0]], [],
1630[[1.y: warning: %expect-rr applies only to GLR parsers [-Wother]
1631]])
1632
1633AT_DATA([[2.y]],
1634[[%expect-rr 1
1635%%
1636exp: 'a' | 'a';
1637]])
1638
1639AT_BISON_CHECK([[2.y]], [[0]], [],
1640[[2.y: warning: %expect-rr applies only to GLR parsers [-Wother]
d87ea54c 16412.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
d1400569
AD
16422.y:3.12-14: warning: rule useless in parser due to conflicts: exp: 'a' [-Wother]
1643]])
1644
1645AT_CLEANUP
1646
1647
1648## ---------------------------------- ##
1649## -W versus %expect and %expect-rr. ##
1650## ---------------------------------- ##
786743d5
JD
1651
1652AT_SETUP([[-W versus %expect and %expect-rr]])
1653
1654AT_DATA([[sr-rr.y]],
1655[[%glr-parser
1656%%
1657start: 'a' | A 'a' | B 'a' ;
1658A: ;
1659B: ;
1660]])
1661AT_DATA([[sr.y]],
1662[[%glr-parser
1663%%
1664start: 'a' | A 'a' ;
1665A: ;
1666]])
1667AT_DATA([[rr.y]],
1668[[%glr-parser
1669%%
1670start: A | B ;
1671A: ;
1672B: ;
1673]])
1674
1675AT_BISON_CHECK([[sr-rr.y]], [[0]], [[]],
d87ea54c
AD
1676[[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
1677sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
786743d5
JD
1678]])
1679AT_BISON_CHECK([[-Wno-conflicts-sr sr-rr.y]], [[0]], [[]],
d87ea54c 1680[[sr-rr.y: warning: 1 reduce/reduce conflict [-Wconflicts-rr]
786743d5
JD
1681]])
1682AT_BISON_CHECK([[-Wno-conflicts-rr sr-rr.y]], [[0]], [[]],
d87ea54c 1683[[sr-rr.y: warning: 1 shift/reduce conflict [-Wconflicts-sr]
786743d5
JD
1684]])
1685
d87ea54c
AD
1686[
1687# This is piece of code is rather complex for a simple task: try every
1688# combinaison of (0 or 1 real SR) x (0 or 1 real RR) x (don't %expect
1689# or %expect 0, 1, or 2 SR) x (don't %expect-rr or %expect-rr 0, 1, or 2
1690# RR).
1691
1692# Number and types of genuine conflicts in the grammar.
1693for gram in sr-rr sr rr; do
1694 # Number of expected s/r conflicts.
786743d5 1695 for sr_exp_i in '' 0 1 2; do
d87ea54c 1696 # Number of expected r/r conflicts.
786743d5
JD
1697 for rr_exp_i in '' 0 1 2; do
1698 test -z "$sr_exp_i" && test -z "$rr_exp_i" && continue
1699
1700 # Build grammar file.
1701 sr_exp=0
1702 rr_exp=0
1703 file=$gram
1704 directives=
1705 if test -n "$sr_exp_i"; then
1706 sr_exp=$sr_exp_i
1707 file=$file-expect-$sr_exp
1708 directives="%expect $sr_exp"
1709 fi
1710 if test -n "$rr_exp_i"; then
1711 rr_exp=$rr_exp_i
1712 file=$file-expect-rr-$rr_exp
1713 directives="$directives %expect-rr $rr_exp"
1714 fi
1715 file=$file.y
1716 echo "$directives" > $file
1717 cat $gram.y >> $file
1718
d87ea54c
AD
1719 # Number of found conflicts.
1720 case $gram in
1721 (sr) sr_count=1; rr_count=0;;
1722 (rr) sr_count=0; rr_count=1;;
1723 (sr-rr) sr_count=1; rr_count=1;;
1724 esac
1725
1726 # Update number of expected conflicts: if %expect is given then
1727 # %expect-rr defaults to 0, and vice-versa. Leave empty if
1728 # nothing expected.
1729 case $sr_exp_i:$rr_exp_i in
1730 ?:) rr_exp_i=0;;
1731 :?) sr_exp_i=0;;
1732 esac
786743d5
JD
1733
1734 # Run tests.
1735 if test $sr_count -eq $sr_exp && test $rr_count -eq $rr_exp; then
1736 ]AT_BISON_CHECK([[-Wnone $file]])[
1737 ]AT_BISON_CHECK([[-Werror $file]])[
1738 else
d87ea54c
AD
1739 {
1740 if test -z "$sr_exp_i" && test "$sr_count" -ne 0; then
1741 echo "warning: $sr_count shift/reduce conflicts"
1742 elif test "$sr_exp_i" -ne "$sr_count"; then
11b19212 1743 echo "error: shift/reduce conflicts: $sr_count found, $sr_exp_i expected"
d87ea54c
AD
1744 fi
1745 if test -z "$rr_exp_i" && test "$rr_count" -ne 0; then
1746 echo "warning: $rr_count reduce/reduce conflicts"
1747 elif test "$rr_exp_i" -ne "$rr_count"; then
11b19212 1748 echo "error: reduce/reduce conflicts: $rr_count found, $rr_exp_i expected"
d87ea54c
AD
1749 fi
1750 } | sed -e "s/^/$file: /" > experr
786743d5
JD
1751 ]AT_BISON_CHECK([[-Wnone $file]], [[1]], [[]], [[experr]])[
1752 ]AT_BISON_CHECK([[-Werror $file]], [[1]], [[]], [[experr]])[
1753 fi
1754 done
1755 done
1756done]
1757
1758AT_CLEANUP