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