]> git.saurik.com Git - bison.git/blob - tests/regression.at
1.34.
[bison.git] / tests / regression.at
1 # Bison Regressions. -*- Autotest -*-
2 # Copyright 2001 Free Software Foundation, Inc.
3
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2, or (at your option)
7 # any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 # 02111-1307, USA.
18
19 AT_BANNER([[Regression tests.]])
20
21
22 ## ------------------- ##
23 ## %nonassoc and eof. ##
24 ## ------------------- ##
25
26 AT_SETUP([%nonassoc and eof])
27
28 AT_DATA([input.y],
29 [[
30 %{
31 #include <stdio.h>
32
33 #define YYERROR_VERBOSE 1
34 #define yyerror(Msg) \
35 do { \
36 fprintf (stderr, "%s\n", Msg); \
37 exit (1); \
38 } while (0)
39
40 /* The current argument. */
41 static const char *input = NULL;
42
43 static int
44 yylex (void)
45 {
46 /* No token stands for end of file. */
47 if (input && *input)
48 return *input++;
49 else
50 return 0;
51 }
52
53 %}
54
55 %nonassoc '<' '>'
56
57 %%
58 expr: expr '<' expr
59 | expr '>' expr
60 | '0'
61 ;
62 %%
63 int
64 main (int argc, const char *argv[])
65 {
66 if (argc > 1)
67 input = argv[1];
68 return yyparse ();
69 }
70 ]])
71
72 # Specify the output files to avoid problems on different file systems.
73 AT_CHECK([bison input.y -o input.c])
74 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
75
76 AT_CHECK([./input '0<0'])
77 # FIXME: This is an actual bug, but a new one, in the sense that
78 # no one has ever spotted it! The messages are *wrong*: there should
79 # be nothing there, it should be expected eof.
80 AT_CHECK([./input '0<0<0'], [1], [],
81 [parse error, unexpected '<', expecting '<' or '>'
82 ])
83
84 AT_CHECK([./input '0>0'])
85 AT_CHECK([./input '0>0>0'], [1], [],
86 [parse error, unexpected '>', expecting '<' or '>'
87 ])
88
89 AT_CHECK([./input '0<0>0'], [1], [],
90 [parse error, unexpected '>', expecting '<' or '>'
91 ])
92
93 AT_CLEANUP
94
95 ## ---------------- ##
96 ## Braces parsing. ##
97 ## ---------------- ##
98
99
100 AT_SETUP([braces parsing])
101
102 AT_DATA([input.y],
103 [[/* Bison used to swallow the character after `}'. */
104
105 %%
106 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
107 %%
108 ]])
109
110 AT_CHECK([bison -v input.y -o input.c], 0, ignore, ignore)
111
112 AT_CHECK([fgrep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
113
114 AT_CLEANUP
115
116
117 ## ------------------ ##
118 ## Duplicate string. ##
119 ## ------------------ ##
120
121
122 AT_SETUP([Duplicate string])
123
124 AT_DATA([duplicate.y],
125 [[/* `Bison -v' used to dump core when two tokens are defined with the same
126 string, as LE and GE below. */
127
128 %token NUM
129 %token LE "<="
130 %token GE "<="
131
132 %%
133 exp: '(' exp ')' | NUM ;
134 %%
135 ]])
136
137 AT_CHECK([bison -v duplicate.y -o duplicate.c], 0, ignore, ignore)
138
139 AT_CLEANUP
140
141
142 ## ------------------------- ##
143 ## Unresolved SR Conflicts. ##
144 ## ------------------------- ##
145
146 AT_SETUP([Unresolved SR Conflicts])
147
148 AT_DATA([input.y],
149 [[%token NUM OP
150 %%
151 exp: exp OP exp | NUM;
152 ]])
153
154 AT_CHECK([bison input.y -o input.c -v], 0, [],
155 [input.y contains 1 shift/reduce conflict.
156 ])
157
158 # Check the contents of the report.
159 AT_CHECK([cat input.output], [],
160 [[State 4 contains 1 shift/reduce conflict.
161
162
163 Grammar
164
165 Number, Line, Rule
166 1 3 exp -> exp OP exp
167 2 3 exp -> NUM
168
169
170 Terminals, with rules where they appear
171
172 $ (-1)
173 error (256)
174 NUM (257) 2
175 OP (258) 1
176
177
178 Nonterminals, with rules where they appear
179
180 exp (5)
181 on left: 1 2, on right: 1
182
183
184 state 0
185
186 NUM shift, and go to state 1
187
188 exp go to state 2
189
190
191
192 state 1
193
194 exp -> NUM . (rule 2)
195
196 $default reduce using rule 2 (exp)
197
198
199
200 state 2
201
202 exp -> exp . OP exp (rule 1)
203
204 $ go to state 5
205 OP shift, and go to state 3
206
207
208
209 state 3
210
211 exp -> exp OP . exp (rule 1)
212
213 NUM shift, and go to state 1
214
215 exp go to state 4
216
217
218
219 state 4
220
221 exp -> exp . OP exp (rule 1)
222 exp -> exp OP exp . (rule 1)
223
224 OP shift, and go to state 3
225
226 OP [reduce using rule 1 (exp)]
227 $default reduce using rule 1 (exp)
228
229
230
231 state 5
232
233 $ go to state 6
234
235
236
237 state 6
238
239 $default accept
240
241
242 ]])
243
244 AT_CLEANUP
245
246
247 ## --------------------- ##
248 ## Solved SR Conflicts. ##
249 ## --------------------- ##
250
251 AT_SETUP([Solved SR Conflicts])
252
253 AT_DATA([input.y],
254 [[%token NUM OP
255 %right OP
256 %%
257 exp: exp OP exp | NUM;
258 ]])
259
260 AT_CHECK([bison input.y -o input.c -v], 0, [], [])
261
262 # Check the contents of the report.
263 AT_CHECK([cat input.output], [],
264 [[Conflict in state 4 between rule 1 and token OP resolved as shift.
265
266
267 Grammar
268
269 Number, Line, Rule
270 1 4 exp -> exp OP exp
271 2 4 exp -> NUM
272
273
274 Terminals, with rules where they appear
275
276 $ (-1)
277 error (256)
278 NUM (257) 2
279 OP (258) 1
280
281
282 Nonterminals, with rules where they appear
283
284 exp (5)
285 on left: 1 2, on right: 1
286
287
288 state 0
289
290 NUM shift, and go to state 1
291
292 exp go to state 2
293
294
295
296 state 1
297
298 exp -> NUM . (rule 2)
299
300 $default reduce using rule 2 (exp)
301
302
303
304 state 2
305
306 exp -> exp . OP exp (rule 1)
307
308 $ go to state 5
309 OP shift, and go to state 3
310
311
312
313 state 3
314
315 exp -> exp OP . exp (rule 1)
316
317 NUM shift, and go to state 1
318
319 exp go to state 4
320
321
322
323 state 4
324
325 exp -> exp . OP exp (rule 1)
326 exp -> exp OP exp . (rule 1)
327
328 OP shift, and go to state 3
329
330 $default reduce using rule 1 (exp)
331
332
333
334 state 5
335
336 $ go to state 6
337
338
339
340 state 6
341
342 $default accept
343
344
345 ]])
346
347 AT_CLEANUP
348
349
350
351
352 ## ------------------- ##
353 ## Rule Line Numbers. ##
354 ## ------------------- ##
355
356 AT_SETUP([Rule Line Numbers])
357
358 AT_DATA([input.y],
359 [[%%
360 expr:
361 'a'
362
363 {
364
365 }
366
367 'b'
368
369 {
370
371 }
372
373 |
374
375
376 {
377
378
379 }
380
381 'c'
382
383 {
384
385 };
386 ]])
387
388 AT_CHECK([bison input.y -o input.c -v], 0, [], [])
389
390 # Check the contents of the report.
391 AT_CHECK([cat input.output], [],
392 [[Grammar
393
394 Number, Line, Rule
395 1 2 @1 -> /* empty */
396 2 2 expr -> 'a' @1 'b'
397 3 15 @2 -> /* empty */
398 4 15 expr -> @2 'c'
399
400
401 Terminals, with rules where they appear
402
403 $ (-1)
404 'a' (97) 2
405 'b' (98) 2
406 'c' (99) 4
407 error (256)
408
409
410 Nonterminals, with rules where they appear
411
412 expr (6)
413 on left: 2 4
414 @1 (7)
415 on left: 1, on right: 2
416 @2 (8)
417 on left: 3, on right: 4
418
419
420 state 0
421
422 'a' shift, and go to state 1
423
424 $default reduce using rule 3 (@2)
425
426 expr go to state 6
427 @2 go to state 2
428
429
430
431 state 1
432
433 expr -> 'a' . @1 'b' (rule 2)
434
435 $default reduce using rule 1 (@1)
436
437 @1 go to state 3
438
439
440
441 state 2
442
443 expr -> @2 . 'c' (rule 4)
444
445 'c' shift, and go to state 4
446
447
448
449 state 3
450
451 expr -> 'a' @1 . 'b' (rule 2)
452
453 'b' shift, and go to state 5
454
455
456
457 state 4
458
459 expr -> @2 'c' . (rule 4)
460
461 $default reduce using rule 4 (expr)
462
463
464
465 state 5
466
467 expr -> 'a' @1 'b' . (rule 2)
468
469 $default reduce using rule 2 (expr)
470
471
472
473 state 6
474
475 $ go to state 7
476
477
478
479 state 7
480
481 $ go to state 8
482
483
484
485 state 8
486
487 $default accept
488
489
490 ]])
491
492 AT_CLEANUP
493
494
495
496 ## -------------------- ##
497 ## %expect not enough. ##
498 ## -------------------- ##
499
500 AT_SETUP([%expect not enough])
501
502 AT_DATA([input.y],
503 [[%token NUM OP
504 %expect 0
505 %%
506 exp: exp OP exp | NUM;
507 ]])
508
509 AT_CHECK([bison input.y -o input.c], 1, [],
510 [input.y contains 1 shift/reduce conflict.
511 expected 0 shift/reduce conflicts
512 ])
513 AT_CLEANUP
514
515
516 ## --------------- ##
517 ## %expect right. ##
518 ## --------------- ##
519
520 AT_SETUP([%expect right])
521
522 AT_DATA([input.y],
523 [[%token NUM OP
524 %expect 1
525 %%
526 exp: exp OP exp | NUM;
527 ]])
528
529 AT_CHECK([bison input.y -o input.c], 0)
530 AT_CLEANUP
531
532
533 ## ------------------ ##
534 ## %expect too much. ##
535 ## ------------------ ##
536
537 AT_SETUP([%expect too much])
538
539 AT_DATA([input.y],
540 [[%token NUM OP
541 %expect 2
542 %%
543 exp: exp OP exp | NUM;
544 ]])
545
546 AT_CHECK([bison input.y -o input.c], 1, [],
547 [input.y contains 1 shift/reduce conflict.
548 expected 2 shift/reduce conflicts
549 ])
550 AT_CLEANUP
551
552
553 ## ---------------------- ##
554 ## Mixing %token styles. ##
555 ## ---------------------- ##
556
557
558 AT_SETUP([Mixing %token styles])
559
560 # Taken from the documentation.
561 AT_DATA([input.y],
562 [[%token <operator> OR "||"
563 %token <operator> LE 134 "<="
564 %left OR "<="
565 %%
566 exp: ;
567 %%
568 ]])
569
570 AT_CHECK([bison -v input.y -o input.c], 0, ignore, ignore)
571
572 AT_CLEANUP
573
574
575
576 ## ---------------------- ##
577 ## %union and --defines. ##
578 ## ---------------------- ##
579
580
581 AT_SETUP([%union and --defines])
582
583 AT_DATA([union.y],
584 [%union
585 {
586 int integer;
587 char *string ;
588 }
589 %%
590 exp: {};
591 ])
592
593 AT_CHECK([bison --defines union.y])
594
595 AT_CLEANUP
596
597
598 ## --------------------------------------- ##
599 ## Duplicate '/' in C comments in %union ##
600 ## --------------------------------------- ##
601
602
603 AT_SETUP([%union and C comments])
604
605 AT_DATA([union-comment.y],
606 [%union
607 {
608 /* The int. */ int integer;
609 /* The string. */ char *string ;
610 }
611 %%
612 exp: {};
613 ])
614
615 AT_CHECK([bison union-comment.y])
616 AT_CHECK([fgrep '//*' union-comment.tab.c], [1], [])
617
618 AT_CLEANUP
619
620
621 ## ---------------- ##
622 ## Invalid inputs. ##
623 ## ---------------- ##
624
625
626 AT_SETUP([Invalid inputs])
627
628 AT_DATA([input.y],
629 [[%%
630 ?
631 default: 'a' }
632 %{
633 %&
634 %a
635 %-
636 ]])
637
638 AT_CHECK([bison input.y], [1], [],
639 [[input.y:2: invalid input: `?'
640 input.y:3: invalid input: `}'
641 input.y:4: invalid input: `%{'
642 input.y:5: invalid input: `%&'
643 input.y:6: invalid input: `%a'
644 input.y:7: invalid input: `%-'
645 ]])
646
647 AT_CLEANUP
648
649
650
651 ## -------------------- ##
652 ## Invalid %directive. ##
653 ## -------------------- ##
654
655
656 AT_SETUP([Invalid %directive])
657
658 AT_DATA([input.y],
659 [[%invalid
660 ]])
661
662 AT_CHECK([bison input.y], [1], [],
663 [[input.y:1: unrecognized: %invalid
664 input.y:1: Skipping to next %
665 input.y:2: fatal error: no input grammar
666 ]])
667
668 AT_CLEANUP
669
670
671
672 ## -------------- ##
673 ## Web2c Report. ##
674 ## -------------- ##
675
676 # The generation of the reduction was once wrong in Bison, and made it
677 # miss some reductions. In the following test case, the reduction on
678 # `undef_id_tok' in state 1 was missing. This is stripped down from
679 # the actual web2c.y.
680
681 AT_SETUP([Web2c Report])
682
683 AT_DATA([input.y],
684 [[%token undef_id_tok const_id_tok
685
686 %start CONST_DEC_PART
687 \f
688 %%
689 CONST_DEC_PART:
690 CONST_DEC_LIST
691 ;
692
693 CONST_DEC_LIST:
694 CONST_DEC
695 | CONST_DEC_LIST CONST_DEC
696 ;
697
698 CONST_DEC:
699 { } undef_id_tok '=' const_id_tok ';'
700 ;
701 %%
702
703 ]])
704
705 AT_CHECK([bison -v input.y])
706
707 AT_CHECK([sed -n 's/ *$//;/^$/!p' input.output], 0,
708 [[Grammar
709 Number, Line, Rule
710 1 6 CONST_DEC_PART -> CONST_DEC_LIST
711 2 10 CONST_DEC_LIST -> CONST_DEC
712 3 12 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC
713 4 15 @1 -> /* empty */
714 5 15 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';'
715 Terminals, with rules where they appear
716 $ (-1)
717 ';' (59) 5
718 '=' (61) 5
719 error (256)
720 undef_id_tok (257) 5
721 const_id_tok (258) 5
722 Nonterminals, with rules where they appear
723 CONST_DEC_PART (7)
724 on left: 1
725 CONST_DEC_LIST (8)
726 on left: 2 3, on right: 1 3
727 CONST_DEC (9)
728 on left: 5, on right: 2 3
729 @1 (10)
730 on left: 4, on right: 5
731 state 0
732 $default reduce using rule 4 (@1)
733 CONST_DEC_PART go to state 9
734 CONST_DEC_LIST go to state 1
735 CONST_DEC go to state 2
736 @1 go to state 3
737 state 1
738 CONST_DEC_PART -> CONST_DEC_LIST . (rule 1)
739 CONST_DEC_LIST -> CONST_DEC_LIST . CONST_DEC (rule 3)
740 undef_id_tok reduce using rule 4 (@1)
741 $default reduce using rule 1 (CONST_DEC_PART)
742 CONST_DEC go to state 4
743 @1 go to state 3
744 state 2
745 CONST_DEC_LIST -> CONST_DEC . (rule 2)
746 $default reduce using rule 2 (CONST_DEC_LIST)
747 state 3
748 CONST_DEC -> @1 . undef_id_tok '=' const_id_tok ';' (rule 5)
749 undef_id_tok shift, and go to state 5
750 state 4
751 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC . (rule 3)
752 $default reduce using rule 3 (CONST_DEC_LIST)
753 state 5
754 CONST_DEC -> @1 undef_id_tok . '=' const_id_tok ';' (rule 5)
755 '=' shift, and go to state 6
756 state 6
757 CONST_DEC -> @1 undef_id_tok '=' . const_id_tok ';' (rule 5)
758 const_id_tok shift, and go to state 7
759 state 7
760 CONST_DEC -> @1 undef_id_tok '=' const_id_tok . ';' (rule 5)
761 ';' shift, and go to state 8
762 state 8
763 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';' . (rule 5)
764 $default reduce using rule 5 (CONST_DEC)
765 state 9
766 $ go to state 10
767 state 10
768 $ go to state 11
769 state 11
770 $default accept
771 ]])
772
773 AT_CLEANUP
774
775
776 ## --------------- ##
777 ## Web2c Actions. ##
778 ## --------------- ##
779
780 # The generation of the mapping `state -> action' was once wrong in
781 # extremely specific situations. web2c.y exhibits this situation.
782 # Below is a stripped version of the grammar. It looks like one can
783 # simplify it further, but just don't: it is tuned to exhibit a bug,
784 # which disapears when applying sane grammar transformations.
785 #
786 # It used to be wrong on yydefact only:
787 #
788 # static const short yydefact[] =
789 # {
790 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
791 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
792 # 0, 0
793 # };
794 #
795 # but let's check all the tables.
796
797
798 AT_SETUP([Web2c Actions])
799
800 AT_DATA([input.y],
801 [[%%
802 statement: struct_stat;
803 struct_stat: /* empty. */ | if else;
804 if: "if" "const" "then" statement;
805 else: "else" statement;
806 %%
807 ]])
808
809 AT_CHECK([bison -v input.y -o input.c])
810
811 # Check only the tables. We don't use --no-parser, because it is
812 # still to be implemented in the experimental branch of Bison.
813 AT_CHECK([[sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c]], 0,
814 [[static const char yytranslate[] =
815 {
816 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
817 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
818 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
819 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
820 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
821 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
822 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
823 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
824 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
825 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
826 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
827 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
828 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
829 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
830 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
831 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
832 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
833 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
834 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
835 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
836 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
837 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
838 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
839 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
840 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
841 2, 2, 2, 2, 2, 2, 1, 3, 4, 5,
842 6
843 };
844 static const short yyprhs[] =
845 {
846 0, 0, 2, 3, 6, 11
847 };
848 static const short yyrhs[] =
849 {
850 8, 0, 0, 9, 10, 0, 3, 4, 5, 7,
851 0, 6, 7, 0
852 };
853 static const short yyrline[] =
854 {
855 0, 2, 3, 3, 4, 5
856 };
857 static const char *const yytname[] =
858 {
859 "$", "error", "$undefined.", "\"if\"", "\"const\"", "\"then\"",
860 "\"else\"", "statement", "struct_stat", "if", "else", 0
861 };
862 static const short yyr1[] =
863 {
864 0, 7, 8, 8, 9, 10
865 };
866 static const short yyr2[] =
867 {
868 0, 1, 0, 2, 4, 2
869 };
870 static const short yydefact[] =
871 {
872 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
873 0, 0, 0
874 };
875 static const short yydefgoto[] =
876 {
877 8, 2, 3, 6
878 };
879 static const short yypact[] =
880 {
881 -2, -1,-32768, -4, 1, -2,-32768, -2,-32768,-32768,
882 4, 5,-32768
883 };
884 static const short yypgoto[] =
885 {
886 0,-32768,-32768,-32768
887 };
888 static const short yytable[] =
889 {
890 10, 1, 5, 4, 11, 12, 7, 9
891 };
892 static const short yycheck[] =
893 {
894 0, 3, 6, 4, 0, 0, 5, 7
895 };
896 ]])
897
898 AT_CLEANUP