How to set max recursion in boost spirit

好久不见. 提交于 2020-04-14 02:58:11

问题


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.


回答1:


Keep track of the depth in a qi::local<> or a phx::ref().

In this case an inherited attribute can take the role of the qi::local quite naturally:

qi::rule<std::string::const_iterator, std::string(size_t depth)> term;
qi::_r1_type _depth;
term %= 
    qi::eps(_depth < 32) >>
    qi::string("(") >> *term(_depth + 1) >> qi::string(")");

term will now fail when depth exceeds 32.

Full Sample

Live On Coliru

#include <iostream>
#include <string>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
namespace qi = boost::spirit::qi;

int main(void) {
    for (size_t n : { 2, 4, 8, 16, 32, 64 }) {
        auto const str = [&n] {
            std::string str;
            str.reserve(n);
            while (n--) { str.insert(str.begin(), '('); str.append(1, ')'); }
            return str;
        }();
        std::cout << "Input length " << str.length() << "\n";

        qi::rule<std::string::const_iterator, std::string(size_t depth)> term;
        qi::_r1_type _depth;
        term %= 
            qi::eps(_depth < 32) >>
            qi::string("(") >> *term(_depth + 1) >> qi::string(")");

        std::string h;

        auto f = str.begin(), l = str.end();
        bool ok = qi::parse(f, l, term(0u), h);
        if (ok)
            std::cout << "Ok: " << h << "\n";
        else
            std::cout << "Fail\n";

        if (f != l)
            std::cout << "Remaining  unparsed: '" << std::string(f, std::min(f + 40, l)) << "'...\n";
    }
}

Output:

Input length 4
Ok: (())
Input length 8
Ok: (((())))
Input length 16
Ok: (((((((())))))))
Input length 32
Ok: (((((((((((((((())))))))))))))))
Input length 64
Ok: (((((((((((((((((((((((((((((((())))))))))))))))))))))))))))))))
Input length 128
Fail
Remaining  unparsed: '(((((((((((((((((((((((((((((((((((((((('...


来源:https://stackoverflow.com/questions/40325709/how-to-set-max-recursion-in-boost-spirit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!