使用Boost.Spirit解析嵌套列表

时间:2017-08-23 21:06:52

标签: c++ boost-spirit

我试图用Boost.Spirit解析嵌套的数字列表。这就是我到目前为止所做的:

//define a test string
std::string string = "[[\t1 , 2 ], [3, 4, 65.4]]";
auto it = string.begin();

//parse the string
std::vector<std::vector<double>> vector;
auto listRule = "[" >> (qi::double_ % ",") >> "]";
auto list2Rule = "[" >> (listRule % ",") >> "]";
bool match = qi::phrase_parse(it, string.end(), list2Rule, ascii::space, vector);

//check if we have a match
std::cout << "matched: " << std::boolalpha << match << '\n';
if (it != string.end())
    std::cout << "unmatched part: " << std::string{it, string.end()} << '\n';

//print the result
std::cout << "result\n";
for (const auto& v : vector) {
    std::cout << "[";
    for (double i : v)
        std::cout << i << ",";
    std::cout << "]\n";
}

以上作品精彩并打印出来:

matched: true
result
[1,2,]
[3,4,65.4,]

我面临的问题是它不接受列表。例如,通过更改字符串如下:

std::string string = "[[\t1 , 2 ], [3, 4, 65.4], []]";

然后我没有匹配(即match == falseit == string.begin())。显然,矢量仍会填充,但最后一个空列表丢失了。任何人都可以解释为什么会出现这种情况,以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

您在Qi域中使用auto用于原型表达式模板 - 99.9%的时间是未定义的行为:

现在,在你修复它的同时,还要使列表主体可选:

<强> Live On Coliru

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

int main() {
    using It      = std::string::const_iterator;
    using Skipper = qi::space_type;

    for(std::string const input : { "[[\t1 , 2 ], [3, 4, 65.4]]", "[[\t1 , 2 ], [3, 4, 65.4], []]", "[]" })
    {
        std::cout << " ---- '" << input << "' ----\n";
        auto it = input.begin();

        //parse the string
        using doubles = std::vector<double>;
        using vectors = std::vector<doubles>;

        qi::rule<It, doubles(), Skipper> doubles_ = "[" >> -(qi::double_ % ",") >> "]";
        qi::rule<It, vectors(), Skipper> vectors_ = "[" >> -(doubles_    % ",") >> "]";

        vectors data;
        bool match = qi::phrase_parse(it, input.end(), vectors_, qi::space, data);

        //check if we have a match
        std::cout << "matched: " << std::boolalpha << match << '\n';
        if (it != input.end())
            std::cout << "unmatched part: " << std::string{it, input.end()} << '\n';

        //print the result
        std::cout << "result\n";
        for (const auto& v : data) {
            std::cout << "[";
            for (double i : v)
                std::cout << i << ",";
            std::cout << "]\n";
        }
    }
}

打印

 ---- '[[   1 , 2 ], [3, 4, 65.4]]' ----
matched: true
result
[1,2,]
[3,4,65.4,]
 ---- '[[   1 , 2 ], [3, 4, 65.4], []]' ----
matched: true
result
[1,2,]
[3,4,65.4,]
[]
 ---- '[]' ----
matched: true
result