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 ## Unresolved SR Conflicts. ##
144 ## ------------------------- ##
146 AT_SETUP([Unresolved SR Conflicts])
148 AT_KEYWORDS([report])
153 exp: exp OP exp | NUM;
156 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
157 [input.y: conflicts: 1 shift/reduce
160 # Check the contents of the report.
161 AT_CHECK([cat input.output], [],
162 [[State 5 conflicts: 1 shift/reduce
173 Terminals, with rules where they appear
181 Nonterminals, with rules where they appear
186 on left: 1 2, on right: 0 1
191 0 $accept: . exp $end
195 NUM shift, and go to state 1
204 $default reduce using rule 2 (exp)
209 0 $accept: exp . $end
212 $end shift, and go to state 3
213 OP shift, and go to state 4
218 0 $accept: exp $end .
229 NUM shift, and go to state 1
237 1 | exp OP exp . [$end, OP]
239 OP shift, and go to state 4
241 OP [reduce using rule 1 (exp)]
242 $default reduce using rule 1 (exp)
249 ## ----------------------- ##
250 ## Resolved SR Conflicts. ##
251 ## ----------------------- ##
253 AT_SETUP([Resolved SR Conflicts])
255 AT_KEYWORDS([report])
261 exp: exp OP exp | NUM;
264 AT_BISON_CHECK([-o input.c --report=all input.y])
266 # Check the contents of the report.
267 AT_CHECK([cat input.output], [],
276 Terminals, with rules where they appear
284 Nonterminals, with rules where they appear
289 on left: 1 2, on right: 0 1
294 0 $accept: . exp $end
298 NUM shift, and go to state 1
307 $default reduce using rule 2 (exp)
312 0 $accept: exp . $end
315 $end shift, and go to state 3
316 OP shift, and go to state 4
321 0 $accept: exp $end .
332 NUM shift, and go to state 1
340 1 | exp OP exp . [$end, OP]
342 $default reduce using rule 1 (exp)
344 Conflict between rule 1 and token OP resolved as reduce (%left OP).
350 ## ---------------------- ##
351 ## %precedence suffices. ##
352 ## ---------------------- ##
354 AT_SETUP([%precedence suffices])
361 "if" cond "then" stmt
362 | "if" cond "then" stmt "else" stmt
371 AT_BISON_CHECK([-o input.c input.y])
376 ## ------------------------------ ##
377 ## %precedence does not suffice. ##
378 ## ------------------------------ ##
380 AT_SETUP([%precedence does not suffice])
387 "if" cond "then" stmt
388 | "if" cond "then" stmt "else" stmt
398 AT_BISON_CHECK([-o input.c input.y], 0, [],
399 [[input.y: conflicts: 1 shift/reduce
400 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond
406 ## -------------------------------- ##
407 ## Defaulted Conflicted Reduction. ##
408 ## -------------------------------- ##
410 # When there are RR conflicts, some rules are disabled. Usually it is
411 # simply displayed as:
413 # $end reduce using rule 3 (num)
414 # $end [reduce using rule 4 (id)]
416 # But when `reduce 3' is the default action, we'd produce:
418 # $end [reduce using rule 4 (id)]
419 # $default reduce using rule 3 (num)
421 # In this precise case (a reduction is masked by the default
422 # reduction), we make the `reduce 3' explicit:
424 # $end reduce using rule 3 (num)
425 # $end [reduce using rule 4 (id)]
426 # $default reduce using rule 3 (num)
428 # Maybe that's not the best display, but then, please propose something
431 AT_SETUP([Defaulted Conflicted Reduction])
432 AT_KEYWORDS([report])
442 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
443 [[input.y: conflicts: 1 reduce/reduce
444 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
447 # Check the contents of the report.
448 AT_CHECK([cat input.output], [],
449 [[Rules useless in parser due to conflicts
454 State 1 conflicts: 1 reduce/reduce
469 Terminals, with rules where they appear
476 Nonterminals, with rules where they appear
481 on left: 1 2, on right: 0
483 on left: 3, on right: 1
485 on left: 4, on right: 2
490 0 $accept: . exp $end
496 '0' shift, and go to state 1
508 $end reduce using rule 3 (num)
509 $end [reduce using rule 4 (id)]
510 $default reduce using rule 3 (num)
515 0 $accept: exp . $end
517 $end shift, and go to state 5
524 $default reduce using rule 1 (exp)
531 $default reduce using rule 2 (exp)
536 0 $accept: exp $end .
546 ## -------------------- ##
547 ## %expect not enough. ##
548 ## -------------------- ##
550 AT_SETUP([%expect not enough])
556 exp: exp OP exp | NUM;
559 AT_BISON_CHECK([-o input.c input.y], 1, [],
560 [input.y: conflicts: 1 shift/reduce
561 input.y: expected 0 shift/reduce conflicts
566 ## --------------- ##
568 ## --------------- ##
570 AT_SETUP([%expect right])
576 exp: exp OP exp | NUM;
579 AT_BISON_CHECK([-o input.c input.y])
583 ## ------------------ ##
584 ## %expect too much. ##
585 ## ------------------ ##
587 AT_SETUP([%expect too much])
593 exp: exp OP exp | NUM;
596 AT_BISON_CHECK([-o input.c input.y], 1, [],
597 [input.y: conflicts: 1 shift/reduce
598 input.y: expected 2 shift/reduce conflicts
603 ## ------------------------------- ##
604 ## %expect with reduce conflicts. ##
605 ## ------------------------------- ##
607 AT_SETUP([%expect with reduce conflicts])
612 program: a 'a' | a a;
616 AT_BISON_CHECK([-o input.c input.y], 1, [],
617 [input.y: conflicts: 1 reduce/reduce
618 input.y: expected 0 reduce/reduce conflicts
623 ## ------------------------- ##
624 ## %prec with user strings. ##
625 ## ------------------------- ##
627 AT_SETUP([%prec with user string])
636 AT_BISON_CHECK([-o input.c input.y])
640 ## -------------------------------- ##
641 ## %no-default-prec without %prec. ##
642 ## -------------------------------- ##
644 AT_SETUP([%no-default-prec without %prec])
660 AT_BISON_CHECK([-o input.c input.y], 0, [],
661 [[input.y: conflicts: 4 shift/reduce
666 ## ----------------------------- ##
667 ## %no-default-prec with %prec. ##
668 ## ----------------------------- ##
670 AT_SETUP([%no-default-prec with %prec])
686 AT_BISON_CHECK([-o input.c input.y])
690 ## --------------- ##
692 ## --------------- ##
694 AT_SETUP([%default-prec])
710 AT_BISON_CHECK([-o input.c input.y])
714 ## ---------------------------------------------- ##
715 ## Unreachable States After Conflict Resolution. ##
716 ## ---------------------------------------------- ##
718 AT_SETUP([[Unreachable States After Conflict Resolution]])
720 # If conflict resolution makes states unreachable, remove those states, report
721 # rules that are then unused, and don't report conflicts in those states. Test
722 # what happens when a nonterminal becomes useless as a result of state removal
723 # since that causes lalr.o's goto map to be rewritten.
731 start: resolved_conflict 'a' reported_conflicts 'a' ;
733 /* S/R conflict resolved as reduce, so the state with item
734 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
735 * unreachable, and the associated production is useless. */
741 /* S/R conflict that need not be reported since it is unreachable because of
742 * the previous conflict resolution. Nonterminal unreachable1 and all its
743 * productions are useless. */
749 /* Likewise for a R/R conflict and nonterminal unreachable2. */
752 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
753 * when their states are renumbered due to state removal. */
762 AT_BISON_CHECK([[--report=all input.y]], 0, [],
763 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
764 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
765 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
766 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
767 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
768 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
769 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
770 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
773 AT_CHECK([[cat input.output]], 0,
774 [[Rules useless in parser due to conflicts
776 2 resolved_conflict: 'a' unreachable1
778 4 unreachable1: 'a' unreachable2
781 6 unreachable2: /* empty */
784 9 reported_conflicts: 'a'
788 State 4 conflicts: 1 shift/reduce
789 State 5 conflicts: 1 reduce/reduce
794 0 $accept: start $end
796 1 start: resolved_conflict 'a' reported_conflicts 'a'
798 2 resolved_conflict: 'a' unreachable1
801 4 unreachable1: 'a' unreachable2
804 6 unreachable2: /* empty */
807 8 reported_conflicts: 'a'
812 Terminals, with rules where they appear
819 Nonterminals, with rules where they appear
824 on left: 1, on right: 0
825 resolved_conflict (6)
826 on left: 2 3, on right: 1
828 on left: 4 5, on right: 2
830 on left: 6 7, on right: 4
831 reported_conflicts (9)
832 on left: 8 9 10, on right: 1
837 0 $accept: . start $end
838 1 start: . resolved_conflict 'a' reported_conflicts 'a'
839 2 resolved_conflict: . 'a' unreachable1
842 $default reduce using rule 3 (resolved_conflict)
845 resolved_conflict go to state 2
847 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
852 0 $accept: start . $end
854 $end shift, and go to state 3
859 1 start: resolved_conflict . 'a' reported_conflicts 'a'
861 'a' shift, and go to state 4
866 0 $accept: start $end .
873 1 start: resolved_conflict 'a' . reported_conflicts 'a'
874 8 reported_conflicts: . 'a'
878 'a' shift, and go to state 5
880 'a' [reduce using rule 10 (reported_conflicts)]
882 reported_conflicts go to state 6
887 8 reported_conflicts: 'a' . ['a']
890 'a' reduce using rule 8 (reported_conflicts)
891 'a' [reduce using rule 9 (reported_conflicts)]
892 $default reduce using rule 8 (reported_conflicts)
897 1 start: resolved_conflict 'a' reported_conflicts . 'a'
899 'a' shift, and go to state 7
904 1 start: resolved_conflict 'a' reported_conflicts 'a' .
906 $default reduce using rule 1 (start)
909 AT_DATA([[input-keep.y]],
910 [[%define lr.keep-unreachable-states
912 AT_CHECK([[cat input.y >> input-keep.y]])
914 AT_BISON_CHECK([[input-keep.y]], 0, [],
915 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
916 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
917 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
918 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
919 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
925 ## ------------------------------------------------------------ ##
926 ## Solved conflicts report for multiple reductions in a state. ##
927 ## ------------------------------------------------------------ ##
929 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
931 # Used to lose earlier solved conflict messages even within a single S/R/R.
951 empty_c1: %prec 'c' ;
952 empty_c2: %prec 'c' ;
953 empty_c3: %prec 'd' ;
955 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
956 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
959 0 $accept: . start $end
974 'b' shift, and go to state 1
976 'c' reduce using rule 13 (empty_c3)
977 $default reduce using rule 9 (empty_a)
980 empty_a go to state 3
981 empty_b go to state 4
982 empty_c1 go to state 5
983 empty_c2 go to state 6
984 empty_c3 go to state 7
986 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
987 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
988 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
989 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
990 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
999 ## ------------------------------------------------------------ ##
1000 ## %nonassoc error actions for multiple reductions in a state. ##
1001 ## ------------------------------------------------------------ ##
1003 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1004 # multiple reductions in a state.
1006 # For a %nonassoc error action token, used to print the first remaining
1007 # reduction on that token without brackets.
1009 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1011 AT_DATA([[input.y]],
1012 [[%nonassoc 'a' 'b' 'c'
1024 empty_a: %prec 'a' ;
1025 empty_b: %prec 'b' ;
1026 empty_c1: %prec 'c' ;
1027 empty_c2: %prec 'c' ;
1028 empty_c3: %prec 'c' ;
1031 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1032 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1035 0 $accept: . start $end
1047 12 empty_c2: . ['c']
1048 13 empty_c3: . ['c']
1050 'a' error (nonassociative)
1051 'b' error (nonassociative)
1052 'c' error (nonassociative)
1054 'c' [reduce using rule 12 (empty_c2)]
1055 'c' [reduce using rule 13 (empty_c3)]
1058 empty_a go to state 2
1059 empty_b go to state 3
1060 empty_c1 go to state 4
1061 empty_c2 go to state 5
1062 empty_c3 go to state 6
1064 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1065 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1066 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').