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