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