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