1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002-2005, 2007-2010 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 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
102 [syntax error, unexpected '<'
105 AT_PARSER_CHECK([./input '0>0'])
106 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
107 [syntax error, unexpected '>'
110 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
111 [syntax error, unexpected '>'
114 # We must disable default reductions in inconsistent states in order to
115 # have an explicit list of all expected tokens. (However, unless we use
116 # canonical LR, lookahead sets are merged for different left contexts,
117 # so it is still possible to have extra incorrect tokens in the expected
118 # list. That just doesn't happen to be a problem for this test case.)
120 AT_BISON_CHECK([-Dlr.default-reductions=consistent -o input.c input.y])
123 AT_PARSER_CHECK([./input '0<0'])
124 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
125 [syntax error, unexpected '<', expecting $end
128 AT_PARSER_CHECK([./input '0>0'])
129 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
130 [syntax error, unexpected '>', expecting $end
133 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
134 [syntax error, unexpected '>', expecting $end
141 ## ------------------------------------------- ##
142 ## parse.error=verbose and consistent errors. ##
143 ## ------------------------------------------- ##
145 AT_SETUP([[parse.error=verbose and consistent errors]])
147 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
149 AT_BISON_CHECK([$1[ -o input.c input.y]])
150 AT_COMPILE([[input]])
152 m4_pushdef([AT_EXPECTING], [m4_if($3, [ab], [[, expecting 'a' or 'b']],
153 $3, [a], [[, expecting 'a']],
154 $3, [b], [[, expecting 'b']])])
156 AT_PARSER_CHECK([[./input]], [[1]], [],
157 [[syntax error, unexpected ]$2[]AT_EXPECTING[
160 m4_popdef([AT_EXPECTING])
164 AT_DATA_GRAMMAR([input.y],
169 void yyerror (char const *);
173 %define parse.error verbose
175 // The point isn't to test IELR here, but state merging happens to
176 // complicate the example.
181 // If yylval=0 here, then we know that the 'a' destructor is being
182 // invoked incorrectly for the 'b' set in the semantic action below.
183 // All 'a' tokens are returned by yylex, which sets yylval=1.
186 fprintf (stderr, "Wrong destructor.\n");
191 // The lookahead assigned by the semantic action isn't needed before
192 // either error action is encountered. In a previous version of Bison,
193 // this was a problem as it meant yychar was not translated into yytoken
194 // before either error action. The second error action thus invoked a
195 // destructor that it selected according to the incorrect yytoken. The
196 // first error action would have reported an incorrect unexpected token
197 // except that, due to another bug, the unexpected token is not reported
198 // at all because the error action is the default action in a consistent
199 // state. That bug still needs to be fixed.
200 start: error-reduce consistent-error 'a' { USE ($3); } ;
203 'a' 'a' consistent-reduction consistent-error 'a'
204 { USE (($1, $2, $5)); }
209 consistent-reduction: /*empty*/ {
210 assert (yychar == YYEMPTY);
217 | /*empty*/ %prec 'a'
220 // Provide another context in which all rules are useful so that this
221 // test case looks a little more realistic.
222 start: 'b' consistent-error 'b' ;
229 static char const *input = "aa";
235 yyerror (char const *msg)
237 fprintf (stderr, "%s\n", msg);
247 # See comments in grammar for why this test doesn't succeed.
250 AT_CONSISTENT_ERRORS_CHECK([], [['b']], [[none]])
251 AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=consistent]],
254 # Canonical LR doesn't foresee the error for 'a'!
255 AT_CONSISTENT_ERRORS_CHECK([[-Dlr.default-reductions=accepting]],
257 AT_CONSISTENT_ERRORS_CHECK([[-Flr.type=canonical-lr]], [[$end]], [[a]])
259 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
265 ## ------------------------- ##
266 ## Unresolved SR Conflicts. ##
267 ## ------------------------- ##
269 AT_SETUP([Unresolved SR Conflicts])
271 AT_KEYWORDS([report])
276 exp: exp OP exp | NUM;
279 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
280 [input.y: conflicts: 1 shift/reduce
283 # Check the contents of the report.
284 AT_CHECK([cat input.output], [],
285 [[State 5 conflicts: 1 shift/reduce
296 Terminals, with rules where they appear
304 Nonterminals, with rules where they appear
309 on left: 1 2, on right: 0 1
314 0 $accept: . exp $end
318 NUM shift, and go to state 1
327 $default reduce using rule 2 (exp)
332 0 $accept: exp . $end
335 $end shift, and go to state 3
336 OP shift, and go to state 4
341 0 $accept: exp $end .
352 NUM shift, and go to state 1
360 1 | exp OP exp . [$end, OP]
362 OP shift, and go to state 4
364 OP [reduce using rule 1 (exp)]
365 $default reduce using rule 1 (exp)
372 ## ----------------------- ##
373 ## Resolved SR Conflicts. ##
374 ## ----------------------- ##
376 AT_SETUP([Resolved SR Conflicts])
378 AT_KEYWORDS([report])
384 exp: exp OP exp | NUM;
387 AT_BISON_CHECK([-o input.c --report=all input.y])
389 # Check the contents of the report.
390 AT_CHECK([cat input.output], [],
399 Terminals, with rules where they appear
407 Nonterminals, with rules where they appear
412 on left: 1 2, on right: 0 1
417 0 $accept: . exp $end
421 NUM shift, and go to state 1
430 $default reduce using rule 2 (exp)
435 0 $accept: exp . $end
438 $end shift, and go to state 3
439 OP shift, and go to state 4
444 0 $accept: exp $end .
455 NUM shift, and go to state 1
463 1 | exp OP exp . [$end, OP]
465 $default reduce using rule 1 (exp)
467 Conflict between rule 1 and token OP resolved as reduce (%left OP).
473 ## ---------------------- ##
474 ## %precedence suffices. ##
475 ## ---------------------- ##
477 AT_SETUP([%precedence suffices])
484 "if" cond "then" stmt
485 | "if" cond "then" stmt "else" stmt
494 AT_BISON_CHECK([-o input.c input.y])
499 ## ------------------------------ ##
500 ## %precedence does not suffice. ##
501 ## ------------------------------ ##
503 AT_SETUP([%precedence does not suffice])
510 "if" cond "then" stmt
511 | "if" cond "then" stmt "else" stmt
521 AT_BISON_CHECK([-o input.c input.y], 0, [],
522 [[input.y: conflicts: 1 shift/reduce
523 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond
529 ## -------------------------------- ##
530 ## Defaulted Conflicted Reduction. ##
531 ## -------------------------------- ##
533 # When there are RR conflicts, some rules are disabled. Usually it is
534 # simply displayed as:
536 # $end reduce using rule 3 (num)
537 # $end [reduce using rule 4 (id)]
539 # But when `reduce 3' is the default action, we'd produce:
541 # $end [reduce using rule 4 (id)]
542 # $default reduce using rule 3 (num)
544 # In this precise case (a reduction is masked by the default
545 # reduction), we make the `reduce 3' explicit:
547 # $end reduce using rule 3 (num)
548 # $end [reduce using rule 4 (id)]
549 # $default reduce using rule 3 (num)
551 # Maybe that's not the best display, but then, please propose something
554 AT_SETUP([Defaulted Conflicted Reduction])
555 AT_KEYWORDS([report])
565 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
566 [[input.y: conflicts: 1 reduce/reduce
567 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
570 # Check the contents of the report.
571 AT_CHECK([cat input.output], [],
572 [[Rules useless in parser due to conflicts
577 State 1 conflicts: 1 reduce/reduce
592 Terminals, with rules where they appear
599 Nonterminals, with rules where they appear
604 on left: 1 2, on right: 0
606 on left: 3, on right: 1
608 on left: 4, on right: 2
613 0 $accept: . exp $end
619 '0' shift, and go to state 1
631 $end reduce using rule 3 (num)
632 $end [reduce using rule 4 (id)]
633 $default reduce using rule 3 (num)
638 0 $accept: exp . $end
640 $end shift, and go to state 5
647 $default reduce using rule 1 (exp)
654 $default reduce using rule 2 (exp)
659 0 $accept: exp $end .
669 ## -------------------- ##
670 ## %expect not enough. ##
671 ## -------------------- ##
673 AT_SETUP([%expect not enough])
679 exp: exp OP exp | NUM;
682 AT_BISON_CHECK([-o input.c input.y], 1, [],
683 [input.y: conflicts: 1 shift/reduce
684 input.y: expected 0 shift/reduce conflicts
689 ## --------------- ##
691 ## --------------- ##
693 AT_SETUP([%expect right])
699 exp: exp OP exp | NUM;
702 AT_BISON_CHECK([-o input.c input.y])
706 ## ------------------ ##
707 ## %expect too much. ##
708 ## ------------------ ##
710 AT_SETUP([%expect too much])
716 exp: exp OP exp | NUM;
719 AT_BISON_CHECK([-o input.c input.y], 1, [],
720 [input.y: conflicts: 1 shift/reduce
721 input.y: expected 2 shift/reduce conflicts
726 ## ------------------------------- ##
727 ## %expect with reduce conflicts. ##
728 ## ------------------------------- ##
730 AT_SETUP([%expect with reduce conflicts])
735 program: a 'a' | a a;
739 AT_BISON_CHECK([-o input.c input.y], 1, [],
740 [input.y: conflicts: 1 reduce/reduce
741 input.y: expected 0 reduce/reduce conflicts
746 ## ------------------------- ##
747 ## %prec with user strings. ##
748 ## ------------------------- ##
750 AT_SETUP([%prec with user string])
759 AT_BISON_CHECK([-o input.c input.y])
763 ## -------------------------------- ##
764 ## %no-default-prec without %prec. ##
765 ## -------------------------------- ##
767 AT_SETUP([%no-default-prec without %prec])
783 AT_BISON_CHECK([-o input.c input.y], 0, [],
784 [[input.y: conflicts: 4 shift/reduce
789 ## ----------------------------- ##
790 ## %no-default-prec with %prec. ##
791 ## ----------------------------- ##
793 AT_SETUP([%no-default-prec with %prec])
809 AT_BISON_CHECK([-o input.c input.y])
813 ## --------------- ##
815 ## --------------- ##
817 AT_SETUP([%default-prec])
833 AT_BISON_CHECK([-o input.c input.y])
837 ## ---------------------------------------------- ##
838 ## Unreachable States After Conflict Resolution. ##
839 ## ---------------------------------------------- ##
841 AT_SETUP([[Unreachable States After Conflict Resolution]])
843 # If conflict resolution makes states unreachable, remove those states, report
844 # rules that are then unused, and don't report conflicts in those states. Test
845 # what happens when a nonterminal becomes useless as a result of state removal
846 # since that causes lalr.o's goto map to be rewritten.
854 start: resolved_conflict 'a' reported_conflicts 'a' ;
856 /* S/R conflict resolved as reduce, so the state with item
857 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
858 * unreachable, and the associated production is useless. */
864 /* S/R conflict that need not be reported since it is unreachable because of
865 * the previous conflict resolution. Nonterminal unreachable1 and all its
866 * productions are useless. */
872 /* Likewise for a R/R conflict and nonterminal unreachable2. */
875 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
876 * when their states are renumbered due to state removal. */
885 AT_BISON_CHECK([[--report=all input.y]], 0, [],
886 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
887 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
888 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
889 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
890 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
891 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
892 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
893 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
896 AT_CHECK([[cat input.output]], 0,
897 [[Rules useless in parser due to conflicts
899 2 resolved_conflict: 'a' unreachable1
901 4 unreachable1: 'a' unreachable2
904 6 unreachable2: /* empty */
907 9 reported_conflicts: 'a'
911 State 4 conflicts: 1 shift/reduce
912 State 5 conflicts: 1 reduce/reduce
917 0 $accept: start $end
919 1 start: resolved_conflict 'a' reported_conflicts 'a'
921 2 resolved_conflict: 'a' unreachable1
924 4 unreachable1: 'a' unreachable2
927 6 unreachable2: /* empty */
930 8 reported_conflicts: 'a'
935 Terminals, with rules where they appear
942 Nonterminals, with rules where they appear
947 on left: 1, on right: 0
948 resolved_conflict (6)
949 on left: 2 3, on right: 1
951 on left: 4 5, on right: 2
953 on left: 6 7, on right: 4
954 reported_conflicts (9)
955 on left: 8 9 10, on right: 1
960 0 $accept: . start $end
961 1 start: . resolved_conflict 'a' reported_conflicts 'a'
962 2 resolved_conflict: . 'a' unreachable1
965 $default reduce using rule 3 (resolved_conflict)
968 resolved_conflict go to state 2
970 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
975 0 $accept: start . $end
977 $end shift, and go to state 3
982 1 start: resolved_conflict . 'a' reported_conflicts 'a'
984 'a' shift, and go to state 4
989 0 $accept: start $end .
996 1 start: resolved_conflict 'a' . reported_conflicts 'a'
997 8 reported_conflicts: . 'a'
1001 'a' shift, and go to state 5
1003 'a' [reduce using rule 10 (reported_conflicts)]
1005 reported_conflicts go to state 6
1010 8 reported_conflicts: 'a' . ['a']
1013 'a' reduce using rule 8 (reported_conflicts)
1014 'a' [reduce using rule 9 (reported_conflicts)]
1015 $default reduce using rule 8 (reported_conflicts)
1020 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1022 'a' shift, and go to state 7
1027 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1029 $default reduce using rule 1 (start)
1032 AT_DATA([[input-keep.y]],
1033 [[%define lr.keep-unreachable-states
1035 AT_CHECK([[cat input.y >> input-keep.y]])
1037 AT_BISON_CHECK([[input-keep.y]], 0, [],
1038 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
1039 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1040 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1041 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1042 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1048 ## ------------------------------------------------------------ ##
1049 ## Solved conflicts report for multiple reductions in a state. ##
1050 ## ------------------------------------------------------------ ##
1052 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1054 # Used to lose earlier solved conflict messages even within a single S/R/R.
1056 AT_DATA([[input.y]],
1072 empty_a: %prec 'a' ;
1073 empty_b: %prec 'b' ;
1074 empty_c1: %prec 'c' ;
1075 empty_c2: %prec 'c' ;
1076 empty_c3: %prec 'd' ;
1078 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1079 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1082 0 $accept: . start $end
1095 13 empty_c3: . ['c']
1097 'b' shift, and go to state 1
1099 'c' reduce using rule 13 (empty_c3)
1100 $default reduce using rule 9 (empty_a)
1103 empty_a go to state 3
1104 empty_b go to state 4
1105 empty_c1 go to state 5
1106 empty_c2 go to state 6
1107 empty_c3 go to state 7
1109 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1110 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1111 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1112 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1113 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1122 ## ------------------------------------------------------------ ##
1123 ## %nonassoc error actions for multiple reductions in a state. ##
1124 ## ------------------------------------------------------------ ##
1126 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1127 # multiple reductions in a state.
1129 # For a %nonassoc error action token, used to print the first remaining
1130 # reduction on that token without brackets.
1132 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1134 AT_DATA([[input.y]],
1135 [[%nonassoc 'a' 'b' 'c'
1147 empty_a: %prec 'a' ;
1148 empty_b: %prec 'b' ;
1149 empty_c1: %prec 'c' ;
1150 empty_c2: %prec 'c' ;
1151 empty_c3: %prec 'c' ;
1154 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1155 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1158 0 $accept: . start $end
1170 12 empty_c2: . ['c']
1171 13 empty_c3: . ['c']
1173 'a' error (nonassociative)
1174 'b' error (nonassociative)
1175 'c' error (nonassociative)
1177 'c' [reduce using rule 12 (empty_c2)]
1178 'c' [reduce using rule 13 (empty_c3)]
1181 empty_a go to state 2
1182 empty_b go to state 3
1183 empty_c1 go to state 4
1184 empty_c2 go to state 5
1185 empty_c3 go to state 6
1187 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1188 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1189 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').