用Boost.spirit解析一个简单的重复文本宏 [英] parsing a simple repeated text macro with Boost.spirit

查看:150
本文介绍了用Boost.spirit解析一个简单的重复文本宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用Boost.Spirit库来解析字符串。它似乎是一个非常好的工具,但也很困难。所以,我想解析一个字符串与一些单词用 / 分隔,并将它们放在一个字符串的向量。这里有一个例子: word1 / word2 / word3 。这是一个简单的任务,我可以这样做与下面的功能:

I'm learning how to use Boost.Spirit library for parsing strings. It seems to be a very nice tool but difficult as well. So, I want to parse a string with some words separated with / and put them in a vector of strings. Here is an example:word1/word2/word3. That's a simple task, I can do this with the following finction:

bool r = phrase_parse(first, last, (+~char_("/") % qi::lit("/")),space,v)

其中 v std :: vector< std :: string> 。但一般来说,我想解析类似 w1 / w2 / w3的 w1 / [w2 / w3] 2 / w4 / w2 / w3 / w4 ,即 [w2 / w3] 2 表示 w2 / w3 重复两次。有谁能给我一些想法?我阅读了文档,但仍然有一些问题。

where v is std::vector<std::string>. But in general, I'd like to parse something like w1/[w2/w3]2/w4 which is equivalent to w1/w2/w3/w2/w3/w4, that is [w2/w3]2 means that w2/w3 is repeated twice. Could anyone give me some ideas on that? I read the documentation but still have some problems.

提前感谢!

推荐答案

演示: Live on Coliru

Fully working demo: live on Coliru

通过一个朴素的方法,这增加了 raw 值可选在] 如果状态是 in_group

What this adds over a naive approach is that raw values are optionally ended at ] if the state is in_group.

我选择使用 inherited属性传递状态 bool )。

此实现允许以及,例如:[w1 / [w2 / w3] 2 / w4] 3

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

namespace phx = boost::phoenix;

int main()
{
    typedef std::string::const_iterator It;
    const std::string input = "[w1/[w2/w3]2/w4]3";

    std::vector<std::string> v;
    It first(input.begin()), last(input.end());

    using namespace boost::spirit::qi;

    rule<It, std::string(bool in_group)> raw;
    rule<It, std::vector<std::string>(bool in_group), space_type> 
        group, 
        delimited;

    _r1_type in_group; // friendly alias for the inherited attribute

    raw       = eps(in_group) >> +~char_("/]") 
              | +~char_("/");

    delimited = (group(in_group)|raw(in_group)) % '/';

    group     = ('[' >> delimited(in_group=true) >> ']' >> int_) 
        [ phx::while_(_2--) 
            [ phx::insert(_val, phx::end(_val), phx::begin(_1), phx::end(_1)) ]
        ];

    BOOST_SPIRIT_DEBUG_NODES((raw)(delimited)(group));

    bool r = phrase_parse(first, last, 
            delimited(false),
            space,v);

    if (r)
        std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}

列印:

w1
w2
w3
w2
w3
w4
w1
w2
w3
w2
w3
w4
w1
w2
w3
w2
w3
w4

(除了调试信息)

这篇关于用Boost.spirit解析一个简单的重复文本宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆