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