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