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