1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007, 2009 Free Software
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.
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.
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/>.
19 AT_BANNER([[Conflicts.]])
22 ## ---------------- ##
24 ## ---------------- ##
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.
31 AT_SETUP([S/R in initial])
37 e: 'e' | /* Nothing. */;
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 */
47 ## ------------------- ##
48 ## %nonassoc and eof. ##
49 ## ------------------- ##
51 AT_SETUP([%nonassoc and eof])
53 AT_DATA_GRAMMAR([input.y],
60 #define YYERROR_VERBOSE 1
62 yyerror (const char *msg)
64 fprintf (stderr, "%s\n", msg);
67 /* The current argument. */
68 static const char *input;
74 if (! (toknum <= strlen (input)))
76 return input[toknum++];
90 main (int argc, const char *argv[])
92 input = argc <= 1 ? "" : argv[1];
97 # Specify the output files to avoid problems on different file systems.
98 AT_BISON_CHECK([-o input.c input.y])
101 AT_PARSER_CHECK([./input '0<0'])
102 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
103 [syntax error, unexpected '<'
106 AT_PARSER_CHECK([./input '0>0'])
107 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
108 [syntax error, unexpected '>'
111 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
112 [syntax error, unexpected '>'
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.)
121 AT_BISON_CHECK([-Dlr.default-reductions=consistent -o input.c input.y])
124 AT_PARSER_CHECK([./input '0<0'])
125 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
126 [syntax error, unexpected '<', expecting $end
129 AT_PARSER_CHECK([./input '0>0'])
130 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
131 [syntax error, unexpected '>', expecting $end
134 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
135 [syntax error, unexpected '>', expecting $end
142 ## -------------------------------------- ##
143 ## %error-verbose and consistent errors. ##
144 ## -------------------------------------- ##
146 AT_SETUP([[%error-verbose and consistent errors]])
148 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
150 AT_BISON_CHECK([$1[ -o input.c input.y]])
151 AT_COMPILE([[input]])
153 m4_pushdef([AT_EXPECTING], [m4_if($3, [ab], [[, expecting 'a' or 'b']],
154 $3, [a], [[, expecting 'a']],
155 $3, [b], [[, expecting 'b']])])
157 AT_PARSER_CHECK([[./input]], [[1]], [],
158 [[syntax error, unexpected ]$2[]AT_EXPECTING[
161 m4_popdef([AT_EXPECTING])
165 AT_DATA_GRAMMAR([input.y],
170 void yyerror (char const *);
176 // The point isn't to test IELR here, but state merging happens to
177 // complicate the example.
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.
187 fprintf (stderr, "Wrong destructor.\n");
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); } ;
204 'a' 'a' consistent-reduction consistent-error 'a'
205 { USE (($1, $2, $5)); }
210 consistent-reduction: /*empty*/ {
211 assert (yychar == YYEMPTY);
218 | /*empty*/ %prec 'a'
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' ;
230 static char const *input = "aa";
236 yyerror (char const *msg)
238 fprintf (stderr, "%s\n", msg);
248 # See comments in grammar for why this test doesn't succeed.
251 AT_CONSISTENT_ERRORS_CHECK([], [['b']], [[none]])
252 AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=consistent]],
255 # Canonical LR doesn't foresee the error for 'a'!
256 AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=accepting]],
258 AT_CONSISTENT_ERRORS_CHECK([[-Flr.type=canonical-lr]], [[$end]], [[a]])
260 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
266 ## ------------------------- ##
267 ## Unresolved SR Conflicts. ##
268 ## ------------------------- ##
270 AT_SETUP([Unresolved SR Conflicts])
272 AT_KEYWORDS([report])
277 exp: exp OP exp | NUM;
280 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
281 [input.y: conflicts: 1 shift/reduce
284 # Check the contents of the report.
285 AT_CHECK([cat input.output], [],
286 [[State 5 conflicts: 1 shift/reduce
297 Terminals, with rules where they appear
305 Nonterminals, with rules where they appear
310 on left: 1 2, on right: 0 1
315 0 $accept: . exp $end
319 NUM shift, and go to state 1
328 $default reduce using rule 2 (exp)
333 0 $accept: exp . $end
336 $end shift, and go to state 3
337 OP shift, and go to state 4
342 0 $accept: exp $end .
353 NUM shift, and go to state 1
361 1 | exp OP exp . [$end, OP]
363 OP shift, and go to state 4
365 OP [reduce using rule 1 (exp)]
366 $default reduce using rule 1 (exp)
373 ## ----------------------- ##
374 ## Resolved SR Conflicts. ##
375 ## ----------------------- ##
377 AT_SETUP([Resolved SR Conflicts])
379 AT_KEYWORDS([report])
385 exp: exp OP exp | NUM;
388 AT_BISON_CHECK([-o input.c --report=all input.y])
390 # Check the contents of the report.
391 AT_CHECK([cat input.output], [],
400 Terminals, with rules where they appear
408 Nonterminals, with rules where they appear
413 on left: 1 2, on right: 0 1
418 0 $accept: . exp $end
422 NUM shift, and go to state 1
431 $default reduce using rule 2 (exp)
436 0 $accept: exp . $end
439 $end shift, and go to state 3
440 OP shift, and go to state 4
445 0 $accept: exp $end .
456 NUM shift, and go to state 1
464 1 | exp OP exp . [$end, OP]
466 $default reduce using rule 1 (exp)
468 Conflict between rule 1 and token OP resolved as reduce (%left OP).
474 ## -------------------------------- ##
475 ## Defaulted Conflicted Reduction. ##
476 ## -------------------------------- ##
478 # When there are RR conflicts, some rules are disabled. Usually it is
479 # simply displayed as:
481 # $end reduce using rule 3 (num)
482 # $end [reduce using rule 4 (id)]
484 # But when `reduce 3' is the default action, we'd produce:
486 # $end [reduce using rule 4 (id)]
487 # $default reduce using rule 3 (num)
489 # In this precise case (a reduction is masked by the default
490 # reduction), we make the `reduce 3' explicit:
492 # $end reduce using rule 3 (num)
493 # $end [reduce using rule 4 (id)]
494 # $default reduce using rule 3 (num)
496 # Maybe that's not the best display, but then, please propose something
499 AT_SETUP([Defaulted Conflicted Reduction])
500 AT_KEYWORDS([report])
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'
515 # Check the contents of the report.
516 AT_CHECK([cat input.output], [],
517 [[Rules useless in parser due to conflicts
522 State 1 conflicts: 1 reduce/reduce
537 Terminals, with rules where they appear
544 Nonterminals, with rules where they appear
549 on left: 1 2, on right: 0
551 on left: 3, on right: 1
553 on left: 4, on right: 2
558 0 $accept: . exp $end
564 '0' shift, and go to state 1
576 $end reduce using rule 3 (num)
577 $end [reduce using rule 4 (id)]
578 $default reduce using rule 3 (num)
583 0 $accept: exp . $end
585 $end shift, and go to state 5
592 $default reduce using rule 1 (exp)
599 $default reduce using rule 2 (exp)
604 0 $accept: exp $end .
614 ## -------------------- ##
615 ## %expect not enough. ##
616 ## -------------------- ##
618 AT_SETUP([%expect not enough])
624 exp: exp OP exp | NUM;
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
634 ## --------------- ##
636 ## --------------- ##
638 AT_SETUP([%expect right])
644 exp: exp OP exp | NUM;
647 AT_BISON_CHECK([-o input.c input.y])
651 ## ------------------ ##
652 ## %expect too much. ##
653 ## ------------------ ##
655 AT_SETUP([%expect too much])
661 exp: exp OP exp | NUM;
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
671 ## ------------------------------- ##
672 ## %expect with reduce conflicts. ##
673 ## ------------------------------- ##
675 AT_SETUP([%expect with reduce conflicts])
680 program: a 'a' | a a;
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
691 ## ------------------------- ##
692 ## %prec with user strings. ##
693 ## ------------------------- ##
695 AT_SETUP([%prec with user string])
704 AT_BISON_CHECK([-o input.c input.y])
708 ## -------------------------------- ##
709 ## %no-default-prec without %prec. ##
710 ## -------------------------------- ##
712 AT_SETUP([%no-default-prec without %prec])
728 AT_BISON_CHECK([-o input.c input.y], 0, [],
729 [[input.y: conflicts: 4 shift/reduce
734 ## ----------------------------- ##
735 ## %no-default-prec with %prec. ##
736 ## ----------------------------- ##
738 AT_SETUP([%no-default-prec with %prec])
754 AT_BISON_CHECK([-o input.c input.y])
758 ## --------------- ##
760 ## --------------- ##
762 AT_SETUP([%default-prec])
778 AT_BISON_CHECK([-o input.c input.y])
782 ## ---------------------------------------------- ##
783 ## Unreachable States After Conflict Resolution. ##
784 ## ---------------------------------------------- ##
786 AT_SETUP([[Unreachable States After Conflict Resolution]])
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.
799 start: resolved_conflict 'a' reported_conflicts 'a' ;
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. */
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. */
817 /* Likewise for a R/R conflict and nonterminal unreachable2. */
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. */
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 */
841 AT_CHECK([[cat input.output]], 0,
842 [[Rules useless in parser due to conflicts
844 2 resolved_conflict: 'a' unreachable1
846 4 unreachable1: 'a' unreachable2
849 6 unreachable2: /* empty */
852 9 reported_conflicts: 'a'
856 State 4 conflicts: 1 shift/reduce
857 State 5 conflicts: 1 reduce/reduce
862 0 $accept: start $end
864 1 start: resolved_conflict 'a' reported_conflicts 'a'
866 2 resolved_conflict: 'a' unreachable1
869 4 unreachable1: 'a' unreachable2
872 6 unreachable2: /* empty */
875 8 reported_conflicts: 'a'
880 Terminals, with rules where they appear
887 Nonterminals, with rules where they appear
892 on left: 1, on right: 0
893 resolved_conflict (6)
894 on left: 2 3, on right: 1
896 on left: 4 5, on right: 2
898 on left: 6 7, on right: 4
899 reported_conflicts (9)
900 on left: 8 9 10, on right: 1
905 0 $accept: . start $end
906 1 start: . resolved_conflict 'a' reported_conflicts 'a'
907 2 resolved_conflict: . 'a' unreachable1
910 $default reduce using rule 3 (resolved_conflict)
913 resolved_conflict go to state 2
915 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
920 0 $accept: start . $end
922 $end shift, and go to state 3
927 1 start: resolved_conflict . 'a' reported_conflicts 'a'
929 'a' shift, and go to state 4
934 0 $accept: start $end .
941 1 start: resolved_conflict 'a' . reported_conflicts 'a'
942 8 reported_conflicts: . 'a'
946 'a' shift, and go to state 5
948 'a' [reduce using rule 10 (reported_conflicts)]
950 reported_conflicts go to state 6
955 8 reported_conflicts: 'a' . ['a']
958 'a' reduce using rule 8 (reported_conflicts)
959 'a' [reduce using rule 9 (reported_conflicts)]
960 $default reduce using rule 8 (reported_conflicts)
965 1 start: resolved_conflict 'a' reported_conflicts . 'a'
967 'a' shift, and go to state 7
972 1 start: resolved_conflict 'a' reported_conflicts 'a' .
974 $default reduce using rule 1 (start)
977 AT_DATA([[input-keep.y]],
978 [[%define lr.keep-unreachable-states
980 AT_CHECK([[cat input.y >> input-keep.y]])
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 */
993 ## ------------------------------------------------------------ ##
994 ## Solved conflicts report for multiple reductions in a state. ##
995 ## ------------------------------------------------------------ ##
997 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
999 # Used to lose earlier solved conflict messages even within a single S/R/R.
1001 AT_DATA([[input.y]],
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' ;
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,
1027 0 $accept: . start $end
1040 13 empty_c3: . ['c']
1042 'b' shift, and go to state 1
1044 'c' reduce using rule 13 (empty_c3)
1045 $default reduce using rule 9 (empty_a)
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
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').
1067 ## ------------------------------------------------------------ ##
1068 ## %nonassoc error actions for multiple reductions in a state. ##
1069 ## ------------------------------------------------------------ ##
1071 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1072 # multiple reductions in a state.
1074 # For a %nonassoc error action token, used to print the first remaining
1075 # reduction on that token without brackets.
1077 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1079 AT_DATA([[input.y]],
1080 [[%nonassoc 'a' 'b' 'c'
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' ;
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,
1103 0 $accept: . start $end
1115 12 empty_c2: . ['c']
1116 13 empty_c3: . ['c']
1118 'a' error (nonassociative)
1119 'b' error (nonassociative)
1120 'c' error (nonassociative)
1122 'c' [reduce using rule 12 (empty_c2)]
1123 'c' [reduce using rule 13 (empty_c3)]
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
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').