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