]> git.saurik.com Git - bison.git/blob - tests/conflicts.at
POSIX: complain if %prec's token was not defined.
[bison.git] / tests / conflicts.at
1 # Exercising Bison on conflicts. -*- Autotest -*-
2
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007, 2009 Free Software
4 # Foundation, Inc.
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 AT_BANNER([[Conflicts.]])
20
21
22 ## ---------------- ##
23 ## S/R in initial. ##
24 ## ---------------- ##
25
26 # I once hacked Bison in such a way that it lost its reductions on the
27 # initial state (because it was confusing it with the last state). It
28 # took me a while to strip down my failures to this simple case. So
29 # make sure it finds the s/r conflict below.
30
31 AT_SETUP([S/R in initial])
32
33 AT_DATA([[input.y]],
34 [[%expect 1
35 %%
36 exp: e 'e';
37 e: 'e' | /* Nothing. */;
38 ]])
39
40 AT_BISON_CHECK([-o input.c input.y], 0, [],
41 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */
42 ]])
43
44 AT_CLEANUP
45
46
47 ## ------------------- ##
48 ## %nonassoc and eof. ##
49 ## ------------------- ##
50
51 AT_SETUP([%nonassoc and eof])
52
53 AT_DATA_GRAMMAR([input.y],
54 [[
55 %{
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 #define YYERROR_VERBOSE 1
61 static void
62 yyerror (const char *msg)
63 {
64 fprintf (stderr, "%s\n", msg);
65 }
66
67 /* The current argument. */
68 static const char *input;
69
70 static int
71 yylex (void)
72 {
73 static size_t toknum;
74 if (! (toknum <= strlen (input)))
75 abort ();
76 return input[toknum++];
77 }
78
79 %}
80
81 %nonassoc '<' '>'
82
83 %%
84 expr: expr '<' expr
85 | expr '>' expr
86 | '0'
87 ;
88 %%
89 int
90 main (int argc, const char *argv[])
91 {
92 input = argc <= 1 ? "" : argv[1];
93 return yyparse ();
94 }
95 ]])
96
97 # Specify the output files to avoid problems on different file systems.
98 AT_BISON_CHECK([-o input.c input.y])
99 AT_COMPILE([input])
100
101 AT_PARSER_CHECK([./input '0<0'])
102 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
103 [syntax error, unexpected '<'
104 ])
105
106 AT_PARSER_CHECK([./input '0>0'])
107 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
108 [syntax error, unexpected '>'
109 ])
110
111 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
112 [syntax error, unexpected '>'
113 ])
114
115 # We must disable default reductions in inconsistent states in order to
116 # have an explicit list of all expected tokens. (However, unless we use
117 # canonical LR, lookahead sets are merged for different left contexts,
118 # so it is still possible to have extra incorrect tokens in the expected
119 # list. That just doesn't happen to be a problem for this test case.)
120
121 AT_BISON_CHECK([-Dlr.default-reductions=consistent -o input.c input.y])
122 AT_COMPILE([input])
123
124 AT_PARSER_CHECK([./input '0<0'])
125 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
126 [syntax error, unexpected '<', expecting $end
127 ])
128
129 AT_PARSER_CHECK([./input '0>0'])
130 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
131 [syntax error, unexpected '>', expecting $end
132 ])
133
134 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
135 [syntax error, unexpected '>', expecting $end
136 ])
137
138 AT_CLEANUP
139
140
141
142 ## -------------------------------------- ##
143 ## %error-verbose and consistent errors. ##
144 ## -------------------------------------- ##
145
146 AT_SETUP([[%error-verbose and consistent errors]])
147
148 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
149
150 AT_BISON_CHECK([$1[ -o input.c input.y]])
151 AT_COMPILE([[input]])
152
153 m4_pushdef([AT_EXPECTING], [m4_if($3, [ab], [[, expecting 'a' or 'b']],
154 $3, [a], [[, expecting 'a']],
155 $3, [b], [[, expecting 'b']])])
156
157 AT_PARSER_CHECK([[./input]], [[1]], [],
158 [[syntax error, unexpected ]$2[]AT_EXPECTING[
159 ]])
160
161 m4_popdef([AT_EXPECTING])
162
163 ])
164
165 AT_DATA_GRAMMAR([input.y],
166 [[%code {
167 #include <assert.h>
168 #include <stdio.h>
169 int yylex (void);
170 void yyerror (char const *);
171 #define USE(Var)
172 }
173
174 %error-verbose
175
176 // The point isn't to test IELR here, but state merging happens to
177 // complicate the example.
178 %define lr.type ielr
179
180 %nonassoc 'a'
181
182 // If yylval=0 here, then we know that the 'a' destructor is being
183 // invoked incorrectly for the 'b' set in the semantic action below.
184 // All 'a' tokens are returned by yylex, which sets yylval=1.
185 %destructor {
186 if (!$$)
187 fprintf (stderr, "Wrong destructor.\n");
188 } 'a'
189
190 %%
191
192 // The lookahead assigned by the semantic action isn't needed before
193 // either error action is encountered. In a previous version of Bison,
194 // this was a problem as it meant yychar was not translated into yytoken
195 // before either error action. The second error action thus invoked a
196 // destructor that it selected according to the incorrect yytoken. The
197 // first error action would have reported an incorrect unexpected token
198 // except that, due to another bug, the unexpected token is not reported
199 // at all because the error action is the default action in a consistent
200 // state. That bug still needs to be fixed.
201 start: error-reduce consistent-error 'a' { USE ($3); } ;
202
203 error-reduce:
204 'a' 'a' consistent-reduction consistent-error 'a'
205 { USE (($1, $2, $5)); }
206 | 'a' error
207 { USE ($1); }
208 ;
209
210 consistent-reduction: /*empty*/ {
211 assert (yychar == YYEMPTY);
212 yylval = 0;
213 yychar = 'b';
214 } ;
215
216 consistent-error:
217 'a' { USE ($1); }
218 | /*empty*/ %prec 'a'
219 ;
220
221 // Provide another context in which all rules are useful so that this
222 // test case looks a little more realistic.
223 start: 'b' consistent-error 'b' ;
224
225 %%
226
227 int
228 yylex (void)
229 {
230 static char const *input = "aa";
231 yylval = 1;
232 return *input++;
233 }
234
235 void
236 yyerror (char const *msg)
237 {
238 fprintf (stderr, "%s\n", msg);
239 }
240
241 int
242 main (void)
243 {
244 return yyparse ();
245 }
246 ]])
247
248 # See comments in grammar for why this test doesn't succeed.
249 AT_XFAIL_IF([[:]])
250
251 AT_CONSISTENT_ERRORS_CHECK([], [['b']], [[none]])
252 AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=consistent]],
253 [['b']], [[none]])
254
255 # Canonical LR doesn't foresee the error for 'a'!
256 AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=accepting]],
257 [[$end]], [[a]])
258 AT_CONSISTENT_ERRORS_CHECK([[-Flr.type=canonical-lr]], [[$end]], [[a]])
259
260 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
261
262 AT_CLEANUP
263
264
265
266 ## ------------------------- ##
267 ## Unresolved SR Conflicts. ##
268 ## ------------------------- ##
269
270 AT_SETUP([Unresolved SR Conflicts])
271
272 AT_KEYWORDS([report])
273
274 AT_DATA([input.y],
275 [[%token NUM OP
276 %%
277 exp: exp OP exp | NUM;
278 ]])
279
280 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
281 [input.y: conflicts: 1 shift/reduce
282 ])
283
284 # Check the contents of the report.
285 AT_CHECK([cat input.output], [],
286 [[State 5 conflicts: 1 shift/reduce
287
288
289 Grammar
290
291 0 $accept: exp $end
292
293 1 exp: exp OP exp
294 2 | NUM
295
296
297 Terminals, with rules where they appear
298
299 $end (0) 0
300 error (256)
301 NUM (258) 2
302 OP (259) 1
303
304
305 Nonterminals, with rules where they appear
306
307 $accept (5)
308 on left: 0
309 exp (6)
310 on left: 1 2, on right: 0 1
311
312
313 state 0
314
315 0 $accept: . exp $end
316 1 exp: . exp OP exp
317 2 | . NUM
318
319 NUM shift, and go to state 1
320
321 exp go to state 2
322
323
324 state 1
325
326 2 exp: NUM .
327
328 $default reduce using rule 2 (exp)
329
330
331 state 2
332
333 0 $accept: exp . $end
334 1 exp: exp . OP exp
335
336 $end shift, and go to state 3
337 OP shift, and go to state 4
338
339
340 state 3
341
342 0 $accept: exp $end .
343
344 $default accept
345
346
347 state 4
348
349 1 exp: . exp OP exp
350 1 | exp OP . exp
351 2 | . NUM
352
353 NUM shift, and go to state 1
354
355 exp go to state 5
356
357
358 state 5
359
360 1 exp: exp . OP exp
361 1 | exp OP exp . [$end, OP]
362
363 OP shift, and go to state 4
364
365 OP [reduce using rule 1 (exp)]
366 $default reduce using rule 1 (exp)
367 ]])
368
369 AT_CLEANUP
370
371
372
373 ## ----------------------- ##
374 ## Resolved SR Conflicts. ##
375 ## ----------------------- ##
376
377 AT_SETUP([Resolved SR Conflicts])
378
379 AT_KEYWORDS([report])
380
381 AT_DATA([input.y],
382 [[%token NUM OP
383 %left OP
384 %%
385 exp: exp OP exp | NUM;
386 ]])
387
388 AT_BISON_CHECK([-o input.c --report=all input.y])
389
390 # Check the contents of the report.
391 AT_CHECK([cat input.output], [],
392 [[Grammar
393
394 0 $accept: exp $end
395
396 1 exp: exp OP exp
397 2 | NUM
398
399
400 Terminals, with rules where they appear
401
402 $end (0) 0
403 error (256)
404 NUM (258) 2
405 OP (259) 1
406
407
408 Nonterminals, with rules where they appear
409
410 $accept (5)
411 on left: 0
412 exp (6)
413 on left: 1 2, on right: 0 1
414
415
416 state 0
417
418 0 $accept: . exp $end
419 1 exp: . exp OP exp
420 2 | . NUM
421
422 NUM shift, and go to state 1
423
424 exp go to state 2
425
426
427 state 1
428
429 2 exp: NUM .
430
431 $default reduce using rule 2 (exp)
432
433
434 state 2
435
436 0 $accept: exp . $end
437 1 exp: exp . OP exp
438
439 $end shift, and go to state 3
440 OP shift, and go to state 4
441
442
443 state 3
444
445 0 $accept: exp $end .
446
447 $default accept
448
449
450 state 4
451
452 1 exp: . exp OP exp
453 1 | exp OP . exp
454 2 | . NUM
455
456 NUM shift, and go to state 1
457
458 exp go to state 5
459
460
461 state 5
462
463 1 exp: exp . OP exp
464 1 | exp OP exp . [$end, OP]
465
466 $default reduce using rule 1 (exp)
467
468 Conflict between rule 1 and token OP resolved as reduce (%left OP).
469 ]])
470
471 AT_CLEANUP
472
473
474 ## -------------------------------- ##
475 ## Defaulted Conflicted Reduction. ##
476 ## -------------------------------- ##
477
478 # When there are RR conflicts, some rules are disabled. Usually it is
479 # simply displayed as:
480 #
481 # $end reduce using rule 3 (num)
482 # $end [reduce using rule 4 (id)]
483 #
484 # But when `reduce 3' is the default action, we'd produce:
485 #
486 # $end [reduce using rule 4 (id)]
487 # $default reduce using rule 3 (num)
488 #
489 # In this precise case (a reduction is masked by the default
490 # reduction), we make the `reduce 3' explicit:
491 #
492 # $end reduce using rule 3 (num)
493 # $end [reduce using rule 4 (id)]
494 # $default reduce using rule 3 (num)
495 #
496 # Maybe that's not the best display, but then, please propose something
497 # else.
498
499 AT_SETUP([Defaulted Conflicted Reduction])
500 AT_KEYWORDS([report])
501
502 AT_DATA([input.y],
503 [[%%
504 exp: num | id;
505 num: '0';
506 id : '0';
507 %%
508 ]])
509
510 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
511 [[input.y: conflicts: 1 reduce/reduce
512 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
513 ]])
514
515 # Check the contents of the report.
516 AT_CHECK([cat input.output], [],
517 [[Rules useless in parser due to conflicts
518
519 4 id: '0'
520
521
522 State 1 conflicts: 1 reduce/reduce
523
524
525 Grammar
526
527 0 $accept: exp $end
528
529 1 exp: num
530 2 | id
531
532 3 num: '0'
533
534 4 id: '0'
535
536
537 Terminals, with rules where they appear
538
539 $end (0) 0
540 '0' (48) 3 4
541 error (256)
542
543
544 Nonterminals, with rules where they appear
545
546 $accept (4)
547 on left: 0
548 exp (5)
549 on left: 1 2, on right: 0
550 num (6)
551 on left: 3, on right: 1
552 id (7)
553 on left: 4, on right: 2
554
555
556 state 0
557
558 0 $accept: . exp $end
559 1 exp: . num
560 2 | . id
561 3 num: . '0'
562 4 id: . '0'
563
564 '0' shift, and go to state 1
565
566 exp go to state 2
567 num go to state 3
568 id go to state 4
569
570
571 state 1
572
573 3 num: '0' . [$end]
574 4 id: '0' . [$end]
575
576 $end reduce using rule 3 (num)
577 $end [reduce using rule 4 (id)]
578 $default reduce using rule 3 (num)
579
580
581 state 2
582
583 0 $accept: exp . $end
584
585 $end shift, and go to state 5
586
587
588 state 3
589
590 1 exp: num .
591
592 $default reduce using rule 1 (exp)
593
594
595 state 4
596
597 2 exp: id .
598
599 $default reduce using rule 2 (exp)
600
601
602 state 5
603
604 0 $accept: exp $end .
605
606 $default accept
607 ]])
608
609 AT_CLEANUP
610
611
612
613
614 ## -------------------- ##
615 ## %expect not enough. ##
616 ## -------------------- ##
617
618 AT_SETUP([%expect not enough])
619
620 AT_DATA([input.y],
621 [[%token NUM OP
622 %expect 0
623 %%
624 exp: exp OP exp | NUM;
625 ]])
626
627 AT_BISON_CHECK([-o input.c input.y], 1, [],
628 [input.y: conflicts: 1 shift/reduce
629 input.y: expected 0 shift/reduce conflicts
630 ])
631 AT_CLEANUP
632
633
634 ## --------------- ##
635 ## %expect right. ##
636 ## --------------- ##
637
638 AT_SETUP([%expect right])
639
640 AT_DATA([input.y],
641 [[%token NUM OP
642 %expect 1
643 %%
644 exp: exp OP exp | NUM;
645 ]])
646
647 AT_BISON_CHECK([-o input.c input.y])
648 AT_CLEANUP
649
650
651 ## ------------------ ##
652 ## %expect too much. ##
653 ## ------------------ ##
654
655 AT_SETUP([%expect too much])
656
657 AT_DATA([input.y],
658 [[%token NUM OP
659 %expect 2
660 %%
661 exp: exp OP exp | NUM;
662 ]])
663
664 AT_BISON_CHECK([-o input.c input.y], 1, [],
665 [input.y: conflicts: 1 shift/reduce
666 input.y: expected 2 shift/reduce conflicts
667 ])
668 AT_CLEANUP
669
670
671 ## ------------------------------- ##
672 ## %expect with reduce conflicts. ##
673 ## ------------------------------- ##
674
675 AT_SETUP([%expect with reduce conflicts])
676
677 AT_DATA([input.y],
678 [[%expect 0
679 %%
680 program: a 'a' | a a;
681 a: 'a';
682 ]])
683
684 AT_BISON_CHECK([-o input.c input.y], 1, [],
685 [input.y: conflicts: 1 reduce/reduce
686 input.y: expected 0 reduce/reduce conflicts
687 ])
688 AT_CLEANUP
689
690
691 ## ------------------------- ##
692 ## %prec with user strings. ##
693 ## ------------------------- ##
694
695 AT_SETUP([%prec with user string])
696
697 AT_DATA([[input.y]],
698 [[%%
699 exp:
700 "foo" %prec "foo"
701 ;
702 ]])
703
704 AT_BISON_CHECK([-o input.c input.y])
705 AT_CLEANUP
706
707
708 ## -------------------------------- ##
709 ## %no-default-prec without %prec. ##
710 ## -------------------------------- ##
711
712 AT_SETUP([%no-default-prec without %prec])
713
714 AT_DATA([[input.y]],
715 [[%left '+'
716 %left '*'
717
718 %%
719
720 %no-default-prec;
721
722 e: e '+' e
723 | e '*' e
724 | '0'
725 ;
726 ]])
727
728 AT_BISON_CHECK([-o input.c input.y], 0, [],
729 [[input.y: conflicts: 4 shift/reduce
730 ]])
731 AT_CLEANUP
732
733
734 ## ----------------------------- ##
735 ## %no-default-prec with %prec. ##
736 ## ----------------------------- ##
737
738 AT_SETUP([%no-default-prec with %prec])
739
740 AT_DATA([[input.y]],
741 [[%left '+'
742 %left '*'
743
744 %%
745
746 %no-default-prec;
747
748 e: e '+' e %prec '+'
749 | e '*' e %prec '*'
750 | '0'
751 ;
752 ]])
753
754 AT_BISON_CHECK([-o input.c input.y])
755 AT_CLEANUP
756
757
758 ## --------------- ##
759 ## %default-prec. ##
760 ## --------------- ##
761
762 AT_SETUP([%default-prec])
763
764 AT_DATA([[input.y]],
765 [[%left '+'
766 %left '*'
767
768 %%
769
770 %default-prec;
771
772 e: e '+' e
773 | e '*' e
774 | '0'
775 ;
776 ]])
777
778 AT_BISON_CHECK([-o input.c input.y])
779 AT_CLEANUP
780
781
782 ## ---------------------------------------------- ##
783 ## Unreachable States After Conflict Resolution. ##
784 ## ---------------------------------------------- ##
785
786 AT_SETUP([[Unreachable States After Conflict Resolution]])
787
788 # If conflict resolution makes states unreachable, remove those states, report
789 # rules that are then unused, and don't report conflicts in those states. Test
790 # what happens when a nonterminal becomes useless as a result of state removal
791 # since that causes lalr.o's goto map to be rewritten.
792
793 AT_DATA([[input.y]],
794 [[%output "input.c"
795 %left 'a'
796
797 %%
798
799 start: resolved_conflict 'a' reported_conflicts 'a' ;
800
801 /* S/R conflict resolved as reduce, so the state with item
802 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
803 * unreachable, and the associated production is useless. */
804 resolved_conflict:
805 'a' unreachable1
806 | %prec 'a'
807 ;
808
809 /* S/R conflict that need not be reported since it is unreachable because of
810 * the previous conflict resolution. Nonterminal unreachable1 and all its
811 * productions are useless. */
812 unreachable1:
813 'a' unreachable2
814 |
815 ;
816
817 /* Likewise for a R/R conflict and nonterminal unreachable2. */
818 unreachable2: | ;
819
820 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
821 * when their states are renumbered due to state removal. */
822 reported_conflicts:
823 'a'
824 | 'a'
825 |
826 ;
827
828 ]])
829
830 AT_BISON_CHECK([[--report=all input.y]], 0, [],
831 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
832 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
833 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
834 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
835 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
836 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
837 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
838 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
839 ]])
840
841 AT_CHECK([[cat input.output]], 0,
842 [[Rules useless in parser due to conflicts
843
844 2 resolved_conflict: 'a' unreachable1
845
846 4 unreachable1: 'a' unreachable2
847 5 | /* empty */
848
849 6 unreachable2: /* empty */
850 7 | /* empty */
851
852 9 reported_conflicts: 'a'
853 10 | /* empty */
854
855
856 State 4 conflicts: 1 shift/reduce
857 State 5 conflicts: 1 reduce/reduce
858
859
860 Grammar
861
862 0 $accept: start $end
863
864 1 start: resolved_conflict 'a' reported_conflicts 'a'
865
866 2 resolved_conflict: 'a' unreachable1
867 3 | /* empty */
868
869 4 unreachable1: 'a' unreachable2
870 5 | /* empty */
871
872 6 unreachable2: /* empty */
873 7 | /* empty */
874
875 8 reported_conflicts: 'a'
876 9 | 'a'
877 10 | /* empty */
878
879
880 Terminals, with rules where they appear
881
882 $end (0) 0
883 'a' (97) 1 2 4 8 9
884 error (256)
885
886
887 Nonterminals, with rules where they appear
888
889 $accept (4)
890 on left: 0
891 start (5)
892 on left: 1, on right: 0
893 resolved_conflict (6)
894 on left: 2 3, on right: 1
895 unreachable1 (7)
896 on left: 4 5, on right: 2
897 unreachable2 (8)
898 on left: 6 7, on right: 4
899 reported_conflicts (9)
900 on left: 8 9 10, on right: 1
901
902
903 state 0
904
905 0 $accept: . start $end
906 1 start: . resolved_conflict 'a' reported_conflicts 'a'
907 2 resolved_conflict: . 'a' unreachable1
908 3 | . ['a']
909
910 $default reduce using rule 3 (resolved_conflict)
911
912 start go to state 1
913 resolved_conflict go to state 2
914
915 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
916
917
918 state 1
919
920 0 $accept: start . $end
921
922 $end shift, and go to state 3
923
924
925 state 2
926
927 1 start: resolved_conflict . 'a' reported_conflicts 'a'
928
929 'a' shift, and go to state 4
930
931
932 state 3
933
934 0 $accept: start $end .
935
936 $default accept
937
938
939 state 4
940
941 1 start: resolved_conflict 'a' . reported_conflicts 'a'
942 8 reported_conflicts: . 'a'
943 9 | . 'a'
944 10 | . ['a']
945
946 'a' shift, and go to state 5
947
948 'a' [reduce using rule 10 (reported_conflicts)]
949
950 reported_conflicts go to state 6
951
952
953 state 5
954
955 8 reported_conflicts: 'a' . ['a']
956 9 | 'a' . ['a']
957
958 'a' reduce using rule 8 (reported_conflicts)
959 'a' [reduce using rule 9 (reported_conflicts)]
960 $default reduce using rule 8 (reported_conflicts)
961
962
963 state 6
964
965 1 start: resolved_conflict 'a' reported_conflicts . 'a'
966
967 'a' shift, and go to state 7
968
969
970 state 7
971
972 1 start: resolved_conflict 'a' reported_conflicts 'a' .
973
974 $default reduce using rule 1 (start)
975 ]])
976
977 AT_DATA([[input-keep.y]],
978 [[%define lr.keep-unreachable-states
979 ]])
980 AT_CHECK([[cat input.y >> input-keep.y]])
981
982 AT_BISON_CHECK([[input-keep.y]], 0, [],
983 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
984 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
985 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
986 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
987 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
988 ]])
989
990 AT_CLEANUP
991
992
993 ## ------------------------------------------------------------ ##
994 ## Solved conflicts report for multiple reductions in a state. ##
995 ## ------------------------------------------------------------ ##
996
997 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
998
999 # Used to lose earlier solved conflict messages even within a single S/R/R.
1000
1001 AT_DATA([[input.y]],
1002 [[%left 'a'
1003 %right 'b'
1004 %right 'c'
1005 %right 'd'
1006 %%
1007 start:
1008 'a'
1009 | empty_a 'a'
1010 | 'b'
1011 | empty_b 'b'
1012 | 'c'
1013 | empty_c1 'c'
1014 | empty_c2 'c'
1015 | empty_c3 'c'
1016 ;
1017 empty_a: %prec 'a' ;
1018 empty_b: %prec 'b' ;
1019 empty_c1: %prec 'c' ;
1020 empty_c2: %prec 'c' ;
1021 empty_c3: %prec 'd' ;
1022 ]])
1023 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1024 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1025 [[state 0
1026
1027 0 $accept: . start $end
1028 1 start: . 'a'
1029 2 | . empty_a 'a'
1030 3 | . 'b'
1031 4 | . empty_b 'b'
1032 5 | . 'c'
1033 6 | . empty_c1 'c'
1034 7 | . empty_c2 'c'
1035 8 | . empty_c3 'c'
1036 9 empty_a: . ['a']
1037 10 empty_b: . []
1038 11 empty_c1: . []
1039 12 empty_c2: . []
1040 13 empty_c3: . ['c']
1041
1042 'b' shift, and go to state 1
1043
1044 'c' reduce using rule 13 (empty_c3)
1045 $default reduce using rule 9 (empty_a)
1046
1047 start go to state 2
1048 empty_a go to state 3
1049 empty_b go to state 4
1050 empty_c1 go to state 5
1051 empty_c2 go to state 6
1052 empty_c3 go to state 7
1053
1054 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1055 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1056 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1057 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1058 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1059
1060
1061 state 1
1062 ]])
1063
1064 AT_CLEANUP
1065
1066
1067 ## ------------------------------------------------------------ ##
1068 ## %nonassoc error actions for multiple reductions in a state. ##
1069 ## ------------------------------------------------------------ ##
1070
1071 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1072 # multiple reductions in a state.
1073
1074 # For a %nonassoc error action token, used to print the first remaining
1075 # reduction on that token without brackets.
1076
1077 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1078
1079 AT_DATA([[input.y]],
1080 [[%nonassoc 'a' 'b' 'c'
1081 %%
1082 start:
1083 'a'
1084 | empty_a 'a'
1085 | 'b'
1086 | empty_b 'b'
1087 | 'c'
1088 | empty_c1 'c'
1089 | empty_c2 'c'
1090 | empty_c3 'c'
1091 ;
1092 empty_a: %prec 'a' ;
1093 empty_b: %prec 'b' ;
1094 empty_c1: %prec 'c' ;
1095 empty_c2: %prec 'c' ;
1096 empty_c3: %prec 'c' ;
1097 ]])
1098
1099 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1100 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1101 [[state 0
1102
1103 0 $accept: . start $end
1104 1 start: . 'a'
1105 2 | . empty_a 'a'
1106 3 | . 'b'
1107 4 | . empty_b 'b'
1108 5 | . 'c'
1109 6 | . empty_c1 'c'
1110 7 | . empty_c2 'c'
1111 8 | . empty_c3 'c'
1112 9 empty_a: . []
1113 10 empty_b: . []
1114 11 empty_c1: . []
1115 12 empty_c2: . ['c']
1116 13 empty_c3: . ['c']
1117
1118 'a' error (nonassociative)
1119 'b' error (nonassociative)
1120 'c' error (nonassociative)
1121
1122 'c' [reduce using rule 12 (empty_c2)]
1123 'c' [reduce using rule 13 (empty_c3)]
1124
1125 start go to state 1
1126 empty_a go to state 2
1127 empty_b go to state 3
1128 empty_c1 go to state 4
1129 empty_c2 go to state 5
1130 empty_c3 go to state 6
1131
1132 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1133 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1134 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1135
1136
1137 state 1
1138 ]])
1139 AT_CLEANUP