]> git.saurik.com Git - bison.git/blob - tests/regression.at
New.
[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([input.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 input.y -o input.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 5 contains 1 shift/reduce conflict.
161
162
163 Grammar
164
165 Number, Line, Rule
166 0 3 $axiom -> exp $
167 1 3 exp -> exp OP exp
168 2 3 exp -> NUM
169
170
171 Terminals, with rules where they appear
172
173 $ (0) 0
174 error (256)
175 NUM (257) 2
176 OP (258) 1
177
178
179 Nonterminals, with rules where they appear
180
181 $axiom (5)
182 on left: 0
183 exp (6)
184 on left: 1 2, on right: 0 1
185
186
187 state 0
188
189 NUM shift, and go to state 1
190
191 exp go to state 2
192
193
194
195 state 1
196
197 exp -> NUM . (rule 2)
198
199 $default reduce using rule 2 (exp)
200
201
202
203 state 2
204
205 $axiom -> exp . $ (rule 0)
206 exp -> exp . OP exp (rule 1)
207
208 $ shift, and go to state 3
209 OP shift, and go to state 4
210
211
212
213 state 3
214
215 $axiom -> exp $ . (rule 0)
216
217 $default accept
218
219
220 state 4
221
222 exp -> exp OP . exp (rule 1)
223
224 NUM shift, and go to state 1
225
226 exp go to state 5
227
228
229
230 state 5
231
232 exp -> exp . OP exp (rule 1)
233 exp -> exp OP exp . (rule 1)
234
235 OP shift, and go to state 4
236
237 OP [reduce using rule 1 (exp)]
238 $default reduce using rule 1 (exp)
239
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 5 between rule 2 and token OP resolved as shift.
265
266
267 Grammar
268
269 Number, Line, Rule
270 0 4 $axiom -> exp $
271 1 4 exp -> exp OP exp
272 2 4 exp -> NUM
273
274
275 Terminals, with rules where they appear
276
277 $ (0) 0
278 error (256)
279 NUM (257) 2
280 OP (258) 1
281
282
283 Nonterminals, with rules where they appear
284
285 $axiom (5)
286 on left: 0
287 exp (6)
288 on left: 1 2, on right: 0 1
289
290
291 state 0
292
293 NUM shift, and go to state 1
294
295 exp go to state 2
296
297
298
299 state 1
300
301 exp -> NUM . (rule 2)
302
303 $default reduce using rule 2 (exp)
304
305
306
307 state 2
308
309 $axiom -> exp . $ (rule 0)
310 exp -> exp . OP exp (rule 1)
311
312 $ shift, and go to state 3
313 OP shift, and go to state 4
314
315
316
317 state 3
318
319 $axiom -> exp $ . (rule 0)
320
321 $default accept
322
323
324 state 4
325
326 exp -> exp OP . exp (rule 1)
327
328 NUM shift, and go to state 1
329
330 exp go to state 5
331
332
333
334 state 5
335
336 exp -> exp . OP exp (rule 1)
337 exp -> exp OP exp . (rule 1)
338
339 OP shift, and go to state 4
340
341 $default reduce using rule 1 (exp)
342
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 0 2 $axiom -> expr $
396 1 2 @1 -> /* empty */
397 2 2 expr -> 'a' @1 'b'
398 3 15 @2 -> /* empty */
399 4 15 expr -> @2 'c'
400
401
402 Terminals, with rules where they appear
403
404 $ (0) 0
405 'a' (97) 2
406 'b' (98) 2
407 'c' (99) 4
408 error (256)
409
410
411 Nonterminals, with rules where they appear
412
413 $axiom (6)
414 on left: 0
415 expr (7)
416 on left: 2 4, on right: 0
417 @1 (8)
418 on left: 1, on right: 2
419 @2 (9)
420 on left: 3, on right: 4
421
422
423 state 0
424
425 'a' shift, and go to state 1
426
427 $default reduce using rule 3 (@2)
428
429 expr go to state 2
430 @2 go to state 3
431
432
433
434 state 1
435
436 expr -> 'a' . @1 'b' (rule 2)
437
438 $default reduce using rule 1 (@1)
439
440 @1 go to state 4
441
442
443
444 state 2
445
446 $axiom -> expr . $ (rule 0)
447
448 $ shift, and go to state 5
449
450
451
452 state 3
453
454 expr -> @2 . 'c' (rule 4)
455
456 'c' shift, and go to state 6
457
458
459
460 state 4
461
462 expr -> 'a' @1 . 'b' (rule 2)
463
464 'b' shift, and go to state 7
465
466
467
468 state 5
469
470 $axiom -> expr $ . (rule 0)
471
472 $default accept
473
474
475 state 6
476
477 expr -> @2 'c' . (rule 4)
478
479 $default reduce using rule 4 (expr)
480
481
482
483 state 7
484
485 expr -> 'a' @1 'b' . (rule 2)
486
487 $default reduce using rule 2 (expr)
488
489
490
491 ]])
492
493 AT_CLEANUP
494
495
496
497 ## -------------------- ##
498 ## %expect not enough. ##
499 ## -------------------- ##
500
501 AT_SETUP([%expect not enough])
502
503 AT_DATA([input.y],
504 [[%token NUM OP
505 %expect 0
506 %%
507 exp: exp OP exp | NUM;
508 ]])
509
510 AT_CHECK([bison input.y -o input.c], 1, [],
511 [input.y contains 1 shift/reduce conflict.
512 expected 0 shift/reduce conflicts
513 ])
514 AT_CLEANUP
515
516
517 ## --------------- ##
518 ## %expect right. ##
519 ## --------------- ##
520
521 AT_SETUP([%expect right])
522
523 AT_DATA([input.y],
524 [[%token NUM OP
525 %expect 1
526 %%
527 exp: exp OP exp | NUM;
528 ]])
529
530 AT_CHECK([bison input.y -o input.c], 0)
531 AT_CLEANUP
532
533
534 ## ------------------ ##
535 ## %expect too much. ##
536 ## ------------------ ##
537
538 AT_SETUP([%expect too much])
539
540 AT_DATA([input.y],
541 [[%token NUM OP
542 %expect 2
543 %%
544 exp: exp OP exp | NUM;
545 ]])
546
547 AT_CHECK([bison input.y -o input.c], 1, [],
548 [input.y contains 1 shift/reduce conflict.
549 expected 2 shift/reduce conflicts
550 ])
551 AT_CLEANUP
552
553
554 ## ---------------------- ##
555 ## Mixing %token styles. ##
556 ## ---------------------- ##
557
558
559 AT_SETUP([Mixing %token styles])
560
561 # Taken from the documentation.
562 AT_DATA([input.y],
563 [[%token <operator> OR "||"
564 %token <operator> LE 134 "<="
565 %left OR "<="
566 %%
567 exp: ;
568 %%
569 ]])
570
571 AT_CHECK([bison -v input.y -o input.c], 0, ignore, ignore)
572
573 AT_CLEANUP
574
575
576
577 ## ---------------- ##
578 ## Invalid inputs. ##
579 ## ---------------- ##
580
581
582 AT_SETUP([Invalid inputs])
583
584 AT_DATA([input.y],
585 [[%%
586 ?
587 default: 'a' }
588 %{
589 %&
590 %a
591 %-
592 ]])
593
594 AT_CHECK([bison input.y], [1], [],
595 [[input.y:2: invalid input: `?'
596 input.y:3: invalid input: `}'
597 input.y:4: invalid input: `%{'
598 input.y:5: invalid input: `%&'
599 input.y:6: invalid input: `%a'
600 input.y:7: invalid input: `%-'
601 ]])
602
603 AT_CLEANUP
604
605
606
607 ## -------------------- ##
608 ## Invalid %directive. ##
609 ## -------------------- ##
610
611
612 AT_SETUP([Invalid %directive])
613
614 AT_DATA([input.y],
615 [[%invalid
616 ]])
617
618 AT_CHECK([bison input.y], [1], [],
619 [[input.y:1: unrecognized: %invalid
620 input.y:1: Skipping to next %
621 input.y:2: fatal error: no input grammar
622 ]])
623
624 AT_CLEANUP
625
626
627
628 ## -------------- ##
629 ## Web2c Report. ##
630 ## -------------- ##
631
632 # The generation of the reduction was once wrong in Bison, and made it
633 # miss some reductions. In the following test case, the reduction on
634 # `undef_id_tok' in state 1 was missing. This is stripped down from
635 # the actual web2c.y.
636
637 AT_SETUP([Web2c Report])
638
639 AT_DATA([input.y],
640 [[%token undef_id_tok const_id_tok
641
642 %start CONST_DEC_PART
643 \f
644 %%
645 CONST_DEC_PART:
646 CONST_DEC_LIST
647 ;
648
649 CONST_DEC_LIST:
650 CONST_DEC
651 | CONST_DEC_LIST CONST_DEC
652 ;
653
654 CONST_DEC:
655 { } undef_id_tok '=' const_id_tok ';'
656 ;
657 %%
658
659 ]])
660
661 AT_CHECK([bison -v input.y])
662
663 AT_CHECK([sed -n 's/ *$//;/^$/!p' input.output], 0,
664 [[Grammar
665 Number, Line, Rule
666 0 6 $axiom -> CONST_DEC_PART $
667 1 6 CONST_DEC_PART -> CONST_DEC_LIST
668 2 10 CONST_DEC_LIST -> CONST_DEC
669 3 12 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC
670 4 15 @1 -> /* empty */
671 5 15 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';'
672 Terminals, with rules where they appear
673 $ (0) 0
674 ';' (59) 5
675 '=' (61) 5
676 error (256)
677 undef_id_tok (257) 5
678 const_id_tok (258) 5
679 Nonterminals, with rules where they appear
680 $axiom (7)
681 on left: 0
682 CONST_DEC_PART (8)
683 on left: 1, on right: 0
684 CONST_DEC_LIST (9)
685 on left: 2 3, on right: 1 3
686 CONST_DEC (10)
687 on left: 5, on right: 2 3
688 @1 (11)
689 on left: 4, on right: 5
690 state 0
691 $default reduce using rule 4 (@1)
692 CONST_DEC_PART go to state 1
693 CONST_DEC_LIST go to state 2
694 CONST_DEC go to state 3
695 @1 go to state 4
696 state 1
697 $axiom -> CONST_DEC_PART . $ (rule 0)
698 $ shift, and go to state 5
699 state 2
700 CONST_DEC_PART -> CONST_DEC_LIST . (rule 1)
701 CONST_DEC_LIST -> CONST_DEC_LIST . CONST_DEC (rule 3)
702 undef_id_tok reduce using rule 4 (@1)
703 $default reduce using rule 1 (CONST_DEC_PART)
704 CONST_DEC go to state 6
705 @1 go to state 4
706 state 3
707 CONST_DEC_LIST -> CONST_DEC . (rule 2)
708 $default reduce using rule 2 (CONST_DEC_LIST)
709 state 4
710 CONST_DEC -> @1 . undef_id_tok '=' const_id_tok ';' (rule 5)
711 undef_id_tok shift, and go to state 7
712 state 5
713 $axiom -> CONST_DEC_PART $ . (rule 0)
714 $default accept
715 state 6
716 CONST_DEC_LIST -> CONST_DEC_LIST CONST_DEC . (rule 3)
717 $default reduce using rule 3 (CONST_DEC_LIST)
718 state 7
719 CONST_DEC -> @1 undef_id_tok . '=' const_id_tok ';' (rule 5)
720 '=' shift, and go to state 8
721 state 8
722 CONST_DEC -> @1 undef_id_tok '=' . const_id_tok ';' (rule 5)
723 const_id_tok shift, and go to state 9
724 state 9
725 CONST_DEC -> @1 undef_id_tok '=' const_id_tok . ';' (rule 5)
726 ';' shift, and go to state 10
727 state 10
728 CONST_DEC -> @1 undef_id_tok '=' const_id_tok ';' . (rule 5)
729 $default reduce using rule 5 (CONST_DEC)
730 ]])
731
732 AT_CLEANUP
733
734
735 ## --------------- ##
736 ## Web2c Actions. ##
737 ## --------------- ##
738
739 # The generation of the mapping `state -> action' was once wrong in
740 # extremely specific situations. web2c.y exhibits this situation.
741 # Below is a stripped version of the grammar. It looks like one can
742 # simplify it further, but just don't: it is tuned to exhibit a bug,
743 # which disapears when applying sane grammar transformations.
744 #
745 # It used to be wrong on yydefact only:
746 #
747 # static const short yydefact[] =
748 # {
749 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
750 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
751 # 0, 0
752 # };
753 #
754 # but let's check all the tables.
755
756
757 AT_SETUP([Web2c Actions])
758
759 AT_DATA([input.y],
760 [[%%
761 statement: struct_stat;
762 struct_stat: /* empty. */ | if else;
763 if: "if" "const" "then" statement;
764 else: "else" statement;
765 %%
766 ]])
767
768 AT_CHECK([bison -v input.y -o input.c])
769
770 # Check only the tables. We don't use --no-parser, because it is
771 # still to be implemented in the experimental branch of Bison.
772 AT_CHECK([[sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c]], 0,
773 [[static const char yytranslate[] =
774 {
775 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
776 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
777 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
778 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
779 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
780 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
781 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
782 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
783 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
784 2, 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, 1, 3, 4, 5,
801 6
802 };
803 static const short yyprhs[] =
804 {
805 0, 0, 3, 5, 6, 9, 14
806 };
807 static const short yyrhs[] =
808 {
809 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
810 4, 5, 8, -1, 6, 8, -1
811 };
812 static const short yyrline[] =
813 {
814 0, 2, 2, 3, 3, 4, 5
815 };
816 static const char *const yytname[] =
817 {
818 "$", "error", "$undefined.", "\"if\"", "\"const\"", "\"then\"",
819 "\"else\"", "$axiom", "statement", "struct_stat", "if", "else", 0
820 };
821 static const short yytoknum[] =
822 {
823 0, 256, 2, 257, 258, 259, 260, -1
824 };
825 static const short yyr1[] =
826 {
827 0, 7, 8, 9, 9, 10, 11
828 };
829 static const short yyr2[] =
830 {
831 0, 2, 1, 0, 2, 4, 2
832 };
833 static const short yydefact[] =
834 {
835 3, 0, 0, 2, 0, 0, 0, 3, 4, 3,
836 6, 5
837 };
838 static const short yydefgoto[] =
839 {
840 -1, 2, 3, 4, 8
841 };
842 static const short yypact[] =
843 {
844 -2, -1, 4,-32768, 0, 2,-32768, -2,-32768, -2,
845 -32768,-32768
846 };
847 static const short yypgoto[] =
848 {
849 -32768, -7,-32768,-32768,-32768
850 };
851 static const short yytable[] =
852 {
853 10, 1, 11, 5, 6, 0, 7, 9
854 };
855 static const short yycheck[] =
856 {
857 7, 3, 9, 4, 0, -1, 6, 5
858 };
859 ]])
860
861 AT_CLEANUP