]> git.saurik.com Git - bison.git/blame_incremental - tests/conflicts.at
Say %language is experimental.
[bison.git] / tests / conflicts.at
... / ...
CommitLineData
1# Exercising Bison on conflicts. -*- Autotest -*-
2
3# Copyright (C) 2002, 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4
5# This program is free software: you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18AT_BANNER([[Conflicts.]])
19
20
21## ---------------- ##
22## S/R in initial. ##
23## ---------------- ##
24
25# I once hacked Bison in such a way that it lost its reductions on the
26# initial state (because it was confusing it with the last state). It
27# took me a while to strip down my failures to this simple case. So
28# make sure it finds the s/r conflict below.
29
30AT_SETUP([S/R in initial])
31
32AT_DATA([[input.y]],
33[[%expect 1
34%%
35exp: e 'e';
36e: 'e' | /* Nothing. */;
37]])
38
39AT_BISON_CHECK([-o input.c input.y], 0, [],
40[[input.y:4.9: warning: rule useless in parser due to conflicts: e: /* empty */
41]])
42
43AT_CLEANUP
44
45
46## ------------------- ##
47## %nonassoc and eof. ##
48## ------------------- ##
49
50AT_SETUP([%nonassoc and eof])
51
52AT_DATA_GRAMMAR([input.y],
53[[
54%{
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58
59#define YYERROR_VERBOSE 1
60static void
61yyerror (const char *msg)
62{
63 fprintf (stderr, "%s\n", msg);
64}
65
66/* The current argument. */
67static const char *input;
68
69static int
70yylex (void)
71{
72 static size_t toknum;
73 if (! (toknum <= strlen (input)))
74 abort ();
75 return input[toknum++];
76}
77
78%}
79
80%nonassoc '<' '>'
81
82%%
83expr: expr '<' expr
84 | expr '>' expr
85 | '0'
86 ;
87%%
88int
89main (int argc, const char *argv[])
90{
91 input = argc <= 1 ? "" : argv[1];
92 return yyparse ();
93}
94]])
95
96# Specify the output files to avoid problems on different file systems.
97AT_BISON_CHECK([-o input.c input.y])
98AT_COMPILE([input])
99
100AT_PARSER_CHECK([./input '0<0'])
101# FIXME: This is an actual bug, but a new one, in the sense that
102# no one has ever spotted it! The messages are *wrong*: there should
103# be nothing there, it should be expected eof.
104AT_PARSER_CHECK([./input '0<0<0'], [1], [],
105 [syntax error, unexpected '<', expecting '<' or '>'
106])
107
108AT_PARSER_CHECK([./input '0>0'])
109AT_PARSER_CHECK([./input '0>0>0'], [1], [],
110 [syntax error, unexpected '>', expecting '<' or '>'
111])
112
113AT_PARSER_CHECK([./input '0<0>0'], [1], [],
114 [syntax error, unexpected '>', expecting '<' or '>'
115])
116
117AT_CLEANUP
118
119
120
121## ------------------------- ##
122## Unresolved SR Conflicts. ##
123## ------------------------- ##
124
125AT_SETUP([Unresolved SR Conflicts])
126
127AT_KEYWORDS([report])
128
129AT_DATA([input.y],
130[[%token NUM OP
131%%
132exp: exp OP exp | NUM;
133]])
134
135AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
136[input.y: conflicts: 1 shift/reduce
137])
138
139# Check the contents of the report.
140AT_CHECK([cat input.output], [],
141[[State 5 conflicts: 1 shift/reduce
142
143
144Grammar
145
146 0 $accept: exp $end
147
148 1 exp: exp OP exp
149 2 | NUM
150
151
152Terminals, with rules where they appear
153
154$end (0) 0
155error (256)
156NUM (258) 2
157OP (259) 1
158
159
160Nonterminals, with rules where they appear
161
162$accept (5)
163 on left: 0
164exp (6)
165 on left: 1 2, on right: 0 1
166
167
168state 0
169
170 0 $accept: . exp $end
171 1 exp: . exp OP exp
172 2 | . NUM
173
174 NUM shift, and go to state 1
175
176 exp go to state 2
177
178
179state 1
180
181 2 exp: NUM .
182
183 $default reduce using rule 2 (exp)
184
185
186state 2
187
188 0 $accept: exp . $end
189 1 exp: exp . OP exp
190
191 $end shift, and go to state 3
192 OP shift, and go to state 4
193
194
195state 3
196
197 0 $accept: exp $end .
198
199 $default accept
200
201
202state 4
203
204 1 exp: . exp OP exp
205 1 | exp OP . exp
206 2 | . NUM
207
208 NUM shift, and go to state 1
209
210 exp go to state 5
211
212
213state 5
214
215 1 exp: exp . OP exp
216 1 | exp OP exp . [$end, OP]
217
218 OP shift, and go to state 4
219
220 OP [reduce using rule 1 (exp)]
221 $default reduce using rule 1 (exp)
222]])
223
224AT_CLEANUP
225
226
227
228## ----------------------- ##
229## Resolved SR Conflicts. ##
230## ----------------------- ##
231
232AT_SETUP([Resolved SR Conflicts])
233
234AT_KEYWORDS([report])
235
236AT_DATA([input.y],
237[[%token NUM OP
238%left OP
239%%
240exp: exp OP exp | NUM;
241]])
242
243AT_BISON_CHECK([-o input.c --report=all input.y])
244
245# Check the contents of the report.
246AT_CHECK([cat input.output], [],
247[[Grammar
248
249 0 $accept: exp $end
250
251 1 exp: exp OP exp
252 2 | NUM
253
254
255Terminals, with rules where they appear
256
257$end (0) 0
258error (256)
259NUM (258) 2
260OP (259) 1
261
262
263Nonterminals, with rules where they appear
264
265$accept (5)
266 on left: 0
267exp (6)
268 on left: 1 2, on right: 0 1
269
270
271state 0
272
273 0 $accept: . exp $end
274 1 exp: . exp OP exp
275 2 | . NUM
276
277 NUM shift, and go to state 1
278
279 exp go to state 2
280
281
282state 1
283
284 2 exp: NUM .
285
286 $default reduce using rule 2 (exp)
287
288
289state 2
290
291 0 $accept: exp . $end
292 1 exp: exp . OP exp
293
294 $end shift, and go to state 3
295 OP shift, and go to state 4
296
297
298state 3
299
300 0 $accept: exp $end .
301
302 $default accept
303
304
305state 4
306
307 1 exp: . exp OP exp
308 1 | exp OP . exp
309 2 | . NUM
310
311 NUM shift, and go to state 1
312
313 exp go to state 5
314
315
316state 5
317
318 1 exp: exp . OP exp
319 1 | exp OP exp . [$end, OP]
320
321 $default reduce using rule 1 (exp)
322
323 Conflict between rule 1 and token OP resolved as reduce (%left OP).
324]])
325
326AT_CLEANUP
327
328
329## -------------------------------- ##
330## Defaulted Conflicted Reduction. ##
331## -------------------------------- ##
332
333# When there are RR conflicts, some rules are disabled. Usually it is
334# simply displayed as:
335#
336# $end reduce using rule 3 (num)
337# $end [reduce using rule 4 (id)]
338#
339# But when `reduce 3' is the default action, we'd produce:
340#
341# $end [reduce using rule 4 (id)]
342# $default reduce using rule 3 (num)
343#
344# In this precise case (a reduction is masked by the default
345# reduction), we make the `reduce 3' explicit:
346#
347# $end reduce using rule 3 (num)
348# $end [reduce using rule 4 (id)]
349# $default reduce using rule 3 (num)
350#
351# Maybe that's not the best display, but then, please propose something
352# else.
353
354AT_SETUP([Defaulted Conflicted Reduction])
355AT_KEYWORDS([report])
356
357AT_DATA([input.y],
358[[%%
359exp: num | id;
360num: '0';
361id : '0';
362%%
363]])
364
365AT_BISON_CHECK([-o input.c --report=all input.y], 0, [],
366[[input.y: conflicts: 1 reduce/reduce
367input.y:4.6-8: warning: rule useless in parser due to conflicts: id: '0'
368]])
369
370# Check the contents of the report.
371AT_CHECK([cat input.output], [],
372[[Rules useless in parser due to conflicts
373
374 4 id: '0'
375
376
377State 1 conflicts: 1 reduce/reduce
378
379
380Grammar
381
382 0 $accept: exp $end
383
384 1 exp: num
385 2 | id
386
387 3 num: '0'
388
389 4 id: '0'
390
391
392Terminals, with rules where they appear
393
394$end (0) 0
395'0' (48) 3 4
396error (256)
397
398
399Nonterminals, with rules where they appear
400
401$accept (4)
402 on left: 0
403exp (5)
404 on left: 1 2, on right: 0
405num (6)
406 on left: 3, on right: 1
407id (7)
408 on left: 4, on right: 2
409
410
411state 0
412
413 0 $accept: . exp $end
414 1 exp: . num
415 2 | . id
416 3 num: . '0'
417 4 id: . '0'
418
419 '0' shift, and go to state 1
420
421 exp go to state 2
422 num go to state 3
423 id go to state 4
424
425
426state 1
427
428 3 num: '0' . [$end]
429 4 id: '0' . [$end]
430
431 $end reduce using rule 3 (num)
432 $end [reduce using rule 4 (id)]
433 $default reduce using rule 3 (num)
434
435
436state 2
437
438 0 $accept: exp . $end
439
440 $end shift, and go to state 5
441
442
443state 3
444
445 1 exp: num .
446
447 $default reduce using rule 1 (exp)
448
449
450state 4
451
452 2 exp: id .
453
454 $default reduce using rule 2 (exp)
455
456
457state 5
458
459 0 $accept: exp $end .
460
461 $default accept
462]])
463
464AT_CLEANUP
465
466
467
468
469## -------------------- ##
470## %expect not enough. ##
471## -------------------- ##
472
473AT_SETUP([%expect not enough])
474
475AT_DATA([input.y],
476[[%token NUM OP
477%expect 0
478%%
479exp: exp OP exp | NUM;
480]])
481
482AT_BISON_CHECK([-o input.c input.y], 1, [],
483[input.y: conflicts: 1 shift/reduce
484input.y: expected 0 shift/reduce conflicts
485])
486AT_CLEANUP
487
488
489## --------------- ##
490## %expect right. ##
491## --------------- ##
492
493AT_SETUP([%expect right])
494
495AT_DATA([input.y],
496[[%token NUM OP
497%expect 1
498%%
499exp: exp OP exp | NUM;
500]])
501
502AT_BISON_CHECK([-o input.c input.y])
503AT_CLEANUP
504
505
506## ------------------ ##
507## %expect too much. ##
508## ------------------ ##
509
510AT_SETUP([%expect too much])
511
512AT_DATA([input.y],
513[[%token NUM OP
514%expect 2
515%%
516exp: exp OP exp | NUM;
517]])
518
519AT_BISON_CHECK([-o input.c input.y], 1, [],
520[input.y: conflicts: 1 shift/reduce
521input.y: expected 2 shift/reduce conflicts
522])
523AT_CLEANUP
524
525
526## ------------------------------ ##
527## %expect with reduce conflicts ##
528## ------------------------------ ##
529
530AT_SETUP([%expect with reduce conflicts])
531
532AT_DATA([input.y],
533[[%expect 0
534%%
535program: a 'a' | a a;
536a: 'a';
537]])
538
539AT_BISON_CHECK([-o input.c input.y], 1, [],
540[input.y: conflicts: 1 reduce/reduce
541input.y: expected 0 reduce/reduce conflicts
542])
543AT_CLEANUP
544
545
546## ------------------------------- ##
547## %no-default-prec without %prec ##
548## ------------------------------- ##
549
550AT_SETUP([%no-default-prec without %prec])
551
552AT_DATA([[input.y]],
553[[%left '+'
554%left '*'
555
556%%
557
558%no-default-prec;
559
560e: e '+' e
561 | e '*' e
562 | '0'
563 ;
564]])
565
566AT_BISON_CHECK([-o input.c input.y], 0, [],
567[[input.y: conflicts: 4 shift/reduce
568]])
569AT_CLEANUP
570
571
572## ---------------------------- ##
573## %no-default-prec with %prec ##
574## ---------------------------- ##
575
576AT_SETUP([%no-default-prec with %prec])
577
578AT_DATA([[input.y]],
579[[%left '+'
580%left '*'
581
582%%
583
584%no-default-prec;
585
586e: e '+' e %prec '+'
587 | e '*' e %prec '*'
588 | '0'
589 ;
590]])
591
592AT_BISON_CHECK([-o input.c input.y])
593AT_CLEANUP
594
595
596## ---------------- ##
597## %default-prec ##
598## ---------------- ##
599
600AT_SETUP([%default-prec])
601
602AT_DATA([[input.y]],
603[[%left '+'
604%left '*'
605
606%%
607
608%default-prec;
609
610e: e '+' e
611 | e '*' e
612 | '0'
613 ;
614]])
615
616AT_BISON_CHECK([-o input.c input.y])
617AT_CLEANUP
618
619
620## ---------------------------------------------- ##
621## Unreachable States After Conflict Resolution. ##
622## ---------------------------------------------- ##
623
624AT_SETUP([[Unreachable States After Conflict Resolution]])
625
626# If conflict resolution makes states unreachable, remove those states, report
627# rules that are then unused, and don't report conflicts in those states. Test
628# what happens when a nonterminal becomes useless as a result of state removal
629# since that causes lalr.o's goto map to be rewritten.
630
631AT_DATA([[input.y]],
632[[%output "input.c"
633%left 'a'
634
635%%
636
637start: resolved_conflict 'a' reported_conflicts 'a' ;
638
639/* S/R conflict resolved as reduce, so the state with item
640 * (resolved_conflict: 'a' . unreachable1) and all it transition successors are
641 * unreachable, and the associated production is useless. */
642resolved_conflict:
643 'a' unreachable1
644 | %prec 'a'
645 ;
646
647/* S/R conflict that need not be reported since it is unreachable because of
648 * the previous conflict resolution. Nonterminal unreachable1 and all its
649 * productions are useless. */
650unreachable1:
651 'a' unreachable2
652 |
653 ;
654
655/* Likewise for a R/R conflict and nonterminal unreachable2. */
656unreachable2: | ;
657
658/* Make sure remaining S/R and R/R conflicts are still reported correctly even
659 * when their states are renumbered due to state removal. */
660reported_conflicts:
661 'a'
662 | 'a'
663 |
664 ;
665
666]])
667
668AT_BISON_CHECK([[--report=all input.y]], 0, [],
669[[input.y: conflicts: 1 shift/reduce, 1 reduce/reduce
670input.y:12.5-20: warning: rule useless in parser due to conflicts: resolved_conflict: 'a' unreachable1
671input.y:20.5-20: warning: rule useless in parser due to conflicts: unreachable1: 'a' unreachable2
672input.y:21.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
673input.y:25.13: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
674input.y:25.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
675input.y:31.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
676input.y:32.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
677]])
678
679AT_CHECK([[cat input.output]], 0,
680[[Rules useless in parser due to conflicts
681
682 2 resolved_conflict: 'a' unreachable1
683
684 4 unreachable1: 'a' unreachable2
685 5 | /* empty */
686
687 6 unreachable2: /* empty */
688 7 | /* empty */
689
690 9 reported_conflicts: 'a'
691 10 | /* empty */
692
693
694State 4 conflicts: 1 shift/reduce
695State 5 conflicts: 1 reduce/reduce
696
697
698Grammar
699
700 0 $accept: start $end
701
702 1 start: resolved_conflict 'a' reported_conflicts 'a'
703
704 2 resolved_conflict: 'a' unreachable1
705 3 | /* empty */
706
707 4 unreachable1: 'a' unreachable2
708 5 | /* empty */
709
710 6 unreachable2: /* empty */
711 7 | /* empty */
712
713 8 reported_conflicts: 'a'
714 9 | 'a'
715 10 | /* empty */
716
717
718Terminals, with rules where they appear
719
720$end (0) 0
721'a' (97) 1 2 4 8 9
722error (256)
723
724
725Nonterminals, with rules where they appear
726
727$accept (4)
728 on left: 0
729start (5)
730 on left: 1, on right: 0
731resolved_conflict (6)
732 on left: 2 3, on right: 1
733unreachable1 (7)
734 on left: 4 5, on right: 2
735unreachable2 (8)
736 on left: 6 7, on right: 4
737reported_conflicts (9)
738 on left: 8 9 10, on right: 1
739
740
741state 0
742
743 0 $accept: . start $end
744 1 start: . resolved_conflict 'a' reported_conflicts 'a'
745 2 resolved_conflict: . 'a' unreachable1
746 3 | . ['a']
747
748 $default reduce using rule 3 (resolved_conflict)
749
750 start go to state 1
751 resolved_conflict go to state 2
752
753 Conflict between rule 3 and token 'a' resolved as reduce (%left 'a').
754
755
756state 1
757
758 0 $accept: start . $end
759
760 $end shift, and go to state 3
761
762
763state 2
764
765 1 start: resolved_conflict . 'a' reported_conflicts 'a'
766
767 'a' shift, and go to state 4
768
769
770state 3
771
772 0 $accept: start $end .
773
774 $default accept
775
776
777state 4
778
779 1 start: resolved_conflict 'a' . reported_conflicts 'a'
780 8 reported_conflicts: . 'a'
781 9 | . 'a'
782 10 | . ['a']
783
784 'a' shift, and go to state 5
785
786 'a' [reduce using rule 10 (reported_conflicts)]
787
788 reported_conflicts go to state 6
789
790
791state 5
792
793 8 reported_conflicts: 'a' . ['a']
794 9 | 'a' . ['a']
795
796 'a' reduce using rule 8 (reported_conflicts)
797 'a' [reduce using rule 9 (reported_conflicts)]
798 $default reduce using rule 8 (reported_conflicts)
799
800
801state 6
802
803 1 start: resolved_conflict 'a' reported_conflicts . 'a'
804
805 'a' shift, and go to state 7
806
807
808state 7
809
810 1 start: resolved_conflict 'a' reported_conflicts 'a' .
811
812 $default reduce using rule 1 (start)
813]])
814
815AT_DATA([[input-keep.y]],
816[[%define lr.keep_unreachable_states
817]])
818AT_CHECK([[cat input.y >> input-keep.y]])
819
820AT_BISON_CHECK([[input-keep.y]], 0, [],
821[[input-keep.y: conflicts: 2 shift/reduce, 2 reduce/reduce
822input-keep.y:22.4: warning: rule useless in parser due to conflicts: unreachable1: /* empty */
823input-keep.y:26.16: warning: rule useless in parser due to conflicts: unreachable2: /* empty */
824input-keep.y:32.5-7: warning: rule useless in parser due to conflicts: reported_conflicts: 'a'
825input-keep.y:33.4: warning: rule useless in parser due to conflicts: reported_conflicts: /* empty */
826]])
827
828AT_CLEANUP
829
830
831## ------------------------------------------------------------ ##
832## Solved conflicts report for multiple reductions in a state. ##
833## ------------------------------------------------------------ ##
834
835AT_SETUP([[Solved conflicts report for multiple reductions in a state]])
836
837# Used to lose earlier solved conflict messages even within a single S/R/R.
838
839AT_DATA([[input.y]],
840[[%left 'a'
841%right 'b'
842%right 'c'
843%right 'd'
844%%
845start:
846 'a'
847 | empty_a 'a'
848 | 'b'
849 | empty_b 'b'
850 | 'c'
851 | empty_c1 'c'
852 | empty_c2 'c'
853 | empty_c3 'c'
854 ;
855empty_a: %prec 'a' ;
856empty_b: %prec 'b' ;
857empty_c1: %prec 'c' ;
858empty_c2: %prec 'c' ;
859empty_c3: %prec 'd' ;
860]])
861AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
862AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
863[[state 0
864
865 0 $accept: . start $end
866 1 start: . 'a'
867 2 | . empty_a 'a'
868 3 | . 'b'
869 4 | . empty_b 'b'
870 5 | . 'c'
871 6 | . empty_c1 'c'
872 7 | . empty_c2 'c'
873 8 | . empty_c3 'c'
874 9 empty_a: . ['a']
875 10 empty_b: . []
876 11 empty_c1: . []
877 12 empty_c2: . []
878 13 empty_c3: . ['c']
879
880 'b' shift, and go to state 1
881
882 'c' reduce using rule 13 (empty_c3)
883 $default reduce using rule 9 (empty_a)
884
885 start go to state 2
886 empty_a go to state 3
887 empty_b go to state 4
888 empty_c1 go to state 5
889 empty_c2 go to state 6
890 empty_c3 go to state 7
891
892 Conflict between rule 9 and token 'a' resolved as reduce (%left 'a').
893 Conflict between rule 10 and token 'b' resolved as shift (%right 'b').
894 Conflict between rule 11 and token 'c' resolved as shift (%right 'c').
895 Conflict between rule 12 and token 'c' resolved as shift (%right 'c').
896 Conflict between rule 13 and token 'c' resolved as reduce ('c' < 'd').
897
898
899state 1
900]])
901
902AT_CLEANUP
903
904
905## ------------------------------------------------------------ ##
906## %nonassoc error actions for multiple reductions in a state. ##
907## ------------------------------------------------------------ ##
908
909# Used to abort when trying to resolve conflicts as %nonassoc error actions for
910# multiple reductions in a state.
911
912# For a %nonassoc error action token, used to print the first remaining
913# reduction on that token without brackets.
914
915AT_SETUP([[%nonassoc error actions for multiple reductions in a state]])
916
917AT_DATA([[input.y]],
918[[%nonassoc 'a' 'b' 'c'
919%%
920start:
921 'a'
922 | empty_a 'a'
923 | 'b'
924 | empty_b 'b'
925 | 'c'
926 | empty_c1 'c'
927 | empty_c2 'c'
928 | empty_c3 'c'
929 ;
930empty_a: %prec 'a' ;
931empty_b: %prec 'b' ;
932empty_c1: %prec 'c' ;
933empty_c2: %prec 'c' ;
934empty_c3: %prec 'c' ;
935]])
936
937AT_BISON_CHECK([[--report=all -o input.c input.y]], 0, [], [ignore])
938AT_CHECK([[cat input.output | sed -n '/^state 0$/,/^state 1$/p']], 0,
939[[state 0
940
941 0 $accept: . start $end
942 1 start: . 'a'
943 2 | . empty_a 'a'
944 3 | . 'b'
945 4 | . empty_b 'b'
946 5 | . 'c'
947 6 | . empty_c1 'c'
948 7 | . empty_c2 'c'
949 8 | . empty_c3 'c'
950 9 empty_a: . []
951 10 empty_b: . []
952 11 empty_c1: . []
953 12 empty_c2: . ['c']
954 13 empty_c3: . ['c']
955
956 'a' error (nonassociative)
957 'b' error (nonassociative)
958 'c' error (nonassociative)
959
960 'c' [reduce using rule 12 (empty_c2)]
961 'c' [reduce using rule 13 (empty_c3)]
962
963 start go to state 1
964 empty_a go to state 2
965 empty_b go to state 3
966 empty_c1 go to state 4
967 empty_c2 go to state 5
968 empty_c3 go to state 6
969
970 Conflict between rule 9 and token 'a' resolved as an error (%nonassoc 'a').
971 Conflict between rule 10 and token 'b' resolved as an error (%nonassoc 'b').
972 Conflict between rule 11 and token 'c' resolved as an error (%nonassoc 'c').
973
974
975state 1
976]])
977AT_CLEANUP