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