期待char_解析器的序列和交替以合成字符串

问题描述 投票:1回答:1

在以下测试案例中,一个alpha和一个序列炸弹的交替具有很长的错误转储,基本上说static assertion failed: The parser expects tuple-like attribute type。凭直觉,我希望整个规则都能产生一个字符串,但事实并非如此。我要么必须将替换的左侧更改为+alpha(使两个向量都成为矢量),要么要走语义动作的路径,至少对于替换中的单个char(附加到_val)。或者,将单独的左侧char_更改为string。无论如何,我想不出什么简单的方法来解析这样的字符串,任何提示都是值得的。 TIA。

#include <iostream>

#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;

namespace grammar {

using x3::char_;
using x3::alpha;
using x3::xdigit;

const auto x =
    x3::rule< struct x_class, std::string > { "x" } =
    char_('/') > alpha >> *(alpha | (char_('#') > xdigit));

} // namespace grammar

int main () {
    std::string input{ "/Foobar#F" }, attr;
    auto iter = input.begin ();

    if (phrase_parse (iter, input.end (), grammar::x, x3::space, attr)) {
        std::cout << attr << std::endl;
    }

    return 0;
}
boost boost-spirit boost-spirit-x3
1个回答
0
投票

我也讨厌这种行为。齐在这方面更自然。

老实说,我并不总是知道如何“修复”它,但>

  • 在这种情况下,您似乎可以使用raw[]-简化语法
  • 有时有助于避免混合operator>operator>>
  • 这是我为您的语法所做的:

[Live On Coliru

] >>
#include <iostream>
#include <boost/spirit/home/x3.hpp>
namespace x3 = boost::spirit::x3;

namespace grammar {
    const auto x =
        x3::rule<struct x_class, std::string> { "x" } =
        x3::raw [ '/' > x3::alpha >> *(x3::alpha | ('#' > x3::xdigit)) ];
}

int main () {
    std::string input{ "/Foobar#F" }, attr;
    auto iter = input.begin ();

    if (phrase_parse (iter, input.end (), grammar::x, x3::space, attr)) {
        std::cout << attr << std::endl;
    }
}

打印

/Foobar#F
© www.soinside.com 2019 - 2024. All rights reserved.