本文介绍了不提倡精神精神行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Boost Spirit解析字符串,如下所示:

I've been trying to parse a string with Boost Spirit like following:

integer_count int1 int2 int3 ... intN

其中N是integer_count.例如,

Where N is the integer_count. For example,

5 1 2 3 4 5

代码如下:

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

namespace qi = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;
using boost::phoenix::ref;

template <typename Iterator>
struct x_grammar : public qi::grammar<Iterator, std::string(), ascii::space_type>
{
public:
    x_grammar() : x_grammar::base_type(start_rule, "x_grammar")
    {
        using namespace qi;
        int repeat_count = 0;
        start_rule = int_[ref(repeat_count) = _1] > repeater > *char_;
        std::cout << "repeat_count == " << repeat_count << std::endl;
        repeater = repeat(repeat_count)[int_[std::cout << _1 << ".\n"]];
    }
private:
    qi::rule<Iterator, std::string(), ascii::space_type> start_rule;
    qi::rule<Iterator, std::string(), ascii::space_type> repeater;
};

int main()
{
    typedef std::string::const_iterator iter;
    std::string storage("5 1 2 3 4 5 garbage");
    iter it_begin(storage.begin());
    iter it_end(storage.end());
    std::string read_data;
    using boost::spirit::ascii::space;
    x_grammar<iter> g;
    try {
        bool r = qi::phrase_parse(it_begin, it_end, g, space, read_data);
        if(r) {
            std::cout << "Pass!\n";
        } else {
            std::cout << "Fail!\n";
        }
    } catch (const qi::expectation_failure<iter>& x) {
        std::cout << "Fail!\n";
    }
}

输出为:

repeat_count == 0
Pass!

这意味着

ref(repeat_count) = _1

从未调用过.那么有没有办法将整数运行时读入局部变量并使用该值进行进一步解析?

Was never invoked.So is there a way to read an integer runtime into a local variable and use that value for further parsing?

推荐答案

我想你的意思是

repeat(boost::phoenix::ref(repeat_count))

此外,当存在语义动作时,规则的自动属性传播也将被禁用.您可以使用%=强制属性传播

Also, automatic attribute propagation is disabled for a rule when there are semantic actions. You can use %= to force attribute propagation

这是一个简单的固定版本 在Coliru上直播

Here's a simple fixed version Live On Coliru

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

namespace qi = boost::spirit::qi;
namespace spirit = boost::spirit;
namespace ascii = boost::spirit::ascii;
using boost::phoenix::ref;

template <typename Iterator>
struct x_grammar : public qi::grammar<Iterator, ascii::space_type>
{
public:
    x_grammar() : x_grammar::base_type(start_rule, "x_grammar")
    {
        using namespace qi;
        int repeat_count = 0;
        start_rule = int_[ref(repeat_count) = _1] > repeater > *char_;
        std::cout << "repeat_count == " << repeat_count << std::endl;
        repeater = repeat(ref(repeat_count))[int_[std::cout << _1 << ".\n"]];
    }
private:
    qi::rule<Iterator, ascii::space_type> start_rule;
    qi::rule<Iterator, ascii::space_type> repeater;
};

int main()
{
    typedef std::string::const_iterator iter;
    std::string storage("5 1 2 3 4 5 garbage");
    iter it_begin(storage.begin());
    iter it_end(storage.end());
    using boost::spirit::ascii::space;
    x_grammar<iter> g;
    try {
        bool r = qi::phrase_parse(it_begin, it_end, g, space);
        if(r) {
            std::cout << "Pass!\n";
        } else {
            std::cout << "Fail!\n";
        }
    } catch (const qi::expectation_failure<iter>& x) {
        std::cout << "Fail!\n";
    }
}

打印

repeat_count == 0
1.
2.
3.
4.
5.
Pass!

注意:显然,repeat_count == 0总是被打印,因为它是在语法构建过程中执行的,而不是解析过程.

Note How, obviously, repeat_count == 0 is always printed, since it's executed during grammar construction, not parsing.

这篇关于不提倡精神精神行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 00:42