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 ## %prec with user strings. ##
605 ## ------------------------- ##
607 AT_SETUP([%prec with user string])
616 AT_BISON_CHECK([-o input.c input.y])
620 ## -------------------------------- ##
621 ## %no-default-prec without %prec. ##
622 ## -------------------------------- ##
624 AT_SETUP([%no-default-prec without %prec])
640 AT_BISON_CHECK([-o input.c input.y], 0, [],
641 [[input.y: conflicts: 4 shift/reduce
646 ## ----------------------------- ##
647 ## %no-default-prec with %prec. ##
648 ## ----------------------------- ##
650 AT_SETUP([%no-default-prec with %prec])
666 AT_BISON_CHECK([-o input.c input.y])
670 ## --------------- ##
672 ## --------------- ##
674 AT_SETUP([%default-prec])
690 AT_BISON_CHECK([-o input.c input.y])
694 ## ---------------------------------------------- ##
695 ## Unreachable States After Conflict Resolution. ##
696 ## ---------------------------------------------- ##
698 AT_SETUP([[Unreachable States After Conflict Resolution]])
700 # If conflict resolution makes states unreachable, remove those states, report
701 # rules that are then unused, and don't report conflicts in those states. Test
702 # what happens when a nonterminal becomes useless as a result of state removal
703 # since that causes lalr.o's goto map to be rewritten.
711 start: resolved_conflict 'a' reported_conflicts 'a' ;
713 /* S/R conflict resolved as reduce, so the state with item
714 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
715 * unreachable, and the associated production is useless. */
721 /* S/R conflict that need not be reported since it is unreachable because of
722 * the previous conflict resolution. Nonterminal unreachable1 and all its
723 * productions are useless. */
729 /* Likewise for a R/R conflict and nonterminal unreachable2. */
732 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
733 * when their states are renumbered due to state removal. */
742 AT_BISON_CHECK([[--report=all input.y]], 0, [],
743 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
744 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
745 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
746 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
747 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
748 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
749 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
750 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
753 AT_CHECK([[cat input.output]], 0,
754 [[Rules useless in parser due to conflicts
756 2 resolved_conflict: 'a' unreachable1
758 4 unreachable1: 'a' unreachable2
761 6 unreachable2: /* empty */
764 9 reported_conflicts: 'a'
768 State 4 conflicts: 1 shift/reduce
769 State 5 conflicts: 1 reduce/reduce
774 0 $accept: start $end
776 1 start: resolved_conflict 'a' reported_conflicts 'a'
778 2 resolved_conflict: 'a' unreachable1
781 4 unreachable1: 'a' unreachable2
784 6 unreachable2: /* empty */
787 8 reported_conflicts: 'a'
792 Terminals, with rules where they appear
799 Nonterminals, with rules where they appear
804 on left: 1, on right: 0
805 resolved_conflict (6)
806 on left: 2 3, on right: 1
808 on left: 4 5, on right: 2
810 on left: 6 7, on right: 4
811 reported_conflicts (9)
812 on left: 8 9 10, on right: 1
817 0 $accept: . start $end
818 1 start: . resolved_conflict 'a' reported_conflicts 'a'
819 2 resolved_conflict: . 'a' unreachable1
822 $default reduce using rule 3 (resolved_conflict)
825 resolved_conflict go to state 2
827 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
832 0 $accept: start . $end
834 $end shift, and go to state 3
839 1 start: resolved_conflict . 'a' reported_conflicts 'a'
841 'a' shift, and go to state 4
846 0 $accept: start $end .
853 1 start: resolved_conflict 'a' . reported_conflicts 'a'
854 8 reported_conflicts: . 'a'
858 'a' shift, and go to state 5
860 'a' [reduce using rule 10 (reported_conflicts)]
862 reported_conflicts go to state 6
867 8 reported_conflicts: 'a' . ['a']
870 'a' reduce using rule 8 (reported_conflicts)
871 'a' [reduce using rule 9 (reported_conflicts)]
872 $default reduce using rule 8 (reported_conflicts)
877 1 start: resolved_conflict 'a' reported_conflicts . 'a'
879 'a' shift, and go to state 7
884 1 start: resolved_conflict 'a' reported_conflicts 'a' .
886 $default reduce using rule 1 (start)
889 AT_DATA([[input-keep.y]],
890 [[%define lr.keep-unreachable-states
892 AT_CHECK([[cat input.y >> input-keep.y]])
894 AT_BISON_CHECK([[input-keep.y]], 0, [],
895 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
896 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
897 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
898 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
899 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
905 ## ------------------------------------------------------------ ##
906 ## Solved conflicts report for multiple reductions in a state. ##
907 ## ------------------------------------------------------------ ##
909 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
911 # Used to lose earlier solved conflict messages even within a single S/R/R.
931 empty_c1: %prec 'c' ;
932 empty_c2: %prec 'c' ;
933 empty_c3: %prec 'd' ;
935 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
936 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
939 0 $accept: . start $end
954 'b' shift, and go to state 1
956 'c' reduce using rule 13 (empty_c3)
957 $default reduce using rule 9 (empty_a)
960 empty_a go to state 3
961 empty_b go to state 4
962 empty_c1 go to state 5
963 empty_c2 go to state 6
964 empty_c3 go to state 7
966 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
967 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
968 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
969 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
970 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
979 ## ------------------------------------------------------------ ##
980 ## %nonassoc error actions for multiple reductions in a state. ##
981 ## ------------------------------------------------------------ ##
983 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
984 # multiple reductions in a state.
986 # For a %nonassoc error action token, used to print the first remaining
987 # reduction on that token without brackets.
989 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
992 [[%nonassoc 'a' 'b' 'c'
1004 empty_a: %prec 'a' ;
1005 empty_b: %prec 'b' ;
1006 empty_c1: %prec 'c' ;
1007 empty_c2: %prec 'c' ;
1008 empty_c3: %prec 'c' ;
1011 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1012 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1015 0 $accept: . start $end
1027 12 empty_c2: . ['c']
1028 13 empty_c3: . ['c']
1030 'a' error (nonassociative)
1031 'b' error (nonassociative)
1032 'c' error (nonassociative)
1034 'c' [reduce using rule 12 (empty_c2)]
1035 'c' [reduce using rule 13 (empty_c3)]
1038 empty_a go to state 2
1039 empty_b go to state 3
1040 empty_c1 go to state 4
1041 empty_c2 go to state 5
1042 empty_c3 go to state 6
1044 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1045 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1046 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').