recursion - C++ Run-time Preempting Stack-Overflows -
i'm trying write library can parse end user files used adding simple user generated content project , want try , make library flexible possible. use recursion tokenisation process of 'objects' goes through chain of 4 functions conflicted how handle potential situation of end user going nested object happy. know can set hard limit on how many times program can recurse make flexible possible. there way can calculate (maximum - 1) number of times process can execute can preempt stack overflow error , return error or handled?
is there way can calculate (maximum - 1) number of times [recursive] process can execute ...
no. it's not guaranteed each stack frame in call chain same size.
transforming recursive implementation iterative 1 simple , well-defined: just
- replace implicit call stack (and function call operation) explicit state container, such
std::stack<state> - instead of recursive call, push new call's
stateonto stack, , continue loop - instead of being called, loop starts popping current
stateoff stack, , processes that, may require pushing newstateon there if have made recursive call
alternatively, can split current recursive descent parser tokenizer , lalr (or similar) parser, both iterative in first place. can generate these flex , bison (lex/yacc), example.
Comments
Post a Comment