]> git.saurik.com Git - bison.git/blob - tests/conflicts.at
yysyntax_error: adjust prior fixes for branch-2.5's lalr1.cc.
[bison.git] / tests / conflicts.at
1 # Exercising Bison on conflicts. -*- Autotest -*-
2
3 # Copyright (C) 2002, 2003, 2004, 2005, 2007, 2009, 2010 Free
4 # Software Foundation, Inc.
5
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.
10 #
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.
15 #
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/>.
18
19 AT_BANNER([[Conflicts.]])
20
21
22 ## ---------------- ##
23 ## S/R in initial. ##
24 ## ---------------- ##
25
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.
30
31 AT_SETUP([S/R in initial])
32
33 AT_DATA([[input.y]],
34 [[%expect 1
35 %%
36 exp: e 'e';
37 e: 'e' | /* Nothing. */;
38 ]])
39
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 */
42 ]])
43
44 AT_CLEANUP
45
46
47 ## ------------------- ##
48 ## %nonassoc and eof. ##
49 ## ------------------- ##
50
51 AT_SETUP([%nonassoc and eof])
52
53 AT_DATA_GRAMMAR([input.y],
54 [[
55 %{
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 #define YYERROR_VERBOSE 1
61 static void
62 yyerror (const char *msg)
63 {
64 fprintf (stderr, "%s\n", msg);
65 }
66
67 /* The current argument. */
68 static const char *input;
69
70 static int
71 yylex (void)
72 {
73 static size_t toknum;
74 if (! (toknum <= strlen (input)))
75 abort ();
76 return input[toknum++];
77 }
78
79 %}
80
81 %nonassoc '<' '>'
82
83 %%
84 expr: expr '<' expr
85 | expr '>' expr
86 | '0'
87 ;
88 %%
89 int
90 main (int argc, const char *argv[])
91 {
92 input = argc <= 1 ? "" : argv[1];
93 return yyparse ();
94 }
95 ]])
96
97 # Specify the output files to avoid problems on different file systems.
98 AT_BISON_CHECK([-o input.c input.y])
99 AT_COMPILE([input])
100
101 AT_PARSER_CHECK([./input '0<0'])
102 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
103 [syntax error, unexpected '<'
104 ])
105
106 AT_PARSER_CHECK([./input '0>0'])
107 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
108 [syntax error, unexpected '>'
109 ])
110
111 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
112 [syntax error, unexpected '>'
113 ])
114
115 # We must disable default reductions in inconsistent states in order to
116 # have an explicit list of all expected tokens. (However, unless we use
117 # canonical LR, lookahead sets are merged for different left contexts,
118 # so it is still possible to have extra incorrect tokens in the expected
119 # list. That just doesn't happen to be a problem for this test case.)
120
121 AT_BISON_CHECK([-Dlr.default-reductions=consistent -o input.c input.y])
122 AT_COMPILE([input])
123
124 AT_PARSER_CHECK([./input '0<0'])
125 AT_PARSER_CHECK([./input '0<0<0'], [1], [],
126 [syntax error, unexpected '<', expecting $end
127 ])
128
129 AT_PARSER_CHECK([./input '0>0'])
130 AT_PARSER_CHECK([./input '0>0>0'], [1], [],
131 [syntax error, unexpected '>', expecting $end
132 ])
133
134 AT_PARSER_CHECK([./input '0<0>0'], [1], [],
135 [syntax error, unexpected '>', expecting $end
136 ])
137
138 AT_CLEANUP
139
140
141
142 ## -------------------------------------- ##
143 ## %error-verbose and consistent errors. ##
144 ## -------------------------------------- ##
145
146 AT_SETUP([[%error-verbose and consistent errors]])
147
148 m4_pushdef([AT_CONSISTENT_ERRORS_CHECK], [
149
150 AT_BISON_OPTION_PUSHDEFS([$1])
151
152 m4_pushdef([AT_YYLEX_PROTOTYPE],
153 [AT_SKEL_CC_IF([[int yylex (yy::parser::semantic_type *lvalp)]],
154 [[int yylex (YYSTYPE *lvalp)]])])
155
156 AT_SKEL_JAVA_IF([AT_DATA], [AT_DATA_GRAMMAR])([input.y],
157 [AT_SKEL_JAVA_IF([[
158
159 %code imports {
160 import java.io.IOException;
161 }]], [[
162
163 %code {]AT_SKEL_CC_IF([[
164 #include <cassert>
165 #include <string>]], [[
166 #include <assert.h>
167 #include <stdio.h>
168 void yyerror (char const *msg);]])[
169 ]AT_YYLEX_PROTOTYPE[;
170 #define USE(Var)
171 }
172
173 ]AT_SKEL_CC_IF([[%defines]], [[%define api.pure]])])[
174
175 ]$1[
176
177 %error-verbose
178
179 %%
180
181 ]$2[
182
183 ]AT_SKEL_JAVA_IF([[%code lexer {]], [[%%]])[
184
185 /*--------.
186 | yylex. |
187 `--------*/]AT_SKEL_JAVA_IF([[
188
189 public String input = "]$3[";
190 public int index = 0;
191 public int yylex ()
192 {
193 if (index < input.length ())
194 return input.charAt (index++);
195 else
196 return 0;
197 }
198 public Object getLVal ()
199 {
200 return new Integer(1);
201 }]], [[
202
203 ]AT_YYLEX_PROTOTYPE[
204 {
205 static char const *input = "]$3[";
206 *lvalp = 1;
207 return *input++;
208 }]])[
209
210 /*----------.
211 | yyerror. |
212 `----------*/]AT_SKEL_JAVA_IF([[
213
214 public void yyerror (String msg)
215 {
216 System.err.println (msg);
217 }
218
219 };
220
221 %%]], [AT_SKEL_CC_IF([[
222
223 void
224 yy::parser::error (const yy::location &, std::string const &msg)
225 {
226 std::cerr << msg << std::endl;
227 }]], [[
228
229 void
230 yyerror (char const *msg)
231 {
232 fprintf (stderr, "%s\n", msg);
233 }]])])[
234
235 /*-------.
236 | main. |
237 `-------*/]AT_SKEL_JAVA_IF([[
238
239 class input
240 {
241 public static void main (String args[]) throws IOException
242 {
243 YYParser p = new YYParser ();
244 p.parse ();
245 }
246 }]], [AT_SKEL_CC_IF([[
247
248 int
249 main (void)
250 {
251 yy::parser parser;
252 return parser.parse ();
253 }]], [[
254
255 int
256 main (void)
257 {
258 return yyparse ();
259 }]])])[
260 ]])
261
262 AT_FULL_COMPILE([[input]])
263
264 m4_pushdef([AT_EXPECTING], [m4_if($5, [ab], [[, expecting 'a' or 'b']],
265 $5, [a], [[, expecting 'a']],
266 $5, [b], [[, expecting 'b']])])
267
268 AT_SKEL_JAVA_IF([AT_JAVA_PARSER_CHECK([[input]], [[0]]],
269 [AT_PARSER_CHECK([[./input]], [[1]]]),
270 [[]],
271 [[syntax error, unexpected ]$4[]AT_EXPECTING[
272 ]])
273
274 m4_popdef([AT_EXPECTING])
275 m4_popdef([AT_YYLEX_PROTOTYPE])
276 AT_BISON_OPTION_POPDEFS
277
278 ])
279
280 m4_pushdef([AT_PREVIOUS_STATE_GRAMMAR],
281 [[%nonassoc 'a';
282
283 start: consistent-error-on-a-a 'a' ;
284
285 consistent-error-on-a-a:
286 'a' default-reduction
287 | 'a' default-reduction 'a'
288 | 'a' shift
289 ;
290
291 default-reduction: /*empty*/ ;
292 shift: 'b' ;
293
294 // Provide another context in which all rules are useful so that this
295 // test case looks a little more realistic.
296 start: 'b' consistent-error-on-a-a 'c' ;
297 ]])
298
299 m4_pushdef([AT_PREVIOUS_STATE_INPUT], [[a]])
300
301 # Unfortunately, no expected tokens are reported even though 'b' can be
302 # accepted. Nevertheless, the main point of this test is to make sure
303 # that at least the unexpected token is reported. In a previous version
304 # of Bison, it wasn't reported because the error is detected in a
305 # consistent state with an error action, and that case always triggered
306 # the simple "syntax error" message.
307 #
308 # The point isn't to test IELR here, but state merging happens to
309 # complicate this example.
310 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr]],
311 [AT_PREVIOUS_STATE_GRAMMAR],
312 [AT_PREVIOUS_STATE_INPUT],
313 [[$end]], [[none]])
314 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
315 %glr-parser]],
316 [AT_PREVIOUS_STATE_GRAMMAR],
317 [AT_PREVIOUS_STATE_INPUT],
318 [[$end]], [[none]])
319 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
320 %language "c++"]],
321 [AT_PREVIOUS_STATE_GRAMMAR],
322 [AT_PREVIOUS_STATE_INPUT],
323 [[$end]], [[none]])
324 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
325 %language "java"]],
326 [AT_PREVIOUS_STATE_GRAMMAR],
327 [AT_PREVIOUS_STATE_INPUT],
328 [[end of input]], [[none]])
329
330 # Even canonical LR doesn't foresee the error for 'a'!
331 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
332 %define lr.default-reductions consistent]],
333 [AT_PREVIOUS_STATE_GRAMMAR],
334 [AT_PREVIOUS_STATE_INPUT],
335 [[$end]], [[ab]])
336 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type ielr
337 %define lr.default-reductions accepting]],
338 [AT_PREVIOUS_STATE_GRAMMAR],
339 [AT_PREVIOUS_STATE_INPUT],
340 [[$end]], [[ab]])
341 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
342 [AT_PREVIOUS_STATE_GRAMMAR],
343 [AT_PREVIOUS_STATE_INPUT],
344 [[$end]], [[ab]])
345
346 m4_popdef([AT_PREVIOUS_STATE_GRAMMAR])
347 m4_popdef([AT_PREVIOUS_STATE_INPUT])
348
349 m4_pushdef([AT_USER_ACTION_GRAMMAR],
350 [[%nonassoc 'a';
351
352 // If $$ = 0 here, then we know that the 'a' destructor is being invoked
353 // incorrectly for the 'b' set in the semantic action below. All 'a'
354 // tokens are returned by yylex, which sets $$ = 1.
355 %destructor {
356 if (!$$)
357 fprintf (stderr, "Wrong destructor.\n");
358 } 'a';
359
360 // Rather than depend on an inconsistent state to induce reading a
361 // lookahead as in the previous grammar, just assign the lookahead in a
362 // semantic action. That lookahead isn't needed before either error
363 // action is encountered. In a previous version of Bison, this was a
364 // problem as it meant yychar was not translated into yytoken before
365 // either error action. The second error action thus invoked a
366 // destructor that it selected according to the incorrect yytoken. The
367 // first error action would have reported an incorrect unexpected token
368 // except that, due to the bug described in the previous grammar, the
369 // unexpected token was not reported at all.
370 start: error-reduce consistent-error 'a' { USE ($][3); } ;
371
372 error-reduce:
373 'a' 'a' consistent-reduction consistent-error 'a'
374 { USE (($][1, $][2, $][5)); }
375 | 'a' error
376 { USE ($][1); }
377 ;
378
379 consistent-reduction: /*empty*/ {
380 assert (yychar == ]AT_SKEL_CC_IF([[yyempty_]], [[YYEMPTY]])[);
381 yylval = 0;
382 yychar = 'b';
383 } ;
384
385 consistent-error:
386 'a' { USE ($][1); }
387 | /*empty*/ %prec 'a'
388 ;
389
390 // Provide another context in which all rules are useful so that this
391 // test case looks a little more realistic.
392 start: 'b' consistent-error 'b' ;
393 ]])
394 m4_pushdef([AT_USER_ACTION_INPUT], [[aa]])
395
396 AT_CONSISTENT_ERRORS_CHECK([[]],
397 [AT_USER_ACTION_GRAMMAR],
398 [AT_USER_ACTION_INPUT],
399 [['b']], [[none]])
400 AT_CONSISTENT_ERRORS_CHECK([[%glr-parser]],
401 [AT_USER_ACTION_GRAMMAR],
402 [AT_USER_ACTION_INPUT],
403 [['b']], [[none]])
404 AT_CONSISTENT_ERRORS_CHECK([[%language "c++"]],
405 [AT_USER_ACTION_GRAMMAR],
406 [AT_USER_ACTION_INPUT],
407 [['b']], [[none]])
408 # No Java test because yychar cannot be manipulated by users.
409
410 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions consistent]],
411 [AT_USER_ACTION_GRAMMAR],
412 [AT_USER_ACTION_INPUT],
413 [['b']], [[none]])
414
415 # Canonical LR doesn't foresee the error for 'a'!
416 AT_CONSISTENT_ERRORS_CHECK([[%define lr.default-reductions accepting]],
417 [AT_USER_ACTION_GRAMMAR],
418 [AT_USER_ACTION_INPUT],
419 [[$end]], [[a]])
420 AT_CONSISTENT_ERRORS_CHECK([[%define lr.type canonical-lr]],
421 [AT_USER_ACTION_GRAMMAR],
422 [AT_USER_ACTION_INPUT],
423 [[$end]], [[a]])
424
425 m4_popdef([AT_USER_ACTION_GRAMMAR])
426 m4_popdef([AT_USER_ACTION_INPUT])
427
428 m4_popdef([AT_CONSISTENT_ERRORS_CHECK])
429
430 AT_CLEANUP
431
432
433
434 ## ------------------------- ##
435 ## Unresolved SR Conflicts. ##
436 ## ------------------------- ##
437
438 AT_SETUP([Unresolved SR Conflicts])
439
440 AT_KEYWORDS([report])
441
442 AT_DATA([input.y],
443 [[%token NUM OP
444 %%
445 exp: exp OP exp | NUM;
446 ]])
447
448 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
449 [input.y: conflicts: 1 shift/reduce
450 ])
451
452 # Check the contents of the report.
453 AT_CHECK([cat input.output], [],
454 [[State 5 conflicts: 1 shift/reduce
455
456
457 Grammar
458
459 0 $accept: exp $end
460
461 1 exp: exp OP exp
462 2 | NUM
463
464
465 Terminals, with rules where they appear
466
467 $end (0) 0
468 error (256)
469 NUM (258) 2
470 OP (259) 1
471
472
473 Nonterminals, with rules where they appear
474
475 $accept (5)
476 on left: 0
477 exp (6)
478 on left: 1 2, on right: 0 1
479
480
481 state 0
482
483 0 $accept: . exp $end
484 1 exp: . exp OP exp
485 2 | . NUM
486
487 NUM shift, and go to state 1
488
489 exp go to state 2
490
491
492 state 1
493
494 2 exp: NUM .
495
496 $default reduce using rule 2 (exp)
497
498
499 state 2
500
501 0 $accept: exp . $end
502 1 exp: exp . OP exp
503
504 $end shift, and go to state 3
505 OP shift, and go to state 4
506
507
508 state 3
509
510 0 $accept: exp $end .
511
512 $default accept
513
514
515 state 4
516
517 1 exp: . exp OP exp
518 1 | exp OP . exp
519 2 | . NUM
520
521 NUM shift, and go to state 1
522
523 exp go to state 5
524
525
526 state 5
527
528 1 exp: exp . OP exp
529 1 | exp OP exp . [$end, OP]
530
531 OP shift, and go to state 4
532
533 OP [reduce using rule 1 (exp)]
534 $default reduce using rule 1 (exp)
535 ]])
536
537 AT_CLEANUP
538
539
540
541 ## ----------------------- ##
542 ## Resolved SR Conflicts. ##
543 ## ----------------------- ##
544
545 AT_SETUP([Resolved SR Conflicts])
546
547 AT_KEYWORDS([report])
548
549 AT_DATA([input.y],
550 [[%token NUM OP
551 %left OP
552 %%
553 exp: exp OP exp | NUM;
554 ]])
555
556 AT_BISON_CHECK([-o input.c --report=all input.y])
557
558 # Check the contents of the report.
559 AT_CHECK([cat input.output], [],
560 [[Grammar
561
562 0 $accept: exp $end
563
564 1 exp: exp OP exp
565 2 | NUM
566
567
568 Terminals, with rules where they appear
569
570 $end (0) 0
571 error (256)
572 NUM (258) 2
573 OP (259) 1
574
575
576 Nonterminals, with rules where they appear
577
578 $accept (5)
579 on left: 0
580 exp (6)
581 on left: 1 2, on right: 0 1
582
583
584 state 0
585
586 0 $accept: . exp $end
587 1 exp: . exp OP exp
588 2 | . NUM
589
590 NUM shift, and go to state 1
591
592 exp go to state 2
593
594
595 state 1
596
597 2 exp: NUM .
598
599 $default reduce using rule 2 (exp)
600
601
602 state 2
603
604 0 $accept: exp . $end
605 1 exp: exp . OP exp
606
607 $end shift, and go to state 3
608 OP shift, and go to state 4
609
610
611 state 3
612
613 0 $accept: exp $end .
614
615 $default accept
616
617
618 state 4
619
620 1 exp: . exp OP exp
621 1 | exp OP . exp
622 2 | . NUM
623
624 NUM shift, and go to state 1
625
626 exp go to state 5
627
628
629 state 5
630
631 1 exp: exp . OP exp
632 1 | exp OP exp . [$end, OP]
633
634 $default reduce using rule 1 (exp)
635
636 Conflict between rule 1 and token OP resolved as reduce (%left OP).
637 ]])
638
639 AT_CLEANUP
640
641
642 ## -------------------------------- ##
643 ## Defaulted Conflicted Reduction. ##
644 ## -------------------------------- ##
645
646 # When there are RR conflicts, some rules are disabled. Usually it is
647 # simply displayed as:
648 #
649 # $end reduce using rule 3 (num)
650 # $end [reduce using rule 4 (id)]
651 #
652 # But when `reduce 3' is the default action, we'd produce:
653 #
654 # $end [reduce using rule 4 (id)]
655 # $default reduce using rule 3 (num)
656 #
657 # In this precise case (a reduction is masked by the default
658 # reduction), we make the `reduce 3' explicit:
659 #
660 # $end reduce using rule 3 (num)
661 # $end [reduce using rule 4 (id)]
662 # $default reduce using rule 3 (num)
663 #
664 # Maybe that's not the best display, but then, please propose something
665 # else.
666
667 AT_SETUP([Defaulted Conflicted Reduction])
668 AT_KEYWORDS([report])
669
670 AT_DATA([input.y],
671 [[%%
672 exp: num | id;
673 num: '0';
674 id : '0';
675 %%
676 ]])
677
678 AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
679 [[input.y: conflicts: 1 reduce/reduce
680 input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
681 ]])
682
683 # Check the contents of the report.
684 AT_CHECK([cat input.output], [],
685 [[Rules useless in parser due to conflicts
686
687 4 id: '0'
688
689
690 State 1 conflicts: 1 reduce/reduce
691
692
693 Grammar
694
695 0 $accept: exp $end
696
697 1 exp: num
698 2 | id
699
700 3 num: '0'
701
702 4 id: '0'
703
704
705 Terminals, with rules where they appear
706
707 $end (0) 0
708 '0' (48) 3 4
709 error (256)
710
711
712 Nonterminals, with rules where they appear
713
714 $accept (4)
715 on left: 0
716 exp (5)
717 on left: 1 2, on right: 0
718 num (6)
719 on left: 3, on right: 1
720 id (7)
721 on left: 4, on right: 2
722
723
724 state 0
725
726 0 $accept: . exp $end
727 1 exp: . num
728 2 | . id
729 3 num: . '0'
730 4 id: . '0'
731
732 '0' shift, and go to state 1
733
734 exp go to state 2
735 num go to state 3
736 id go to state 4
737
738
739 state 1
740
741 3 num: '0' . [$end]
742 4 id: '0' . [$end]
743
744 $end reduce using rule 3 (num)
745 $end [reduce using rule 4 (id)]
746 $default reduce using rule 3 (num)
747
748
749 state 2
750
751 0 $accept: exp . $end
752
753 $end shift, and go to state 5
754
755
756 state 3
757
758 1 exp: num .
759
760 $default reduce using rule 1 (exp)
761
762
763 state 4
764
765 2 exp: id .
766
767 $default reduce using rule 2 (exp)
768
769
770 state 5
771
772 0 $accept: exp $end .
773
774 $default accept
775 ]])
776
777 AT_CLEANUP
778
779
780
781
782 ## -------------------- ##
783 ## %expect not enough. ##
784 ## -------------------- ##
785
786 AT_SETUP([%expect not enough])
787
788 AT_DATA([input.y],
789 [[%token NUM OP
790 %expect 0
791 %%
792 exp: exp OP exp | NUM;
793 ]])
794
795 AT_BISON_CHECK([-o input.c input.y], 1, [],
796 [input.y: conflicts: 1 shift/reduce
797 input.y: expected 0 shift/reduce conflicts
798 ])
799 AT_CLEANUP
800
801
802 ## --------------- ##
803 ## %expect right. ##
804 ## --------------- ##
805
806 AT_SETUP([%expect right])
807
808 AT_DATA([input.y],
809 [[%token NUM OP
810 %expect 1
811 %%
812 exp: exp OP exp | NUM;
813 ]])
814
815 AT_BISON_CHECK([-o input.c input.y])
816 AT_CLEANUP
817
818
819 ## ------------------ ##
820 ## %expect too much. ##
821 ## ------------------ ##
822
823 AT_SETUP([%expect too much])
824
825 AT_DATA([input.y],
826 [[%token NUM OP
827 %expect 2
828 %%
829 exp: exp OP exp | NUM;
830 ]])
831
832 AT_BISON_CHECK([-o input.c input.y], 1, [],
833 [input.y: conflicts: 1 shift/reduce
834 input.y: expected 2 shift/reduce conflicts
835 ])
836 AT_CLEANUP
837
838
839 ## ------------------------------- ##
840 ## %expect with reduce conflicts. ##
841 ## ------------------------------- ##
842
843 AT_SETUP([%expect with reduce conflicts])
844
845 AT_DATA([input.y],
846 [[%expect 0
847 %%
848 program: a 'a' | a a;
849 a: 'a';
850 ]])
851
852 AT_BISON_CHECK([-o input.c input.y], 1, [],
853 [input.y: conflicts: 1 reduce/reduce
854 input.y: expected 0 reduce/reduce conflicts
855 ])
856 AT_CLEANUP
857
858
859 ## ------------------------- ##
860 ## %prec with user strings. ##
861 ## ------------------------- ##
862
863 AT_SETUP([%prec with user string])
864
865 AT_DATA([[input.y]],
866 [[%%
867 exp:
868 "foo" %prec "foo"
869 ;
870 ]])
871
872 AT_BISON_CHECK([-o input.c input.y])
873 AT_CLEANUP
874
875
876 ## -------------------------------- ##
877 ## %no-default-prec without %prec. ##
878 ## -------------------------------- ##
879
880 AT_SETUP([%no-default-prec without %prec])
881
882 AT_DATA([[input.y]],
883 [[%left '+'
884 %left '*'
885
886 %%
887
888 %no-default-prec;
889
890 e: e '+' e
891 | e '*' e
892 | '0'
893 ;
894 ]])
895
896 AT_BISON_CHECK([-o input.c input.y], 0, [],
897 [[input.y: conflicts: 4 shift/reduce
898 ]])
899 AT_CLEANUP
900
901
902 ## ----------------------------- ##
903 ## %no-default-prec with %prec. ##
904 ## ----------------------------- ##
905
906 AT_SETUP([%no-default-prec with %prec])
907
908 AT_DATA([[input.y]],
909 [[%left '+'
910 %left '*'
911
912 %%
913
914 %no-default-prec;
915
916 e: e '+' e %prec '+'
917 | e '*' e %prec '*'
918 | '0'
919 ;
920 ]])
921
922 AT_BISON_CHECK([-o input.c input.y])
923 AT_CLEANUP
924
925
926 ## --------------- ##
927 ## %default-prec. ##
928 ## --------------- ##
929
930 AT_SETUP([%default-prec])
931
932 AT_DATA([[input.y]],
933 [[%left '+'
934 %left '*'
935
936 %%
937
938 %default-prec;
939
940 e: e '+' e
941 | e '*' e
942 | '0'
943 ;
944 ]])
945
946 AT_BISON_CHECK([-o input.c input.y])
947 AT_CLEANUP
948
949
950 ## ---------------------------------------------- ##
951 ## Unreachable States After Conflict Resolution. ##
952 ## ---------------------------------------------- ##
953
954 AT_SETUP([[Unreachable States After Conflict Resolution]])
955
956 # If conflict resolution makes states unreachable, remove those states, report
957 # rules that are then unused, and don't report conflicts in those states. Test
958 # what happens when a nonterminal becomes useless as a result of state removal
959 # since that causes lalr.o's goto map to be rewritten.
960
961 AT_DATA([[input.y]],
962 [[%output "input.c"
963 %left 'a'
964
965 %%
966
967 start: resolved_conflict 'a' reported_conflicts 'a' ;
968
969 /* S/R conflict resolved as reduce, so the state with item
970 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
971 * unreachable, and the associated production is useless. */
972 resolved_conflict:
973 'a' unreachable1
974 | %prec 'a'
975 ;
976
977 /* S/R conflict that need not be reported since it is unreachable because of
978 * the previous conflict resolution. Nonterminal unreachable1 and all its
979 * productions are useless. */
980 unreachable1:
981 'a' unreachable2
982 |
983 ;
984
985 /* Likewise for a R/R conflict and nonterminal unreachable2. */
986 unreachable2: | ;
987
988 /* Make sure remaining S/R and R/R conflicts are still reported correctly even
989 * when their states are renumbered due to state removal. */
990 reported_conflicts:
991 'a'
992 | 'a'
993 |
994 ;
995
996 ]])
997
998 AT_BISON_CHECK([[--report=all input.y]], 0, [],
999 [[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
1000 input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
1001 input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
1002 input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1003 input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1004 input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1005 input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1006 input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1007 ]])
1008
1009 AT_CHECK([[cat input.output]], 0,
1010 [[Rules useless in parser due to conflicts
1011
1012 2 resolved_conflict: 'a' unreachable1
1013
1014 4 unreachable1: 'a' unreachable2
1015 5 | /* empty */
1016
1017 6 unreachable2: /* empty */
1018 7 | /* empty */
1019
1020 9 reported_conflicts: 'a'
1021 10 | /* empty */
1022
1023
1024 State 4 conflicts: 1 shift/reduce
1025 State 5 conflicts: 1 reduce/reduce
1026
1027
1028 Grammar
1029
1030 0 $accept: start $end
1031
1032 1 start: resolved_conflict 'a' reported_conflicts 'a'
1033
1034 2 resolved_conflict: 'a' unreachable1
1035 3 | /* empty */
1036
1037 4 unreachable1: 'a' unreachable2
1038 5 | /* empty */
1039
1040 6 unreachable2: /* empty */
1041 7 | /* empty */
1042
1043 8 reported_conflicts: 'a'
1044 9 | 'a'
1045 10 | /* empty */
1046
1047
1048 Terminals, with rules where they appear
1049
1050 $end (0) 0
1051 'a' (97) 1 2 4 8 9
1052 error (256)
1053
1054
1055 Nonterminals, with rules where they appear
1056
1057 $accept (4)
1058 on left: 0
1059 start (5)
1060 on left: 1, on right: 0
1061 resolved_conflict (6)
1062 on left: 2 3, on right: 1
1063 unreachable1 (7)
1064 on left: 4 5, on right: 2
1065 unreachable2 (8)
1066 on left: 6 7, on right: 4
1067 reported_conflicts (9)
1068 on left: 8 9 10, on right: 1
1069
1070
1071 state 0
1072
1073 0 $accept: . start $end
1074 1 start: . resolved_conflict 'a' reported_conflicts 'a'
1075 2 resolved_conflict: . 'a' unreachable1
1076 3 | . ['a']
1077
1078 $default reduce using rule 3 (resolved_conflict)
1079
1080 start go to state 1
1081 resolved_conflict go to state 2
1082
1083 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
1084
1085
1086 state 1
1087
1088 0 $accept: start . $end
1089
1090 $end shift, and go to state 3
1091
1092
1093 state 2
1094
1095 1 start: resolved_conflict . 'a' reported_conflicts 'a'
1096
1097 'a' shift, and go to state 4
1098
1099
1100 state 3
1101
1102 0 $accept: start $end .
1103
1104 $default accept
1105
1106
1107 state 4
1108
1109 1 start: resolved_conflict 'a' . reported_conflicts 'a'
1110 8 reported_conflicts: . 'a'
1111 9 | . 'a'
1112 10 | . ['a']
1113
1114 'a' shift, and go to state 5
1115
1116 'a' [reduce using rule 10 (reported_conflicts)]
1117
1118 reported_conflicts go to state 6
1119
1120
1121 state 5
1122
1123 8 reported_conflicts: 'a' . ['a']
1124 9 | 'a' . ['a']
1125
1126 'a' reduce using rule 8 (reported_conflicts)
1127 'a' [reduce using rule 9 (reported_conflicts)]
1128 $default reduce using rule 8 (reported_conflicts)
1129
1130
1131 state 6
1132
1133 1 start: resolved_conflict 'a' reported_conflicts . 'a'
1134
1135 'a' shift, and go to state 7
1136
1137
1138 state 7
1139
1140 1 start: resolved_conflict 'a' reported_conflicts 'a' .
1141
1142 $default reduce using rule 1 (start)
1143 ]])
1144
1145 AT_DATA([[input-keep.y]],
1146 [[%define lr.keep-unreachable-states
1147 ]])
1148 AT_CHECK([[cat input.y >> input-keep.y]])
1149
1150 AT_BISON_CHECK([[input-keep.y]], 0, [],
1151 [[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
1152 input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
1153 input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
1154 input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
1155 input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
1156 ]])
1157
1158 AT_CLEANUP
1159
1160
1161 ## ------------------------------------------------------------ ##
1162 ## Solved conflicts report for multiple reductions in a state. ##
1163 ## ------------------------------------------------------------ ##
1164
1165 AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
1166
1167 # Used to lose earlier solved conflict messages even within a single S/R/R.
1168
1169 AT_DATA([[input.y]],
1170 [[%left 'a'
1171 %right 'b'
1172 %right 'c'
1173 %right 'd'
1174 %%
1175 start:
1176 'a'
1177 | empty_a 'a'
1178 | 'b'
1179 | empty_b 'b'
1180 | 'c'
1181 | empty_c1 'c'
1182 | empty_c2 'c'
1183 | empty_c3 'c'
1184 ;
1185 empty_a: %prec 'a' ;
1186 empty_b: %prec 'b' ;
1187 empty_c1: %prec 'c' ;
1188 empty_c2: %prec 'c' ;
1189 empty_c3: %prec 'd' ;
1190 ]])
1191 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1192 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1193 [[state 0
1194
1195 0 $accept: . start $end
1196 1 start: . 'a'
1197 2 | . empty_a 'a'
1198 3 | . 'b'
1199 4 | . empty_b 'b'
1200 5 | . 'c'
1201 6 | . empty_c1 'c'
1202 7 | . empty_c2 'c'
1203 8 | . empty_c3 'c'
1204 9 empty_a: . ['a']
1205 10 empty_b: . []
1206 11 empty_c1: . []
1207 12 empty_c2: . []
1208 13 empty_c3: . ['c']
1209
1210 'b' shift, and go to state 1
1211
1212 'c' reduce using rule 13 (empty_c3)
1213 $default reduce using rule 9 (empty_a)
1214
1215 start go to state 2
1216 empty_a go to state 3
1217 empty_b go to state 4
1218 empty_c1 go to state 5
1219 empty_c2 go to state 6
1220 empty_c3 go to state 7
1221
1222 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
1223 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
1224 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
1225 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
1226 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
1227
1228
1229 state 1
1230 ]])
1231
1232 AT_CLEANUP
1233
1234
1235 ## ------------------------------------------------------------ ##
1236 ## %nonassoc error actions for multiple reductions in a state. ##
1237 ## ------------------------------------------------------------ ##
1238
1239 # Used to abort when trying to resolve conflicts as %nonassoc error actions for
1240 # multiple reductions in a state.
1241
1242 # For a %nonassoc error action token, used to print the first remaining
1243 # reduction on that token without brackets.
1244
1245 AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
1246
1247 AT_DATA([[input.y]],
1248 [[%nonassoc 'a' 'b' 'c'
1249 %%
1250 start:
1251 'a'
1252 | empty_a 'a'
1253 | 'b'
1254 | empty_b 'b'
1255 | 'c'
1256 | empty_c1 'c'
1257 | empty_c2 'c'
1258 | empty_c3 'c'
1259 ;
1260 empty_a: %prec 'a' ;
1261 empty_b: %prec 'b' ;
1262 empty_c1: %prec 'c' ;
1263 empty_c2: %prec 'c' ;
1264 empty_c3: %prec 'c' ;
1265 ]])
1266
1267 AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
1268 AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
1269 [[state 0
1270
1271 0 $accept: . start $end
1272 1 start: . 'a'
1273 2 | . empty_a 'a'
1274 3 | . 'b'
1275 4 | . empty_b 'b'
1276 5 | . 'c'
1277 6 | . empty_c1 'c'
1278 7 | . empty_c2 'c'
1279 8 | . empty_c3 'c'
1280 9 empty_a: . []
1281 10 empty_b: . []
1282 11 empty_c1: . []
1283 12 empty_c2: . ['c']
1284 13 empty_c3: . ['c']
1285
1286 'a' error (nonassociative)
1287 'b' error (nonassociative)
1288 'c' error (nonassociative)
1289
1290 'c' [reduce using rule 12 (empty_c2)]
1291 'c' [reduce using rule 13 (empty_c3)]
1292
1293 start go to state 1
1294 empty_a go to state 2
1295 empty_b go to state 3
1296 empty_c1 go to state 4
1297 empty_c2 go to state 5
1298 empty_c3 go to state 6
1299
1300 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
1301 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
1302 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
1303
1304
1305 state 1
1306 ]])
1307 AT_CLEANUP