1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008, 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 ## parse.error=verbose and consistent errors. ##
144 ## ------------------------------------------- ##
146 AT_SETUP([[parse.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 *);
174 %define parse.error verbose
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 ## %precedence suffices. ##
476 ## ---------------------- ##
478 AT_SETUP([%precedence suffices])
485 "if" cond "then" stmt
486 | "if" cond "then" stmt "else" stmt
495 AT_BISON_CHECK([-o input.c input.y])
500 ## ------------------------------ ##
501 ## %precedence does not suffice. ##
502 ## ------------------------------ ##
504 AT_SETUP([%precedence does not suffice])
511 "if" cond "then" stmt
512 | "if" cond "then" stmt "else" stmt
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
530 ## -------------------------------- ##
531 ## Defaulted Conflicted Reduction. ##
532 ## -------------------------------- ##
534 # When there are RR conflicts, some rules are disabled. Usually it is
535 # simply displayed as:
537 # $end reduce using rule 3 (num)
538 # $end [reduce using rule 4 (id)]
540 # But when `reduce 3' is the default action, we'd produce:
542 # $end [reduce using rule 4 (id)]
543 # $default reduce using rule 3 (num)
545 # In this precise case (a reduction is masked by the default
546 # reduction), we make the `reduce 3' explicit:
548 # $end reduce using rule 3 (num)
549 # $end [reduce using rule 4 (id)]
550 # $default reduce using rule 3 (num)
552 # Maybe that's not the best display, but then, please propose something
555 AT_SETUP([Defaulted Conflicted Reduction])
556 AT_KEYWORDS([report])
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'
571 # Check the contents of the report.
572 AT_CHECK([cat input.output], [],
573 [[Rules useless in parser due to conflicts
578 State 1 conflicts: 1 reduce/reduce
593 Terminals, with rules where they appear
600 Nonterminals, with rules where they appear
605 on left: 1 2, on right: 0
607 on left: 3, on right: 1
609 on left: 4, on right: 2
614 0 $accept: . exp $end
620 '0' shift, and go to state 1
632 $end reduce using rule 3 (num)
633 $end [reduce using rule 4 (id)]
634 $default reduce using rule 3 (num)
639 0 $accept: exp . $end
641 $end shift, and go to state 5
648 $default reduce using rule 1 (exp)
655 $default reduce using rule 2 (exp)
660 0 $accept: exp $end .
670 ## -------------------- ##
671 ## %expect not enough. ##
672 ## -------------------- ##
674 AT_SETUP([%expect not enough])
680 exp: exp OP exp | NUM;
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
690 ## --------------- ##
692 ## --------------- ##
694 AT_SETUP([%expect right])
700 exp: exp OP exp | NUM;
703 AT_BISON_CHECK([-o input.c input.y])
707 ## ------------------ ##
708 ## %expect too much. ##
709 ## ------------------ ##
711 AT_SETUP([%expect too much])
717 exp: exp OP exp | NUM;
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
727 ## ------------------------------- ##
728 ## %expect with reduce conflicts. ##
729 ## ------------------------------- ##
731 AT_SETUP([%expect with reduce conflicts])
736 program: a 'a' | a a;
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
747 ## ------------------------- ##
748 ## %prec with user strings. ##
749 ## ------------------------- ##
751 AT_SETUP([%prec with user string])
760 AT_BISON_CHECK([-o input.c input.y])
764 ## -------------------------------- ##
765 ## %no-default-prec without %prec. ##
766 ## -------------------------------- ##
768 AT_SETUP([%no-default-prec without %prec])
784 AT_BISON_CHECK([-o input.c input.y], 0, [],
785 [[input.y: conflicts: 4 shift/reduce
790 ## ----------------------------- ##
791 ## %no-default-prec with %prec. ##
792 ## ----------------------------- ##
794 AT_SETUP([%no-default-prec with %prec])
810 AT_BISON_CHECK([-o input.c input.y])
814 ## --------------- ##
816 ## --------------- ##
818 AT_SETUP([%default-prec])
834 AT_BISON_CHECK([-o input.c input.y])
838 ## ---------------------------------------------- ##
839 ## Unreachable States After Conflict Resolution. ##
840 ## ---------------------------------------------- ##
842 AT_SETUP([[Unreachable States After Conflict Resolution]])
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.
855 start: resolved_conflict 'a' reported_conflicts 'a' ;
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. */
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. */
873 /* Likewise for a R/R conflict and nonterminal unreachable2. */
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. */
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 */
897 AT_CHECK([[cat input.output]], 0,
898 [[Rules useless in parser due to conflicts
900 2 resolved_conflict: 'a' unreachable1
902 4 unreachable1: 'a' unreachable2
905 6 unreachable2: /* empty */
908 9 reported_conflicts: 'a'
912 State 4 conflicts: 1 shift/reduce
913 State 5 conflicts: 1 reduce/reduce
918 0 $accept: start $end
920 1 start: resolved_conflict 'a' reported_conflicts 'a'
922 2 resolved_conflict: 'a' unreachable1
925 4 unreachable1: 'a' unreachable2
928 6 unreachable2: /* empty */
931 8 reported_conflicts: 'a'
936 Terminals, with rules where they appear
943 Nonterminals, with rules where they appear
948 on left: 1, on right: 0
949 resolved_conflict (6)
950 on left: 2 3, on right: 1
952 on left: 4 5, on right: 2
954 on left: 6 7, on right: 4
955 reported_conflicts (9)
956 on left: 8 9 10, on right: 1
961 0 $accept: . start $end
962 1 start: . resolved_conflict 'a' reported_conflicts 'a'
963 2 resolved_conflict: . 'a' unreachable1
966 $default reduce using rule 3 (resolved_conflict)
969 resolved_conflict go to state 2
971 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
976 0 $accept: start . $end
978 $end shift, and go to state 3
983 1 start: resolved_conflict . 'a' reported_conflicts 'a'
985 'a' shift, and go to state 4
990 0 $accept: start $end .
997 1 start: resolved_conflict 'a' . reported_conflicts 'a'
998 8 reported_conflicts: . 'a'
1002 'a' shift, and go to state 5
1004 'a' [reduce using rule 10 (reported_conflicts)]
1006 reported_conflicts go to state 6
1011 8 reported_conflicts: 'a' . ['a']
1014 'a' reduce using rule 8 (reported_conflicts)
1015 'a' [reduce using rule 9 (reported_conflicts)]
1016 $default reduce using rule 8 (reported_conflicts)
1021 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1023 'a' shift, and go to state 7
1028 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1030 $default reduce using rule 1 (start)
1033 AT_DATA([[input-keep.y]],
1034 [[%define lr.keep-unreachable-states
1036 AT_CHECK([[cat input.y >> input-keep.y]])
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 */
1049 ## ------------------------------------------------------------ ##
1050 ## Solved conflicts report for multiple reductions in a state. ##
1051 ## ------------------------------------------------------------ ##
1053 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1055 # Used to lose earlier solved conflict messages even within a single S/R/R.
1057 AT_DATA([[input.y]],
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' ;
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,
1083 0 $accept: . start $end
1096 13 empty_c3: . ['c']
1098 'b' shift, and go to state 1
1100 'c' reduce using rule 13 (empty_c3)
1101 $default reduce using rule 9 (empty_a)
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
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').
1123 ## ------------------------------------------------------------ ##
1124 ## %nonassoc error actions for multiple reductions in a state. ##
1125 ## ------------------------------------------------------------ ##
1127 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1128 # multiple reductions in a state.
1130 # For a %nonassoc error action token, used to print the first remaining
1131 # reduction on that token without brackets.
1133 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1135 AT_DATA([[input.y]],
1136 [[%nonassoc 'a' 'b' 'c'
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' ;
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,
1159 0 $accept: . start $end
1171 12 empty_c2: . ['c']
1172 13 empty_c3: . ['c']
1174 'a' error (nonassociative)
1175 'b' error (nonassociative)
1176 'c' error (nonassociative)
1178 'c' [reduce using rule 12 (empty_c2)]
1179 'c' [reduce using rule 13 (empty_c3)]
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
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').