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 ## %no-default-prec without %prec ##
549 ## ------------------------------- ##
551 AT_SETUP([%no-default-prec without %prec])
567 AT_BISON_CHECK([-o input.c input.y], 0, [],
568 [[input.y: conflicts: 4 shift/reduce
573 ## ---------------------------- ##
574 ## %no-default-prec with %prec ##
575 ## ---------------------------- ##
577 AT_SETUP([%no-default-prec with %prec])
593 AT_BISON_CHECK([-o input.c input.y])
597 ## ---------------- ##
599 ## ---------------- ##
601 AT_SETUP([%default-prec])
617 AT_BISON_CHECK([-o input.c input.y])
621 ## ---------------------------------------------- ##
622 ## Unreachable States After Conflict Resolution. ##
623 ## ---------------------------------------------- ##
625 AT_SETUP([[Unreachable States After Conflict Resolution]])
627 # If conflict resolution makes states unreachable, remove those states, report
628 # rules that are then unused, and don't report conflicts in those states. Test
629 # what happens when a nonterminal becomes useless as a result of state removal
630 # since that causes lalr.o's goto map to be rewritten.
638 start: resolved_conflict 'a' reported_conflicts 'a' ;
640 /* S/R conflict resolved as reduce, so the state with item
641 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
642 * unreachable, and the associated production is useless. */
648 /* S/R conflict that need not be reported since it is unreachable because of
649 * the previous conflict resolution. Nonterminal unreachable1 and all its
650 * productions are useless. */
656 /* Likewise for a R/R conflict and nonterminal unreachable2. */
659 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
660 * when their states are renumbered due to state removal. */
669 AT_BISON_CHECK([[--report=all input.y]], 0, [],
670 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
671 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
672 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
673 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
674 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
675 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
676 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
677 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
680 AT_CHECK([[cat input.output]], 0,
681 [[Rules useless in parser due to conflicts
683 2 resolved_conflict: 'a' unreachable1
685 4 unreachable1: 'a' unreachable2
688 6 unreachable2: /* empty */
691 9 reported_conflicts: 'a'
695 State 4 conflicts: 1 shift/reduce
696 State 5 conflicts: 1 reduce/reduce
701 0 $accept: start $end
703 1 start: resolved_conflict 'a' reported_conflicts 'a'
705 2 resolved_conflict: 'a' unreachable1
708 4 unreachable1: 'a' unreachable2
711 6 unreachable2: /* empty */
714 8 reported_conflicts: 'a'
719 Terminals, with rules where they appear
726 Nonterminals, with rules where they appear
731 on left: 1, on right: 0
732 resolved_conflict (6)
733 on left: 2 3, on right: 1
735 on left: 4 5, on right: 2
737 on left: 6 7, on right: 4
738 reported_conflicts (9)
739 on left: 8 9 10, on right: 1
744 0 $accept: . start $end
745 1 start: . resolved_conflict 'a' reported_conflicts 'a'
746 2 resolved_conflict: . 'a' unreachable1
749 $default reduce using rule 3 (resolved_conflict)
752 resolved_conflict go to state 2
754 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
759 0 $accept: start . $end
761 $end shift, and go to state 3
766 1 start: resolved_conflict . 'a' reported_conflicts 'a'
768 'a' shift, and go to state 4
773 0 $accept: start $end .
780 1 start: resolved_conflict 'a' . reported_conflicts 'a'
781 8 reported_conflicts: . 'a'
785 'a' shift, and go to state 5
787 'a' [reduce using rule 10 (reported_conflicts)]
789 reported_conflicts go to state 6
794 8 reported_conflicts: 'a' . ['a']
797 'a' reduce using rule 8 (reported_conflicts)
798 'a' [reduce using rule 9 (reported_conflicts)]
799 $default reduce using rule 8 (reported_conflicts)
804 1 start: resolved_conflict 'a' reported_conflicts . 'a'
806 'a' shift, and go to state 7
811 1 start: resolved_conflict 'a' reported_conflicts 'a' .
813 $default reduce using rule 1 (start)
816 AT_DATA([[input-keep.y]],
817 [[%define lr.keep_unreachable_states
819 AT_CHECK([[cat input.y >> input-keep.y]])
821 AT_BISON_CHECK([[input-keep.y]], 0, [],
822 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
823 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
824 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
825 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
826 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
832 ## ------------------------------------------------------------ ##
833 ## Solved conflicts report for multiple reductions in a state. ##
834 ## ------------------------------------------------------------ ##
836 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
838 # Used to lose earlier solved conflict messages even within a single S/R/R.
858 empty_c1: %prec 'c' ;
859 empty_c2: %prec 'c' ;
860 empty_c3: %prec 'd' ;
862 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
863 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
866 0 $accept: . start $end
881 'b' shift, and go to state 1
883 'c' reduce using rule 13 (empty_c3)
884 $default reduce using rule 9 (empty_a)
887 empty_a go to state 3
888 empty_b go to state 4
889 empty_c1 go to state 5
890 empty_c2 go to state 6
891 empty_c3 go to state 7
893 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
894 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
895 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
896 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
897 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
906 ## ------------------------------------------------------------ ##
907 ## %nonassoc error actions for multiple reductions in a state. ##
908 ## ------------------------------------------------------------ ##
910 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
911 # multiple reductions in a state.
913 # For a %nonassoc error action token, used to print the first remaining
914 # reduction on that token without brackets.
916 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
919 [[%nonassoc 'a' 'b' 'c'
933 empty_c1: %prec 'c' ;
934 empty_c2: %prec 'c' ;
935 empty_c3: %prec 'c' ;
938 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
939 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
942 0 $accept: . start $end
957 'a' error (nonassociative)
958 'b' error (nonassociative)
959 'c' error (nonassociative)
961 'c' [reduce using rule 12 (empty_c2)]
962 'c' [reduce using rule 13 (empty_c3)]
965 empty_a go to state 2
966 empty_b go to state 3
967 empty_c1 go to state 4
968 empty_c2 go to state 5
969 empty_c3 go to state 6
971 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
972 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
973 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').