]> git.saurik.com Git - bison.git/blob - tests/conflicts.at
Fix handling of yychar manipulation in user semantic actions.
[bison.git] / tests / conflicts.at
1 # Exercising Bison on conflicts. -*- Autotest -*-
2
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 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 ## parse.error=verbose and consistent errors. ##
144 ## ------------------------------------------- ##
145
146 AT_SETUP([[parse.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 %define parse.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 ## %precedence suffices. ##
476 ## ---------------------- ##
477
478 AT_SETUP([%precedence suffices])
479
480 AT_DATA([input.y],
481 [[%precedence "then"
482 %precedence "else"
483 %%
484 stmt:
485 "if" cond "then" stmt
486 | "if" cond "then" stmt "else" stmt
487 | "stmt"
488 ;
489
490 cond:
491 "exp"
492 ;
493 ]])
494
495 AT_BISON_CHECK([-o input.c input.y])
496
497 AT_CLEANUP
498
499
500 ## ------------------------------ ##
501 ## %precedence does not suffice. ##
502 ## ------------------------------ ##
503
504 AT_SETUP([%precedence does not suffice])
505
506 AT_DATA([input.y],
507 [[%precedence "then"
508 %precedence "else"
509 %%
510 stmt:
511 "if" cond "then" stmt
512 | "if" cond "then" stmt "else" stmt
513 | "stmt"
514 ;
515
516 cond:
517 "exp"
518 | cond "then" cond
519 ;
520 ]])
521
522 AT_BISON_CHECK([-o input.c input.y], 0, [],
523 [[input.y: conflicts: 1 shift/reduce
524 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond
525 ]])
526
527 AT_CLEANUP
528
529
530 ## -------------------------------- ##
531 ## Defaulted Conflicted Reduction. ##
532 ## -------------------------------- ##
533
534 # When there are RR conflicts, some rules are disabled. Usually it is
535 # simply displayed as:
536 #
537 # $end reduce using rule 3 (num)
538 # $end [reduce using rule 4 (id)]
539 #
540 # But when `reduce 3' is the default action, we'd produce:
541 #
542 # $end [reduce using rule 4 (id)]
543 # $default reduce using rule 3 (num)
544 #
545 # In this precise case (a reduction is masked by the default
546 # reduction), we make the `reduce 3' explicit:
547 #
548 # $end reduce using rule 3 (num)
549 # $end [reduce using rule 4 (id)]
550 # $default reduce using rule 3 (num)
551 #
552 # Maybe that's not the best display, but then, please propose something
553 # else.
554
555 AT_SETUP([Defaulted Conflicted Reduction])
556 AT_KEYWORDS([report])
557
558 AT_DATA([input.y],
559 [[%%
560 exp: num | id;
561 num: '0';
562 id : '0';
563 %%
564 ]])
565
566 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
567 [[input.y: conflicts: 1 reduce/reduce
568 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
569 ]])
570
571 # Check the contents of the report.
572 AT_CHECK([cat input.output], [],
573 [[Rules useless in parser due to conflicts
574
575 4 id: '0'
576
577
578 State 1 conflicts: 1 reduce/reduce
579
580
581 Grammar
582
583 0 $accept: exp $end
584
585 1 exp: num
586 2 | id
587
588 3 num: '0'
589
590 4 id: '0'
591
592
593 Terminals, with rules where they appear
594
595 $end (0) 0
596 '0' (48) 3 4
597 error (256)
598
599
600 Nonterminals, with rules where they appear
601
602 $accept (4)
603 on left: 0
604 exp (5)
605 on left: 1 2, on right: 0
606 num (6)
607 on left: 3, on right: 1
608 id (7)
609 on left: 4, on right: 2
610
611
612 state 0
613
614 0 $accept: . exp $end
615 1 exp: . num
616 2 | . id
617 3 num: . '0'
618 4 id: . '0'
619
620 '0' shift, and go to state 1
621
622 exp go to state 2
623 num go to state 3
624 id go to state 4
625
626
627 state 1
628
629 3 num: '0' . [$end]
630 4 id: '0' . [$end]
631
632 $end reduce using rule 3 (num)
633 $end [reduce using rule 4 (id)]
634 $default reduce using rule 3 (num)
635
636
637 state 2
638
639 0 $accept: exp . $end
640
641 $end shift, and go to state 5
642
643
644 state 3
645
646 1 exp: num .
647
648 $default reduce using rule 1 (exp)
649
650
651 state 4
652
653 2 exp: id .
654
655 $default reduce using rule 2 (exp)
656
657
658 state 5
659
660 0 $accept: exp $end .
661
662 $default accept
663 ]])
664
665 AT_CLEANUP
666
667
668
669
670 ## -------------------- ##
671 ## %expect not enough. ##
672 ## -------------------- ##
673
674 AT_SETUP([%expect not enough])
675
676 AT_DATA([input.y],
677 [[%token NUM OP
678 %expect 0
679 %%
680 exp: exp OP exp | NUM;
681 ]])
682
683 AT_BISON_CHECK([-o input.c input.y], 1, [],
684 [input.y: conflicts: 1 shift/reduce
685 input.y: expected 0 shift/reduce conflicts
686 ])
687 AT_CLEANUP
688
689
690 ## --------------- ##
691 ## %expect right. ##
692 ## --------------- ##
693
694 AT_SETUP([%expect right])
695
696 AT_DATA([input.y],
697 [[%token NUM OP
698 %expect 1
699 %%
700 exp: exp OP exp | NUM;
701 ]])
702
703 AT_BISON_CHECK([-o input.c input.y])
704 AT_CLEANUP
705
706
707 ## ------------------ ##
708 ## %expect too much. ##
709 ## ------------------ ##
710
711 AT_SETUP([%expect too much])
712
713 AT_DATA([input.y],
714 [[%token NUM OP
715 %expect 2
716 %%
717 exp: exp OP exp | NUM;
718 ]])
719
720 AT_BISON_CHECK([-o input.c input.y], 1, [],
721 [input.y: conflicts: 1 shift/reduce
722 input.y: expected 2 shift/reduce conflicts
723 ])
724 AT_CLEANUP
725
726
727 ## ------------------------------- ##
728 ## %expect with reduce conflicts. ##
729 ## ------------------------------- ##
730
731 AT_SETUP([%expect with reduce conflicts])
732
733 AT_DATA([input.y],
734 [[%expect 0
735 %%
736 program: a 'a' | a a;
737 a: 'a';
738 ]])
739
740 AT_BISON_CHECK([-o input.c input.y], 1, [],
741 [input.y: conflicts: 1 reduce/reduce
742 input.y: expected 0 reduce/reduce conflicts
743 ])
744 AT_CLEANUP
745
746
747 ## ------------------------- ##
748 ## %prec with user strings. ##
749 ## ------------------------- ##
750
751 AT_SETUP([%prec with user string])
752
753 AT_DATA([[input.y]],
754 [[%%
755 exp:
756 "foo" %prec "foo"
757 ;
758 ]])
759
760 AT_BISON_CHECK([-o input.c input.y])
761 AT_CLEANUP
762
763
764 ## -------------------------------- ##
765 ## %no-default-prec without %prec. ##
766 ## -------------------------------- ##
767
768 AT_SETUP([%no-default-prec without %prec])
769
770 AT_DATA([[input.y]],
771 [[%left '+'
772 %left '*'
773
774 %%
775
776 %no-default-prec;
777
778 e: e '+' e
779 | e '*' e
780 | '0'
781 ;
782 ]])
783
784 AT_BISON_CHECK([-o input.c input.y], 0, [],
785 [[input.y: conflicts: 4 shift/reduce
786 ]])
787 AT_CLEANUP
788
789
790 ## ----------------------------- ##
791 ## %no-default-prec with %prec. ##
792 ## ----------------------------- ##
793
794 AT_SETUP([%no-default-prec with %prec])
795
796 AT_DATA([[input.y]],
797 [[%left '+'
798 %left '*'
799
800 %%
801
802 %no-default-prec;
803
804 e: e '+' e %prec '+'
805 | e '*' e %prec '*'
806 | '0'
807 ;
808 ]])
809
810 AT_BISON_CHECK([-o input.c input.y])
811 AT_CLEANUP
812
813
814 ## --------------- ##
815 ## %default-prec. ##
816 ## --------------- ##
817
818 AT_SETUP([%default-prec])
819
820 AT_DATA([[input.y]],
821 [[%left '+'
822 %left '*'
823
824 %%
825
826 %default-prec;
827
828 e: e '+' e
829 | e '*' e
830 | '0'
831 ;
832 ]])
833
834 AT_BISON_CHECK([-o input.c input.y])
835 AT_CLEANUP
836
837
838 ## ---------------------------------------------- ##
839 ## Unreachable States After Conflict Resolution. ##
840 ## ---------------------------------------------- ##
841
842 AT_SETUP([[Unreachable States After Conflict Resolution]])
843
844 # If conflict resolution makes states unreachable, remove those states, report
845 # rules that are then unused, and don't report conflicts in those states. Test
846 # what happens when a nonterminal becomes useless as a result of state removal
847 # since that causes lalr.o's goto map to be rewritten.
848
849 AT_DATA([[input.y]],
850 [[%output "input.c"
851 %left 'a'
852
853 %%
854
855 start: resolved_conflict 'a' reported_conflicts 'a' ;
856
857 /* S/R conflict resolved as reduce, so the state with item
858 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
859 * unreachable, and the associated production is useless. */
860 resolved_conflict:
861 'a' unreachable1
862 | %prec 'a'
863 ;
864
865 /* S/R conflict that need not be reported since it is unreachable because of
866 * the previous conflict resolution. Nonterminal unreachable1 and all its
867 * productions are useless. */
868 unreachable1:
869 'a' unreachable2
870 |
871 ;
872
873 /* Likewise for a R/R conflict and nonterminal unreachable2. */
874 unreachable2: | ;
875
876 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
877 * when their states are renumbered due to state removal. */
878 reported_conflicts:
879 'a'
880 | 'a'
881 |
882 ;
883
884 ]])
885
886 AT_BISON_CHECK([[--report=all input.y]], 0, [],
887 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
888 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
889 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
890 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
891 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
892 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
893 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
894 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
895 ]])
896
897 AT_CHECK([[cat input.output]], 0,
898 [[Rules useless in parser due to conflicts
899
900 2 resolved_conflict: 'a' unreachable1
901
902 4 unreachable1: 'a' unreachable2
903 5 | /* empty */
904
905 6 unreachable2: /* empty */
906 7 | /* empty */
907
908 9 reported_conflicts: 'a'
909 10 | /* empty */
910
911
912 State 4 conflicts: 1 shift/reduce
913 State 5 conflicts: 1 reduce/reduce
914
915
916 Grammar
917
918 0 $accept: start $end
919
920 1 start: resolved_conflict 'a' reported_conflicts 'a'
921
922 2 resolved_conflict: 'a' unreachable1
923 3 | /* empty */
924
925 4 unreachable1: 'a' unreachable2
926 5 | /* empty */
927
928 6 unreachable2: /* empty */
929 7 | /* empty */
930
931 8 reported_conflicts: 'a'
932 9 | 'a'
933 10 | /* empty */
934
935
936 Terminals, with rules where they appear
937
938 $end (0) 0
939 'a' (97) 1 2 4 8 9
940 error (256)
941
942
943 Nonterminals, with rules where they appear
944
945 $accept (4)
946 on left: 0
947 start (5)
948 on left: 1, on right: 0
949 resolved_conflict (6)
950 on left: 2 3, on right: 1
951 unreachable1 (7)
952 on left: 4 5, on right: 2
953 unreachable2 (8)
954 on left: 6 7, on right: 4
955 reported_conflicts (9)
956 on left: 8 9 10, on right: 1
957
958
959 state 0
960
961 0 $accept: . start $end
962 1 start: . resolved_conflict 'a' reported_conflicts 'a'
963 2 resolved_conflict: . 'a' unreachable1
964 3 | . ['a']
965
966 $default reduce using rule 3 (resolved_conflict)
967
968 start go to state 1
969 resolved_conflict go to state 2
970
971 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
972
973
974 state 1
975
976 0 $accept: start . $end
977
978 $end shift, and go to state 3
979
980
981 state 2
982
983 1 start: resolved_conflict . 'a' reported_conflicts 'a'
984
985 'a' shift, and go to state 4
986
987
988 state 3
989
990 0 $accept: start $end .
991
992 $default accept
993
994
995 state 4
996
997 1 start: resolved_conflict 'a' . reported_conflicts 'a'
998 8 reported_conflicts: . 'a'
999 9 | . 'a'
1000 10 | . ['a']
1001
1002 'a' shift, and go to state 5
1003
1004 'a' [reduce using rule 10 (reported_conflicts)]
1005
1006 reported_conflicts go to state 6
1007
1008
1009 state 5
1010
1011 8 reported_conflicts: 'a' . ['a']
1012 9 | 'a' . ['a']
1013
1014 'a' reduce using rule 8 (reported_conflicts)
1015 'a' [reduce using rule 9 (reported_conflicts)]
1016 $default reduce using rule 8 (reported_conflicts)
1017
1018
1019 state 6
1020
1021 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1022
1023 'a' shift, and go to state 7
1024
1025
1026 state 7
1027
1028 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1029
1030 $default reduce using rule 1 (start)
1031 ]])
1032
1033 AT_DATA([[input-keep.y]],
1034 [[%define lr.keep-unreachable-states
1035 ]])
1036 AT_CHECK([[cat input.y >> input-keep.y]])
1037
1038 AT_BISON_CHECK([[input-keep.y]], 0, [],
1039 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
1040 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1041 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1042 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1043 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1044 ]])
1045
1046 AT_CLEANUP
1047
1048
1049 ## ------------------------------------------------------------ ##
1050 ## Solved conflicts report for multiple reductions in a state. ##
1051 ## ------------------------------------------------------------ ##
1052
1053 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1054
1055 # Used to lose earlier solved conflict messages even within a single S/R/R.
1056
1057 AT_DATA([[input.y]],
1058 [[%left 'a'
1059 %right 'b'
1060 %right 'c'
1061 %right 'd'
1062 %%
1063 start:
1064 'a'
1065 | empty_a 'a'
1066 | 'b'
1067 | empty_b 'b'
1068 | 'c'
1069 | empty_c1 'c'
1070 | empty_c2 'c'
1071 | empty_c3 'c'
1072 ;
1073 empty_a: %prec 'a' ;
1074 empty_b: %prec 'b' ;
1075 empty_c1: %prec 'c' ;
1076 empty_c2: %prec 'c' ;
1077 empty_c3: %prec 'd' ;
1078 ]])
1079 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1080 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1081 [[state 0
1082
1083 0 $accept: . start $end
1084 1 start: . 'a'
1085 2 | . empty_a 'a'
1086 3 | . 'b'
1087 4 | . empty_b 'b'
1088 5 | . 'c'
1089 6 | . empty_c1 'c'
1090 7 | . empty_c2 'c'
1091 8 | . empty_c3 'c'
1092 9 empty_a: . ['a']
1093 10 empty_b: . []
1094 11 empty_c1: . []
1095 12 empty_c2: . []
1096 13 empty_c3: . ['c']
1097
1098 'b' shift, and go to state 1
1099
1100 'c' reduce using rule 13 (empty_c3)
1101 $default reduce using rule 9 (empty_a)
1102
1103 start go to state 2
1104 empty_a go to state 3
1105 empty_b go to state 4
1106 empty_c1 go to state 5
1107 empty_c2 go to state 6
1108 empty_c3 go to state 7
1109
1110 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1111 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1112 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1113 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1114 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1115
1116
1117 state 1
1118 ]])
1119
1120 AT_CLEANUP
1121
1122
1123 ## ------------------------------------------------------------ ##
1124 ## %nonassoc error actions for multiple reductions in a state. ##
1125 ## ------------------------------------------------------------ ##
1126
1127 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1128 # multiple reductions in a state.
1129
1130 # For a %nonassoc error action token, used to print the first remaining
1131 # reduction on that token without brackets.
1132
1133 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1134
1135 AT_DATA([[input.y]],
1136 [[%nonassoc 'a' 'b' 'c'
1137 %%
1138 start:
1139 'a'
1140 | empty_a 'a'
1141 | 'b'
1142 | empty_b 'b'
1143 | 'c'
1144 | empty_c1 'c'
1145 | empty_c2 'c'
1146 | empty_c3 'c'
1147 ;
1148 empty_a: %prec 'a' ;
1149 empty_b: %prec 'b' ;
1150 empty_c1: %prec 'c' ;
1151 empty_c2: %prec 'c' ;
1152 empty_c3: %prec 'c' ;
1153 ]])
1154
1155 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1156 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1157 [[state 0
1158
1159 0 $accept: . start $end
1160 1 start: . 'a'
1161 2 | . empty_a 'a'
1162 3 | . 'b'
1163 4 | . empty_b 'b'
1164 5 | . 'c'
1165 6 | . empty_c1 'c'
1166 7 | . empty_c2 'c'
1167 8 | . empty_c3 'c'
1168 9 empty_a: . []
1169 10 empty_b: . []
1170 11 empty_c1: . []
1171 12 empty_c2: . ['c']
1172 13 empty_c3: . ['c']
1173
1174 'a' error (nonassociative)
1175 'b' error (nonassociative)
1176 'c' error (nonassociative)
1177
1178 'c' [reduce using rule 12 (empty_c2)]
1179 'c' [reduce using rule 13 (empty_c3)]
1180
1181 start go to state 1
1182 empty_a go to state 2
1183 empty_b go to state 3
1184 empty_c1 go to state 4
1185 empty_c2 go to state 5
1186 empty_c3 go to state 6
1187
1188 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1189 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1190 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1191
1192
1193 state 1
1194 ]])
1195 AT_CLEANUP