c++ - 如何在 boost::spirit::qi 中将某些语义 Action 排除在 AST 之外

标签 c++ boost-spirit boost-spirit-qi

我尝试使用 boost::spirit::qi 解析大量文件。解析不是问题,但有些文件包含我想跳过的噪音。构建一个简单的解析器(不使用 boost::spirit::qi)验证我可以通过跳过行首不匹配规则的任何内容来避免噪音。因此,我正在寻找一种方法来编写基于行的解析器,在不匹配任何规则时跳过行。

下面的示例允许语法在完全不匹配的情况下跳过行,但是“垃圾”规则仍然插入一个空的 V() 实例,这是不需要的行为。 在示例中使用\r 而不是\n 是有意的,因为我在文件中同时遇到了\n、\r 和\r\n。

#include <iostream>
#include <string>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/std_tuple.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phx = boost::phoenix;

using V = std::tuple<std::string, double, double, double>;

namespace client {
    template <typename Iterator>
    struct VGrammar : qi::grammar<Iterator, std::vector<V>(), ascii::space_type> {
        VGrammar() : VGrammar::base_type(start) {
            using namespace qi;

            v %= string("v") > double_ > double_ > double_;
            junk = +(char_ - eol);
            start %= +(v | junk);

            v.name("v");
            junk.name("junk");
            start.name("start");

            using phx::val;
            using phx::construct;

            on_error<fail>(
                start,
                std::cout
                    << val("Error! Expecting \n\n'")
                    << qi::_4
                    << val("'\n\n here: \n\n'")
                    << construct<std::string>(qi::_3, qi::_2)
                    << val("'")
                    << std::endl
            );

            //debug(v);
            //debug(junk);
            //debug(start);
        }

        qi::rule<Iterator> junk;
        //qi::rule<Iterator, qi::unused_type()> junk; // Doesn't work either
        //qi::rule<Iterator, qi::unused_type(), qi::unused_type()> junk; // Doesn't work either
        qi::rule<Iterator, V(), ascii::space_type> v;
        qi::rule<Iterator, std::vector<V>(), ascii::space_type> start;
    };
} // namespace client

int main(int argc, char* argv[]) {
    using iterator_type = std::string::const_iterator;

    std::string input = "";
    input += "v 1 2 3\r";         // keep v 1 2 3
    input += "o a b c\r";         // parse as junk
    input += "v 4 5 6 v 7 8 9\r"; // keep v 4 5 6, but parse v 7 8 9 as junk
    input += "   v 10 11 12\r\r"; // parse as junk

    iterator_type iter = input.begin();
    const iterator_type end = input.end();
    std::vector<V> parsed_output;
    client::VGrammar<iterator_type> v_grammar;

    std::cout << "run" << std::endl;
    bool r = phrase_parse(iter, end, v_grammar, ascii::space, parsed_output);
    std::cout << "done ... r: " << (r ? "true" : "false") << ", iter==end: " << ((iter == end) ? "true" : "false") << std::endl;

    if (r && (iter == end)) {
        BOOST_FOREACH(V const& v_row, parsed_output) {
            std::cout << std::get<0>(v_row) << ", " << std::get<1>(v_row) << ", " << std::get<2>(v_row) << ", " << std::get<3>(v_row) << std::endl;
        }
    }

    return EXIT_SUCCESS;
}

这是示例的输出:

run
done ... r: true, iter==end: true
v, 1, 2, 3
, 0, 0, 0
v, 4, 5, 6
v, 7, 8, 9
v, 10, 11, 12

这就是我真正希望解析器返回的内容。

run
done ... r: true, iter==end: true
v, 1, 2, 3
v, 4, 5, 6

我现在的主要问题是防止“垃圾”规则添加空的 V() 对象。我该如何做到这一点?还是我想多了?

我已经尝试将 lit(junk) 添加到开始规则中,因为 lit() 不返回任何内容,但这不会编译。它失败了:“静态断言失败:error_invalid_expression”。

我还尝试将垃圾规则的语义操作设置为 qi::unused_type() 但在这种情况下规则仍然创建一个空的 V()。

我知道以下问题,但它们没有解决这个特定问题。我之前已经尝试过 comment skipper,但看起来我必须重新实现 skipper 中的所有解析规则才能识别噪音。我的示例灵感来自于最后一个链接中的解决方案:

How to skip line/block/nested-block comments in Boost.Spirit?

How to parse entries followed by semicolon or newline (boost::spirit)?

版本信息:

Linux debian 4.9.0-7-amd64 #1 SMP Debian 4.9.110-3+deb9u2 (2018-08-13) x86_64 GNU/Linux
g++ (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
#define BOOST_VERSION 106200

和:

Linux raspberrypi 4.14.24-v7+ #1097 SMP Mon Mar 5 16:42:05 GMT 2018 armv7l GNU/Linux
g++ (Raspbian 4.9.2-10+deb8u1) 4.9.2
#define BOOST_VERSION 106200 

对于那些想知道的人:是的,我正在尝试解析类似于 Wavefront OBJ 文件的文件,而且我知道已经有很多可用的解析器。然而,我正在解析的数据是一个更大的数据结构的一部分,它也需要解析,因此构建一个新的解析器确实有意义。

最佳答案

您想要实现的目标称为错误恢复。

不幸的是,Spirit 没有很好的方法来做这件事(也有一些内部决策很难从外部做出)。但是,在您的情况下,通过语法重写很容易实现。

#include <iostream>
#include <string>
#include <vector>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/std_tuple.hpp>

namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
namespace phx = boost::phoenix;

using V = std::tuple<std::string, double, double, double>;

namespace client {
    template <typename Iterator>
    struct VGrammar : qi::grammar<Iterator, std::vector<V>()> {
        VGrammar() : VGrammar::base_type(start) {
            using namespace qi;

            v = skip(blank)[no_skip[string("v")] > double_ > double_ > double_];
            junk = +(char_ - eol);
            start = (v || -junk) % eol;

            v.name("v");
            junk.name("junk");
            start.name("start");

            using phx::val;
            using phx::construct;

            on_error<fail>(
                start,
                std::cout
                << val("Error! Expecting \n\n'")
                << qi::_4
                << val("'\n\n here: \n\n'")
                << construct<std::string>(qi::_3, qi::_2)
                << val("'")
                << std::endl
                );

            //debug(v);
            //debug(junk);
            //debug(start);
        }

        qi::rule<Iterator> junk;
        //qi::rule<Iterator, qi::unused_type()> junk; // Doesn't work either
        //qi::rule<Iterator, qi::unused_type(), qi::unused_type()> junk; // Doesn't work either
        qi::rule<Iterator, V()> v;
        qi::rule<Iterator, std::vector<V>()> start;
    };
} // namespace client

int main(int argc, char* argv[]) {
    using iterator_type = std::string::const_iterator;

    std::string input = "";
    input += "v 1 2 3\r";         // keep v 1 2 3
    input += "o a b c\r";         // parse as junk
    input += "v 4 5 6 v 7 8 9\r"; // keep v 4 5 6, but parse v 7 8 9 as junk
    input += "   v 10 11 12\r\r"; // parse as junk

    iterator_type iter = input.begin();
    const iterator_type end = input.end();
    std::vector<V> parsed_output;
    client::VGrammar<iterator_type> v_grammar;

    std::cout << "run" << std::endl;
    bool r = parse(iter, end, v_grammar, parsed_output);
    std::cout << "done ... r: " << (r ? "true" : "false") << ", iter==end: " << ((iter == end) ? "true" : "false") << std::endl;

    if (r && (iter == end)) {
        BOOST_FOREACH(V const& v_row, parsed_output) {
            std::cout << std::get<0>(v_row) << ", " << std::get<1>(v_row) << ", " << std::get<2>(v_row) << ", " << std::get<3>(v_row) << std::endl;
        }
    }

    return EXIT_SUCCESS;
}

关于c++ - 如何在 boost::spirit::qi 中将某些语义 Action 排除在 AST 之外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52005150/

相关文章:

C++ 将 *this 传递给基本构造函数

c++ - Spirit V2 和 X3 的状态

c++ - 更多 spirit 疯狂 - 解析器类型(规则与 int_parser<>)和元编程技术

c++ - 无法在 Spirit::Qi 中定义规则

c++ - 顺序或解析器 a || b

c++ - 使用 Boost Spirit 解析带有数组的结构

c++ - Waitforsingleobject 在尝试打开 Notepad++ 时有效,但在 Firefox 中立即返回

c++ - 具有指针数据类型的非类型函数模板参数

c++ - 将GPU版本的FFMPEG编译为OpenCV

c++ - Spirit X3 : attribute of alternative parser, 不是 `char` ,而是 `variant<char, char>`