1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007, 2008 Free Software Foundation, Inc.
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 AT_BANNER([[Conflicts.]])
21 ## ---------------- ##
23 ## ---------------- ##
25 # I once hacked Bison in such a way that it lost its reductions on the
26 # initial state (because it was confusing it with the last state). It
27 # took me a while to strip down my failures to this simple case. So
28 # make sure it finds the s/r conflict below.
30 AT_SETUP([S/R in initial])
36 e: 'e' | /* Nothing. */;
39 AT_BISON_CHECK([-o input.c input.y], 0, [],
40 [[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */
46 ## ------------------- ##
47 ## %nonassoc and eof. ##
48 ## ------------------- ##
50 AT_SETUP([%nonassoc and eof])
52 AT_DATA_GRAMMAR([input.y],
59 #define YYERROR_VERBOSE 1
61 yyerror (const char *msg)
63 fprintf (stderr, "%s\n", msg);
66 /* The current argument. */
67 static const char *input;
73 if (! (toknum <= strlen (input)))
75 return input[toknum++];
89 main (int argc, const char *argv[])
91 input = argc <= 1 ? "" : argv[1];
96 # Specify the output files to avoid problems on different file systems.
97 AT_BISON_CHECK([-o input.c input.y])
100 AT_PARSER_CHECK([./input '0<0'])
101 # FIXME: This is an actual bug, but a new one, in the sense that
102 # no one has ever spotted it! The messages are *wrong*: there should
103 # be nothing there, it should be expected eof.
104 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
105 [syntax error, unexpected '<', expecting '<' or '>'
108 AT_PARSER_CHECK([./input '0>0'])
109 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
110 [syntax error, unexpected '>', expecting '<' or '>'
113 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
114 [syntax error, unexpected '>', expecting '<' or '>'
121 ## ------------------------- ##
122 ## Unresolved SR Conflicts. ##
123 ## ------------------------- ##
125 AT_SETUP([Unresolved SR Conflicts])
127 AT_KEYWORDS([report])
132 exp: exp OP exp | NUM;
135 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
136 [input.y: conflicts: 1 shift/reduce
139 # Check the contents of the report.
140 AT_CHECK([cat input.output], [],
141 [[State 5 conflicts: 1 shift/reduce
152 Terminals, with rules where they appear
160 Nonterminals, with rules where they appear
165 on left: 1 2, on right: 0 1
170 0 $accept: . exp $end
174 NUM shift, and go to state 1
183 $default reduce using rule 2 (exp)
188 0 $accept: exp . $end
191 $end shift, and go to state 3
192 OP shift, and go to state 4
197 0 $accept: exp $end .
208 NUM shift, and go to state 1
216 1 | exp OP exp . [$end, OP]
218 OP shift, and go to state 4
220 OP [reduce using rule 1 (exp)]
221 $default reduce using rule 1 (exp)
228 ## ----------------------- ##
229 ## Resolved SR Conflicts. ##
230 ## ----------------------- ##
232 AT_SETUP([Resolved SR Conflicts])
234 AT_KEYWORDS([report])
240 exp: exp OP exp | NUM;
243 AT_BISON_CHECK([-o input.c --report=all input.y])
245 # Check the contents of the report.
246 AT_CHECK([cat input.output], [],
255 Terminals, with rules where they appear
263 Nonterminals, with rules where they appear
268 on left: 1 2, on right: 0 1
273 0 $accept: . exp $end
277 NUM shift, and go to state 1
286 $default reduce using rule 2 (exp)
291 0 $accept: exp . $end
294 $end shift, and go to state 3
295 OP shift, and go to state 4
300 0 $accept: exp $end .
311 NUM shift, and go to state 1
319 1 | exp OP exp . [$end, OP]
321 $default reduce using rule 1 (exp)
323 Conflict between rule 1 and token OP resolved as reduce (%left OP).
329 ## ---------------------- ##
330 ## %precedence suffices. ##
331 ## ---------------------- ##
333 AT_SETUP([%precedence suffices])
340 "if" cond "then" stmt
341 | "if" cond "then" stmt "else" stmt
350 AT_BISON_CHECK([-o input.c input.y])
355 ## ------------------------------ ##
356 ## %precedence does not suffice. ##
357 ## ------------------------------ ##
359 AT_SETUP([%precedence does not suffice])
366 "if" cond "then" stmt
367 | "if" cond "then" stmt "else" stmt
377 AT_BISON_CHECK([-o input.c input.y], 0, [],
378 [[input.y: conflicts: 1 shift/reduce
379 input.y:12.3-18: warning: rule useless in parser due to conflicts: cond: cond "then" cond
385 ## -------------------------------- ##
386 ## Defaulted Conflicted Reduction. ##
387 ## -------------------------------- ##
389 # When there are RR conflicts, some rules are disabled. Usually it is
390 # simply displayed as:
392 # $end reduce using rule 3 (num)
393 # $end [reduce using rule 4 (id)]
395 # But when `reduce 3' is the default action, we'd produce:
397 # $end [reduce using rule 4 (id)]
398 # $default reduce using rule 3 (num)
400 # In this precise case (a reduction is masked by the default
401 # reduction), we make the `reduce 3' explicit:
403 # $end reduce using rule 3 (num)
404 # $end [reduce using rule 4 (id)]
405 # $default reduce using rule 3 (num)
407 # Maybe that's not the best display, but then, please propose something
410 AT_SETUP([Defaulted Conflicted Reduction])
411 AT_KEYWORDS([report])
421 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
422 [[input.y: conflicts: 1 reduce/reduce
423 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
426 # Check the contents of the report.
427 AT_CHECK([cat input.output], [],
428 [[Rules useless in parser due to conflicts
433 State 1 conflicts: 1 reduce/reduce
448 Terminals, with rules where they appear
455 Nonterminals, with rules where they appear
460 on left: 1 2, on right: 0
462 on left: 3, on right: 1
464 on left: 4, on right: 2
469 0 $accept: . exp $end
475 '0' shift, and go to state 1
487 $end reduce using rule 3 (num)
488 $end [reduce using rule 4 (id)]
489 $default reduce using rule 3 (num)
494 0 $accept: exp . $end
496 $end shift, and go to state 5
503 $default reduce using rule 1 (exp)
510 $default reduce using rule 2 (exp)
515 0 $accept: exp $end .
525 ## -------------------- ##
526 ## %expect not enough. ##
527 ## -------------------- ##
529 AT_SETUP([%expect not enough])
535 exp: exp OP exp | NUM;
538 AT_BISON_CHECK([-o input.c input.y], 1, [],
539 [input.y: conflicts: 1 shift/reduce
540 input.y: expected 0 shift/reduce conflicts
545 ## --------------- ##
547 ## --------------- ##
549 AT_SETUP([%expect right])
555 exp: exp OP exp | NUM;
558 AT_BISON_CHECK([-o input.c input.y])
562 ## ------------------ ##
563 ## %expect too much. ##
564 ## ------------------ ##
566 AT_SETUP([%expect too much])
572 exp: exp OP exp | NUM;
575 AT_BISON_CHECK([-o input.c input.y], 1, [],
576 [input.y: conflicts: 1 shift/reduce
577 input.y: expected 2 shift/reduce conflicts
582 ## ------------------------------ ##
583 ## %expect with reduce conflicts ##
584 ## ------------------------------ ##
586 AT_SETUP([%expect with reduce conflicts])
591 program: a 'a' | a a;
595 AT_BISON_CHECK([-o input.c input.y], 1, [],
596 [input.y: conflicts: 1 reduce/reduce
597 input.y: expected 0 reduce/reduce conflicts
602 ## ------------------------------- ##
603 ## %no-default-prec without %prec ##
604 ## ------------------------------- ##
606 AT_SETUP([%no-default-prec without %prec])
622 AT_BISON_CHECK([-o input.c input.y], 0, [],
623 [[input.y: conflicts: 4 shift/reduce
628 ## ---------------------------- ##
629 ## %no-default-prec with %prec ##
630 ## ---------------------------- ##
632 AT_SETUP([%no-default-prec with %prec])
648 AT_BISON_CHECK([-o input.c input.y])
652 ## ---------------- ##
654 ## ---------------- ##
656 AT_SETUP([%default-prec])
672 AT_BISON_CHECK([-o input.c input.y])
676 ## ---------------------------------------------- ##
677 ## Unreachable States After Conflict Resolution. ##
678 ## ---------------------------------------------- ##
680 AT_SETUP([[Unreachable States After Conflict Resolution]])
682 # If conflict resolution makes states unreachable, remove those states, report
683 # rules that are then unused, and don't report conflicts in those states. Test
684 # what happens when a nonterminal becomes useless as a result of state removal
685 # since that causes lalr.o's goto map to be rewritten.
693 start: resolved_conflict 'a' reported_conflicts 'a' ;
695 /* S/R conflict resolved as reduce, so the state with item
696 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
697 * unreachable, and the associated production is useless. */
703 /* S/R conflict that need not be reported since it is unreachable because of
704 * the previous conflict resolution. Nonterminal unreachable1 and all its
705 * productions are useless. */
711 /* Likewise for a R/R conflict and nonterminal unreachable2. */
714 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
715 * when their states are renumbered due to state removal. */
724 AT_BISON_CHECK([[--report=all input.y]], 0, [],
725 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
726 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
727 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
728 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
729 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
730 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
731 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
732 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
735 AT_CHECK([[cat input.output]], 0,
736 [[Rules useless in parser due to conflicts
738 2 resolved_conflict: 'a' unreachable1
740 4 unreachable1: 'a' unreachable2
743 6 unreachable2: /* empty */
746 9 reported_conflicts: 'a'
750 State 4 conflicts: 1 shift/reduce
751 State 5 conflicts: 1 reduce/reduce
756 0 $accept: start $end
758 1 start: resolved_conflict 'a' reported_conflicts 'a'
760 2 resolved_conflict: 'a' unreachable1
763 4 unreachable1: 'a' unreachable2
766 6 unreachable2: /* empty */
769 8 reported_conflicts: 'a'
774 Terminals, with rules where they appear
781 Nonterminals, with rules where they appear
786 on left: 1, on right: 0
787 resolved_conflict (6)
788 on left: 2 3, on right: 1
790 on left: 4 5, on right: 2
792 on left: 6 7, on right: 4
793 reported_conflicts (9)
794 on left: 8 9 10, on right: 1
799 0 $accept: . start $end
800 1 start: . resolved_conflict 'a' reported_conflicts 'a'
801 2 resolved_conflict: . 'a' unreachable1
804 $default reduce using rule 3 (resolved_conflict)
807 resolved_conflict go to state 2
809 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
814 0 $accept: start . $end
816 $end shift, and go to state 3
821 1 start: resolved_conflict . 'a' reported_conflicts 'a'
823 'a' shift, and go to state 4
828 0 $accept: start $end .
835 1 start: resolved_conflict 'a' . reported_conflicts 'a'
836 8 reported_conflicts: . 'a'
840 'a' shift, and go to state 5
842 'a' [reduce using rule 10 (reported_conflicts)]
844 reported_conflicts go to state 6
849 8 reported_conflicts: 'a' . ['a']
852 'a' reduce using rule 8 (reported_conflicts)
853 'a' [reduce using rule 9 (reported_conflicts)]
854 $default reduce using rule 8 (reported_conflicts)
859 1 start: resolved_conflict 'a' reported_conflicts . 'a'
861 'a' shift, and go to state 7
866 1 start: resolved_conflict 'a' reported_conflicts 'a' .
868 $default reduce using rule 1 (start)
871 AT_DATA([[input-keep.y]],
872 [[%define lr.keep_unreachable_states
874 AT_CHECK([[cat input.y >> input-keep.y]])
876 AT_BISON_CHECK([[input-keep.y]], 0, [],
877 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
878 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
879 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
880 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
881 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
887 ## ------------------------------------------------------------ ##
888 ## Solved conflicts report for multiple reductions in a state. ##
889 ## ------------------------------------------------------------ ##
891 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
893 # Used to lose earlier solved conflict messages even within a single S/R/R.
913 empty_c1: %prec 'c' ;
914 empty_c2: %prec 'c' ;
915 empty_c3: %prec 'd' ;
917 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
918 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
921 0 $accept: . start $end
936 'b' shift, and go to state 1
938 'c' reduce using rule 13 (empty_c3)
939 $default reduce using rule 9 (empty_a)
942 empty_a go to state 3
943 empty_b go to state 4
944 empty_c1 go to state 5
945 empty_c2 go to state 6
946 empty_c3 go to state 7
948 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
949 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
950 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
951 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
952 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
961 ## ------------------------------------------------------------ ##
962 ## %nonassoc error actions for multiple reductions in a state. ##
963 ## ------------------------------------------------------------ ##
965 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
966 # multiple reductions in a state.
968 # For a %nonassoc error action token, used to print the first remaining
969 # reduction on that token without brackets.
971 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
974 [[%nonassoc 'a' 'b' 'c'
988 empty_c1: %prec 'c' ;
989 empty_c2: %prec 'c' ;
990 empty_c3: %prec 'c' ;
993 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
994 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
997 0 $accept: . start $end
1009 12 empty_c2: . ['c']
1010 13 empty_c3: . ['c']
1012 'a' error (nonassociative)
1013 'b' error (nonassociative)
1014 'c' error (nonassociative)
1016 'c' [reduce using rule 12 (empty_c2)]
1017 'c' [reduce using rule 13 (empty_c3)]
1020 empty_a go to state 2
1021 empty_b go to state 3
1022 empty_c1 go to state 4
1023 empty_c2 go to state 5
1024 empty_c3 go to state 6
1026 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1027 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1028 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').