1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007, 2009, 2010 Free
4 # Software Foundation, Inc.
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_DATA_GRAMMAR([input.y],
155 void yyerror (char const *);
172 static char const *input = "]$3[";
178 yyerror (char const *msg)
180 fprintf (stderr, "%s\n", msg);
189 AT_BISON_CHECK([[-o input.c input.y]])
190 AT_COMPILE([[input]])
192 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
193 $5, [a], [[, expecting 'a']],
194 $5, [b], [[, expecting 'b']])])
196 AT_PARSER_CHECK([[./input]], [[1]], [[]],
197 [[syntax error, unexpected ]$4[]AT_EXPECTING[
200 m4_popdef([AT_EXPECTING])
204 m4_pushdef([AT_USER_ACTION_GRAMMAR],
207 // If yylval=0 here, then we know that the 'a' destructor is being
208 // invoked incorrectly for the 'b' set in the semantic action below.
209 // All 'a' tokens are returned by yylex, which sets yylval=1.
212 fprintf (stderr, "Wrong destructor.\n");
215 // The lookahead assigned by the semantic action isn't needed before
216 // either error action is encountered. In a previous version of Bison,
217 // this was a problem as it meant yychar was not translated into yytoken
218 // before either error action. The second error action thus invoked a
219 // destructor that it selected according to the incorrect yytoken. The
220 // first error action would have reported an incorrect unexpected token
221 // except that, due to another bug, the unexpected token is not reported
222 // at all because the error action is the default action in a consistent
223 // state. That bug still needs to be fixed.
224 start: error-reduce consistent-error 'a' { USE ($][3); } ;
227 'a' 'a' consistent-reduction consistent-error 'a'
228 { USE (($][1, $][2, $][5)); }
233 consistent-reduction: /*empty*/ {
234 assert (yychar == YYEMPTY);
241 | /*empty*/ %prec 'a'
244 // Provide another context in which all rules are useful so that this
245 // test case looks a little more realistic.
246 start: 'b' consistent-error 'b' ;
248 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
250 # See comments in grammar for why this test doesn't succeed.
253 AT_CONSISTENT_ERRORS_CHECK([[]],
254 [AT_USER_ACTION_GRAMMAR],
255 [AT_USER_ACTION_INPUT],
257 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]],
258 [AT_USER_ACTION_GRAMMAR],
259 [AT_USER_ACTION_INPUT],
262 # Canonical LR doesn't foresee the error for 'a'!
263 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions accepting]],
264 [AT_USER_ACTION_GRAMMAR],
265 [AT_USER_ACTION_INPUT],
267 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
268 [AT_USER_ACTION_GRAMMAR],
269 [AT_USER_ACTION_INPUT],
272 m4_popdef([AT_USER_ACTION_GRAMMAR])
273 m4_popdef([AT_USER_ACTION_INPUT])
275 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
281 ## ------------------------- ##
282 ## Unresolved SR Conflicts. ##
283 ## ------------------------- ##
285 AT_SETUP([Unresolved SR Conflicts])
287 AT_KEYWORDS([report])
292 exp: exp OP exp | NUM;
295 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
296 [input.y: conflicts: 1 shift/reduce
299 # Check the contents of the report.
300 AT_CHECK([cat input.output], [],
301 [[State 5 conflicts: 1 shift/reduce
312 Terminals, with rules where they appear
320 Nonterminals, with rules where they appear
325 on left: 1 2, on right: 0 1
330 0 $accept: . exp $end
334 NUM shift, and go to state 1
343 $default reduce using rule 2 (exp)
348 0 $accept: exp . $end
351 $end shift, and go to state 3
352 OP shift, and go to state 4
357 0 $accept: exp $end .
368 NUM shift, and go to state 1
376 1 | exp OP exp . [$end, OP]
378 OP shift, and go to state 4
380 OP [reduce using rule 1 (exp)]
381 $default reduce using rule 1 (exp)
388 ## ----------------------- ##
389 ## Resolved SR Conflicts. ##
390 ## ----------------------- ##
392 AT_SETUP([Resolved SR Conflicts])
394 AT_KEYWORDS([report])
400 exp: exp OP exp | NUM;
403 AT_BISON_CHECK([-o input.c --report=all input.y])
405 # Check the contents of the report.
406 AT_CHECK([cat input.output], [],
415 Terminals, with rules where they appear
423 Nonterminals, with rules where they appear
428 on left: 1 2, on right: 0 1
433 0 $accept: . exp $end
437 NUM shift, and go to state 1
446 $default reduce using rule 2 (exp)
451 0 $accept: exp . $end
454 $end shift, and go to state 3
455 OP shift, and go to state 4
460 0 $accept: exp $end .
471 NUM shift, and go to state 1
479 1 | exp OP exp . [$end, OP]
481 $default reduce using rule 1 (exp)
483 Conflict between rule 1 and token OP resolved as reduce (%left OP).
489 ## -------------------------------- ##
490 ## Defaulted Conflicted Reduction. ##
491 ## -------------------------------- ##
493 # When there are RR conflicts, some rules are disabled. Usually it is
494 # simply displayed as:
496 # $end reduce using rule 3 (num)
497 # $end [reduce using rule 4 (id)]
499 # But when `reduce 3' is the default action, we'd produce:
501 # $end [reduce using rule 4 (id)]
502 # $default reduce using rule 3 (num)
504 # In this precise case (a reduction is masked by the default
505 # reduction), we make the `reduce 3' explicit:
507 # $end reduce using rule 3 (num)
508 # $end [reduce using rule 4 (id)]
509 # $default reduce using rule 3 (num)
511 # Maybe that's not the best display, but then, please propose something
514 AT_SETUP([Defaulted Conflicted Reduction])
515 AT_KEYWORDS([report])
525 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
526 [[input.y: conflicts: 1 reduce/reduce
527 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
530 # Check the contents of the report.
531 AT_CHECK([cat input.output], [],
532 [[Rules useless in parser due to conflicts
537 State 1 conflicts: 1 reduce/reduce
552 Terminals, with rules where they appear
559 Nonterminals, with rules where they appear
564 on left: 1 2, on right: 0
566 on left: 3, on right: 1
568 on left: 4, on right: 2
573 0 $accept: . exp $end
579 '0' shift, and go to state 1
591 $end reduce using rule 3 (num)
592 $end [reduce using rule 4 (id)]
593 $default reduce using rule 3 (num)
598 0 $accept: exp . $end
600 $end shift, and go to state 5
607 $default reduce using rule 1 (exp)
614 $default reduce using rule 2 (exp)
619 0 $accept: exp $end .
629 ## -------------------- ##
630 ## %expect not enough. ##
631 ## -------------------- ##
633 AT_SETUP([%expect not enough])
639 exp: exp OP exp | NUM;
642 AT_BISON_CHECK([-o input.c input.y], 1, [],
643 [input.y: conflicts: 1 shift/reduce
644 input.y: expected 0 shift/reduce conflicts
649 ## --------------- ##
651 ## --------------- ##
653 AT_SETUP([%expect right])
659 exp: exp OP exp | NUM;
662 AT_BISON_CHECK([-o input.c input.y])
666 ## ------------------ ##
667 ## %expect too much. ##
668 ## ------------------ ##
670 AT_SETUP([%expect too much])
676 exp: exp OP exp | NUM;
679 AT_BISON_CHECK([-o input.c input.y], 1, [],
680 [input.y: conflicts: 1 shift/reduce
681 input.y: expected 2 shift/reduce conflicts
686 ## ------------------------------- ##
687 ## %expect with reduce conflicts. ##
688 ## ------------------------------- ##
690 AT_SETUP([%expect with reduce conflicts])
695 program: a 'a' | a a;
699 AT_BISON_CHECK([-o input.c input.y], 1, [],
700 [input.y: conflicts: 1 reduce/reduce
701 input.y: expected 0 reduce/reduce conflicts
706 ## ------------------------- ##
707 ## %prec with user strings. ##
708 ## ------------------------- ##
710 AT_SETUP([%prec with user string])
719 AT_BISON_CHECK([-o input.c input.y])
723 ## -------------------------------- ##
724 ## %no-default-prec without %prec. ##
725 ## -------------------------------- ##
727 AT_SETUP([%no-default-prec without %prec])
743 AT_BISON_CHECK([-o input.c input.y], 0, [],
744 [[input.y: conflicts: 4 shift/reduce
749 ## ----------------------------- ##
750 ## %no-default-prec with %prec. ##
751 ## ----------------------------- ##
753 AT_SETUP([%no-default-prec with %prec])
769 AT_BISON_CHECK([-o input.c input.y])
773 ## --------------- ##
775 ## --------------- ##
777 AT_SETUP([%default-prec])
793 AT_BISON_CHECK([-o input.c input.y])
797 ## ---------------------------------------------- ##
798 ## Unreachable States After Conflict Resolution. ##
799 ## ---------------------------------------------- ##
801 AT_SETUP([[Unreachable States After Conflict Resolution]])
803 # If conflict resolution makes states unreachable, remove those states, report
804 # rules that are then unused, and don't report conflicts in those states. Test
805 # what happens when a nonterminal becomes useless as a result of state removal
806 # since that causes lalr.o's goto map to be rewritten.
814 start: resolved_conflict 'a' reported_conflicts 'a' ;
816 /* S/R conflict resolved as reduce, so the state with item
817 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
818 * unreachable, and the associated production is useless. */
824 /* S/R conflict that need not be reported since it is unreachable because of
825 * the previous conflict resolution. Nonterminal unreachable1 and all its
826 * productions are useless. */
832 /* Likewise for a R/R conflict and nonterminal unreachable2. */
835 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
836 * when their states are renumbered due to state removal. */
845 AT_BISON_CHECK([[--report=all input.y]], 0, [],
846 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
847 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
848 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
849 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
850 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
851 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
852 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
853 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
856 AT_CHECK([[cat input.output]], 0,
857 [[Rules useless in parser due to conflicts
859 2 resolved_conflict: 'a' unreachable1
861 4 unreachable1: 'a' unreachable2
864 6 unreachable2: /* empty */
867 9 reported_conflicts: 'a'
871 State 4 conflicts: 1 shift/reduce
872 State 5 conflicts: 1 reduce/reduce
877 0 $accept: start $end
879 1 start: resolved_conflict 'a' reported_conflicts 'a'
881 2 resolved_conflict: 'a' unreachable1
884 4 unreachable1: 'a' unreachable2
887 6 unreachable2: /* empty */
890 8 reported_conflicts: 'a'
895 Terminals, with rules where they appear
902 Nonterminals, with rules where they appear
907 on left: 1, on right: 0
908 resolved_conflict (6)
909 on left: 2 3, on right: 1
911 on left: 4 5, on right: 2
913 on left: 6 7, on right: 4
914 reported_conflicts (9)
915 on left: 8 9 10, on right: 1
920 0 $accept: . start $end
921 1 start: . resolved_conflict 'a' reported_conflicts 'a'
922 2 resolved_conflict: . 'a' unreachable1
925 $default reduce using rule 3 (resolved_conflict)
928 resolved_conflict go to state 2
930 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
935 0 $accept: start . $end
937 $end shift, and go to state 3
942 1 start: resolved_conflict . 'a' reported_conflicts 'a'
944 'a' shift, and go to state 4
949 0 $accept: start $end .
956 1 start: resolved_conflict 'a' . reported_conflicts 'a'
957 8 reported_conflicts: . 'a'
961 'a' shift, and go to state 5
963 'a' [reduce using rule 10 (reported_conflicts)]
965 reported_conflicts go to state 6
970 8 reported_conflicts: 'a' . ['a']
973 'a' reduce using rule 8 (reported_conflicts)
974 'a' [reduce using rule 9 (reported_conflicts)]
975 $default reduce using rule 8 (reported_conflicts)
980 1 start: resolved_conflict 'a' reported_conflicts . 'a'
982 'a' shift, and go to state 7
987 1 start: resolved_conflict 'a' reported_conflicts 'a' .
989 $default reduce using rule 1 (start)
992 AT_DATA([[input-keep.y]],
993 [[%define lr.keep-unreachable-states
995 AT_CHECK([[cat input.y >> input-keep.y]])
997 AT_BISON_CHECK([[input-keep.y]], 0, [],
998 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
999 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1000 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1001 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1002 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1008 ## ------------------------------------------------------------ ##
1009 ## Solved conflicts report for multiple reductions in a state. ##
1010 ## ------------------------------------------------------------ ##
1012 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1014 # Used to lose earlier solved conflict messages even within a single S/R/R.
1016 AT_DATA([[input.y]],
1032 empty_a: %prec 'a' ;
1033 empty_b: %prec 'b' ;
1034 empty_c1: %prec 'c' ;
1035 empty_c2: %prec 'c' ;
1036 empty_c3: %prec 'd' ;
1038 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1039 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1042 0 $accept: . start $end
1055 13 empty_c3: . ['c']
1057 'b' shift, and go to state 1
1059 'c' reduce using rule 13 (empty_c3)
1060 $default reduce using rule 9 (empty_a)
1063 empty_a go to state 3
1064 empty_b go to state 4
1065 empty_c1 go to state 5
1066 empty_c2 go to state 6
1067 empty_c3 go to state 7
1069 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1070 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1071 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1072 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1073 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1082 ## ------------------------------------------------------------ ##
1083 ## %nonassoc error actions for multiple reductions in a state. ##
1084 ## ------------------------------------------------------------ ##
1086 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1087 # multiple reductions in a state.
1089 # For a %nonassoc error action token, used to print the first remaining
1090 # reduction on that token without brackets.
1092 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1094 AT_DATA([[input.y]],
1095 [[%nonassoc 'a' 'b' 'c'
1107 empty_a: %prec 'a' ;
1108 empty_b: %prec 'b' ;
1109 empty_c1: %prec 'c' ;
1110 empty_c2: %prec 'c' ;
1111 empty_c3: %prec 'c' ;
1114 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1115 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1118 0 $accept: . start $end
1130 12 empty_c2: . ['c']
1131 13 empty_c3: . ['c']
1133 'a' error (nonassociative)
1134 'b' error (nonassociative)
1135 'c' error (nonassociative)
1137 'c' [reduce using rule 12 (empty_c2)]
1138 'c' [reduce using rule 13 (empty_c3)]
1141 empty_a go to state 2
1142 empty_b go to state 3
1143 empty_c1 go to state 4
1144 empty_c2 go to state 5
1145 empty_c3 go to state 6
1147 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1148 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1149 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').