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