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 # FIXME: This is an actual bug, but a new one, in the sense that
103 # no one has ever spotted it! The messages are *wrong*: there should
104 # be nothing there, it should be expected eof.
105 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
106 [syntax error, unexpected '<', expecting '<' or '>'
109 AT_PARSER_CHECK([./input '0>0'])
110 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
111 [syntax error, unexpected '>', expecting '<' or '>'
114 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
115 [syntax error, unexpected '>', expecting '<' or '>'
122 ## ------------------------- ##
123 ## Unresolved SR Conflicts. ##
124 ## ------------------------- ##
126 AT_SETUP([Unresolved SR Conflicts])
128 AT_KEYWORDS([report])
133 exp: exp OP exp | NUM;
136 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
137 [input.y: conflicts: 1 shift/reduce
140 # Check the contents of the report.
141 AT_CHECK([cat input.output], [],
142 [[State 5 conflicts: 1 shift/reduce
153 Terminals, with rules where they appear
161 Nonterminals, with rules where they appear
166 on left: 1 2, on right: 0 1
171 0 $accept: . exp $end
175 NUM shift, and go to state 1
184 $default reduce using rule 2 (exp)
189 0 $accept: exp . $end
192 $end shift, and go to state 3
193 OP shift, and go to state 4
198 0 $accept: exp $end .
209 NUM shift, and go to state 1
217 1 | exp OP exp . [$end, OP]
219 OP shift, and go to state 4
221 OP [reduce using rule 1 (exp)]
222 $default reduce using rule 1 (exp)
229 ## ----------------------- ##
230 ## Resolved SR Conflicts. ##
231 ## ----------------------- ##
233 AT_SETUP([Resolved SR Conflicts])
235 AT_KEYWORDS([report])
241 exp: exp OP exp | NUM;
244 AT_BISON_CHECK([-o input.c --report=all input.y])
246 # Check the contents of the report.
247 AT_CHECK([cat input.output], [],
256 Terminals, with rules where they appear
264 Nonterminals, with rules where they appear
269 on left: 1 2, on right: 0 1
274 0 $accept: . exp $end
278 NUM shift, and go to state 1
287 $default reduce using rule 2 (exp)
292 0 $accept: exp . $end
295 $end shift, and go to state 3
296 OP shift, and go to state 4
301 0 $accept: exp $end .
312 NUM shift, and go to state 1
320 1 | exp OP exp . [$end, OP]
322 $default reduce using rule 1 (exp)
324 Conflict between rule 1 and token OP resolved as reduce (%left OP).
330 ## ---------------------- ##
331 ## %precedence suffices. ##
332 ## ---------------------- ##
334 AT_SETUP([%precedence suffices])
341 "if" cond "then" stmt
342 | "if" cond "then" stmt "else" stmt
351 AT_BISON_CHECK([-o input.c input.y])
356 ## ------------------------------ ##
357 ## %precedence does not suffice. ##
358 ## ------------------------------ ##
360 AT_SETUP([%precedence does not suffice])
367 "if" cond "then" stmt
368 | "if" cond "then" stmt "else" stmt
378 AT_BISON_CHECK([-o input.c input.y], 0, [],
379 [[input.y: conflicts: 1 shift/reduce
380 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond
386 ## -------------------------------- ##
387 ## Defaulted Conflicted Reduction. ##
388 ## -------------------------------- ##
390 # When there are RR conflicts, some rules are disabled. Usually it is
391 # simply displayed as:
393 # $end reduce using rule 3 (num)
394 # $end [reduce using rule 4 (id)]
396 # But when `reduce 3' is the default action, we'd produce:
398 # $end [reduce using rule 4 (id)]
399 # $default reduce using rule 3 (num)
401 # In this precise case (a reduction is masked by the default
402 # reduction), we make the `reduce 3' explicit:
404 # $end reduce using rule 3 (num)
405 # $end [reduce using rule 4 (id)]
406 # $default reduce using rule 3 (num)
408 # Maybe that's not the best display, but then, please propose something
411 AT_SETUP([Defaulted Conflicted Reduction])
412 AT_KEYWORDS([report])
422 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
423 [[input.y: conflicts: 1 reduce/reduce
424 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
427 # Check the contents of the report.
428 AT_CHECK([cat input.output], [],
429 [[Rules useless in parser due to conflicts
434 State 1 conflicts: 1 reduce/reduce
449 Terminals, with rules where they appear
456 Nonterminals, with rules where they appear
461 on left: 1 2, on right: 0
463 on left: 3, on right: 1
465 on left: 4, on right: 2
470 0 $accept: . exp $end
476 '0' shift, and go to state 1
488 $end reduce using rule 3 (num)
489 $end [reduce using rule 4 (id)]
490 $default reduce using rule 3 (num)
495 0 $accept: exp . $end
497 $end shift, and go to state 5
504 $default reduce using rule 1 (exp)
511 $default reduce using rule 2 (exp)
516 0 $accept: exp $end .
526 ## -------------------- ##
527 ## %expect not enough. ##
528 ## -------------------- ##
530 AT_SETUP([%expect not enough])
536 exp: exp OP exp | NUM;
539 AT_BISON_CHECK([-o input.c input.y], 1, [],
540 [input.y: conflicts: 1 shift/reduce
541 input.y: expected 0 shift/reduce conflicts
546 ## --------------- ##
548 ## --------------- ##
550 AT_SETUP([%expect right])
556 exp: exp OP exp | NUM;
559 AT_BISON_CHECK([-o input.c input.y])
563 ## ------------------ ##
564 ## %expect too much. ##
565 ## ------------------ ##
567 AT_SETUP([%expect too much])
573 exp: exp OP exp | NUM;
576 AT_BISON_CHECK([-o input.c input.y], 1, [],
577 [input.y: conflicts: 1 shift/reduce
578 input.y: expected 2 shift/reduce conflicts
583 ## ------------------------------ ##
584 ## %expect with reduce conflicts ##
585 ## ------------------------------ ##
587 AT_SETUP([%expect with reduce conflicts])
592 program: a 'a' | a a;
596 AT_BISON_CHECK([-o input.c input.y], 1, [],
597 [input.y: conflicts: 1 reduce/reduce
598 input.y: expected 0 reduce/reduce conflicts
603 ## ------------------------------- ##
604 ## %no-default-prec without %prec ##
605 ## ------------------------------- ##
607 AT_SETUP([%no-default-prec without %prec])
623 AT_BISON_CHECK([-o input.c input.y], 0, [],
624 [[input.y: conflicts: 4 shift/reduce
629 ## ---------------------------- ##
630 ## %no-default-prec with %prec ##
631 ## ---------------------------- ##
633 AT_SETUP([%no-default-prec with %prec])
649 AT_BISON_CHECK([-o input.c input.y])
653 ## ---------------- ##
655 ## ---------------- ##
657 AT_SETUP([%default-prec])
673 AT_BISON_CHECK([-o input.c input.y])
677 ## ---------------------------------------------- ##
678 ## Unreachable States After Conflict Resolution. ##
679 ## ---------------------------------------------- ##
681 AT_SETUP([[Unreachable States After Conflict Resolution]])
683 # If conflict resolution makes states unreachable, remove those states, report
684 # rules that are then unused, and don't report conflicts in those states. Test
685 # what happens when a nonterminal becomes useless as a result of state removal
686 # since that causes lalr.o's goto map to be rewritten.
694 start: resolved_conflict 'a' reported_conflicts 'a' ;
696 /* S/R conflict resolved as reduce, so the state with item
697 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
698 * unreachable, and the associated production is useless. */
704 /* S/R conflict that need not be reported since it is unreachable because of
705 * the previous conflict resolution. Nonterminal unreachable1 and all its
706 * productions are useless. */
712 /* Likewise for a R/R conflict and nonterminal unreachable2. */
715 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
716 * when their states are renumbered due to state removal. */
725 AT_BISON_CHECK([[--report=all input.y]], 0, [],
726 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
727 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
728 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
729 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
730 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
731 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
732 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
733 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
736 AT_CHECK([[cat input.output]], 0,
737 [[Rules useless in parser due to conflicts
739 2 resolved_conflict: 'a' unreachable1
741 4 unreachable1: 'a' unreachable2
744 6 unreachable2: /* empty */
747 9 reported_conflicts: 'a'
751 State 4 conflicts: 1 shift/reduce
752 State 5 conflicts: 1 reduce/reduce
757 0 $accept: start $end
759 1 start: resolved_conflict 'a' reported_conflicts 'a'
761 2 resolved_conflict: 'a' unreachable1
764 4 unreachable1: 'a' unreachable2
767 6 unreachable2: /* empty */
770 8 reported_conflicts: 'a'
775 Terminals, with rules where they appear
782 Nonterminals, with rules where they appear
787 on left: 1, on right: 0
788 resolved_conflict (6)
789 on left: 2 3, on right: 1
791 on left: 4 5, on right: 2
793 on left: 6 7, on right: 4
794 reported_conflicts (9)
795 on left: 8 9 10, on right: 1
800 0 $accept: . start $end
801 1 start: . resolved_conflict 'a' reported_conflicts 'a'
802 2 resolved_conflict: . 'a' unreachable1
805 $default reduce using rule 3 (resolved_conflict)
808 resolved_conflict go to state 2
810 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
815 0 $accept: start . $end
817 $end shift, and go to state 3
822 1 start: resolved_conflict . 'a' reported_conflicts 'a'
824 'a' shift, and go to state 4
829 0 $accept: start $end .
836 1 start: resolved_conflict 'a' . reported_conflicts 'a'
837 8 reported_conflicts: . 'a'
841 'a' shift, and go to state 5
843 'a' [reduce using rule 10 (reported_conflicts)]
845 reported_conflicts go to state 6
850 8 reported_conflicts: 'a' . ['a']
853 'a' reduce using rule 8 (reported_conflicts)
854 'a' [reduce using rule 9 (reported_conflicts)]
855 $default reduce using rule 8 (reported_conflicts)
860 1 start: resolved_conflict 'a' reported_conflicts . 'a'
862 'a' shift, and go to state 7
867 1 start: resolved_conflict 'a' reported_conflicts 'a' .
869 $default reduce using rule 1 (start)
872 AT_DATA([[input-keep.y]],
873 [[%define lr.keep-unreachable-states
875 AT_CHECK([[cat input.y >> input-keep.y]])
877 AT_BISON_CHECK([[input-keep.y]], 0, [],
878 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
879 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
880 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
881 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
882 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
888 ## ------------------------------------------------------------ ##
889 ## Solved conflicts report for multiple reductions in a state. ##
890 ## ------------------------------------------------------------ ##
892 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
894 # Used to lose earlier solved conflict messages even within a single S/R/R.
914 empty_c1: %prec 'c' ;
915 empty_c2: %prec 'c' ;
916 empty_c3: %prec 'd' ;
918 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
919 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
922 0 $accept: . start $end
937 'b' shift, and go to state 1
939 'c' reduce using rule 13 (empty_c3)
940 $default reduce using rule 9 (empty_a)
943 empty_a go to state 3
944 empty_b go to state 4
945 empty_c1 go to state 5
946 empty_c2 go to state 6
947 empty_c3 go to state 7
949 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
950 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
951 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
952 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
953 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
962 ## ------------------------------------------------------------ ##
963 ## %nonassoc error actions for multiple reductions in a state. ##
964 ## ------------------------------------------------------------ ##
966 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
967 # multiple reductions in a state.
969 # For a %nonassoc error action token, used to print the first remaining
970 # reduction on that token without brackets.
972 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
975 [[%nonassoc 'a' 'b' 'c'
989 empty_c1: %prec 'c' ;
990 empty_c2: %prec 'c' ;
991 empty_c3: %prec 'c' ;
994 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
995 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
998 0 $accept: . start $end
1010 12 empty_c2: . ['c']
1011 13 empty_c3: . ['c']
1013 'a' error (nonassociative)
1014 'b' error (nonassociative)
1015 'c' error (nonassociative)
1017 'c' [reduce using rule 12 (empty_c2)]
1018 'c' [reduce using rule 13 (empty_c3)]
1021 empty_a go to state 2
1022 empty_b go to state 3
1023 empty_c1 go to state 4
1024 empty_c2 go to state 5
1025 empty_c3 go to state 6
1027 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1028 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1029 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').