gpt4 book ai didi

c++ - 在 boost spirit 语义 Action 中通过 std::map 过滤合成属性

转载 作者:行者123 更新时间:2023-11-30 01:24:01 25 4
gpt4 key购买 nike

我有一个案例,我想通过 std::map 过滤规则内作为合成属性出现的值。

  • map 是预先生成的,在解析过程中不会改变。
  • 映射的性质和真正的解析器意味着查找永远不会失败(尽管元素的实际数量可能非常大)
  • 非常适合这个问题的常用方法(使用符号表)并不适合实际情况。在实际问题中,查找是有条件的,基于一个属性,该属性直到很晚才在解析中出现(在某种程度上被删除的规则中)。

我的尝试:

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <iostream>
#include <vector>
#include <map>

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

int main() {

std::map<unsigned int, unsigned int> myMap;
myMap[1] = 100; myMap[2] = 200; myMap[3] = 300;

std::string test = "1 2 3";
std::vector<unsigned int> results;

qi::rule<std::string::iterator, unsigned int()> r
= qi::uint_ [qi::_val = phx::at(myMap, qi::_1)];

qi::parse(test.begin(), test.end(), ( r % " " ), results);

BOOST_FOREACH(unsigned int &x, results) {
std::cout << x << "\n";
}
}

我想我印象深刻,因为凤凰支持 stl containers ,这应该有效。但是我在规则行上遇到编译错误。如果我用经典的无意义的 [qi::_val = qi::_1] 替换语义操作,这个错误就会消失(不足为奇)。

MSVS10 下的编译器错误和往常一样长得惊人,但这里首先提到我的代码文件:(在 C:\code\Compiler2\spirit_test.cpp(25)... 第 25 行是规则 r)

C:\boost_1_50_0\boost/spirit/home/qi/nonterminal/rule.hpp(191) : see reference to function template instantiatio
n 'void boost::spirit::qi::rule<Iterator,T1>::define<boost::mpl::false_,Expr>(boost::spirit::qi::rule<Iterator,T1> &,con
st Expr &,boost::mpl::true_)' being compiled
with
[
Iterator=std::_String_iterator<char,std::char_traits<char>,std::allocator<char>>,
T1=unsigned int (void),
Expr=boost::proto::exprns_::expr<boost::proto::tagns_::tag::subscript,boost::proto::argsns_::list2<const boo
st::spirit::terminal<boost::spirit::tag::uint_> &,const boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::p
roto::tagns_::tag::assign,boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal,b
oost::proto::argsns_::term<boost::spirit::attribute<0>>,0>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost
::phoenix::detail::tag::function_eval,boost::proto::argsns_::list3<boost::proto::exprns_::basic_expr<boost::proto::tagns
_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::stl::at_impl>,0>,boost::phoenix::actor<boost::proto::exprns
_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::reference_wrapper<std::map<unsigned
int,unsigned int>>>,0>>,boost::phoenix::actor<boost::spirit::argument<0>>>,3>>>,2>> &>,2>
]
C:\code\Compiler2\spirit_test.cpp(25) : see reference to function template instantiation 'boost::spirit::qi
::rule<Iterator,T1>::rule<boost::proto::exprns_::expr<Tag,Args,Arity>>(const Expr &,const std::string &)' being compiled

with
[
Iterator=std::_String_iterator<char,std::char_traits<char>,std::allocator<char>>,
T1=unsigned int (void),
Tag=boost::proto::tagns_::tag::subscript,
Args=boost::proto::argsns_::list2<const boost::spirit::terminal<boost::spirit::tag::uint_> &,const boost::ph
oenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::assign,boost::proto::argsns_::list2<boost::pro
to::exprns_::expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::spirit::attribute<0>>,0>,boost:
:phoenix::actor<boost::proto::exprns_::basic_expr<boost::phoenix::detail::tag::function_eval,boost::proto::argsns_::list
3<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::stl:
:at_impl>,0>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::a
rgsns_::term<boost::reference_wrapper<std::map<unsigned int,unsigned int>>>,0>>,boost::phoenix::actor<boost::spirit::arg
ument<0>>>,3>>>,2>> &>,
Arity=2,
Expr=boost::proto::exprns_::expr<boost::proto::tagns_::tag::subscript,boost::proto::argsns_::list2<const boo
st::spirit::terminal<boost::spirit::tag::uint_> &,const boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::p
roto::tagns_::tag::assign,boost::proto::argsns_::list2<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal,b
oost::proto::argsns_::term<boost::spirit::attribute<0>>,0>,boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost
::phoenix::detail::tag::function_eval,boost::proto::argsns_::list3<boost::proto::exprns_::basic_expr<boost::proto::tagns
_::tag::terminal,boost::proto::argsns_::term<boost::phoenix::stl::at_impl>,0>,boost::phoenix::actor<boost::proto::exprns
_::basic_expr<boost::proto::tagns_::tag::terminal,boost::proto::argsns_::term<boost::reference_wrapper<std::map<unsigned
int,unsigned int>>>,0>>,boost::phoenix::actor<boost::spirit::argument<0>>>,3>>>,2>> &>,2>
]
C:\boost_1_50_0\boost/proto/transform/default.hpp(154) : error C2440: '=' : cannot convert from 'std::pair<_Ty1,_Ty2>' t
o 'unsigned int'
with
[
_Ty1=const unsigned int,
_Ty2=unsigned int
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

最佳答案

转自评论

尝试 qi::_val = phx::ref(myMap)[qi::_1] 而不是 qi::_val = phx::at(myMap, qi::_1 )

关于c++ - 在 boost spirit 语义 Action 中通过 std::map 过滤合成属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14205154/

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