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 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
103 [syntax error, unexpected '<'
106 AT_PARSER_CHECK([./input '0>0'])
107 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
108 [syntax error, unexpected '>'
111 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
112 [syntax error, unexpected '>'
119 ## ------------------------- ##
120 ## Unresolved SR Conflicts. ##
121 ## ------------------------- ##
123 AT_SETUP([Unresolved SR Conflicts])
125 AT_KEYWORDS([report])
130 exp: exp OP exp | NUM;
133 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
134 [input.y: conflicts: 1 shift/reduce
137 # Check the contents of the report.
138 AT_CHECK([cat input.output], [],
139 [[State 5 conflicts: 1 shift/reduce
150 Terminals, with rules where they appear
158 Nonterminals, with rules where they appear
163 on left: 1 2, on right: 0 1
168 0 $accept: . exp $end
172 NUM shift, and go to state 1
181 $default reduce using rule 2 (exp)
186 0 $accept: exp . $end
189 $end shift, and go to state 3
190 OP shift, and go to state 4
195 0 $accept: exp $end .
206 NUM shift, and go to state 1
214 1 | exp OP exp . [$end, OP]
216 OP shift, and go to state 4
218 OP [reduce using rule 1 (exp)]
219 $default reduce using rule 1 (exp)
226 ## ----------------------- ##
227 ## Resolved SR Conflicts. ##
228 ## ----------------------- ##
230 AT_SETUP([Resolved SR Conflicts])
232 AT_KEYWORDS([report])
238 exp: exp OP exp | NUM;
241 AT_BISON_CHECK([-o input.c --report=all input.y])
243 # Check the contents of the report.
244 AT_CHECK([cat input.output], [],
253 Terminals, with rules where they appear
261 Nonterminals, with rules where they appear
266 on left: 1 2, on right: 0 1
271 0 $accept: . exp $end
275 NUM shift, and go to state 1
284 $default reduce using rule 2 (exp)
289 0 $accept: exp . $end
292 $end shift, and go to state 3
293 OP shift, and go to state 4
298 0 $accept: exp $end .
309 NUM shift, and go to state 1
317 1 | exp OP exp . [$end, OP]
319 $default reduce using rule 1 (exp)
321 Conflict between rule 1 and token OP resolved as reduce (%left OP).
327 ## -------------------------------- ##
328 ## Defaulted Conflicted Reduction. ##
329 ## -------------------------------- ##
331 # When there are RR conflicts, some rules are disabled. Usually it is
332 # simply displayed as:
334 # $end reduce using rule 3 (num)
335 # $end [reduce using rule 4 (id)]
337 # But when `reduce 3' is the default action, we'd produce:
339 # $end [reduce using rule 4 (id)]
340 # $default reduce using rule 3 (num)
342 # In this precise case (a reduction is masked by the default
343 # reduction), we make the `reduce 3' explicit:
345 # $end reduce using rule 3 (num)
346 # $end [reduce using rule 4 (id)]
347 # $default reduce using rule 3 (num)
349 # Maybe that's not the best display, but then, please propose something
352 AT_SETUP([Defaulted Conflicted Reduction])
353 AT_KEYWORDS([report])
363 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
364 [[input.y: conflicts: 1 reduce/reduce
365 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
368 # Check the contents of the report.
369 AT_CHECK([cat input.output], [],
370 [[Rules useless in parser due to conflicts
375 State 1 conflicts: 1 reduce/reduce
390 Terminals, with rules where they appear
397 Nonterminals, with rules where they appear
402 on left: 1 2, on right: 0
404 on left: 3, on right: 1
406 on left: 4, on right: 2
411 0 $accept: . exp $end
417 '0' shift, and go to state 1
429 $end reduce using rule 3 (num)
430 $end [reduce using rule 4 (id)]
431 $default reduce using rule 3 (num)
436 0 $accept: exp . $end
438 $end shift, and go to state 5
445 $default reduce using rule 1 (exp)
452 $default reduce using rule 2 (exp)
457 0 $accept: exp $end .
467 ## -------------------- ##
468 ## %expect not enough. ##
469 ## -------------------- ##
471 AT_SETUP([%expect not enough])
477 exp: exp OP exp | NUM;
480 AT_BISON_CHECK([-o input.c input.y], 1, [],
481 [input.y: conflicts: 1 shift/reduce
482 input.y: expected 0 shift/reduce conflicts
487 ## --------------- ##
489 ## --------------- ##
491 AT_SETUP([%expect right])
497 exp: exp OP exp | NUM;
500 AT_BISON_CHECK([-o input.c input.y])
504 ## ------------------ ##
505 ## %expect too much. ##
506 ## ------------------ ##
508 AT_SETUP([%expect too much])
514 exp: exp OP exp | NUM;
517 AT_BISON_CHECK([-o input.c input.y], 1, [],
518 [input.y: conflicts: 1 shift/reduce
519 input.y: expected 2 shift/reduce conflicts
524 ## ------------------------------- ##
525 ## %expect with reduce conflicts. ##
526 ## ------------------------------- ##
528 AT_SETUP([%expect with reduce conflicts])
533 program: a 'a' | a a;
537 AT_BISON_CHECK([-o input.c input.y], 1, [],
538 [input.y: conflicts: 1 reduce/reduce
539 input.y: expected 0 reduce/reduce conflicts
544 ## ------------------------- ##
545 ## %prec with user strings. ##
546 ## ------------------------- ##
548 AT_SETUP([%prec with user string])
557 AT_BISON_CHECK([-o input.c input.y])
561 ## -------------------------------- ##
562 ## %no-default-prec without %prec. ##
563 ## -------------------------------- ##
565 AT_SETUP([%no-default-prec without %prec])
581 AT_BISON_CHECK([-o input.c input.y], 0, [],
582 [[input.y: conflicts: 4 shift/reduce
587 ## ----------------------------- ##
588 ## %no-default-prec with %prec. ##
589 ## ----------------------------- ##
591 AT_SETUP([%no-default-prec with %prec])
607 AT_BISON_CHECK([-o input.c input.y])
611 ## --------------- ##
613 ## --------------- ##
615 AT_SETUP([%default-prec])
631 AT_BISON_CHECK([-o input.c input.y])
635 ## ---------------------------------------------- ##
636 ## Unreachable States After Conflict Resolution. ##
637 ## ---------------------------------------------- ##
639 AT_SETUP([[Unreachable States After Conflict Resolution]])
641 # If conflict resolution makes states unreachable, remove those states, report
642 # rules that are then unused, and don't report conflicts in those states. Test
643 # what happens when a nonterminal becomes useless as a result of state removal
644 # since that causes lalr.o's goto map to be rewritten.
652 start: resolved_conflict 'a' reported_conflicts 'a' ;
654 /* S/R conflict resolved as reduce, so the state with item
655 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
656 * unreachable, and the associated production is useless. */
662 /* S/R conflict that need not be reported since it is unreachable because of
663 * the previous conflict resolution. Nonterminal unreachable1 and all its
664 * productions are useless. */
670 /* Likewise for a R/R conflict and nonterminal unreachable2. */
673 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
674 * when their states are renumbered due to state removal. */
683 AT_BISON_CHECK([[--report=all input.y]], 0, [],
684 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
685 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
686 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
687 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
688 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
689 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
690 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
691 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
694 AT_CHECK([[cat input.output]], 0,
695 [[Rules useless in parser due to conflicts
697 2 resolved_conflict: 'a' unreachable1
699 4 unreachable1: 'a' unreachable2
702 6 unreachable2: /* empty */
705 9 reported_conflicts: 'a'
709 State 4 conflicts: 1 shift/reduce
710 State 5 conflicts: 1 reduce/reduce
715 0 $accept: start $end
717 1 start: resolved_conflict 'a' reported_conflicts 'a'
719 2 resolved_conflict: 'a' unreachable1
722 4 unreachable1: 'a' unreachable2
725 6 unreachable2: /* empty */
728 8 reported_conflicts: 'a'
733 Terminals, with rules where they appear
740 Nonterminals, with rules where they appear
745 on left: 1, on right: 0
746 resolved_conflict (6)
747 on left: 2 3, on right: 1
749 on left: 4 5, on right: 2
751 on left: 6 7, on right: 4
752 reported_conflicts (9)
753 on left: 8 9 10, on right: 1
758 0 $accept: . start $end
759 1 start: . resolved_conflict 'a' reported_conflicts 'a'
760 2 resolved_conflict: . 'a' unreachable1
763 $default reduce using rule 3 (resolved_conflict)
766 resolved_conflict go to state 2
768 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
773 0 $accept: start . $end
775 $end shift, and go to state 3
780 1 start: resolved_conflict . 'a' reported_conflicts 'a'
782 'a' shift, and go to state 4
787 0 $accept: start $end .
794 1 start: resolved_conflict 'a' . reported_conflicts 'a'
795 8 reported_conflicts: . 'a'
799 'a' shift, and go to state 5
801 'a' [reduce using rule 10 (reported_conflicts)]
803 reported_conflicts go to state 6
808 8 reported_conflicts: 'a' . ['a']
811 'a' reduce using rule 8 (reported_conflicts)
812 'a' [reduce using rule 9 (reported_conflicts)]
813 $default reduce using rule 8 (reported_conflicts)
818 1 start: resolved_conflict 'a' reported_conflicts . 'a'
820 'a' shift, and go to state 7
825 1 start: resolved_conflict 'a' reported_conflicts 'a' .
827 $default reduce using rule 1 (start)
830 AT_DATA([[input-keep.y]],
831 [[%define lr.keep-unreachable-states
833 AT_CHECK([[cat input.y >> input-keep.y]])
835 AT_BISON_CHECK([[input-keep.y]], 0, [],
836 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
837 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
838 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
839 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
840 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
846 ## ------------------------------------------------------------ ##
847 ## Solved conflicts report for multiple reductions in a state. ##
848 ## ------------------------------------------------------------ ##
850 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
852 # Used to lose earlier solved conflict messages even within a single S/R/R.
872 empty_c1: %prec 'c' ;
873 empty_c2: %prec 'c' ;
874 empty_c3: %prec 'd' ;
876 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
877 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
880 0 $accept: . start $end
895 'b' shift, and go to state 1
897 'c' reduce using rule 13 (empty_c3)
898 $default reduce using rule 9 (empty_a)
901 empty_a go to state 3
902 empty_b go to state 4
903 empty_c1 go to state 5
904 empty_c2 go to state 6
905 empty_c3 go to state 7
907 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
908 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
909 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
910 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
911 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
920 ## ------------------------------------------------------------ ##
921 ## %nonassoc error actions for multiple reductions in a state. ##
922 ## ------------------------------------------------------------ ##
924 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
925 # multiple reductions in a state.
927 # For a %nonassoc error action token, used to print the first remaining
928 # reduction on that token without brackets.
930 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
933 [[%nonassoc 'a' 'b' 'c'
947 empty_c1: %prec 'c' ;
948 empty_c2: %prec 'c' ;
949 empty_c3: %prec 'c' ;
952 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
953 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
956 0 $accept: . start $end
971 'a' error (nonassociative)
972 'b' error (nonassociative)
973 'c' error (nonassociative)
975 'c' [reduce using rule 12 (empty_c2)]
976 'c' [reduce using rule 13 (empty_c3)]
979 empty_a go to state 2
980 empty_b go to state 3
981 empty_c1 go to state 4
982 empty_c2 go to state 5
983 empty_c3 go to state 6
985 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
986 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
987 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').