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