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 ## 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 ## Defaulted Conflicted Reduction. ##
352 ## -------------------------------- ##
354 # When there are RR conflicts, some rules are disabled. Usually it is
355 # simply displayed as:
357 # $end reduce using rule 3 (num)
358 # $end [reduce using rule 4 (id)]
360 # But when `reduce 3' is the default action, we'd produce:
362 # $end [reduce using rule 4 (id)]
363 # $default reduce using rule 3 (num)
365 # In this precise case (a reduction is masked by the default
366 # reduction), we make the `reduce 3' explicit:
368 # $end reduce using rule 3 (num)
369 # $end [reduce using rule 4 (id)]
370 # $default reduce using rule 3 (num)
372 # Maybe that's not the best display, but then, please propose something
375 AT_SETUP([Defaulted Conflicted Reduction])
376 AT_KEYWORDS([report])
386 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
387 [[input.y: conflicts: 1 reduce/reduce
388 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
391 # Check the contents of the report.
392 AT_CHECK([cat input.output], [],
393 [[Rules useless in parser due to conflicts
398 State 1 conflicts: 1 reduce/reduce
413 Terminals, with rules where they appear
420 Nonterminals, with rules where they appear
425 on left: 1 2, on right: 0
427 on left: 3, on right: 1
429 on left: 4, on right: 2
434 0 $accept: . exp $end
440 '0' shift, and go to state 1
452 $end reduce using rule 3 (num)
453 $end [reduce using rule 4 (id)]
454 $default reduce using rule 3 (num)
459 0 $accept: exp . $end
461 $end shift, and go to state 5
468 $default reduce using rule 1 (exp)
475 $default reduce using rule 2 (exp)
480 0 $accept: exp $end .
490 ## -------------------- ##
491 ## %expect not enough. ##
492 ## -------------------- ##
494 AT_SETUP([%expect not enough])
500 exp: exp OP exp | NUM;
503 AT_BISON_CHECK([-o input.c input.y], 1, [],
504 [input.y: conflicts: 1 shift/reduce
505 input.y: expected 0 shift/reduce conflicts
510 ## --------------- ##
512 ## --------------- ##
514 AT_SETUP([%expect right])
520 exp: exp OP exp | NUM;
523 AT_BISON_CHECK([-o input.c input.y])
527 ## ------------------ ##
528 ## %expect too much. ##
529 ## ------------------ ##
531 AT_SETUP([%expect too much])
537 exp: exp OP exp | NUM;
540 AT_BISON_CHECK([-o input.c input.y], 1, [],
541 [input.y: conflicts: 1 shift/reduce
542 input.y: expected 2 shift/reduce conflicts
547 ## ------------------------------- ##
548 ## %expect with reduce conflicts. ##
549 ## ------------------------------- ##
551 AT_SETUP([%expect with reduce conflicts])
556 program: a 'a' | a a;
560 AT_BISON_CHECK([-o input.c input.y], 1, [],
561 [input.y: conflicts: 1 reduce/reduce
562 input.y: expected 0 reduce/reduce conflicts
567 ## ------------------------- ##
568 ## %prec with user strings. ##
569 ## ------------------------- ##
571 AT_SETUP([%prec with user string])
580 AT_BISON_CHECK([-o input.c input.y])
584 ## -------------------------------- ##
585 ## %no-default-prec without %prec. ##
586 ## -------------------------------- ##
588 AT_SETUP([%no-default-prec without %prec])
604 AT_BISON_CHECK([-o input.c input.y], 0, [],
605 [[input.y: conflicts: 4 shift/reduce
610 ## ----------------------------- ##
611 ## %no-default-prec with %prec. ##
612 ## ----------------------------- ##
614 AT_SETUP([%no-default-prec with %prec])
630 AT_BISON_CHECK([-o input.c input.y])
634 ## --------------- ##
636 ## --------------- ##
638 AT_SETUP([%default-prec])
654 AT_BISON_CHECK([-o input.c input.y])
658 ## ---------------------------------------------- ##
659 ## Unreachable States After Conflict Resolution. ##
660 ## ---------------------------------------------- ##
662 AT_SETUP([[Unreachable States After Conflict Resolution]])
664 # If conflict resolution makes states unreachable, remove those states, report
665 # rules that are then unused, and don't report conflicts in those states. Test
666 # what happens when a nonterminal becomes useless as a result of state removal
667 # since that causes lalr.o's goto map to be rewritten.
675 start: resolved_conflict 'a' reported_conflicts 'a' ;
677 /* S/R conflict resolved as reduce, so the state with item
678 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
679 * unreachable, and the associated production is useless. */
685 /* S/R conflict that need not be reported since it is unreachable because of
686 * the previous conflict resolution. Nonterminal unreachable1 and all its
687 * productions are useless. */
693 /* Likewise for a R/R conflict and nonterminal unreachable2. */
696 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
697 * when their states are renumbered due to state removal. */
706 AT_BISON_CHECK([[--report=all input.y]], 0, [],
707 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
708 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
709 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
710 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
711 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
712 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
713 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
714 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
717 AT_CHECK([[cat input.output]], 0,
718 [[Rules useless in parser due to conflicts
720 2 resolved_conflict: 'a' unreachable1
722 4 unreachable1: 'a' unreachable2
725 6 unreachable2: /* empty */
728 9 reported_conflicts: 'a'
732 State 4 conflicts: 1 shift/reduce
733 State 5 conflicts: 1 reduce/reduce
738 0 $accept: start $end
740 1 start: resolved_conflict 'a' reported_conflicts 'a'
742 2 resolved_conflict: 'a' unreachable1
745 4 unreachable1: 'a' unreachable2
748 6 unreachable2: /* empty */
751 8 reported_conflicts: 'a'
756 Terminals, with rules where they appear
763 Nonterminals, with rules where they appear
768 on left: 1, on right: 0
769 resolved_conflict (6)
770 on left: 2 3, on right: 1
772 on left: 4 5, on right: 2
774 on left: 6 7, on right: 4
775 reported_conflicts (9)
776 on left: 8 9 10, on right: 1
781 0 $accept: . start $end
782 1 start: . resolved_conflict 'a' reported_conflicts 'a'
783 2 resolved_conflict: . 'a' unreachable1
786 $default reduce using rule 3 (resolved_conflict)
789 resolved_conflict go to state 2
791 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
796 0 $accept: start . $end
798 $end shift, and go to state 3
803 1 start: resolved_conflict . 'a' reported_conflicts 'a'
805 'a' shift, and go to state 4
810 0 $accept: start $end .
817 1 start: resolved_conflict 'a' . reported_conflicts 'a'
818 8 reported_conflicts: . 'a'
822 'a' shift, and go to state 5
824 'a' [reduce using rule 10 (reported_conflicts)]
826 reported_conflicts go to state 6
831 8 reported_conflicts: 'a' . ['a']
834 'a' reduce using rule 8 (reported_conflicts)
835 'a' [reduce using rule 9 (reported_conflicts)]
836 $default reduce using rule 8 (reported_conflicts)
841 1 start: resolved_conflict 'a' reported_conflicts . 'a'
843 'a' shift, and go to state 7
848 1 start: resolved_conflict 'a' reported_conflicts 'a' .
850 $default reduce using rule 1 (start)
853 AT_DATA([[input-keep.y]],
854 [[%define lr.keep-unreachable-states
856 AT_CHECK([[cat input.y >> input-keep.y]])
858 AT_BISON_CHECK([[input-keep.y]], 0, [],
859 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
860 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
861 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
862 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
863 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
869 ## ------------------------------------------------------------ ##
870 ## Solved conflicts report for multiple reductions in a state. ##
871 ## ------------------------------------------------------------ ##
873 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
875 # Used to lose earlier solved conflict messages even within a single S/R/R.
895 empty_c1: %prec 'c' ;
896 empty_c2: %prec 'c' ;
897 empty_c3: %prec 'd' ;
899 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
900 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
903 0 $accept: . start $end
918 'b' shift, and go to state 1
920 'c' reduce using rule 13 (empty_c3)
921 $default reduce using rule 9 (empty_a)
924 empty_a go to state 3
925 empty_b go to state 4
926 empty_c1 go to state 5
927 empty_c2 go to state 6
928 empty_c3 go to state 7
930 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
931 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
932 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
933 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
934 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
943 ## ------------------------------------------------------------ ##
944 ## %nonassoc error actions for multiple reductions in a state. ##
945 ## ------------------------------------------------------------ ##
947 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
948 # multiple reductions in a state.
950 # For a %nonassoc error action token, used to print the first remaining
951 # reduction on that token without brackets.
953 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
956 [[%nonassoc 'a' 'b' 'c'
970 empty_c1: %prec 'c' ;
971 empty_c2: %prec 'c' ;
972 empty_c3: %prec 'c' ;
975 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
976 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
979 0 $accept: . start $end
994 'a' error (nonassociative)
995 'b' error (nonassociative)
996 'c' error (nonassociative)
998 'c' [reduce using rule 12 (empty_c2)]
999 'c' [reduce using rule 13 (empty_c3)]
1002 empty_a go to state 2
1003 empty_b go to state 3
1004 empty_c1 go to state 4
1005 empty_c2 go to state 5
1006 empty_c3 go to state 6
1008 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1009 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1010 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').