]> git.saurik.com Git - bison.git/blame - tests/torture.at
* src/gram.h (item_number_t): New, the type of item numbers in
[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
6d7d248e
AD
143# AT_DATA_STACK_TORTURE(C-PROLOGUE)
144# ---------------------------------
145# A parser specialized in torturing the stack size.
146m4_define([AT_DATA_STACK_TORTURE],
147[# A grammar of parens growing the stack thanks to right recursion.
148# exp:
149AT_DATA([input.y],
150[[%{
151#include <stdio.h>
152#include <stdlib.h>
153#include <assert.h>
154]$1[
155 static int yylex (void);
156 static void yyerror (const char *msg);
6d7d248e
AD
157#define YYPRINT(File, Type, Value) \
158 fprintf (File, " (%d, stack size = %d, max = %d)", \
159 Value, yyssp - yyss + 1, yystacksize);
160%}
04d843a2 161%error-verbose
6d7d248e
AD
162%debug
163%token WAIT_FOR_EOF
164%%
165exp: WAIT_FOR_EOF exp | ;
166%%
167static void
168yyerror (const char *msg)
169{
170 fprintf (stderr, "%s\n", msg);
171 exit (1);
172}
173
174/* There are YYLVAL_MAX of WAIT_FOR_EOFs. */
175unsigned int yylval_max;
176
177static int
178yylex (void)
179{
180 if (yylval--)
181 return WAIT_FOR_EOF;
182 else
183 return EOF;
184}
185
186int
187main (int argc, const char **argv)
188{
189 assert (argc == 2);
190 yylval = atoi (argv[1]);
191 yydebug = 1;
192 return yyparse ();
193}
194]])
195AT_CHECK([bison input.y -o input.c])
196AT_CHECK([$CC $CFLAGS $CPPFLAGS input.c -o input], 0, [], [ignore])
6d7d248e
AD
197])
198
199
200## -------------------------------------- ##
201## Exploding the Stack Size with Alloca. ##
202## -------------------------------------- ##
203
204AT_SETUP([Exploding the Stack Size with Alloca])
205
206AT_DATA_STACK_TORTURE
207
208# Below the limit of 200.
30d2f3d5 209AT_CHECK([./input 20], 0, [], [ignore])
6d7d248e 210# Two enlargements: 2 * 2 * 200.
30d2f3d5 211AT_CHECK([./input 900], 0, [], [ignore])
6d7d248e
AD
212# Fails: beyond the limit of 10,000 (which we don't reach anyway since we
213# multiply by two starting at 200 => 5120 is the last possible).
30d2f3d5 214AT_CHECK([./input 10000], 1, [], [ignore])
6d7d248e
AD
215
216AT_CLEANUP
217
218
219
220
221## -------------------------------------- ##
222## Exploding the Stack Size with Malloc. ##
223## -------------------------------------- ##
224
225AT_SETUP([Exploding the Stack Size with Malloc])
226
000f1a3c 227AT_DATA_STACK_TORTURE([[#define YYSTACK_USE_ALLOCA 0]])
6d7d248e
AD
228
229# Below the limit of 200.
30d2f3d5 230AT_CHECK([./input 20], 0, [], [ignore])
6d7d248e 231# Two enlargements: 2 * 2 * 200.
30d2f3d5 232AT_CHECK([./input 900], 0, [], [ignore])
6d7d248e
AD
233# Fails: beyond the limit of 10,000 (which we don't reach anyway since we
234# multiply by two starting at 200 => 5120 is the possible).
30d2f3d5 235AT_CHECK([./input 10000], 1, [], [ignore])
6d7d248e
AD
236
237AT_CLEANUP