gpt4 book ai didi

c++ - 无法在 boost spirit 中指定 skipper (编译器错误)

转载 作者:行者123 更新时间:2023-11-30 03:45:05 26 4
gpt4 key购买 nike

我正在尝试使用 boost::spirit 构建一个解析器,除了其他内容外,它应该能够解析成对的整数,例如“(3,4)”。

我的代码可以正常工作,但我也想接受包含空格的对,即“( 4 , 6 )”。当尝试添加 space_type skipper 时,如 boost spirit tutorial 中所示以下代码无法编译。我看不出我正在犯的严重错误(我怀疑使用了错误的命名空间,但它似乎是正确的)。

#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/fusion/include/std_pair.hpp>

using namespace std;
using namespace boost::spirit::qi;

using position = std::pair<int, int>;

template<typename Iterator>
struct position_grammar : grammar<Iterator, position(), boost::spirit::ascii::space_type> {
position_grammar() : position_grammar::base_type(start) {
start = lit("(") >> int_ >> lit(",") >> int_ >> lit(")");
}
rule<Iterator, position(),boost::spirit::ascii::space_type> start;
};

int main() {
std::string s = "(1,2)";
position_grammar<std::string::iterator> grammar;
position p;
bool r = parse(s.begin(), s.end(), grammar,p);
cout << "parsing: " << r << " position: " << p.first << ", " << p.second << "\n";
return 0;
}

编译器输出:

In file included from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14:0,
from /usr/include/boost/spirit/home/qi.hpp:21,
from /usr/include/boost/spirit/include/qi.hpp:16,
from /home/pansen/proggs/foo/src/foo/model/routing_parser_test.cpp:2:
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp: In instantiation of ‘bool boost::spirit::qi::rule<Iterator, T1, T2, T3, T4>::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Context = boost::spirit::context<boost::fusion::cons<std::pair<int, int>&, boost::fusion::nil_>, boost::spirit::locals<> >; Skipper = boost::spirit::unused_type; Attribute = std::pair<int, int>; Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; T1 = std::pair<int, int>(); T2 = 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::ascii> >, 0l>; T3 = boost::spirit::unused_type; T4 = boost::spirit::unused_type]’:
/usr/include/boost/spirit/home/qi/reference.hpp:43:72: required from ‘bool boost::spirit::qi::reference<Subject>::parse(Iterator&, const Iterator&, Context&, const Skipper&, Attribute&) const [with Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; Context = boost::spirit::context<boost::fusion::cons<std::pair<int, int>&, boost::fusion::nil_>, boost::spirit::locals<> >; Skipper = boost::spirit::unused_type; Attribute = std::pair<int, int>; Subject = const boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >, std::pair<int, int>(), 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::ascii> >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type>]’
/usr/include/boost/spirit/home/qi/parse.hpp:86:82: required from ‘bool boost::spirit::qi::parse(Iterator&, Iterator, const Expr&, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; Expr = position_grammar<__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> > >; Attr = std::pair<int, int>]’
/usr/include/boost/spirit/home/qi/parse.hpp:98:25: required from ‘bool boost::spirit::qi::parse(const Iterator&, Iterator, const Expr&, Attr&) [with Iterator = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >; Expr = position_grammar<__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> > >; Attr = std::pair<int, int>]’
/home/pansen/proggs/foo/src/foo/model/routing_parser_test.cpp:23:46: required from here
/usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:304:17: error: no match for call to ‘(const function_type {aka const boost::function<bool(__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, const __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, boost::spirit::context<boost::fusion::cons<std::pair<int, int>&, boost::fusion::nil_>, boost::fusion::vector0<> >&, const boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >&)>}) (__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, const __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&, boost::spirit::qi::rule<__gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >, std::pair<int, int>(), 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::ascii> >, 0l>, boost::spirit::unused_type, boost::spirit::unused_type>::context_type&, const boost::spirit::unused_type&)’
if (f(first, last, context, skipper))
^
In file included from /usr/include/boost/function/detail/maybe_include.hpp:33:0,
from /usr/include/boost/function/detail/function_iterate.hpp:14,
from /usr/include/boost/preprocessor/iteration/detail/iter/forward1.hpp:67,
from /usr/include/boost/function.hpp:64,
from /usr/include/boost/spirit/home/qi/nonterminal/rule.hpp:16,
from /usr/include/boost/spirit/home/qi/nonterminal.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:21,
from /usr/include/boost/spirit/include/qi.hpp:16,
from /home/pansen/proggs/foo/src/foo/model/routing_parser_test.cpp:2:
/usr/include/boost/function/function_template.hpp:767:17: note: candidate: boost::function4<R, T1, T2, T3, T4>::result_type boost::function4<R, T1, T2, T3, T4>::operator()(T0, T1, T2, T3) const [with R = bool; T0 = __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&; T1 = const __gnu_cxx::__normal_iterator<char*, std::__cxx11::basic_string<char> >&; T2 = boost::spirit::context<boost::fusion::cons<std::pair<int, int>&, boost::fusion::nil_>, boost::fusion::vector0<> >&; T3 = const boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >&; boost::function4<R, T1, T2, T3, T4>::result_type = bool]
result_type operator()(BOOST_FUNCTION_PARMS) const
^
/usr/include/boost/function/function_template.hpp:767:17: note: no known conversion for argument 4 from ‘const boost::spirit::unused_type’ to ‘const boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >&’

错误指向 if (f(first, last, context, skipper)) 看起来像错误 here (见 sehe 的回答)。但是我已经正确指定了属性(至少我是这么认为的)。

最佳答案

如果你想使用 skipper ,你必须通过调用 phrase_parse 而不是 parse 来传递它的一个实例:

bool r = phrase_parse(s.begin(), s.end(), grammar, qi::space, p);

或者,您可以隐藏有关 skipper 的详细信息,方法是使用 skip(qi::space) [ real_start_rule_with_skipper ]

在封闭的无 skipper 规则中声明它

另请参阅:Boost spirit skipper issues

注意事项:

  • 不要使用 using namespace - 尤其是不要同时使用 stdboost::spirit: :qi 同时。您始终可以在本地使用 using namespace qi;
  • 无需将每个文字包装在 lit() 中。这是简化的规则:

    position_rule = '(' >> qi::int_ >> ',' >> qi::int_ >> ')';

演示

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/std_pair.hpp>

namespace qi = boost::spirit::qi;

using position = std::pair<int, int>;

template <typename Iterator> struct position_grammar : qi::grammar<Iterator, position()> {
position_grammar() : position_grammar::base_type(start) {

position_rule = '(' >> qi::int_ >> ',' >> qi::int_ >> ')';
start = qi::skip(boost::spirit::ascii::space) [ position_rule ];

}
qi::rule<Iterator, position()> start;
qi::rule<Iterator, position(), boost::spirit::ascii::space_type> position_rule;
};

int main() {
std::string s = " ( 1\t,\r\n2 )";
position_grammar<std::string::iterator> grammar;
position p;

bool r = parse(s.begin(), s.end(), grammar, p);

std::cout << "parsing: " << r << " position: " << p.first << ", " << p.second << "\n";
}

打印

parsing: 1 position: 1, 2

关于c++ - 无法在 boost spirit 中指定 skipper (编译器错误),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35072964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com