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