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