从语义动作更新合成属性值

n0r*_*0rd 3 c++ boost boost-phoenix boost-spirit-qi

我想了解在boost :: spirit :: qi的引擎下究竟发生了什么.假设我们有简单的解析器,它解析并计算由数字和加/减操作组成的表达式:

int main()
{
    std::string INPUT_DATA = "12e-1 + 3.4 - .67";
    typedef std::string::iterator iterator_type;
    iterator_type begin = std::begin(INPUT_DATA);
    iterator_type end = std::end(INPUT_DATA);

    namespace qi = boost::spirit::qi;
    namespace ascii = boost::spirit::qi::ascii;

    auto parser = qi::double_[qi::_val = qi::_1]                      // (1)
        >> *(
                (qi::lit('+') >> qi::double_[qi::_val += qi::_1])     // (2)
                |
                (qi::lit('-') >> qi::double_[qi::_val -= qi::_1])     // (3)
            );

    double result;
    bool ok = qi::phrase_parse(begin, end, parser, ascii::space, result);

    if ( ok  && begin == end)
    {
        std::cout << "parsed, result = " << result << std::endl;
    }
    else
    {
        std::cout << "not parsed" << std::endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

如何在行qi::_val中进行语义动作(1),(2)并(3)引用相同的值?如何在不使用boost :: phoenix的情况下实现相同的结果?

我想我必须编写一堆函数来接收解析后的值qi::double_,但是我应该怎么做呢?如何访问解析器的合成值?

seh*_*ehe 5

除了评论中提供的精美的低级信息,让我告诉你精神的方式.

当然,我将结束一个不使用语义动作的演示.是的,它涉及更多代码,但它也将解析与评估分离.这在更复杂的情况下是好的(考虑回溯的解析器).

1.

从稍微简化代码开始:第1步

auto parser = 
       double_                  [_val  = _1]      // (1)
    >> *(   (lit('+') >> double_[_val += _1])     // (2)
          | (lit('-') >> double_[_val -= _1])     // (3)
        );
Run Code Online (Sandbox Code Playgroud)

2.

当然,您可以使用常规绑定功能:步骤2

void add_operand(double& lhs, double rhs) { lhs += rhs; }
void sub_operand(double& lhs, double rhs) { lhs -= rhs; }

auto parser = 
       double_                  [_val  = _1]
    >> *(   (lit('+') >> double_[bind(add_operand, _val, _1)])
          | (lit('-') >> double_[bind(sub_operand, _val, _1)])
        );
Run Code Online (Sandbox Code Playgroud)

3.

现在,使用BOOST_PHOENIX_ADAPT_FUNCTION使它稍微漂亮一点:第3步

BOOST_PHOENIX_ADAPT_FUNCTION(void, add_, add_operand, 2)
BOOST_PHOENIX_ADAPT_FUNCTION(void, sub_, sub_operand, 2)

       double_                  [_val  = _1]
    >> *(   (lit('+') >> double_[add_(_val, _1)])
          | (lit('-') >> double_[sub_(_val, _1)])
        );
Run Code Online (Sandbox Code Playgroud)

4.

或者您可以使用仿函数:第4步

struct add_operand { 
    template<typename...> struct result { typedef void type; };
    template<typename L, typename R>
    void operator()(L& lhs, R rhs) const { lhs += rhs; } 
};

struct sub_operand { 
    template<typename...> struct result { typedef void type; };
    template<typename L, typename R>
    void operator()(L& lhs, R rhs) const { lhs -= rhs; } 
};

    auto parser = 
           double_                  [_val  = _1]
        >> *(   (lit('+') >> double_[bind(add_operand(), _val, _1)])
              | (lit('-') >> double_[bind(sub_operand(), _val, _1)])
            );
Run Code Online (Sandbox Code Playgroud)

哎哟,非常漂亮.


5.

但是,不用担心,你也可以适应这些:第5步

phx::function<add_operand> add_;
phx::function<sub_operand> sub_;

auto parser = 
       double_                  [_val  = _1]
    >> *(   (lit('+') >> double_[add_(_val, _1)])
          | (lit('-') >> double_[sub_(_val, _1)])
        );
Run Code Online (Sandbox Code Playgroud)

最后:去亲

最后,通过使用简单的AST,您可以在没有任何语义操作的情况下完成此操作:

rule<iterator_type, term<add>()     , ascii::space_type> add_term;
rule<iterator_type, term<subtract>(), ascii::space_type> sub_term;
rule<iterator_type, expression()    , ascii::space_type> parser;

add_term = '+' >> double_;
sub_term = '-' >> double_;
parser   = double_ >> *(add_term|sub_term);
Run Code Online (Sandbox Code Playgroud)

现在我们解析成表达式AST:

expression result;
ok = phrase_parse(begin, end, parser, ascii::space, result);
Run Code Online (Sandbox Code Playgroud)

我们使用eval函数打印结果:

std::cout << "parsed, result = " << eval(result) << std::endl;
Run Code Online (Sandbox Code Playgroud)

它是如何工作的?你自己看:

#define BOOST_SPIRIT_USE_PHOENIX_V3
#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>

/////////////////
// AST
template <typename> struct term {
    term(double value=0) : value(value) {}
    double value;
};

using operation = boost::variant<term<struct add>, term<struct subtract> >;

struct expression
{
    double initial;
    std::vector<operation> operations;
};

BOOST_FUSION_ADAPT_STRUCT(expression, (double, initial)(std::vector<operation>,operations))
// End of AST
/////////////////

double eval(expression const& e)
{
    double result = e.initial;

    struct visitor : boost::static_visitor<> {
        double& _v; visitor(double& ref) : _v(ref) {}
        void operator()(term<add>      const& rhs) const { _v += rhs.value; }
        void operator()(term<subtract> const& rhs) const { _v -= rhs.value; }
    };

    for(auto& o : e.operations)
        boost::apply_visitor(visitor(result), o);
    return result;
}

int main()
{
    const std::string INPUT_DATA = "12e-1 + 3.4 - .67";
    typedef std::string::const_iterator iterator_type;
    iterator_type begin = std::begin(INPUT_DATA);
    iterator_type end   = std::end(INPUT_DATA);

    namespace qi    = boost::spirit::qi;
    namespace ascii = boost::spirit::qi::ascii;

    bool ok;
    expression result;
    {
        using namespace qi;

        rule<iterator_type, term<add>()     , ascii::space_type> add_term;
        rule<iterator_type, term<subtract>(), ascii::space_type> sub_term;
        rule<iterator_type, expression()    , ascii::space_type> parser;

        add_term = '+' >> double_;
        sub_term = '-' >> double_;
        parser   = double_ >> *(add_term|sub_term);

        ok = phrase_parse(begin, end, parser, ascii::space, result);
    }

    if (ok  && begin == end)
        std::cout << "parsed, result = " << eval(result) << std::endl;
    else
        std::cout << "not parsed" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)