简体   繁体   English

Boost Spirit占位符类型转换

[英]Boost Spirit placeholder type conversion

I'm trying to write a parser that (as a first step, of course it will be expanded a lot) parses a double and creates an object of my class ExpressionTree by passing that double to a factory method of my class. 我正在尝试编写一个解析器(作为第一步,当然它将被扩展很多)解析一个double并通过将该double传递给我的类的工厂方法来创建我的类ExpressionTree的对象。 This was my first try 这是我的第一次尝试

struct operands : qi::grammar<string::iterator, ExpressionTree()> 
{

    operands() : operands::base_type(start) 
    {
        start = qi::double_[qi::_val = ExpressionTree::number(qi::_1)];
    }

    qi::rule<string::iterator, ExpressionTree()> start;

};

This doesn't compile ( can't convert from boost::spirit::_1_type to double ) because (if I understand correctly) qi::_1 is not a double but only evaluates to a double. 这不能编译( can't convert from boost::spirit::_1_type to double )因为(如果我理解正确的话) qi::_1不是double,而只是求值为double。

I tried using boost::bind(&ExpressionTree::number, _1) in any way but I don't know how I then could get the result assigned to the attribute _val 我尝试以任何方式使用boost::bind(&ExpressionTree::number, _1) ,但我不知道如何将结果分配给属性_val

I would be grateful if anyone could point me in the right direction. 如果有人能指出我正确的方向,我将不胜感激。

You need lazy actors in the semantic actions. 你需要在语义动作中使用懒惰的演员。

I'm assuming number is a static unary function or a non-static nullary (instead of, eg a type): 我假设number是一个静态的一元函数或一个非静态的nullary(而不是,例如一个类型):

start = qi::double_ [ qi::_val = boost::phoenix::bind(&ExpressionTree::number, qi::_1)];

If it were a type: 如果它是一个类型:

start = qi::double_ [ qi::_val = boost::phoenix::construct<ExpressionTree::number>(qi::_1)];

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM