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