Previous: , Up: The Bison Parser Algorithm   [Contents][Index]


5.10 Memory Management, and How to Avoid Memory Exhaustion

The Bison parser stack can run out of memory if too many tokens are shifted and not reduced. When this happens, the parser function yyparse calls yyerror and then returns 2.

Because Bison parsers have growing stacks, hitting the upper limit usually results from using a right recursion instead of a left recursion, see Recursive Rules.

By defining the macro YYMAXDEPTH, you can control how deep the parser stack can become before memory is exhausted. Define the macro with a value that is an integer. This value is the maximum number of tokens that can be shifted (and not reduced) before overflow.

The stack space allowed is not necessarily allocated. If you specify a large value for YYMAXDEPTH, the parser normally allocates a small stack at first, and then makes it bigger by stages as needed. This increasing allocation happens automatically and silently. Therefore, you do not need to make YYMAXDEPTH painfully small merely to save space for ordinary inputs that do not need much stack.

However, do not allow YYMAXDEPTH to be a value so large that arithmetic overflow could occur when calculating the size of the stack space. Also, do not allow YYMAXDEPTH to be less than YYINITDEPTH.

The default value of YYMAXDEPTH, if you do not define it, is 10000.

You can control how much stack is allocated initially by defining the macro YYINITDEPTH to a positive integer. For the deterministic parser in C, this value must be a compile-time constant unless you are assuming C99 or some other target language or compiler that allows variable-length arrays. The default is 200.

Do not allow YYINITDEPTH to be greater than YYMAXDEPTH.

You can generate a deterministic parser containing C++ user code from the default (C) skeleton, as well as from the C++ skeleton (see C++ Parsers). However, if you do use the default skeleton and want to allow the parsing stack to grow, be careful not to use semantic types or location types that require non-trivial copy constructors. The C skeleton bypasses these constructors when copying data to new, larger stacks.


Previous: Generalized LR (GLR) Parsing, Up: The Bison Parser Algorithm   [Contents][Index]