]> git.saurik.com Git - bison.git/blame - tests/torture.at
Propagate more token_number_t.
[bison.git] / tests / torture.at
CommitLineData
6d7d248e 1# Torturing Bison. -*- Autotest -*-
817e9f41 2# Copyright (C) 2001, 2002 Free Software Foundation, Inc.
6d7d248e
AD
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
19AT_BANNER([[Torture Tests.]])
20
21
817e9f41
AD
22## ------------------------------------- ##
23## Creating a large artificial grammar. ##
24## ------------------------------------- ##
25
26# AT_DATA_TRIANGULAR_GRAMMAR(FILE-NAME, SIZE)
27# -------------------------------------------
28# Create FILE-NAME, containing a self checking parser for a huge
29# triangular grammar.
30# FIXME: The `10 *' below are there to avoid clashes with predefined
31# tokens. These clashes should be exercised, I'm afraid something
32# is broken wrt previous Bisons.
33m4_define([AT_DATA_TRIANGULAR_GRAMMAR],
34[AT_DATA([[gengram.pl]],
35[[#! /usr/bin/perl -w
36
37use strict;
38my $max = $ARGV[0] || 10;
39
40print <<EOF;
41%{
42#include <stdio.h>
43#include <stdlib.h>
44#include <assert.h>
45
46#define YYERROR_VERBOSE 1
47#define YYDEBUG 1
48
49static int yylex (void);
50static void yyerror (const char *msg);
51%}
52%union
53{
54 int val;
55};
56
57%token END "end"
58%type <val> exp input
59EOF
60
61for my $size (1 .. $max)
62 {
63 print "%token \"$size\" ", $size * 10, "\n";
64 };
65
66print <<EOF;
67%%
68input:
69 exp { assert (\@S|@1 == 0); \$\$ = \@S|@1; }
70| input exp { assert (\@S|@2 == \@S|@1 + 1); \$\$ = \@S|@2; }
71;
72
73exp:
74 END
75 { \$\$ = 0; }
76EOF
77
78for my $size (1 .. $max)
79 {
80 use Text::Wrap;
81 print wrap ("| ", " ",
82 (map { "\"$_\"" } (1 .. $size)),
83 " END \n"),
84 " { \$\$ = $size; }\n";
85 };
86print ";\n";
87
88print <<EOF;
89%%
90static int
91yylex (void)
92{
93 static int inner = 1;
94 static int outer = 0;
95 if (outer > $max)
96 return 0;
97 else if (inner > outer)
98 {
99 inner = 1;
100 ++outer;
101 return END;
102 }
103 return inner++ * 10;
104}
105
106static void
107yyerror (const char *msg)
108{
109 fprintf (stderr, "%s\\n", msg);
110}
111
112int
113main (void)
114{
115 yydebug = !!getenv ("YYDEBUG");
116 return yyparse ();
117}
118EOF
119]])
120
121AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
122mv stdout $1
123])
124
125
126## -------------- ##
127## Big triangle. ##
128## -------------- ##
129
817e9f41
AD
130AT_SETUP([Big triangle])
131
355e7c1c
AD
132# I have been able to go up to 2000 on my machine.
133# I tried 3000, a 29Mb grammar file, but then my system killed bison.
134AT_DATA_TRIANGULAR_GRAMMAR([input.y], [500])
817e9f41
AD
135AT_CHECK([bison input.y -v -o input.c])
136AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
137AT_CHECK([./input])
138
139AT_CLEANUP
140
141
142
62a3e4f0
AD
143# AT_DATA_HORIZONTAL_GRAMMAR(FILE-NAME, SIZE)
144# -------------------------------------------
145# Create FILE-NAME, containing a self checking parser for a huge
146# horizontal grammar.
147# FIXME: The `10 *' below are there to avoid clashes with predefined
148# tokens. These clashes should be exercised, I'm afraid something
149# is broken wrt previous Bisons.
150m4_define([AT_DATA_HORIZONTAL_GRAMMAR],
151[AT_DATA([[gengram.pl]],
152[[#! /usr/bin/perl -w
153
154use strict;
155my $max = $ARGV[0] || 10;
156
157print <<EOF;
158%{
159#include <stdio.h>
160#include <stdlib.h>
161#include <assert.h>
162
163#define YYERROR_VERBOSE 1
164#define YYDEBUG 1
165
166static int yylex (void);
167static void yyerror (const char *msg);
168%}
169EOF
170
171for my $size (1 .. $max)
172 {
173 print "%token \"$size\" ", $size * 10, "\n";
174 };
175
176print <<EOF;
177%%
178EOF
179
180use Text::Wrap;
181print
182 wrap ("exp: ", " ",
183 (map { "\"$_\"" } (1 .. $max)), ";"),
184 "\n";
185
186print <<EOF;
187%%
188static int
189yylex (void)
190{
191 static int counter = 1;
192 if (counter > $max)
193 return 0;
194 else
195 return counter++ * 10;
196}
197
198static void
199yyerror (const char *msg)
200{
201 fprintf (stderr, "%s\\n", msg);
202}
203
204int
205main (void)
206{
207 yydebug = !!getenv ("YYDEBUG");
208 return yyparse ();
209}
210EOF
211]])
212
213AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
214mv stdout $1
215])
216
217
218## ---------------- ##
219## Big horizontal. ##
220## ---------------- ##
221
222AT_SETUP([Big horizontal])
223
224# I have been able to go up to 10000 on my machine, but I had to
225# increase the maximum stack size (* 100). It gave:
226#
227# input.y 263k
228# input.tab.c 1.3M
229# input 453k
230#
231# gengram.pl 10000 0.70s user 0.01s sys 99% cpu 0.711 total
232# bison input.y 730.56s user 0.53s sys 99% cpu 12:12.34 total
233# gcc -Wall input.tab.c -o input 5.81s user 0.20s sys 100% cpu 6.01 total
234# ./input 0.00s user 0.01s sys 108% cpu 0.01 total
235#
236AT_DATA_HORIZONTAL_GRAMMAR([input.y], [1000])
237AT_CHECK([bison input.y -v -o input.c])
238AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
239AT_CHECK([./input])
240
241AT_CLEANUP
242
243
244
6d7d248e
AD
245# AT_DATA_STACK_TORTURE(C-PROLOGUE)
246# ---------------------------------
247# A parser specialized in torturing the stack size.
248m4_define([AT_DATA_STACK_TORTURE],
249[# A grammar of parens growing the stack thanks to right recursion.
250# exp:
251AT_DATA([input.y],
252[[%{
253#include <stdio.h>
254#include <stdlib.h>
255#include <assert.h>
256]$1[
257 static int yylex (void);
258 static void yyerror (const char *msg);
6d7d248e
AD
259#define YYPRINT(File, Type, Value) \
260 fprintf (File, " (%d, stack size = %d, max = %d)", \
261 Value, yyssp - yyss + 1, yystacksize);
262%}
04d843a2 263%error-verbose
6d7d248e
AD
264%debug
265%token WAIT_FOR_EOF
266%%
267exp: WAIT_FOR_EOF exp | ;
268%%
269static void
270yyerror (const char *msg)
271{
272 fprintf (stderr, "%s\n", msg);
273 exit (1);
274}
275
276/* There are YYLVAL_MAX of WAIT_FOR_EOFs. */
277unsigned int yylval_max;
278
279static int
280yylex (void)
281{
282 if (yylval--)
283 return WAIT_FOR_EOF;
284 else
285 return EOF;
286}
287
288int
289main (int argc, const char **argv)
290{
291 assert (argc == 2);
292 yylval = atoi (argv[1]);
293 yydebug = 1;
294 return yyparse ();
295}
296]])
297AT_CHECK([bison input.y -o input.c])
298AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
6d7d248e
AD
299])
300
301
302## -------------------------------------- ##
303## Exploding the Stack Size with Alloca. ##
304## -------------------------------------- ##
305
306AT_SETUP([Exploding the Stack Size with Alloca])
307
308AT_DATA_STACK_TORTURE
309
310# Below the limit of 200.
30d2f3d5 311AT_CHECK([./input 20], 0, [], [ignore])
6d7d248e 312# Two enlargements: 2 * 2 * 200.
30d2f3d5 313AT_CHECK([./input 900], 0, [], [ignore])
6d7d248e
AD
314# Fails: beyond the limit of 10,000 (which we don't reach anyway since we
315# multiply by two starting at 200 => 5120 is the last possible).
30d2f3d5 316AT_CHECK([./input 10000], 1, [], [ignore])
6d7d248e
AD
317
318AT_CLEANUP
319
320
321
322
323## -------------------------------------- ##
324## Exploding the Stack Size with Malloc. ##
325## -------------------------------------- ##
326
327AT_SETUP([Exploding the Stack Size with Malloc])
328
000f1a3c 329AT_DATA_STACK_TORTURE([[#define YYSTACK_USE_ALLOCA 0]])
6d7d248e
AD
330
331# Below the limit of 200.
30d2f3d5 332AT_CHECK([./input 20], 0, [], [ignore])
6d7d248e 333# Two enlargements: 2 * 2 * 200.
30d2f3d5 334AT_CHECK([./input 900], 0, [], [ignore])
6d7d248e
AD
335# Fails: beyond the limit of 10,000 (which we don't reach anyway since we
336# multiply by two starting at 200 => 5120 is the possible).
30d2f3d5 337AT_CHECK([./input 10000], 1, [], [ignore])
6d7d248e
AD
338
339AT_CLEANUP