1 # Exercising Bison on conflicts. -*- Autotest -*-
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007 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 2, or (at your option)
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, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 AT_BANNER([[Conflicts.]])
23 ## ---------------- ##
25 ## ---------------- ##
27 # I once hacked Bison in such a way that it lost its reductions on the
28 # initial state (because it was confusing it with the last state). It
29 # took me a while to strip down my failures to this simple case. So
30 # make sure it finds the s/r conflict below.
32 AT_SETUP([S/R in initial])
38 e: 'e' | /* Nothing. */;
41 AT_CHECK([bison -o input.c input.y], 0, [],
42 [[input.y:4.9: warning: rule never reduced because of conflicts: e: /* empty */
48 ## ------------------- ##
49 ## %nonassoc and eof. ##
50 ## ------------------- ##
52 AT_SETUP([%nonassoc and eof])
54 AT_DATA_GRAMMAR([input.y],
61 #define YYERROR_VERBOSE 1
63 yyerror (const char *msg)
65 fprintf (stderr, "%s\n", msg);
68 /* The current argument. */
69 static const char *input;
75 if (! (toknum <= strlen (input)))
77 return input[toknum++];
91 main (int argc, const char *argv[])
93 input = argc <= 1 ? "" : argv[1];
98 # Specify the output files to avoid problems on different file systems.
99 AT_CHECK([bison -o input.c input.y])
102 AT_PARSER_CHECK([./input '0<0'])
103 # FIXME: This is an actual bug, but a new one, in the sense that
104 # no one has ever spotted it! The messages are *wrong*: there should
105 # be nothing there, it should be expected eof.
106 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
107 [syntax error, unexpected '<', expecting '<' or '>'
110 AT_PARSER_CHECK([./input '0>0'])
111 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
112 [syntax error, unexpected '>', expecting '<' or '>'
115 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
116 [syntax error, unexpected '>', expecting '<' or '>'
123 ## ------------------------- ##
124 ## Unresolved SR Conflicts. ##
125 ## ------------------------- ##
127 AT_SETUP([Unresolved SR Conflicts])
129 AT_KEYWORDS([report])
134 exp: exp OP exp | NUM;
137 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
138 [input.y: conflicts: 1 shift/reduce
141 # Check the contents of the report.
142 AT_CHECK([cat input.output], [],
143 [[State 5 conflicts: 1 shift/reduce
154 Terminals, with rules where they appear
162 Nonterminals, with rules where they appear
167 on left: 1 2, on right: 0 1
172 0 $accept: . exp $end
176 NUM shift, and go to state 1
185 $default reduce using rule 2 (exp)
190 0 $accept: exp . $end
193 $end shift, and go to state 3
194 OP shift, and go to state 4
199 0 $accept: exp $end .
210 NUM shift, and go to state 1
217 1 exp: exp . OP exp [$end, OP]
218 1 | exp OP exp . [$end, OP]
220 OP shift, and go to state 4
222 OP [reduce using rule 1 (exp)]
223 $default reduce using rule 1 (exp)
230 ## ----------------------- ##
231 ## Resolved SR Conflicts. ##
232 ## ----------------------- ##
234 AT_SETUP([Resolved SR Conflicts])
236 AT_KEYWORDS([report])
242 exp: exp OP exp | NUM;
245 AT_CHECK([bison -o input.c --report=all input.y])
247 # Check the contents of the report.
248 AT_CHECK([cat input.output], [],
257 Terminals, with rules where they appear
265 Nonterminals, with rules where they appear
270 on left: 1 2, on right: 0 1
275 0 $accept: . exp $end
279 NUM shift, and go to state 1
288 $default reduce using rule 2 (exp)
293 0 $accept: exp . $end
296 $end shift, and go to state 3
297 OP shift, and go to state 4
302 0 $accept: exp $end .
313 NUM shift, and go to state 1
320 1 exp: exp . OP exp [$end, OP]
321 1 | exp OP exp . [$end, OP]
323 $default reduce using rule 1 (exp)
325 Conflict between rule 1 and token OP resolved as reduce (%left OP).
331 ## -------------------------------- ##
332 ## Defaulted Conflicted Reduction. ##
333 ## -------------------------------- ##
335 # When there are RR conflicts, some rules are disabled. Usually it is
336 # simply displayed as:
338 # $end reduce using rule 3 (num)
339 # $end [reduce using rule 4 (id)]
341 # But when `reduce 3' is the default action, we'd produce:
343 # $end [reduce using rule 4 (id)]
344 # $default reduce using rule 3 (num)
346 # In this precise case (a reduction is masked by the default
347 # reduction), we make the `reduce 3' explicit:
349 # $end reduce using rule 3 (num)
350 # $end [reduce using rule 4 (id)]
351 # $default reduce using rule 3 (num)
353 # Maybe that's not the best display, but then, please propose something
356 AT_SETUP([Defaulted Conflicted Reduction])
357 AT_KEYWORDS([report])
367 AT_CHECK([bison -o input.c --report=all input.y], 0, [],
368 [[input.y: conflicts: 1 reduce/reduce
369 input.y:4.6-8: warning: rule never reduced because of conflicts: id: '0'
372 # Check the contents of the report.
373 AT_CHECK([cat input.output], [],
374 [[Rules never reduced
379 State 1 conflicts: 1 reduce/reduce
394 Terminals, with rules where they appear
401 Nonterminals, with rules where they appear
406 on left: 1 2, on right: 0
408 on left: 3, on right: 1
410 on left: 4, on right: 2
415 0 $accept: . exp $end
421 '0' shift, and go to state 1
433 $end reduce using rule 3 (num)
434 $end [reduce using rule 4 (id)]
435 $default reduce using rule 3 (num)
440 0 $accept: exp . $end
442 $end shift, and go to state 5
449 $default reduce using rule 1 (exp)
456 $default reduce using rule 2 (exp)
461 0 $accept: exp $end .
471 ## -------------------- ##
472 ## %expect not enough. ##
473 ## -------------------- ##
475 AT_SETUP([%expect not enough])
481 exp: exp OP exp | NUM;
484 AT_CHECK([bison -o input.c input.y], 1, [],
485 [input.y: conflicts: 1 shift/reduce
486 input.y: expected 0 shift/reduce conflicts
491 ## --------------- ##
493 ## --------------- ##
495 AT_SETUP([%expect right])
501 exp: exp OP exp | NUM;
504 AT_CHECK([bison -o input.c input.y])
508 ## ------------------ ##
509 ## %expect too much. ##
510 ## ------------------ ##
512 AT_SETUP([%expect too much])
518 exp: exp OP exp | NUM;
521 AT_CHECK([bison -o input.c input.y], 1, [],
522 [input.y: conflicts: 1 shift/reduce
523 input.y: expected 2 shift/reduce conflicts
528 ## ------------------------------ ##
529 ## %expect with reduce conflicts ##
530 ## ------------------------------ ##
532 AT_SETUP([%expect with reduce conflicts])
537 program: a 'a' | a a;
541 AT_CHECK([bison -o input.c input.y], 1, [],
542 [input.y: conflicts: 1 reduce/reduce
543 input.y: expected 0 reduce/reduce conflicts
548 ## ------------------------------- ##
549 ## %no-default-prec without %prec ##
550 ## ------------------------------- ##
552 AT_SETUP([%no-default-prec without %prec])
568 AT_CHECK([bison -o input.c input.y], 0, [],
569 [[input.y: conflicts: 4 shift/reduce
574 ## ---------------------------- ##
575 ## %no-default-prec with %prec ##
576 ## ---------------------------- ##
578 AT_SETUP([%no-default-prec with %prec])
594 AT_CHECK([bison -o input.c input.y])
598 ## ---------------- ##
600 ## ---------------- ##
602 AT_SETUP([%default-prec])
618 AT_CHECK([bison -o input.c input.y])
622 ## ---------------------------------------------- ##
623 ## Unreachable States After Conflict Resolution. ##
624 ## ---------------------------------------------- ##
626 AT_SETUP([[Unreachable States After Conflict Resolution]])
628 # If conflict resolution makes states unreachable, remove those states, report
629 # rules that are then unused, and don't report conflicts in those states. Test
630 # what happens when a nonterminal becomes useless as a result of state removal
631 # since that causes lalr.o's goto map to be rewritten.
639 start: resolved_conflict 'a' reported_conflicts 'a' ;
641 /* S/R conflict resolved as shift, so the state with item
642 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
643 * unreachable, and the associated production is useless. */
649 /* S/R conflict that need not be reported since it is unreachable because of
650 * the previous conflict resolution. Nonterminal unreachable1 and all its
651 * productions are useless. */
657 /* Likewise for a R/R conflict and nonterminal unreachable2. */
660 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
661 * when their states are renumbered due to state removal. */
670 AT_CHECK([[bison --report=all input.y]], 0, [],
671 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
672 input.y:12.5-20: warning: rule never reduced because of conflicts: resolved_conflict: 'a' unreachable1
673 input.y:20.5-20: warning: rule never reduced because of conflicts: unreachable1: 'a' unreachable2
674 input.y:21.4: warning: rule never reduced because of conflicts: unreachable1: /* empty */
675 input.y:25.13: warning: rule never reduced because of conflicts: unreachable2: /* empty */
676 input.y:25.16: warning: rule never reduced because of conflicts: unreachable2: /* empty */
677 input.y:31.5-7: warning: rule never reduced because of conflicts: reported_conflicts: 'a'
678 input.y:32.4: warning: rule never reduced because of conflicts: reported_conflicts: /* empty */
681 AT_CHECK([[cat input.output]], 0,
682 [[Rules never reduced
684 2 resolved_conflict: 'a' unreachable1
686 4 unreachable1: 'a' unreachable2
689 6 unreachable2: /* empty */
692 9 reported_conflicts: 'a'
696 State 4 conflicts: 1 shift/reduce
697 State 5 conflicts: 1 reduce/reduce
702 0 $accept: start $end
704 1 start: resolved_conflict 'a' reported_conflicts 'a'
706 2 resolved_conflict: 'a' unreachable1
709 4 unreachable1: 'a' unreachable2
712 6 unreachable2: /* empty */
715 8 reported_conflicts: 'a'
720 Terminals, with rules where they appear
727 Nonterminals, with rules where they appear
732 on left: 1, on right: 0
733 resolved_conflict (6)
734 on left: 2 3, on right: 1
736 on left: 4 5, on right: 2
738 on left: 6 7, on right: 4
739 reported_conflicts (9)
740 on left: 8 9 10, on right: 1
745 0 $accept: . start $end
746 1 start: . resolved_conflict 'a' reported_conflicts 'a'
747 2 resolved_conflict: . 'a' unreachable1
750 $default reduce using rule 3 (resolved_conflict)
753 resolved_conflict go to state 2
755 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
760 0 $accept: start . $end
762 $end shift, and go to state 3
767 1 start: resolved_conflict . 'a' reported_conflicts 'a'
769 'a' shift, and go to state 4
774 0 $accept: start $end .
781 1 start: resolved_conflict 'a' . reported_conflicts 'a'
782 8 reported_conflicts: . 'a'
786 'a' shift, and go to state 5
788 'a' [reduce using rule 10 (reported_conflicts)]
790 reported_conflicts go to state 6
795 8 reported_conflicts: 'a' . ['a']
798 'a' reduce using rule 8 (reported_conflicts)
799 'a' [reduce using rule 9 (reported_conflicts)]
800 $default reduce using rule 8 (reported_conflicts)
805 1 start: resolved_conflict 'a' reported_conflicts . 'a'
807 'a' shift, and go to state 7
812 1 start: resolved_conflict 'a' reported_conflicts 'a' .
814 $default reduce using rule 1 (start)
820 ## ------------------------------------------------------------ ##
821 ## Solved conflicts report for multiple reductions in a state. ##
822 ## ------------------------------------------------------------ ##
824 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
826 # Used to lose earlier solved conflict messages even within a single S/R/R.
846 empty_c1: %prec 'c' ;
847 empty_c2: %prec 'c' ;
848 empty_c3: %prec 'd' ;
850 AT_CHECK([[bison --report=all -o input.c input.y]], 0, [], [ignore])
851 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
854 0 $accept: . start $end
869 'b' shift, and go to state 1
871 'c' reduce using rule 13 (empty_c3)
872 $default reduce using rule 9 (empty_a)
875 empty_a go to state 3
876 empty_b go to state 4
877 empty_c1 go to state 5
878 empty_c2 go to state 6
879 empty_c3 go to state 7
881 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
882 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
883 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
884 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
885 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
894 ## ------------------------------------------------------------ ##
895 ## %nonassoc error actions for multiple reductions in a state. ##
896 ## ------------------------------------------------------------ ##
898 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
899 # multiple reductions in a state.
901 # For a %nonassoc error action token, used to print the first remaining
902 # reduction on that token without brackets.
904 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
907 [[%nonassoc 'a' 'b' 'c'
921 empty_c1: %prec 'c' ;
922 empty_c2: %prec 'c' ;
923 empty_c3: %prec 'c' ;
926 AT_CHECK([[bison --report=all -o input.c input.y]], 0, [], [ignore])
927 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
930 0 $accept: . start $end
945 'a' error (nonassociative)
946 'b' error (nonassociative)
947 'c' error (nonassociative)
949 'c' [reduce using rule 12 (empty_c2)]
950 'c' [reduce using rule 13 (empty_c3)]
953 empty_a go to state 2
954 empty_b go to state 3
955 empty_c1 go to state 4
956 empty_c2 go to state 5
957 empty_c3 go to state 6
959 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
960 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
961 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').