gpt4 book ai didi

c++ - 如何从 spirit 语义规则绑定(bind)/调用存储在 fusion::vector 中的 boost::function?

转载 作者:太空狗 更新时间:2023-10-29 21:46:09 25 4
gpt4 key购买 nike

我正在尝试通过 qi::symbols 将一些关键字处理程序(方法)映射为 boost::function 类型的值。所以如果找到关键字我想调用方法。但我无法从这张 map 绑定(bind)方法。编译器因 phoenix::bind 上的一堆错误而失败。我做错了什么?

部分代码如下:

template <typename Iterator>
struct Grammar : qi::grammar<Iterator, AST::FunctionCall(), ascii::space_type>
{
Grammar():
Grammar::base_type(query),
{
...
operand =
predicate [phoenix::bind(phoenix::at_c<0>(qi::_1), this, phoenix::at_c<1>(qi::_1))]; // **Compiler fails here**
...

predicate =
(pred_tbl > '(')
> -(primary_expr % ',')
> ')';
...

pred_tbl.add
("composing", &RQL::composing)
);
}

qi::rule<Iterator, fusion::vector<Predicate, PredicateArgList>(), ascii::space_type> predicate;

typedef std::vector<AST::Value> PredicateArgList;
typedef boost::function<void (Grammar*, const PredicateArgList& args)> Predicate;
qi::symbols<char, Predicate> pred_tbl;

void composing(const PredicateArgList& args);
};

编译器错误:

error C2903: 'result' : symbol is neither a class template nor a function template  c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp  115 
error C2039: 'result' : is not a member of 'boost::function<Signature>' c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 115
error C2059: syntax error : '<' c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 115
error C2238: unexpected token(s) preceding ';' c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 116
error C2065: 'function_apply' : undeclared identifier c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 124
error C2955: 'boost::mpl::eval_if' : use of class template requires template argument list c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 125
error C2146: syntax error : missing ';' before identifier 'type' c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 126
error C3254: 'boost::phoenix::detail::function_eval<2>::result<Env,F,A0,A1>' : class contains explicit override 'type' but does not derive from an interface that contains the function declaration c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 126
error C2838: 'type' : illegal qualified name in member declaration c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 126
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 126
error C2602: 'boost::phoenix::detail::function_eval<2>::result<Env,F,A0,A1>::type' is not a member of a base class of 'boost::phoenix::detail::function_eval<2>::result<Env,F,A0,A1>' c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 126
error C2868: 'boost::phoenix::detail::function_eval<2>::result<Env,F,A0,A1>::type' : illegal syntax for using-declaration; expected qualified-name c:\work\include\boost-1_41\boost\spirit\home\phoenix\core\detail\function_eval.hpp 126

最佳答案

我想丢失的链接可能是

#define BOOST_SPIRIT_USE_PHOENIX_V3

http://liveworkspace.org/code/3uvyb4$1 上观看直播

它打印

Hello world from 'Grammar::composing(PredicateArgList const&)' args:3

如预期。

更新 附言。我可能更喜欢像这样编写绑定(bind)调用:

Grammar(): Grammar::base_type(predicate)
{
using phx::bind;
using namespace qi;

as<PredicateArgList> coerce;

predicate =
(pred_tbl > '('
> coerce [ -(primary_expr % ',') ]
> ')')
[ phx::bind(_1, this, _2)]
;

// ...

我认为 _1 更清晰和 _2而不是 at_c<n>调用。

引用代码如下:

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/function.hpp>
#include <boost/phoenix/fusion.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/qi.hpp>

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

namespace AST
{
struct FunctionCall {};
using Value = int;
}

template <typename Iterator>
struct Grammar : qi::grammar<Iterator, AST::FunctionCall(), ascii::space_type>
{
Grammar(): Grammar::base_type(predicate)
{
using phx::bind;
using namespace qi;

as<PredicateArgList> coerce;

predicate =
(pred_tbl > '('
> coerce [ -(primary_expr % ',') ]
> ')')
[ phx::bind(_1, this, _2)]
;

pred_tbl.add
("composing", &Grammar::composing)
;

primary_expr = qi::int_;
}

typedef std::vector<AST::Value> PredicateArgList;
typedef boost::function<void (Grammar*, PredicateArgList const&)> Predicate;
qi::symbols<char, Predicate> pred_tbl;

qi::rule<Iterator, AST::Value(), ascii::space_type> primary_expr;
qi::rule<Iterator, AST::FunctionCall(), ascii::space_type> predicate;

void composing(const PredicateArgList& args)
{
std::cout << "Hello world from 'Grammar::composing(PredicateArgList const&)' args:" << args.size() << "\n";
}
};

int main()
{
const std::string input("composing (1, 2, 3)");
auto f = begin(input), l = end(input);

Grammar<decltype(f)> p;

bool ok = qi::phrase_parse(f,l,p,ascii::space);
return ok?0:255;
}

关于c++ - 如何从 spirit 语义规则绑定(bind)/调用存储在 fusion::vector 中的 boost::function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15811875/

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