]> git.saurik.com Git - bison.git/blob - tests/torture.at
* src/symtab.c, src/symtab.c (symbol_class_set)
[bison.git] / tests / torture.at
1 # Torturing Bison. -*- Autotest -*-
2 # Copyright (C) 2001, 2002 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([[Torture Tests.]])
20
21
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 m4_define([AT_DATA_TRIANGULAR_GRAMMAR],
31 [AT_DATA([[gengram.pl]],
32 [[#! /usr/bin/perl -w
33
34 use strict;
35 my $max = $ARGV[0] || 10;
36
37 print <<EOF;
38 %{
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <assert.h>
42
43 #define YYERROR_VERBOSE 1
44 #define YYDEBUG 1
45
46 static int yylex (void);
47 static void yyerror (const char *msg);
48 %}
49 %union
50 {
51 int val;
52 };
53
54 %token END "end"
55 %type <val> exp input
56 EOF
57
58 for my $size (1 .. $max)
59 {
60 print "%token \"$size\" ", $size, "\n";
61 };
62
63 print <<EOF;
64 %%
65 input:
66 exp { assert (\@S|@1 == 0); \$\$ = \@S|@1; }
67 | input exp { assert (\@S|@2 == \@S|@1 + 1); \$\$ = \@S|@2; }
68 ;
69
70 exp:
71 END
72 { \$\$ = 0; }
73 EOF
74
75 for my $size (1 .. $max)
76 {
77 use Text::Wrap;
78 print wrap ("| ", " ",
79 (map { "\"$_\"" } (1 .. $size)),
80 " END \n"),
81 " { \$\$ = $size; }\n";
82 };
83 print ";\n";
84
85 print <<EOF;
86 %%
87 static int
88 yylex (void)
89 {
90 static int inner = 1;
91 static int outer = 0;
92 if (outer > $max)
93 return 0;
94 else if (inner > outer)
95 {
96 inner = 1;
97 ++outer;
98 return END;
99 }
100 return inner++;
101 }
102
103 static void
104 yyerror (const char *msg)
105 {
106 fprintf (stderr, "%s\\n", msg);
107 }
108
109 int
110 main (void)
111 {
112 yydebug = !!getenv ("YYDEBUG");
113 return yyparse ();
114 }
115 EOF
116 ]])
117
118 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
119 mv stdout $1
120 ])
121
122
123 ## -------------- ##
124 ## Big triangle. ##
125 ## -------------- ##
126
127 AT_SETUP([Big triangle])
128
129 # I have been able to go up to 2000 on my machine.
130 # I tried 3000, a 29Mb grammar file, but then my system killed bison.
131 AT_DATA_TRIANGULAR_GRAMMAR([input.y], [500])
132 AT_CHECK([bison input.y -v -o input.c])
133 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
134 AT_CHECK([./input])
135
136 AT_CLEANUP
137
138
139
140 # AT_DATA_HORIZONTAL_GRAMMAR(FILE-NAME, SIZE)
141 # -------------------------------------------
142 # Create FILE-NAME, containing a self checking parser for a huge
143 # horizontal grammar.
144 m4_define([AT_DATA_HORIZONTAL_GRAMMAR],
145 [AT_DATA([[gengram.pl]],
146 [[#! /usr/bin/perl -w
147
148 use strict;
149 my $max = $ARGV[0] || 10;
150
151 print <<EOF;
152 %{
153 #include <stdio.h>
154 #include <stdlib.h>
155 #include <assert.h>
156
157 #define YYERROR_VERBOSE 1
158 #define YYDEBUG 1
159
160 static int yylex (void);
161 static void yyerror (const char *msg);
162 %}
163 EOF
164
165 for my $size (1 .. $max)
166 {
167 print "%token \"$size\" ", $size, "\n";
168 };
169
170 print <<EOF;
171 %%
172 EOF
173
174 use Text::Wrap;
175 print
176 wrap ("exp: ", " ",
177 (map { "\"$_\"" } (1 .. $max)), ";"),
178 "\n";
179
180 print <<EOF;
181 %%
182 static int
183 yylex (void)
184 {
185 static int counter = 1;
186 if (counter > $max)
187 return 0;
188 else
189 return counter++;
190 }
191
192 static void
193 yyerror (const char *msg)
194 {
195 fprintf (stderr, "%s\\n", msg);
196 }
197
198 int
199 main (void)
200 {
201 yydebug = !!getenv ("YYDEBUG");
202 return yyparse ();
203 }
204 EOF
205 ]])
206
207 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
208 mv stdout $1
209 ])
210
211
212 ## ---------------- ##
213 ## Big horizontal. ##
214 ## ---------------- ##
215
216 AT_SETUP([Big horizontal])
217
218 # I have been able to go up to 10000 on my machine, but I had to
219 # increase the maximum stack size (* 100). It gave:
220 #
221 # input.y 263k
222 # input.tab.c 1.3M
223 # input 453k
224 #
225 # gengram.pl 10000 0.70s user 0.01s sys 99% cpu 0.711 total
226 # bison input.y 730.56s user 0.53s sys 99% cpu 12:12.34 total
227 # gcc -Wall input.tab.c -o input 5.81s user 0.20s sys 100% cpu 6.01 total
228 # ./input 0.00s user 0.01s sys 108% cpu 0.01 total
229 #
230 AT_DATA_HORIZONTAL_GRAMMAR([input.y], [1000])
231 AT_CHECK([bison input.y -v -o input.c])
232 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
233 AT_CHECK([./input])
234
235 AT_CLEANUP
236
237
238
239 # AT_DATA_LOOKAHEADS_GRAMMAR(FILE-NAME, SIZE)
240 # -------------------------------------------
241 # Create FILE-NAME, containing a self checking parser for a grammar
242 # requiring SIZE lookaheads.
243 m4_define([AT_DATA_LOOKAHEADS_GRAMMAR],
244 [AT_DATA([[gengram.pl]],
245 [[#! /usr/bin/perl -w
246
247 use strict;
248 use Text::Wrap;
249 my $max = $ARGV[0] || 10;
250
251 print <<EOF;
252 %{
253 #include <stdio.h>
254 #include <stdlib.h>
255 #include <assert.h>
256
257 #define YYERROR_VERBOSE 1
258 #define YYDEBUG 1
259
260 static int yylex (void);
261 static void yyerror (const char *msg);
262 %}
263 %union
264 {
265 int val;
266 };
267
268 %type <val> input exp
269 %token token
270 EOF
271
272 print
273 wrap ("%type <val> ",
274 " ",
275 map { "token$_" } (1 .. $max)),
276 "\n";
277
278 for my $count (1 .. $max)
279 {
280 print "%token \"$count\" $count\n";
281 };
282
283 print <<EOF;
284 %%
285 input:
286 exp { assert (\@S|@1 == 1); \$\$ = \@S|@1; }
287 | input exp { assert (\@S|@2 == \@S|@1 + 1); \$\$ = \@S|@2; }
288 ;
289
290 exp:
291 token1 "1" { assert (\@S|@1 == 1); }
292 EOF
293
294 for my $count (2 .. $max)
295 {
296 print "| token$count \"$count\" { assert (\@S|@1 == $count); }\n";
297 };
298 print ";\n";
299
300 for my $count (1 .. $max)
301 {
302 print "token$count: token { \$\$ = $count; };\n";
303 };
304
305 print <<EOF;
306 %%
307 static int
308 yylex (void)
309 {
310 static int return_token = 1;
311 static int counter = 1;
312 if (counter > $max)
313 return 0;
314 if (return_token)
315 {
316 return_token = 0;
317 return token;
318 }
319 return_token = 1;
320 return counter++;
321 }
322
323 static void
324 yyerror (const char *msg)
325 {
326 fprintf (stderr, "%s\\n", msg);
327 }
328
329 int
330 main (void)
331 {
332 yydebug = !!getenv ("YYDEBUG");
333 return yyparse ();
334 }
335 EOF
336 ]])
337
338 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
339 mv stdout $1
340 ])
341
342
343 ## ----------------- ##
344 ## Many lookaheads. ##
345 ## ----------------- ##
346
347 AT_SETUP([Many lookaheads])
348
349 AT_DATA_LOOKAHEADS_GRAMMAR([input.y], [1000])
350 AT_CHECK([bison input.y -v -o input.c])
351 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
352 AT_CHECK([./input])
353
354 AT_CLEANUP
355
356
357
358 # AT_DATA_STACK_TORTURE(C-PROLOGUE)
359 # ---------------------------------
360 # A parser specialized in torturing the stack size.
361 m4_define([AT_DATA_STACK_TORTURE],
362 [# A grammar of parens growing the stack thanks to right recursion.
363 # exp:
364 AT_DATA([input.y],
365 [[%{
366 #include <stdio.h>
367 #include <stdlib.h>
368 #include <assert.h>
369 ]$1[
370 static int yylex (void);
371 static void yyerror (const char *msg);
372 #define YYPRINT(File, Type, Value) \
373 fprintf (File, " (%d, stack size = %d, max = %d)", \
374 Value, yyssp - yyss + 1, yystacksize);
375 %}
376 %error-verbose
377 %debug
378 %token WAIT_FOR_EOF
379 %%
380 exp: WAIT_FOR_EOF exp | ;
381 %%
382 static void
383 yyerror (const char *msg)
384 {
385 fprintf (stderr, "%s\n", msg);
386 exit (1);
387 }
388
389 /* There are YYLVAL_MAX of WAIT_FOR_EOFs. */
390 unsigned int yylval_max;
391
392 static int
393 yylex (void)
394 {
395 if (yylval--)
396 return WAIT_FOR_EOF;
397 else
398 return EOF;
399 }
400
401 int
402 main (int argc, const char **argv)
403 {
404 assert (argc == 2);
405 yylval = atoi (argv[1]);
406 yydebug = 1;
407 return yyparse ();
408 }
409 ]])
410 AT_CHECK([bison input.y -o input.c])
411 AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
412 ])
413
414
415 ## -------------------------------------- ##
416 ## Exploding the Stack Size with Alloca. ##
417 ## -------------------------------------- ##
418
419 AT_SETUP([Exploding the Stack Size with Alloca])
420
421 AT_DATA_STACK_TORTURE
422
423 # Below the limit of 200.
424 AT_CHECK([./input 20], 0, [], [ignore])
425 # Two enlargements: 2 * 2 * 200.
426 AT_CHECK([./input 900], 0, [], [ignore])
427 # Fails: beyond the limit of 10,000 (which we don't reach anyway since we
428 # multiply by two starting at 200 => 5120 is the last possible).
429 AT_CHECK([./input 10000], 1, [], [ignore])
430
431 AT_CLEANUP
432
433
434
435
436 ## -------------------------------------- ##
437 ## Exploding the Stack Size with Malloc. ##
438 ## -------------------------------------- ##
439
440 AT_SETUP([Exploding the Stack Size with Malloc])
441
442 AT_DATA_STACK_TORTURE([[#define YYSTACK_USE_ALLOCA 0]])
443
444 # Below the limit of 200.
445 AT_CHECK([./input 20], 0, [], [ignore])
446 # Two enlargements: 2 * 2 * 200.
447 AT_CHECK([./input 900], 0, [], [ignore])
448 # Fails: beyond the limit of 10,000 (which we don't reach anyway since we
449 # multiply by two starting at 200 => 5120 is the possible).
450 AT_CHECK([./input 10000], 1, [], [ignore])
451
452 AT_CLEANUP