06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5: The Bison Parser Algorithm 81<br />

5.9 Memory Management, and How to Avoid Memory<br />

Exhaustion<br />

The Bison parser stack can run out of memory if too many tokens are shifted and not<br />

reduced. When this happens, the parser function yyparse calls yyerror and then returns<br />

2.<br />

Because Bison parsers have growing stacks, hitting the upper limit usually results from<br />

using a right recursion instead of a left recursion, See Section 3.4 [Recursive Rules], page 45.<br />

By defining the macro YYMAXDEPTH, you can control how deep the parser stack can<br />

become before memory is exhausted. Define the macro with a value that is an integer.<br />

This value is the maximum number of tokens that can be shifted (and not reduced) before<br />

overflow.<br />

The stack space allowed is not necessarily allocated. If you specify a large value for<br />

YYMAXDEPTH, the parser normally allocates a small stack at first, and then makes it bigger by<br />

stages as needed. This increasing allocation happens automatically and silently. Therefore,<br />

you do not need to make YYMAXDEPTH painfully small merely to save space for ordinary<br />

inputs that do not need much stack.<br />

However, do not allow YYMAXDEPTH to be a value so large that arithmetic overflow could<br />

occur when calculating the size of the stack space. Also, do not allow YYMAXDEPTH to be<br />

less than YYINITDEPTH.<br />

The default value of YYMAXDEPTH, if you do not define it, is 10000.<br />

You can control how much stack is allocated initially by defining the macro YYINITDEPTH<br />

to a positive integer. For the C LALR(1) parser, this value must be a compile-time constant<br />

unless you are assuming C99 or some other target language or compiler that allows variablelength<br />

arrays. The default is 200.<br />

Do not allow YYINITDEPTH to be greater than YYMAXDEPTH.<br />

Because of semantical differences between C and C++, the LALR(1) parsers in C produced<br />

by Bison cannot grow when compiled by C++ compilers. In this precise case (compiling a<br />

C parser as C++) you are suggested to grow YYINITDEPTH. The Bison maintainers hope to<br />

fix this deficiency in a future release.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!