1 # Torturing Bison. -*- Autotest -*-
2 # Copyright (C) 2001, 2002 Free Software Foundation, Inc.
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)
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.
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
19 AT_BANNER([[Torture Tests.]])
22 ## ------------------------------------- ##
23 ## Creating a large artificial grammar. ##
24 ## ------------------------------------- ##
26 # AT_DATA_TRIANGULAR_GRAMMAR(FILE-NAME, SIZE)
27 # -------------------------------------------
28 # Create FILE-NAME, containing a self checking parser for a huge
30 m4_define([AT_DATA_TRIANGULAR_GRAMMAR],
31 [AT_DATA([[gengram.pl]],
35 my $max = $ARGV[0] || 10;
43 #define YYERROR_VERBOSE 1
46 static int yylex (void);
47 static void yyerror (const char *msg);
58 for my $size (1 .. $max)
60 print "%token t$size $size \"$size\"\n";
66 exp { assert (\@S|@1 == 0); \$\$ = \@S|@1; }
67 | input exp { assert (\@S|@2 == \@S|@1 + 1); \$\$ = \@S|@2; }
75 for my $size (1 .. $max)
78 print wrap ("| ", " ",
79 (map { "\"$_\"" } (1 .. $size)),
81 " { \$\$ = $size; }\n";
94 else if (inner > outer)
104 yyerror (const char *msg)
106 fprintf (stderr, "%s\\n", msg);
112 yydebug = !!getenv ("YYDEBUG");
118 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
127 AT_SETUP([Big triangle])
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 # With 500 and the new parser, which consume far too much memory,
132 # it gets killed too. Of course the parser is to be cleaned.
133 AT_DATA_TRIANGULAR_GRAMMAR([input.y], [200])
134 AT_CHECK([bison input.y -v -o input.c])
136 AT_PARSER_CHECK([./input])
142 # AT_DATA_HORIZONTAL_GRAMMAR(FILE-NAME, SIZE)
143 # -------------------------------------------
144 # Create FILE-NAME, containing a self checking parser for a huge
145 # horizontal grammar.
146 m4_define([AT_DATA_HORIZONTAL_GRAMMAR],
147 [AT_DATA([[gengram.pl]],
148 [[#! /usr/bin/perl -w
151 my $max = $ARGV[0] || 10;
159 #define YYERROR_VERBOSE 1
162 static int yylex (void);
163 static void yyerror (const char *msg);
167 for my $size (1 .. $max)
169 print "%token t$size $size \"$size\"\n";
179 (map { "\"$_\"" } (1 .. $max)), ";"),
187 static int counter = 1;
195 yyerror (const char *msg)
197 fprintf (stderr, "%s\\n", msg);
203 yydebug = !!getenv ("YYDEBUG");
209 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
214 ## ---------------- ##
215 ## Big horizontal. ##
216 ## ---------------- ##
218 AT_SETUP([Big horizontal])
220 # I have been able to go up to 10000 on my machine, but I had to
221 # increase the maximum stack size (* 100). It gave:
227 # gengram.pl 10000 0.70s user 0.01s sys 99% cpu 0.711 total
228 # bison input.y 730.56s user 0.53s sys 99% cpu 12:12.34 total
229 # gcc -Wall input.tab.c -o input 5.81s user 0.20s sys 100% cpu 6.01 total
230 # ./input 0.00s user 0.01s sys 108% cpu 0.01 total
232 AT_DATA_HORIZONTAL_GRAMMAR([input.y], [1000])
233 AT_CHECK([bison input.y -v -o input.c])
235 AT_PARSER_CHECK([./input])
241 # AT_DATA_LOOKAHEADS_GRAMMAR(FILE-NAME, SIZE)
242 # -------------------------------------------
243 # Create FILE-NAME, containing a self checking parser for a grammar
244 # requiring SIZE lookaheads.
245 m4_define([AT_DATA_LOOKAHEADS_GRAMMAR],
246 [AT_DATA([[gengram.pl]],
247 [[#! /usr/bin/perl -w
251 my $max = $ARGV[0] || 10;
259 #define YYERROR_VERBOSE 1
262 static int yylex (void);
263 static void yyerror (const char *msg);
270 %type <val> input exp
275 wrap ("%type <val> ",
277 map { "n$_" } (1 .. $max)),
280 for my $count (1 .. $max)
282 print "%token t$count $count \"$count\"\n";
288 exp { assert (\@S|@1 == 1); \$\$ = \@S|@1; }
289 | input exp { assert (\@S|@2 == \@S|@1 + 1); \$\$ = \@S|@2; }
293 n1 "1" { assert (\@S|@1 == 1); }
296 for my $count (2 .. $max)
298 print "| n$count \"$count\" { assert (\@S|@1 == $count); }\n";
302 for my $count (1 .. $max)
304 print "n$count: token { \$\$ = $count; };\n";
312 static int return_token = 1;
313 static int counter = 1;
326 yyerror (const char *msg)
328 fprintf (stderr, "%s\\n", msg);
334 yydebug = !!getenv ("YYDEBUG");
340 AT_CHECK([perl -w ./gengram.pl $2 || exit 77], 0, [stdout])
345 ## ----------------- ##
346 ## Many lookaheads. ##
347 ## ----------------- ##
349 AT_SETUP([Many lookaheads])
351 AT_DATA_LOOKAHEADS_GRAMMAR([input.y], [1000])
352 AT_CHECK([bison input.y -v -o input.c])
354 AT_PARSER_CHECK([./input])
360 # AT_DATA_STACK_TORTURE(C-PROLOGUE)
361 # ---------------------------------
362 # A parser specialized in torturing the stack size.
363 m4_define([AT_DATA_STACK_TORTURE],
364 [# A grammar of parens growing the stack thanks to right recursion.
372 static int yylex (void);
373 static void yyerror (const char *msg);
379 exp: WAIT_FOR_EOF exp | ;
382 yyerror (const char *msg)
384 fprintf (stderr, "%s\n", msg);
388 /* There are YYLVAL_MAX of WAIT_FOR_EOFs. */
389 unsigned int yylval_max;
401 main (int argc, const char **argv)
404 yylval = atoi (argv[1]);
409 AT_CHECK([bison input.y -o input.c])
414 ## -------------------------------------- ##
415 ## Exploding the Stack Size with Alloca. ##
416 ## -------------------------------------- ##
418 AT_SETUP([Exploding the Stack Size with Alloca])
420 AT_DATA_STACK_TORTURE
422 # Below the limit of 200.
423 AT_PARSER_CHECK([./input 20], 0, [], [ignore])
424 # Two enlargements: 2 * 2 * 200.
425 AT_PARSER_CHECK([./input 900], 0, [], [ignore])
426 # Fails: beyond the limit of 10,000 (which we don't reach anyway since we
427 # multiply by two starting at 200 => 5120 is the last possible).
428 AT_PARSER_CHECK([./input 10000], 1, [], [ignore])
435 ## -------------------------------------- ##
436 ## Exploding the Stack Size with Malloc. ##
437 ## -------------------------------------- ##
439 AT_SETUP([Exploding the Stack Size with Malloc])
441 AT_DATA_STACK_TORTURE([[#define YYSTACK_USE_ALLOCA 0]])
443 # Below the limit of 200.
444 AT_PARSER_CHECK([./input 20], 0, [], [ignore])
445 # Two enlargements: 2 * 2 * 200.
446 AT_PARSER_CHECK([./input 900], 0, [], [ignore])
447 # Fails: beyond the limit of 10,000 (which we don't reach anyway since we
448 # multiply by two starting at 200 => 5120 is the possible).
449 AT_PARSER_CHECK([./input 10000], 1, [], [ignore])