完全工作的演示:住在科利鲁 http://coliru.stacked-crooked.com/view?id=a5d6bd0dfd0fd832103f5165dac13ead-f468fd186ebe6d5f25efae86bd4f0130
与天真的方法相比,这增加了raw
值可以选择结束于]
如果状态是in_group
.
我选择通过该州使用继承属性 (bool
).
这个实现允许nested以及子组,例如:"[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"));
}
Prints:
w1
w2
w3
w2
w3
w4
w1
w2
w3
w2
w3
w4
w1
w2
w3
w2
w3
w4
(除了调试信息)