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