Using boost::spirit, if I have a recursive rule to parse parentheses
rule<std::string::iterator, std::string()> term;
term %= string("(") >> *term >> string(")");
how do I limit the maximum amount of recursion? For example, if I try to parse a million nested parentheses, I get a segfault because the stack size has been exceeded. To be concrete, here is a complete sample.
#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
int main(void)
{
using namespace boost::spirit;
using namespace boost::spirit::qi;
const size_t string_size = 1000000;
std::string str;
str.resize(string_size);
for (size_t s=0; s<str.size()/2; ++s)
{
str[s]='(';
str[str.size() - s -1] = ')';
}
rule<std::string::iterator, std::string()> term;
term %= string("(") >> *term >> string(")");
std::string h;
parse(str.begin(), str.end(), term, h);
}
I compiled it with the command
g++ simple.cxx -o simple -std=c++11
It works fine if I set string_size
to 1000 instead of 1000000.