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 # 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 ## Defaulted Conflicted Reduction. ##
332 ## -------------------------------- ##
334 # When there are RR conflicts, some rules are disabled. Usually it is
335 # simply displayed as:
337 # $end reduce using rule 3 (num)
338 # $end [reduce using rule 4 (id)]
340 # But when `reduce 3' is the default action, we'd produce:
342 # $end [reduce using rule 4 (id)]
343 # $default reduce using rule 3 (num)
345 # In this precise case (a reduction is masked by the default
346 # reduction), we make the `reduce 3' explicit:
348 # $end reduce using rule 3 (num)
349 # $end [reduce using rule 4 (id)]
350 # $default reduce using rule 3 (num)
352 # Maybe that's not the best display, but then, please propose something
355 AT_SETUP([Defaulted Conflicted Reduction])
356 AT_KEYWORDS([report])
366 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
367 [[input.y: conflicts: 1 reduce/reduce
368 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
371 # Check the contents of the report.
372 AT_CHECK([cat input.output], [],
373 [[Rules useless in parser due to conflicts
378 State 1 conflicts: 1 reduce/reduce
393 Terminals, with rules where they appear
400 Nonterminals, with rules where they appear
405 on left: 1 2, on right: 0
407 on left: 3, on right: 1
409 on left: 4, on right: 2
414 0 $accept: . exp $end
420 '0' shift, and go to state 1
432 $end reduce using rule 3 (num)
433 $end [reduce using rule 4 (id)]
434 $default reduce using rule 3 (num)
439 0 $accept: exp . $end
441 $end shift, and go to state 5
448 $default reduce using rule 1 (exp)
455 $default reduce using rule 2 (exp)
460 0 $accept: exp $end .
470 ## -------------------- ##
471 ## %expect not enough. ##
472 ## -------------------- ##
474 AT_SETUP([%expect not enough])
480 exp: exp OP exp | NUM;
483 AT_BISON_CHECK([-o input.c input.y], 1, [],
484 [input.y: conflicts: 1 shift/reduce
485 input.y: expected 0 shift/reduce conflicts
490 ## --------------- ##
492 ## --------------- ##
494 AT_SETUP([%expect right])
500 exp: exp OP exp | NUM;
503 AT_BISON_CHECK([-o input.c input.y])
507 ## ------------------ ##
508 ## %expect too much. ##
509 ## ------------------ ##
511 AT_SETUP([%expect too much])
517 exp: exp OP exp | NUM;
520 AT_BISON_CHECK([-o input.c input.y], 1, [],
521 [input.y: conflicts: 1 shift/reduce
522 input.y: expected 2 shift/reduce conflicts
527 ## ------------------------------ ##
528 ## %expect with reduce conflicts ##
529 ## ------------------------------ ##
531 AT_SETUP([%expect with reduce conflicts])
536 program: a 'a' | a a;
540 AT_BISON_CHECK([-o input.c input.y], 1, [],
541 [input.y: conflicts: 1 reduce/reduce
542 input.y: expected 0 reduce/reduce conflicts
547 ## ------------------------- ##
548 ## %prec with user strings. ##
549 ## ------------------------- ##
551 AT_SETUP([%prec with user string])
560 AT_BISON_CHECK([-o input.c input.y])
564 ## -------------------------------- ##
565 ## %no-default-prec without %prec. ##
566 ## -------------------------------- ##
568 AT_SETUP([%no-default-prec without %prec])
584 AT_BISON_CHECK([-o input.c input.y], 0, [],
585 [[input.y: conflicts: 4 shift/reduce
590 ## ---------------------------- ##
591 ## %no-default-prec with %prec ##
592 ## ---------------------------- ##
594 AT_SETUP([%no-default-prec with %prec])
610 AT_BISON_CHECK([-o input.c input.y])
614 ## ---------------- ##
616 ## ---------------- ##
618 AT_SETUP([%default-prec])
634 AT_BISON_CHECK([-o input.c input.y])
638 ## ---------------------------------------------- ##
639 ## Unreachable States After Conflict Resolution. ##
640 ## ---------------------------------------------- ##
642 AT_SETUP([[Unreachable States After Conflict Resolution]])
644 # If conflict resolution makes states unreachable, remove those states, report
645 # rules that are then unused, and don't report conflicts in those states. Test
646 # what happens when a nonterminal becomes useless as a result of state removal
647 # since that causes lalr.o's goto map to be rewritten.
655 start: resolved_conflict 'a' reported_conflicts 'a' ;
657 /* S/R conflict resolved as reduce, so the state with item
658 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
659 * unreachable, and the associated production is useless. */
665 /* S/R conflict that need not be reported since it is unreachable because of
666 * the previous conflict resolution. Nonterminal unreachable1 and all its
667 * productions are useless. */
673 /* Likewise for a R/R conflict and nonterminal unreachable2. */
676 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
677 * when their states are renumbered due to state removal. */
686 AT_BISON_CHECK([[--report=all input.y]], 0, [],
687 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
688 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
689 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
690 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
691 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
692 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
693 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
694 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
697 AT_CHECK([[cat input.output]], 0,
698 [[Rules useless in parser due to conflicts
700 2 resolved_conflict: 'a' unreachable1
702 4 unreachable1: 'a' unreachable2
705 6 unreachable2: /* empty */
708 9 reported_conflicts: 'a'
712 State 4 conflicts: 1 shift/reduce
713 State 5 conflicts: 1 reduce/reduce
718 0 $accept: start $end
720 1 start: resolved_conflict 'a' reported_conflicts 'a'
722 2 resolved_conflict: 'a' unreachable1
725 4 unreachable1: 'a' unreachable2
728 6 unreachable2: /* empty */
731 8 reported_conflicts: 'a'
736 Terminals, with rules where they appear
743 Nonterminals, with rules where they appear
748 on left: 1, on right: 0
749 resolved_conflict (6)
750 on left: 2 3, on right: 1
752 on left: 4 5, on right: 2
754 on left: 6 7, on right: 4
755 reported_conflicts (9)
756 on left: 8 9 10, on right: 1
761 0 $accept: . start $end
762 1 start: . resolved_conflict 'a' reported_conflicts 'a'
763 2 resolved_conflict: . 'a' unreachable1
766 $default reduce using rule 3 (resolved_conflict)
769 resolved_conflict go to state 2
771 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
776 0 $accept: start . $end
778 $end shift, and go to state 3
783 1 start: resolved_conflict . 'a' reported_conflicts 'a'
785 'a' shift, and go to state 4
790 0 $accept: start $end .
797 1 start: resolved_conflict 'a' . reported_conflicts 'a'
798 8 reported_conflicts: . 'a'
802 'a' shift, and go to state 5
804 'a' [reduce using rule 10 (reported_conflicts)]
806 reported_conflicts go to state 6
811 8 reported_conflicts: 'a' . ['a']
814 'a' reduce using rule 8 (reported_conflicts)
815 'a' [reduce using rule 9 (reported_conflicts)]
816 $default reduce using rule 8 (reported_conflicts)
821 1 start: resolved_conflict 'a' reported_conflicts . 'a'
823 'a' shift, and go to state 7
828 1 start: resolved_conflict 'a' reported_conflicts 'a' .
830 $default reduce using rule 1 (start)
833 AT_DATA([[input-keep.y]],
834 [[%define lr.keep-unreachable-states
836 AT_CHECK([[cat input.y >> input-keep.y]])
838 AT_BISON_CHECK([[input-keep.y]], 0, [],
839 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
840 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
841 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
842 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
843 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
849 ## ------------------------------------------------------------ ##
850 ## Solved conflicts report for multiple reductions in a state. ##
851 ## ------------------------------------------------------------ ##
853 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
855 # Used to lose earlier solved conflict messages even within a single S/R/R.
875 empty_c1: %prec 'c' ;
876 empty_c2: %prec 'c' ;
877 empty_c3: %prec 'd' ;
879 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
880 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
883 0 $accept: . start $end
898 'b' shift, and go to state 1
900 'c' reduce using rule 13 (empty_c3)
901 $default reduce using rule 9 (empty_a)
904 empty_a go to state 3
905 empty_b go to state 4
906 empty_c1 go to state 5
907 empty_c2 go to state 6
908 empty_c3 go to state 7
910 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
911 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
912 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
913 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
914 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
923 ## ------------------------------------------------------------ ##
924 ## %nonassoc error actions for multiple reductions in a state. ##
925 ## ------------------------------------------------------------ ##
927 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
928 # multiple reductions in a state.
930 # For a %nonassoc error action token, used to print the first remaining
931 # reduction on that token without brackets.
933 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
936 [[%nonassoc 'a' 'b' 'c'
950 empty_c1: %prec 'c' ;
951 empty_c2: %prec 'c' ;
952 empty_c3: %prec 'c' ;
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 'a' error (nonassociative)
975 'b' error (nonassociative)
976 'c' error (nonassociative)
978 'c' [reduce using rule 12 (empty_c2)]
979 'c' [reduce using rule 13 (empty_c3)]
982 empty_a go to state 2
983 empty_b go to state 3
984 empty_c1 go to state 4
985 empty_c2 go to state 5
986 empty_c3 go to state 6
988 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
989 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
990 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').