本文介绍了使用boost :: spirit :: qi :: phrase_parse()与qi ::语法时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个IRC消息解析器与 Boost.Spirit 但我得到一个(很长)的错误,当我尝试解析一个输入。我已遵循示例。此外,我使用 g ++ 4.7 -std = c ++ 11 。只有当我在 test.cpp 上调用 phrase_parse()时才出现错误,而不是当我创建 message_grammar

I'm doing an IRC message parser with Boost.Spirit but I'm getting a (very long) error when I try to parse an input. I've followed the "Roman Numerals" example. Also, I'm using g++4.7 with -std=c++11. The error occurs only when I call phrase_parse() on test.cpp, not when I make an instance of message_grammar.

语法类别为:

class message_grammar : qi::grammar<std::string::const_iterator, std::string()>
{
public:
    message_grammar() : base_type(m_message)
    {
        using qi::_val;
        using qi::_1;
        using boost::spirit::ascii::char_;
        using qi::lit;

        qi::rule<std::string::const_iterator, std::string()> alpha, graph, number, special, user,
            nick, chn, channel;

        alpha  = qi::as_string[qi::alpha];
        graph  = qi::as_string[qi::graph];
        number = qi::as_string[char_('0', '9')];
        chn    = qi::as_string[(char_('#') | char_('$'))];

        special = qi::as_string[
                char_('-') | char_('[') | char_(']') | char_('\\')
            |   char_('`') | char_('^') | char_('{') | char_('}')
        ];

        user = (
                graph   [_val = std::string("USER%% ") + _1]
            >>  *graph[_val += _1]
        );

        nick = (
                alpha[_val = std::string("NICK%% ") + _1]
            >>
            *(
                    alpha
                |   number
                |   special
            )[_val += _1]
        );

        channel = (
                chn                         [_val = std::string("CHAN%% ") + _1]
            >>  +(alpha | number | special) [_val += _1]
        );

        m_message = (
                nick    [_val = _1]
            >>  alpha   [_val += std::string(" ") + _1]
            >>  channel [_val += std::string(" ") + _1]
            >>  user    [_val += std::string(" ") + _1]
        );
    }


    qi::rule<std::string::const_iterator, std::string()> m_message;
};

,测试功能为:

int main()
{
    string str, parsed_str;
    getline(cin, str);

    irc::message_grammar grammar;

    auto it = str.begin();
    auto end = str.end();

    bool ok = qi::phrase_parse(it, end,
        grammar
    , qi::space, parsed_str);

    if (ok && it == end) {
        cout << "OK" << endl;
        cout << parsed_str << endl;
    }
    else {
        cout << "NO " << ok << endl;
    }
}

并显示错误讯息:

murilo@mac:cleverbot$ g++-mp-4.7 -std=c++11 test.cpp -Wall
In file included from /opt/local/include/boost/proto/core.hpp:23:0,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/domain.hpp: In instantiation of 'struct boost::proto::domainns_::domain<>::as_expr<irc::message_grammar>':
/opt/local/include/boost/proto/traits.hpp:266:71:   required from 'struct boost::proto::result_of::as_expr<irc::message_grammar, boost::proto::domainns_::default_domain>'
/opt/local/include/boost/spirit/home/support/meta_compiler.hpp:221:16:   required from 'struct boost::spirit::traits::matches<boost::spirit::qi::domain, irc::message_grammar>'
/opt/local/include/boost/spirit/home/qi/parse.hpp:155:9:   required from 'bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const Expr&, const Skipper&, boost::spirit::qi::skip_flag, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; Expr = irc::message_grammar; Skipper = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0l>; Attr = std::basic_string<char>]'
/opt/local/include/boost/spirit/home/qi/parse.hpp:197:86:   required from 'bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const Expr&, const Skipper&, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; Expr = irc::message_grammar; Skipper = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0l>; Attr = std::basic_string<char>]'
test.cpp:20:25:   required from here
/opt/local/include/boost/proto/extends.hpp:551:13: error: 'typedef void boost::proto::exprns_::extends<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, 0l>, boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()>, boost::proto::domainns_::default_domain, 0l>::proto_is_expr_' is inaccessible
In file included from /opt/local/include/boost/proto/traits.hpp:32:0,
                 from /opt/local/include/boost/proto/expr.hpp:27,
                 from /opt/local/include/boost/proto/core.hpp:17,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/domain.hpp:141:13: error: within this context
In file included from /opt/local/include/boost/proto/core.hpp:23:0,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/extends.hpp:551:13: error: 'typedef void boost::proto::exprns_::extends<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, 0l>, boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()>, boost::proto::domainns_::default_domain, 0l>::proto_is_expr_' is inaccessible
In file included from /opt/local/include/boost/proto/traits.hpp:32:0,
                 from /opt/local/include/boost/proto/expr.hpp:27,
                 from /opt/local/include/boost/proto/core.hpp:17,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/domain.hpp:141:13: error: within this context
In file included from /opt/local/include/boost/proto/core.hpp:23:0,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/extends.hpp:551:13: error: 'typedef void boost::proto::exprns_::extends<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, 0l>, boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()>, boost::proto::domainns_::default_domain, 0l>::proto_is_expr_' is inaccessible
In file included from /opt/local/include/boost/proto/traits.hpp:32:0,
                 from /opt/local/include/boost/proto/expr.hpp:27,
                 from /opt/local/include/boost/proto/core.hpp:17,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/domain.hpp:142:17: error: within this context
In file included from /opt/local/include/boost/proto/core.hpp:23:0,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/extends.hpp:551:13: error: 'typedef void boost::proto::exprns_::extends<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, 0l>, boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()>, boost::proto::domainns_::default_domain, 0l>::proto_is_expr_' is inaccessible
In file included from /opt/local/include/boost/proto/traits.hpp:32:0,
                 from /opt/local/include/boost/proto/expr.hpp:27,
                 from /opt/local/include/boost/proto/core.hpp:17,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/domain.hpp:143:56: error: within this context
In file included from /opt/local/include/boost/proto/core.hpp:23:0,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/extends.hpp:551:13: error: 'typedef void boost::proto::exprns_::extends<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, 0l>, boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()>, boost::proto::domainns_::default_domain, 0l>::proto_is_expr_' is inaccessible
In file included from /opt/local/include/boost/proto/traits.hpp:32:0,
                 from /opt/local/include/boost/proto/expr.hpp:27,
                 from /opt/local/include/boost/proto/core.hpp:17,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/domain.hpp:146:29: error: within this context
In file included from /opt/local/include/boost/proto/core.hpp:23:0,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/extends.hpp:551:13: error: 'typedef void boost::proto::exprns_::extends<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, 0l>, boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()>, boost::proto::domainns_::default_domain, 0l>::proto_is_expr_' is inaccessible
In file included from /opt/local/include/boost/proto/traits.hpp:32:0,
                 from /opt/local/include/boost/proto/expr.hpp:27,
                 from /opt/local/include/boost/proto/core.hpp:17,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/domain.hpp:146:29: error: within this context
In file included from /opt/local/include/boost/proto/core.hpp:23:0,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/extends.hpp:551:13: error: 'typedef void boost::proto::exprns_::extends<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, 0l>, boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()>, boost::proto::domainns_::default_domain, 0l>::proto_is_expr_' is inaccessible
In file included from /opt/local/include/boost/proto/traits.hpp:32:0,
                 from /opt/local/include/boost/proto/expr.hpp:27,
                 from /opt/local/include/boost/proto/core.hpp:17,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/domain.hpp:146:46: error: within this context
In file included from /opt/local/include/boost/proto/core.hpp:23:0,
                 from /opt/local/include/boost/proto/proto.hpp:12,
                 from /opt/local/include/boost/spirit/home/support/meta_compiler.hpp:19,
                 from /opt/local/include/boost/spirit/home/qi/meta_compiler.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi/action.hpp:14,
                 from /opt/local/include/boost/spirit/home/qi.hpp:14,
                 from /opt/local/include/boost/spirit/include/qi.hpp:16,
                 from ircparser.h:6,
                 from test.cpp:1:
/opt/local/include/boost/proto/matches.hpp: In instantiation of 'struct boost::proto::matches<irc::message_grammar, boost::spirit::meta_compiler<boost::spirit::qi::domain>::meta_grammar>':
/opt/local/include/boost/spirit/home/support/meta_compiler.hpp:221:16:   required from 'struct boost::spirit::traits::matches<boost::spirit::qi::domain, irc::message_grammar>'
/opt/local/include/boost/spirit/home/qi/parse.hpp:155:9:   required from 'bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const Expr&, const Skipper&, boost::spirit::qi::skip_flag, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; Expr = irc::message_grammar; Skipper = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0l>; Attr = std::basic_string<char>]'
/opt/local/include/boost/spirit/home/qi/parse.hpp:197:86:   required from 'bool boost::spirit::qi::phrase_parse(Iterator&, Iterator, const Expr&, const Skipper&, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; Expr = irc::message_grammar; Skipper = boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::standard> >, 0l>; Attr = std::basic_string<char>]'
test.cpp:20:25:   required from here
/opt/local/include/boost/proto/extends.hpp:551:13: error: 'typedef struct boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()> boost::proto::exprns_::extends<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::qi::reference<const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>(), boost::spirit::unused_type, boost::spirit::unused_type, boost::spirit::unused_type> > >, 0l>, boost::spirit::qi::grammar<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::basic_string<char>()>, boost::proto::domainns_::default_domain, 0l>::proto_derived_expr' is inaccessible


推荐答案

您应该将 message_grammar 声明为结构而不是类。
此外,您可以从 qi :: grammar 公开导出您的类:

You should declare message_grammar as a struct rather than a class.Also, you can derive your class publicly from qi::grammar:

class message_grammar : public qi::grammar<std::string::const_iterator, std::string()>
{
}

您还可以找到有用。

这篇关于使用boost :: spirit :: qi :: phrase_parse()与qi ::语法时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 12:47