3

在一个简单的解析器测试Live On Coliru中,

std::string str("x123x");
boost::iterator_range<boost::range_iterator<decltype(str)>::type> attr;
if( x3::parse( boost::begin(str), boost::end(str), x3::lit('x') >> x3::raw[+x3::digit] >> x3::lit('x'), attr ) ) {
    std::cout<<"Match! attr = "<<attr<<std::endl;
} else {
    std::cout<<"Not match!"<<std::endl;
}

解析器

x3::lit('x') >> x3::raw[+x3::digit] >> x3::lit('x')

应该合成一个 type 的属性boost::iterator_range<Iterator>。但它无法编译。如果我们删除两者中的任何一个x3::lit('x'),它就会编译。尽管Live on Coliru ,但相同的代码可以用 qi 编译。

4

1 回答 1

2

有趣的。实际上它确实编译:

Live On Coliru

#include <iostream>
#include <boost/spirit/home/x3.hpp>

namespace x3 = boost::spirit::x3;

int main() {
    std::string const str("x123x");
    boost::iterator_range<std::string::const_iterator> attr;
    if(x3::parse(boost::begin(str), boost::end(str), x3::raw[+x3::digit], attr)) {
        std::cout<<"Match! attr = "<<attr<<std::endl;
    } else {
        std::cout<<"Not match!"<<std::endl;
    }
}

让它崩溃的是周围的环境:

// simple (ok):
x3::parse(boost::begin(str), boost::end(str), x3::raw[+x3::digit], attr);
// ok:
parse(boost::begin(str), boost::end(str), x3::eps >> x3::raw[+x3::digit], attr);
parse(boost::begin(str), boost::end(str), x3::raw[+x3::digit] >> x3::eps, attr);
// breaks:
parse(boost::begin(str), boost::end(str), x3::eps >> x3::raw[+x3::digit] >> x3::eps, attr);

我的猜测是元编程iterator_range在这种情况下错误地将其视为融合序列。当然,我认为这是一个错误。

您应该向上游报告。

可悲的是,我还没有找到“顺势疗法”的解决方法。

于 2017-03-23T00:21:43.733 回答