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