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