1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
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.
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.
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/>.
18 AT_BANNER([[Conflicts.]])
21 ## ---------------- ##
23 ## ---------------- ##
25 # I once hacked Bison in such a way that it lost its reductions on the
26 # initial state (because it was confusing it with the last state). It
27 # took me a while to strip down my failures to this simple case. So
28 # make sure it finds the s/r conflict below.
30 AT_SETUP([S/R in initial])
36 e: 'e' | /* Nothing. */;
39 AT_BISON_CHECK([-o input.c input.y], 0, [],
40 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */
46 ## ------------------- ##
47 ## %nonassoc and eof. ##
48 ## ------------------- ##
50 AT_SETUP([%nonassoc and eof])
52 AT_DATA_GRAMMAR([input.y],
59 #define YYERROR_VERBOSE 1
61 yyerror (const char *msg)
63 fprintf (stderr, "%s\n", msg);
66 /* The current argument. */
67 static const char *input;
73 if (! (toknum <= strlen (input)))
75 return input[toknum++];
89 main (int argc, const char *argv[])
91 input = argc <= 1 ? "" : argv[1];
96 # Specify the output files to avoid problems on different file systems.
97 AT_BISON_CHECK([-o input.c input.y])
100 AT_PARSER_CHECK([./input '0<0'])
101 # FIXME: This is an actual bug, but a new one, in the sense that
102 # no one has ever spotted it! The messages are *wrong*: there should
103 # be nothing there, it should be expected eof.
104 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
105 [syntax error, unexpected '<', expecting '<' or '>'
108 AT_PARSER_CHECK([./input '0>0'])
109 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
110 [syntax error, unexpected '>', expecting '<' or '>'
113 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
114 [syntax error, unexpected '>', expecting '<' or '>'
121 ## ------------------------- ##
122 ## Unresolved SR Conflicts. ##
123 ## ------------------------- ##
125 AT_SETUP([Unresolved SR Conflicts])
127 AT_KEYWORDS([report])
132 exp: exp OP exp | NUM;
135 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
136 [input.y: conflicts: 1 shift/reduce
139 # Check the contents of the report.
140 AT_CHECK([cat input.output], [],
141 [[State 5 conflicts: 1 shift/reduce
152 Terminals, with rules where they appear
160 Nonterminals, with rules where they appear
165 on left: 1 2, on right: 0 1
170 0 $accept: . exp $end
174 NUM shift, and go to state 1
183 $default reduce using rule 2 (exp)
188 0 $accept: exp . $end
191 $end shift, and go to state 3
192 OP shift, and go to state 4
197 0 $accept: exp $end .
208 NUM shift, and go to state 1
216 1 | exp OP exp . [$end, OP]
218 OP shift, and go to state 4
220 OP [reduce using rule 1 (exp)]
221 $default reduce using rule 1 (exp)
228 ## ----------------------- ##
229 ## Resolved SR Conflicts. ##
230 ## ----------------------- ##
232 AT_SETUP([Resolved SR Conflicts])
234 AT_KEYWORDS([report])
240 exp: exp OP exp | NUM;
243 AT_BISON_CHECK([-o input.c --report=all input.y])
245 # Check the contents of the report.
246 AT_CHECK([cat input.output], [],
255 Terminals, with rules where they appear
263 Nonterminals, with rules where they appear
268 on left: 1 2, on right: 0 1
273 0 $accept: . exp $end
277 NUM shift, and go to state 1
286 $default reduce using rule 2 (exp)
291 0 $accept: exp . $end
294 $end shift, and go to state 3
295 OP shift, and go to state 4
300 0 $accept: exp $end .
311 NUM shift, and go to state 1
319 1 | exp OP exp . [$end, OP]
321 $default reduce using rule 1 (exp)
323 Conflict between rule 1 and token OP resolved as reduce (%left OP).
329 ## -------------------------------- ##
330 ## Defaulted Conflicted Reduction. ##
331 ## -------------------------------- ##
333 # When there are RR conflicts, some rules are disabled. Usually it is
334 # simply displayed as:
336 # $end reduce using rule 3 (num)
337 # $end [reduce using rule 4 (id)]
339 # But when `reduce 3' is the default action, we'd produce:
341 # $end [reduce using rule 4 (id)]
342 # $default reduce using rule 3 (num)
344 # In this precise case (a reduction is masked by the default
345 # reduction), we make the `reduce 3' explicit:
347 # $end reduce using rule 3 (num)
348 # $end [reduce using rule 4 (id)]
349 # $default reduce using rule 3 (num)
351 # Maybe that's not the best display, but then, please propose something
354 AT_SETUP([Defaulted Conflicted Reduction])
355 AT_KEYWORDS([report])
365 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
366 [[input.y: conflicts: 1 reduce/reduce
367 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
370 # Check the contents of the report.
371 AT_CHECK([cat input.output], [],
372 [[Rules useless in parser due to conflicts
377 State 1 conflicts: 1 reduce/reduce
392 Terminals, with rules where they appear
399 Nonterminals, with rules where they appear
404 on left: 1 2, on right: 0
406 on left: 3, on right: 1
408 on left: 4, on right: 2
413 0 $accept: . exp $end
419 '0' shift, and go to state 1
431 $end reduce using rule 3 (num)
432 $end [reduce using rule 4 (id)]
433 $default reduce using rule 3 (num)
438 0 $accept: exp . $end
440 $end shift, and go to state 5
447 $default reduce using rule 1 (exp)
454 $default reduce using rule 2 (exp)
459 0 $accept: exp $end .
469 ## -------------------- ##
470 ## %expect not enough. ##
471 ## -------------------- ##
473 AT_SETUP([%expect not enough])
479 exp: exp OP exp | NUM;
482 AT_BISON_CHECK([-o input.c input.y], 1, [],
483 [input.y: conflicts: 1 shift/reduce
484 input.y: expected 0 shift/reduce conflicts
489 ## --------------- ##
491 ## --------------- ##
493 AT_SETUP([%expect right])
499 exp: exp OP exp | NUM;
502 AT_BISON_CHECK([-o input.c input.y])
506 ## ------------------ ##
507 ## %expect too much. ##
508 ## ------------------ ##
510 AT_SETUP([%expect too much])
516 exp: exp OP exp | NUM;
519 AT_BISON_CHECK([-o input.c input.y], 1, [],
520 [input.y: conflicts: 1 shift/reduce
521 input.y: expected 2 shift/reduce conflicts
526 ## ------------------------------ ##
527 ## %expect with reduce conflicts ##
528 ## ------------------------------ ##
530 AT_SETUP([%expect with reduce conflicts])
535 program: a 'a' | a a;
539 AT_BISON_CHECK([-o input.c input.y], 1, [],
540 [input.y: conflicts: 1 reduce/reduce
541 input.y: expected 0 reduce/reduce conflicts
546 ## ------------------------------- ##
547 ## %no-default-prec without %prec ##
548 ## ------------------------------- ##
550 AT_SETUP([%no-default-prec without %prec])
566 AT_BISON_CHECK([-o input.c input.y], 0, [],
567 [[input.y: conflicts: 4 shift/reduce
572 ## ---------------------------- ##
573 ## %no-default-prec with %prec ##
574 ## ---------------------------- ##
576 AT_SETUP([%no-default-prec with %prec])
592 AT_BISON_CHECK([-o input.c input.y])
596 ## ---------------- ##
598 ## ---------------- ##
600 AT_SETUP([%default-prec])
616 AT_BISON_CHECK([-o input.c input.y])
620 ## ---------------------------------------------- ##
621 ## Unreachable States After Conflict Resolution. ##
622 ## ---------------------------------------------- ##
624 AT_SETUP([[Unreachable States After Conflict Resolution]])
626 # If conflict resolution makes states unreachable, remove those states, report
627 # rules that are then unused, and don't report conflicts in those states. Test
628 # what happens when a nonterminal becomes useless as a result of state removal
629 # since that causes lalr.o's goto map to be rewritten.
637 start: resolved_conflict 'a' reported_conflicts 'a' ;
639 /* S/R conflict resolved as reduce, so the state with item
640 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
641 * unreachable, and the associated production is useless. */
647 /* S/R conflict that need not be reported since it is unreachable because of
648 * the previous conflict resolution. Nonterminal unreachable1 and all its
649 * productions are useless. */
655 /* Likewise for a R/R conflict and nonterminal unreachable2. */
658 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
659 * when their states are renumbered due to state removal. */
668 AT_BISON_CHECK([[--report=all input.y]], 0, [],
669 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
670 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
671 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
672 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
673 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
674 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
675 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
676 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
679 AT_CHECK([[cat input.output]], 0,
680 [[Rules useless in parser due to conflicts
682 2 resolved_conflict: 'a' unreachable1
684 4 unreachable1: 'a' unreachable2
687 6 unreachable2: /* empty */
690 9 reported_conflicts: 'a'
694 State 4 conflicts: 1 shift/reduce
695 State 5 conflicts: 1 reduce/reduce
700 0 $accept: start $end
702 1 start: resolved_conflict 'a' reported_conflicts 'a'
704 2 resolved_conflict: 'a' unreachable1
707 4 unreachable1: 'a' unreachable2
710 6 unreachable2: /* empty */
713 8 reported_conflicts: 'a'
718 Terminals, with rules where they appear
725 Nonterminals, with rules where they appear
730 on left: 1, on right: 0
731 resolved_conflict (6)
732 on left: 2 3, on right: 1
734 on left: 4 5, on right: 2
736 on left: 6 7, on right: 4
737 reported_conflicts (9)
738 on left: 8 9 10, on right: 1
743 0 $accept: . start $end
744 1 start: . resolved_conflict 'a' reported_conflicts 'a'
745 2 resolved_conflict: . 'a' unreachable1
748 $default reduce using rule 3 (resolved_conflict)
751 resolved_conflict go to state 2
753 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
758 0 $accept: start . $end
760 $end shift, and go to state 3
765 1 start: resolved_conflict . 'a' reported_conflicts 'a'
767 'a' shift, and go to state 4
772 0 $accept: start $end .
779 1 start: resolved_conflict 'a' . reported_conflicts 'a'
780 8 reported_conflicts: . 'a'
784 'a' shift, and go to state 5
786 'a' [reduce using rule 10 (reported_conflicts)]
788 reported_conflicts go to state 6
793 8 reported_conflicts: 'a' . ['a']
796 'a' reduce using rule 8 (reported_conflicts)
797 'a' [reduce using rule 9 (reported_conflicts)]
798 $default reduce using rule 8 (reported_conflicts)
803 1 start: resolved_conflict 'a' reported_conflicts . 'a'
805 'a' shift, and go to state 7
810 1 start: resolved_conflict 'a' reported_conflicts 'a' .
812 $default reduce using rule 1 (start)
815 AT_DATA([[input-keep.y]],
816 [[%define lr.keep_unreachable_states
818 AT_CHECK([[cat input.y >> input-keep.y]])
820 AT_BISON_CHECK([[input-keep.y]], 0, [],
821 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
822 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
823 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
824 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
825 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
831 ## ------------------------------------------------------------ ##
832 ## Solved conflicts report for multiple reductions in a state. ##
833 ## ------------------------------------------------------------ ##
835 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
837 # Used to lose earlier solved conflict messages even within a single S/R/R.
857 empty_c1: %prec 'c' ;
858 empty_c2: %prec 'c' ;
859 empty_c3: %prec 'd' ;
861 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
862 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
865 0 $accept: . start $end
880 'b' shift, and go to state 1
882 'c' reduce using rule 13 (empty_c3)
883 $default reduce using rule 9 (empty_a)
886 empty_a go to state 3
887 empty_b go to state 4
888 empty_c1 go to state 5
889 empty_c2 go to state 6
890 empty_c3 go to state 7
892 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
893 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
894 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
895 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
896 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
905 ## ------------------------------------------------------------ ##
906 ## %nonassoc error actions for multiple reductions in a state. ##
907 ## ------------------------------------------------------------ ##
909 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
910 # multiple reductions in a state.
912 # For a %nonassoc error action token, used to print the first remaining
913 # reduction on that token without brackets.
915 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
918 [[%nonassoc 'a' 'b' 'c'
932 empty_c1: %prec 'c' ;
933 empty_c2: %prec 'c' ;
934 empty_c3: %prec 'c' ;
937 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
938 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
941 0 $accept: . start $end
956 'a' error (nonassociative)
957 'b' error (nonassociative)
958 'c' error (nonassociative)
960 'c' [reduce using rule 12 (empty_c2)]
961 'c' [reduce using rule 13 (empty_c3)]
964 empty_a go to state 2
965 empty_b go to state 3
966 empty_c1 go to state 4
967 empty_c2 go to state 5
968 empty_c3 go to state 6
970 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
971 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
972 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').