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