卡住了移植旧版boost :: spirit代码

问题描述 投票:1回答:1

我正在将一些旧代码从VS2010&boost1.53移植到VS2017&boost1.71。

我在尝试编译它的最后两个小时被卡住了。

代码是:

#include <string>
#include <vector>
#include <fstream>
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi = boost::spirit::qi;
using qi::_1; using qi::_2; using qi::_3; using qi::_4;

enum TYPE { SEND, CHECK, COMMENT };

struct Command
{
    TYPE type;
    std::string id;
    std::string arg1;
    std::string arg2;
    bool checking;
};

class Parser
{
    typedef boost::spirit::istream_iterator It;
    typedef std::vector<Command> Commands;

    struct deferred_fill
    {
        template <typename R, typename S, typename T, typename U> struct result { typedef void type; };//Not really sure still necessary
        typedef void result_type;//Not really sure still necessary

        void operator() (boost::iterator_range<It> const& id, boost::iterator_range<It> const& arg1, bool checking, Command& command) const
        {
            command.type = TYPE::SEND;
            command.id.assign(id.begin(), id.end());
            command.arg1.assign(arg1.begin(), arg1.end());
            command.checking = checking;
        }
    };

private:
    qi::symbols<char, bool> value;
    qi::rule<It> ignore;
    qi::rule<It, Command()> send;
    qi::rule<It, Commands()> start;
    boost::phoenix::function<deferred_fill> fill;

public:
    std::vector<Command> commands;

    Parser()
    {
        using namespace qi;
        using boost::phoenix::push_back;

        value.add("TRUE", true)
                 ("FALSE", false);

        send = ("SEND_CONFIRM" >> *blank >> '(' >> *blank >> raw[*~char_(',')] >> ','
                                                >> *blank >> raw[*~char_(',')] >> ','
                                                >> *blank >> value >> *blank >> ')' >> *blank >> ';')[fill(_1, _2, _3, _val)];

        ignore = *~char_("\r\n");

        start = (send[push_back(_val, _1)] | ignore) % eol;
    }

    void parse(const std::string& path)
    {
        std::ifstream in(path, std::ios_base::in);
        if (!in) return;

        in >> std::noskipws;//No white space skipping
        boost::spirit::istream_iterator first(in);
        boost::spirit::istream_iterator last;

        qi::parse(first, last, start, commands);
    }
};

int main(int argc, char* argv[])
{
    Parser parser;
    parser.parse("file.txt");

    return 0;
}

编译器以另一种方式抱怨(仅复制第一行):

1>z:\externos\boost_1_71_0\boost\phoenix\core\detail\function_eval.hpp(116): error C2039: 'type': no es un miembro de 'boost::result_of<const Parser::deferred_fill (std::vector<Value,std::allocator<char>> &,std::vector<Value,std::allocator<char>> &,boost::iterator_range<Parser::It> &,Command &)>'
1>        with
1>        [
1>            Value=char
1>        ]
1>z:\externos\boost_1_71_0\boost\phoenix\core\detail\function_eval.hpp(114): note: vea la declaración de 'boost::result_of<const Parser::deferred_fill (std::vector<Value,std::allocator<char>> &,std::vector<Value,std::allocator<char>> &,boost::iterator_range<Parser::It> &,Command &)>'
1>        with
1>        [
1>            Value=char
1>        ]
1>z:\externos\boost_1_71_0\boost\phoenix\core\detail\function_eval.hpp(89): note: vea la referencia a la creación de instancias de plantilla clase de 'boost::phoenix::detail::function_eval::result_impl<F,void (Head,const boost::phoenix::actor<boost::spirit::argument<1>>&,const boost::phoenix::actor<boost::spirit::argument<2>>&,const boost::phoenix::actor<boost::spirit::attribute<0>>&),const boost::phoenix::vector2<Env,Actions> &>' que se está compilando
1>        with
1>        [
1>            F=const boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<Parser::deferred_fill>,0> &,
1>            Head=const boost::phoenix::actor<boost::spirit::argument<0>> &,
1>            Env=boost::phoenix::vector4<const boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::phoenix::detail::tag::function_eval,boost::proto::argsns_::list5<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<Parser::deferred_fill>,0>,boost::phoenix::actor<boost::spirit::argument<0>>,boost::phoenix::actor<boost::spirit::argument<1>>,boost::phoenix::actor<boost::spirit::argument<2>>,boost::phoenix::actor<boost::spirit::attribute<0>>>,5>> *,boost::fusion::vector<std::vector<char,std::allocator<char>>,std::vector<char,std::allocator<char>>,boost::iterator_range<Parser::It>,std::vector<char,std::allocator<char>>,boost::iterator_range<Parser::It>,std::vector<char,std::allocator<char>>,bool,std::vector<char,std::allocator<char>>,std::vector<char,std::allocator<char>>> &,boost::spirit::context<boost::fusion::cons<Command &,boost::fusion::nil_>,boost::fusion::vector<>> &,bool &> &,
1>            Actions=const boost::phoenix::default_actions &
1>        ]

我想这个错误与使用boost :: spirit :: istream_iterator而不是char *有关,但我不知道如何修复它才能再次工作。

我的想法已经用完了,任何人都可以看到我的错误在哪里?

boost boost-spirit
1个回答
1
投票

噢你在做很棒的事情。不幸的是,它过于复杂。

因此,让我们先修复,然后简化。

错误

就像你说的,

void operator() (boost::iterator_range<It> const& id, boost::iterator_range<It> const& arg1, bool checking, Command& command) const

与实际调用的内容不匹配:

void Parser::deferred_fill::operator()(T&& ...) const [with T = {std::vector<char>&, std::vector<char>&, boost::iterator_range<boost::spirit::basic_istream_iterator<...> >&, Command&}]

原因不是迭代器(可以看到boost::spirit__istream_iterator好。]

但是是因为您将other东西当作属性。原来*blank将属性公开为vector<char>。因此,您可以通过omit[]将其“修复”。相反,我们将其包装在ignore这样的无属性规则中,以减少混乱。

现在可以通过它调用

void Parser::deferred_fill::operator()(T&& ...) const [with T = {boost::iterator_range<It>&, boost::iterator_range<It>&, bool&, Command&}]

因此它兼容并编译。解析:

SEND_CONFIRM("this is the id part", "this is arg1", TRUE);

使用

Parser parser;
parser.parse("file.txt");

std::cout << std::boolalpha;
for (auto& cmd : parser.commands) {
    std::cout << '{' << cmd.id << ", "
        << cmd.arg1 << ", "
        << cmd.arg2 << ", "
        << cmd.checking << "}\n";
}

打印

{"this is the id part", "this is arg1", , TRUE}

让我们改善一下

  • 这需要一个队长
  • 这要求自动传播属性
  • 风格的其他元素

船长

而不是显式“调用”一个船长,让我们使用内置功能:

rule<It, Attr(), Skipper> x;

定义一个规则,该规则跳过由Skipper类型的解析器匹配的输入序列。您实际上需要传递该类型的船长。

  • 使用qi::phrase_parse代替qi::parse
  • 通过使用qi::skip()指令

我一直提倡第二种方法,因为它提供了更友好,更不易出错的界面。

因此声明了船长类型:

qi::rule<It, Command(), qi::blank_type> send;

我们可以将规则简化为:

    send = (lit("SEND_CONFIRM") >> '(' 
            >> raw[*~char_(',')] >> ','
            >> raw[*~char_(',')] >> ','
            >> value >> ')' >> ';')
        [fill(_1, _2, _3, _val)];

并且比通过start规则传递一个船长:

    start = skip(blank) [
            (send[push_back(_val, _1)] | ignore) % eol
        ];

就这些。仍然可以编译并匹配相同的对象。

Live On Coliru

跳过词首

仍然是同一主题,lexemes实际上禁止了船长¹,因此您不必raw[]。这也将公开的属性更改为vector<char>

void operator() (std::vector<char> const& id, std::vector<char> const& arg1, bool checking, Command& command) const

Live On Coliru

自动属性传播

Qi具有语义动作,但其真正的优势在于它们是可选的:Boost Spirit: "Semantic actions are evil"?

  • [push_back(_val, _1)实际上是*p+pp % delim²的自动属性传播语义,所以将其删除:

    start = skip(blank) [
            (send | ignore) % eol
        ];
    

    (请注意,send|ignore实际上是合成的optional<Command> m,这对于自动传播是合适的)

  • std::vectorstd::string属性兼容,例如。因此,如果我们可以为arg2添加占位符,则可以匹配Command结构布局:

    send = lit("SEND_CONFIRM") >> '(' 
        >> attr(SEND) // fill type
        >> lexeme[*~char_(',')] >> ','
        >> lexeme[*~char_(',')] >> ','
        >> attr(std::string()) // fill for arg2
        >> value >> ')' >> ';'
    ;
    

    现在要删除fill及其实现,我们必须将Command修改为融合序列:

    BOOST_FUSION_ADAPT_STRUCT(Command, type, id, arg1, arg2, checking)
    

样式1的元素

为您的Command类型使用名称空间使ADL更容易使用Command的operator<<重载,例如我们可以std::cout << cmd;

至此,所有这些工作仅需部分代码即可:Live On Coliru

样式2的元素

  • 如果可以,请使解析器成为无状态。这意味着它可以是const,因此您可以:

    • 无需昂贵的结构即可重复使用
    • 优化器还有更多工作要用
    • 更具可测试性(有状态的东西很难证明等幂)

    因此,不要让commands成为成员,而只是将它们返回。在此过程中,我们可以将parse设置为状态方法

  • 代替对迭代器类型进行硬编码,可以灵活地将其作为模板参数。这样,如果您在multi_pass_adaptor缓冲区,istream_iteratorchar[]的某个时刻有命令,则不必担心stringstring_view的开销。

  • [此外,使用合适的入口点从qi::grammar派生您的解析器意味着您可以像其他解析器一样将其用作解析器表达式(实际上是a non-terminal,就像rule<>一样。]

  • 考虑启用规则调试(请参见示例)

  • 完整代码

    Live On Coliru

#define BOOST_SPIRIT_DEBUG
#include <boost/spirit/include/qi.hpp>
#include <fstream>

namespace qi = boost::spirit::qi;

namespace Commands {
    enum TYPE { SEND, CHECK, COMMENT };
    enum BOOL { FALSE, TRUE };

    struct Command {
        TYPE type;
        std::string id;
        std::string arg1;
        std::string arg2;
        BOOL checking;
    };

    typedef std::vector<Command> Commands;

    // for (debug) output
    static inline std::ostream& operator<<(std::ostream& os, TYPE t) {
        switch (t) {
            case SEND: return os << "SEND";
            case CHECK: return os << "CHECK";
            case COMMENT: return os << "COMMENT";
        }
        return os << "(unknown)";
    }
    static inline std::ostream& operator<<(std::ostream& os, BOOL b) {
        return os << (b?"TRUE":"FALSE");
    }

    using boost::fusion::operator<<;
}
BOOST_FUSION_ADAPT_STRUCT(Commands::Command, type, id, arg1, arg2, checking)

namespace Commands {
    template <typename It>
    class Parser : public qi::grammar<It, Commands()> {
      public:
        Commands commands;

        Parser() : Parser::base_type(start) {
            using namespace qi;

            value.add("TRUE", TRUE)
                     ("FALSE", FALSE);

            send = lit("SEND_CONFIRM") >> '(' 
                >> attr(SEND) // fill type
                >> lexeme[*~char_(',')] >> ','
                >> lexeme[*~char_(',')] >> ','
                >> attr(std::string()) // fill for arg2
                >> value >> ')' >> ';'
            ;

            ignore = +~char_("\r\n");

            start = skip(blank) [
                    (send | ignore) % eol
                ];

            BOOST_SPIRIT_DEBUG_NODES((start)(send)(ignore))
        }

      private:
        qi::symbols<char, BOOL> value;
        qi::rule<It> ignore;
        qi::rule<It, Command(), qi::blank_type> send;
        qi::rule<It, Commands()> start;
    };

    static Commands parse(std::istream& in) {
        using It = boost::spirit::istream_iterator;

        static const Parser<It> parser;

        It first(in >> std::noskipws), //No white space skipping
           last;

        Commands commands;
        if (!qi::parse(first, last, parser, commands)) {
            throw std::runtime_error("command parse error");
        }

        return commands; // c++11 move semantics
    }
}

int main() {
    try {
        for (auto& cmd : Commands::parse(std::cin))
            std::cout << cmd << "\n";
    } catch(std::exception const& e) {
        std::cout << e.what() << "\n";
    }
}

打印

(SEND "this is the id part" "this is arg1"  TRUE)

或者确实定义了BOOST_SPIRIT_DEBUG:

<start>
  <try>SEND_CONFIRM("this i</try>
  <send>
    <try>SEND_CONFIRM("this i</try>
    <success>\n</success>
    <attributes>[[SEND, [", t, h, i, s,  , i, s,  , t, h, e,  , i, d,  , p, a, r, t, "], [", t, h, i, s,  , i, s,  , a, r, g, 1, "], [], TRUE]]</attributes>
  </send>
  <send>
    <try></try>
    <fail/>
  </send>
  <ignore>
    <try></try>
    <fail/>
  </ignore>
  <success>\n</success>
  <attributes>[[[SEND, [", t, h, i, s,  , i, s,  , t, h, e,  , i, d,  , p, a, r, t, "], [", t, h, i, s,  , i, s,  , a, r, g, 1, "], [], TRUE]]]</attributes>
</start>

¹根据需要进行预跳过;参见Boost spirit skipper issues

²(然后是一些,但不要离题)

© www.soinside.com 2019 - 2024. All rights reserved.