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