]> git.saurik.com Git - bison.git/blob - tests/regression.at
59eddbb22964be57802530cc20d3303a8fca9111
[bison.git] / tests / regression.at
1 # Bison Regressions. -*- Autotest -*-
2 # Copyright (C) 2001, 2002, 2003 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 ## Early token definitions. ##
24 ## ------------------------- ##
25
26
27 AT_SETUP([Early token definitions])
28
29 # Found in GCJ: they expect the tokens to be defined before the user
30 # prologue, so that they can use the token definitions in it.
31
32 AT_DATA_GRAMMAR([input.y],
33 [[%{
34 void yyerror (const char *s);
35 int yylex (void);
36 %}
37
38 %union
39 {
40 int val;
41 };
42 %{
43 #ifndef MY_TOKEN
44 # error "MY_TOKEN not defined."
45 #endif
46 %}
47 %token MY_TOKEN
48 %%
49 exp: MY_TOKEN;
50 %%
51 ]])
52
53 AT_CHECK([bison -o input.c input.y])
54 AT_COMPILE([input.o], [-c input.c])
55
56 AT_CLEANUP
57
58
59
60 ## ---------------- ##
61 ## Braces parsing. ##
62 ## ---------------- ##
63
64
65 AT_SETUP([Braces parsing])
66
67 AT_DATA([input.y],
68 [[/* Bison used to swallow the character after `}'. */
69
70 %%
71 exp: { tests = {{{{{{{{{{}}}}}}}}}}; };
72 %%
73 ]])
74
75 AT_CHECK([bison -v -o input.c input.y])
76
77 AT_CHECK([grep 'tests = {{{{{{{{{{}}}}}}}}}};' input.c], 0, [ignore])
78
79 AT_CLEANUP
80
81
82 ## ------------------ ##
83 ## Duplicate string. ##
84 ## ------------------ ##
85
86
87 AT_SETUP([Duplicate string])
88
89 AT_DATA([input.y],
90 [[/* `Bison -v' used to dump core when two tokens are defined with the same
91 string, as LE and GE below. */
92
93 %token NUM
94 %token LE "<="
95 %token GE "<="
96
97 %%
98 exp: '(' exp ')' | NUM ;
99 %%
100 ]])
101
102 AT_CHECK([bison -v -o input.c input.y], 0, [],
103 [[input.y:6.8-14: warning: symbol `"<="' used more than once as a literal string
104 ]])
105
106 AT_CLEANUP
107
108
109 ## ------------------- ##
110 ## Rule Line Numbers. ##
111 ## ------------------- ##
112
113 AT_SETUP([Rule Line Numbers])
114
115 AT_KEYWORDS([report])
116
117 AT_DATA([input.y],
118 [[%%
119 expr:
120 'a'
121
122 {
123
124 }
125
126 'b'
127
128 {
129
130 }
131
132 |
133
134
135 {
136
137
138 }
139
140 'c'
141
142 {
143
144 };
145 ]])
146
147 AT_CHECK([bison -o input.c -v input.y])
148
149 # Check the contents of the report.
150 AT_CHECK([cat input.output], [],
151 [[Grammar
152
153 0 $accept: expr $end
154
155 1 @1: /* empty */
156
157 2 expr: 'a' @1 'b'
158
159 3 @2: /* empty */
160
161 4 expr: @2 'c'
162
163
164 Terminals, with rules where they appear
165
166 $end (0) 0
167 'a' (97) 2
168 'b' (98) 2
169 'c' (99) 4
170 error (256)
171
172
173 Nonterminals, with rules where they appear
174
175 $accept (6)
176 on left: 0
177 expr (7)
178 on left: 2 4, on right: 0
179 @1 (8)
180 on left: 1, on right: 2
181 @2 (9)
182 on left: 3, on right: 4
183
184
185 state 0
186
187 0 $accept: . expr $end
188
189 'a' shift, and go to state 1
190
191 $default reduce using rule 3 (@2)
192
193 expr go to state 2
194 @2 go to state 3
195
196
197 state 1
198
199 2 expr: 'a' . @1 'b'
200
201 $default reduce using rule 1 (@1)
202
203 @1 go to state 4
204
205
206 state 2
207
208 0 $accept: expr . $end
209
210 $end shift, and go to state 5
211
212
213 state 3
214
215 4 expr: @2 . 'c'
216
217 'c' shift, and go to state 6
218
219
220 state 4
221
222 2 expr: 'a' @1 . 'b'
223
224 'b' shift, and go to state 7
225
226
227 state 5
228
229 0 $accept: expr $end .
230
231 $default accept
232
233
234 state 6
235
236 4 expr: @2 'c' .
237
238 $default reduce using rule 4 (expr)
239
240
241 state 7
242
243 2 expr: 'a' @1 'b' .
244
245 $default reduce using rule 2 (expr)
246 ]])
247
248 AT_CLEANUP
249
250
251
252 ## ---------------------- ##
253 ## Mixing %token styles. ##
254 ## ---------------------- ##
255
256
257 AT_SETUP([Mixing %token styles])
258
259 # Taken from the documentation.
260 AT_DATA([input.y],
261 [[%token <operator> OR "||"
262 %token <operator> LE 134 "<="
263 %left OR "<="
264 %%
265 exp: ;
266 %%
267 ]])
268
269 AT_CHECK([bison -v -o input.c input.y])
270
271 AT_CLEANUP
272
273
274
275 ## ---------------- ##
276 ## Invalid inputs. ##
277 ## ---------------- ##
278
279
280 AT_SETUP([Invalid inputs])
281
282 AT_DATA([input.y],
283 [[%%
284 ?
285 default: 'a' }
286 %&
287 %a-does-not-exist
288 %-
289 %{
290 ]])
291
292 AT_CHECK([bison input.y], [1], [],
293 [[input.y:2.1: invalid character: `?'
294 input.y:3.14: invalid character: `}'
295 input.y:4.1: invalid character: `%'
296 input.y:4.2: invalid character: `&'
297 input.y:5.1-17: invalid directive: `%a-does-not-exist'
298 input.y:6.1: invalid character: `%'
299 input.y:6.2: invalid character: `-'
300 input.y:7.1-8.0: missing `%}' at end of file
301 input.y:7.1-8.0: syntax error, unexpected "%{...%}"
302 ]])
303
304 AT_CLEANUP
305
306
307
308 ## ------------------- ##
309 ## Token definitions. ##
310 ## ------------------- ##
311
312
313 AT_SETUP([Token definitions])
314
315 # Bison managed, when fed with `%token 'f' "f"' to #define 'f'!
316 AT_DATA_GRAMMAR([input.y],
317 [%{
318 void yyerror (const char *s);
319 int yylex (void);
320 %}
321 [%token MYEOF 0 "end of file"
322 %token 'a' "a"
323 %token b "b"
324 %token c 'c'
325 %token 'd' d
326 %%
327 exp: "a";
328 ]])
329
330 AT_CHECK([bison -o input.c input.y])
331 AT_COMPILE([input.o], [-c input.c])
332 AT_CLEANUP
333
334
335
336 ## -------------------- ##
337 ## Characters Escapes. ##
338 ## -------------------- ##
339
340
341 AT_SETUP([Characters Escapes])
342
343 AT_DATA_GRAMMAR([input.y],
344 [%{
345 void yyerror (const char *s);
346 int yylex (void);
347 %}
348 [%%
349 exp:
350 '\'' "\'"
351 | '\"' "\""
352 | '"' "'"
353 ;
354 ]])
355 # Pacify font-lock-mode: "
356
357 AT_CHECK([bison -o input.c input.y])
358 AT_COMPILE([input.o], [-c input.c])
359 AT_CLEANUP
360
361
362
363 ## -------------- ##
364 ## Web2c Report. ##
365 ## -------------- ##
366
367 # The generation of the reduction was once wrong in Bison, and made it
368 # miss some reductions. In the following test case, the reduction on
369 # `undef_id_tok' in state 1 was missing. This is stripped down from
370 # the actual web2c.y.
371
372 AT_SETUP([Web2c Report])
373
374 AT_KEYWORDS([report])
375
376 AT_DATA([input.y],
377 [[%token undef_id_tok const_id_tok
378
379 %start CONST_DEC_PART
380 \f
381 %%
382 CONST_DEC_PART:
383 CONST_DEC_LIST
384 ;
385
386 CONST_DEC_LIST:
387 CONST_DEC
388 | CONST_DEC_LIST CONST_DEC
389 ;
390
391 CONST_DEC:
392 { } undef_id_tok '=' const_id_tok ';'
393 ;
394 %%
395 ]])
396
397 AT_CHECK([bison -v input.y])
398 AT_CHECK([cat input.output], 0,
399 [[Grammar
400
401 0 $accept: CONST_DEC_PART $end
402
403 1 CONST_DEC_PART: CONST_DEC_LIST
404
405 2 CONST_DEC_LIST: CONST_DEC
406 3 | CONST_DEC_LIST CONST_DEC
407
408 4 @1: /* empty */
409
410 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';'
411
412
413 Terminals, with rules where they appear
414
415 $end (0) 0
416 ';' (59) 5
417 '=' (61) 5
418 error (256)
419 undef_id_tok (258) 5
420 const_id_tok (259) 5
421
422
423 Nonterminals, with rules where they appear
424
425 $accept (7)
426 on left: 0
427 CONST_DEC_PART (8)
428 on left: 1, on right: 0
429 CONST_DEC_LIST (9)
430 on left: 2 3, on right: 1 3
431 CONST_DEC (10)
432 on left: 5, on right: 2 3
433 @1 (11)
434 on left: 4, on right: 5
435
436
437 state 0
438
439 0 $accept: . CONST_DEC_PART $end
440
441 $default reduce using rule 4 (@1)
442
443 CONST_DEC_PART go to state 1
444 CONST_DEC_LIST go to state 2
445 CONST_DEC go to state 3
446 @1 go to state 4
447
448
449 state 1
450
451 0 $accept: CONST_DEC_PART . $end
452
453 $end shift, and go to state 5
454
455
456 state 2
457
458 1 CONST_DEC_PART: CONST_DEC_LIST .
459 3 CONST_DEC_LIST: CONST_DEC_LIST . CONST_DEC
460
461 undef_id_tok reduce using rule 4 (@1)
462 $default reduce using rule 1 (CONST_DEC_PART)
463
464 CONST_DEC go to state 6
465 @1 go to state 4
466
467
468 state 3
469
470 2 CONST_DEC_LIST: CONST_DEC .
471
472 $default reduce using rule 2 (CONST_DEC_LIST)
473
474
475 state 4
476
477 5 CONST_DEC: @1 . undef_id_tok '=' const_id_tok ';'
478
479 undef_id_tok shift, and go to state 7
480
481
482 state 5
483
484 0 $accept: CONST_DEC_PART $end .
485
486 $default accept
487
488
489 state 6
490
491 3 CONST_DEC_LIST: CONST_DEC_LIST CONST_DEC .
492
493 $default reduce using rule 3 (CONST_DEC_LIST)
494
495
496 state 7
497
498 5 CONST_DEC: @1 undef_id_tok . '=' const_id_tok ';'
499
500 '=' shift, and go to state 8
501
502
503 state 8
504
505 5 CONST_DEC: @1 undef_id_tok '=' . const_id_tok ';'
506
507 const_id_tok shift, and go to state 9
508
509
510 state 9
511
512 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok . ';'
513
514 ';' shift, and go to state 10
515
516
517 state 10
518
519 5 CONST_DEC: @1 undef_id_tok '=' const_id_tok ';' .
520
521 $default reduce using rule 5 (CONST_DEC)
522 ]])
523
524 AT_CLEANUP
525
526
527 ## --------------- ##
528 ## Web2c Actions. ##
529 ## --------------- ##
530
531 # The generation of the mapping `state -> action' was once wrong in
532 # extremely specific situations. web2c.y exhibits this situation.
533 # Below is a stripped version of the grammar. It looks like one can
534 # simplify it further, but just don't: it is tuned to exhibit a bug,
535 # which disapears when applying sane grammar transformations.
536 #
537 # It used to be wrong on yydefact only:
538 #
539 # static const short yydefact[] =
540 # {
541 # - 2, 0, 1, 0, 0, 2, 3, 2, 5, 4,
542 # + 2, 0, 1, 0, 0, 0, 3, 2, 5, 4,
543 # 0, 0
544 # };
545 #
546 # but let's check all the tables.
547
548
549 AT_SETUP([Web2c Actions])
550
551 AT_KEYWORDS([report])
552
553 AT_DATA([input.y],
554 [[%%
555 statement: struct_stat;
556 struct_stat: /* empty. */ | if else;
557 if: "if" "const" "then" statement;
558 else: "else" statement;
559 %%
560 ]])
561
562 AT_CHECK([bison -v -o input.c input.y])
563
564 # Check only the tables. We don't use --no-parser, because it is
565 # still to be implemented in the experimental branch of Bison.
566 [sed -n 's/ *$//;/^static const.*\[\] =/,/^}/p' input.c >tables.c]
567
568 AT_CHECK([[cat tables.c]], 0,
569 [[static const unsigned char yytranslate[] =
570 {
571 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
572 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
573 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
574 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
575 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
576 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
577 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
578 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
579 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
580 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
581 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
582 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
583 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
584 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
585 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
586 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
587 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
588 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
589 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
590 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
591 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
592 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
593 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
594 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
595 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
596 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
597 5, 6
598 };
599 static const unsigned char yyprhs[] =
600 {
601 0, 0, 3, 5, 6, 9, 14
602 };
603 static const yysigned_char yyrhs[] =
604 {
605 8, 0, -1, 9, -1, -1, 10, 11, -1, 3,
606 4, 5, 8, -1, 6, 8, -1
607 };
608 static const unsigned char yyrline[] =
609 {
610 0, 2, 2, 3, 3, 4, 5
611 };
612 static const char *const yytname[] =
613 {
614 "$end", "error", "$undefined", "\"if\"", "\"const\"", "\"then\"",
615 "\"else\"", "$accept", "statement", "struct_stat", "if", "else", 0
616 };
617 static const unsigned short yytoknum[] =
618 {
619 0, 256, 257, 258, 259, 260, 261
620 };
621 static const unsigned char yyr1[] =
622 {
623 0, 7, 8, 9, 9, 10, 11
624 };
625 static const unsigned char yyr2[] =
626 {
627 0, 2, 1, 0, 2, 4, 2
628 };
629 static const unsigned char yydefact[] =
630 {
631 3, 0, 0, 2, 0, 0, 1, 3, 4, 3,
632 6, 5
633 };
634 static const yysigned_char yydefgoto[] =
635 {
636 -1, 2, 3, 4, 8
637 };
638 static const yysigned_char yypact[] =
639 {
640 -2, -1, 4, -8, 0, 2, -8, -2, -8, -2,
641 -8, -8
642 };
643 static const yysigned_char yypgoto[] =
644 {
645 -8, -7, -8, -8, -8
646 };
647 static const unsigned char yytable[] =
648 {
649 10, 1, 11, 5, 6, 0, 7, 9
650 };
651 static const yysigned_char yycheck[] =
652 {
653 7, 3, 9, 4, 0, -1, 6, 5
654 };
655 static const unsigned char yystos[] =
656 {
657 0, 3, 8, 9, 10, 4, 0, 6, 11, 5,
658 8, 8
659 };
660 ]])
661
662 AT_CLEANUP
663
664
665 ## ------------------------- ##
666 ## yycheck Bound Violation. ##
667 ## ------------------------- ##
668
669
670 # _AT_DATA_DANCER_Y(BISON-OPTIONS)
671 # --------------------------------
672 # The following grammar, taken from Andrew Suffield's GPL'd implementation
673 # of DGMTP, the Dancer Generic Message Transport Protocol, used to violate
674 # yycheck's bounds where issuing a verbose error message. Keep this test
675 # so that possible bound checking compilers could check all the skeletons.
676 m4_define([_AT_DATA_DANCER_Y],
677 [AT_DATA_GRAMMAR([dancer.y],
678 [%{
679 AT_LALR1_CC_IF(
680 [static int yylex (int *lval);],
681 [#include <stdio.h>
682 static void yyerror (const char *s);
683 static int yylex ();])
684 %}
685 $1
686 %token ARROW INVALID NUMBER STRING DATA
687 %defines
688 %verbose
689 %error-verbose
690 /* Grammar follows */
691 %%
692 line: header body
693 ;
694
695 header: '<' from ARROW to '>' type ':'
696 | '<' ARROW to '>' type ':'
697 | ARROW to type ':'
698 | type ':'
699 | '<' '>'
700 ;
701
702 from: DATA
703 | STRING
704 | INVALID
705 ;
706
707 to: DATA
708 | STRING
709 | INVALID
710 ;
711
712 type: DATA
713 | STRING
714 | INVALID
715 ;
716
717 body: /* empty */
718 | body member
719 ;
720
721 member: STRING
722 | DATA
723 | '+' NUMBER
724 | '-' NUMBER
725 | NUMBER
726 | INVALID
727 ;
728 %%
729 AT_LALR1_CC_IF(
730 [/* Currently, print_ is required in C++. */
731 void
732 yy::Parser::print_ ()
733 {
734 }
735
736 /* A C++ error reporting function. */
737 void
738 yy::Parser::error_ ()
739 {
740 std::cerr << message << std::endl;
741 }
742
743 int
744 yyparse (void)
745 {
746 yy::Parser parser (!!YYDEBUG);
747 return parser.parse ();
748 }
749 ],
750 [static void
751 yyerror (const char *s)
752 {
753 fprintf (stderr, "%s\n", s);
754 }])
755
756 static int
757 yylex (AT_LALR1_CC_IF([int *lval]))
758 [{
759 static int toknum = 0;
760 int tokens[] =
761 {
762 ':', -1
763 };
764 return tokens[toknum++];
765 }]
766
767 int
768 main (void)
769 {
770 return yyparse ();
771 }
772 ])
773 ])# _AT_DATA_DANCER_Y
774
775
776 # AT_CHECK_DANCER(BISON-OPTIONS)
777 # ------------------------------
778 # Generate the grammar, compile it, run it.
779 m4_define([AT_CHECK_DANCER],
780 [AT_SETUP([Dancer $1])
781 AT_BISON_OPTION_PUSHDEFS([$1])
782 _AT_DATA_DANCER_Y([$1])
783 AT_CHECK([bison -o dancer.c dancer.y])
784 AT_LALR1_CC_IF([AT_COMPILE_CXX([dancer])],
785 [AT_COMPILE([dancer])])
786 AT_PARSER_CHECK([./dancer], 1, [],
787 [syntax error, unexpected ':'
788 ])
789 AT_BISON_OPTION_POPDEFS
790 AT_CLEANUP
791 ])
792
793 AT_CHECK_DANCER()
794 AT_CHECK_DANCER([%glr-parser])
795 AT_CHECK_DANCER([%skeleton "lalr1.cc"])