c++ - 使用 boost::spirit 在解析为结构时将解析值默认为较早的值

标签 c++ boost-spirit boost-spirit-qi

我通常熟悉使用 qi::attr 为解析输入中缺少的条目实现“默认值”。但当需要从早期解析中提取默认值时,我还没有看到如何执行此操作。

我正在尝试解析为以下结构:

struct record_struct {

    std::string Name;
    uint8_t Distance;
    uint8_t TravelDistance;
    std::string Comment;
};

来自相对简单的“(text)(number)[(number)][//comment]”格式,其中第二个数字和注释都是可选的。如果第二个数字不存在,则其值应设置为与第一个数字相同。

下面是工作代码的简化示例,但它并不能完全满足我的要求。此版本只是默认为 0 而不是正确的值。如果可能的话,我想将两个整数的解析隔离到单独的解析器规则,而不放弃使用融合结构。

我尝试过但尚未编译的事情:

  • qi::attr(0) 替换为 qi::attr(qi::_2)
  • 尝试在 attr 与语义操作 `qi::attr(0)[qi::_3 = qi::_2] 匹配后进行修改

完整的测试代码:

#include <string>
#include <cstdint>
#include <boost/spirit/include/qi.hpp>

struct record_struct {

    std::string Name;
    uint8_t Distance;
    uint8_t TravelDistance;
    std::string Comment;
};

BOOST_FUSION_ADAPT_STRUCT(
    record_struct,
    (std::string, Name)
    (uint8_t, Distance)
    (uint8_t, TravelDistance)
    (std::string, Comment)
)

std::ostream &operator<<(std::ostream &o, const record_struct &s) {
    o << s.Name << " (" << +s.Distance << ":" << +s.TravelDistance << ") " << s.Comment;
    return o;
}

bool test(std::string s) {
    std::string::const_iterator iter = s.begin();
    std::string::const_iterator end = s.end();
    record_struct result;
    namespace qi = boost::spirit::qi;
    bool parsed = boost::spirit::qi::parse(iter, end, (
                    +(qi::alnum | '_') >> qi::omit[+qi::space]
                    >> qi::uint_ >> ((qi::omit[+qi::space] >> qi::uint_) | qi::attr(0))
                    >> ((qi::omit[+qi::space] >> "//" >> +qi::char_) | qi::attr(""))
                ), result);
    if (parsed) std::cout << "Parsed: " << result << "\n";
    else std::cout << "Failed: " << std::string(iter, end) << "\n";
    return parsed;
}

int main(int argc, char **argv) {

    if (!test("Milan 20 22")) return 1;
    if (!test("Paris 8 9 // comment")) return 1;
    if (!test("London 5")) return 1;
    if (!test("Rome 1 //not a real comment")) return 1;
    return 0;
}

输出:

Parsed: Milan (20:22)
Parsed: Paris (8:9)  comment
Parsed: London (5:0)
Parsed: Rome (1:0) not a real comment

我想看到的输出:

Parsed: Milan (20:22)
Parsed: Paris (8:9)  comment
Parsed: London (5:5)
Parsed: Rome (1:1) not a real comment

最佳答案

首先,不要拼写 omit[+space] ,只需使用船长:

bool parsed = qi::phrase_parse(iter, end, (
                   qi::lexeme[+(alnum | '_')]
                >> uint_ >> (uint_ | attr(0))
                >> (("//" >> lexeme[+qi::char_]) | attr(""))
            ), qi::space, result);

在这里,qi::space是船长。 lexeme[]避免在子表达式内部跳过(参见 Boost spirit skipper issues )。

接下来,您可以通过多种方式做到这一点。

  1. 使用本地属性临时存储值:

    Live On Coliru

    rule<It, record_struct(), locals<uint8_t>, space_type> g;
    
    g %= lexeme[+(alnum | '_')]
         >> uint_ [_a = _1] >> (uint_ | attr(_a))
         >> -("//" >> lexeme[+char_]);
    
    parsed = phrase_parse(iter, end, g, space, result);
    

    这需要

    • 一个qi::rule声明声明qi::locals<uint8_t> ; qi::_a是该本地属性的占位符
    • 将规则初始化为“自动规则”( docs ),即使用 %=这样语义 Action 就不会覆盖属性传播
  2. 这里有一个古怪的混合体,你实际上并没有使用 locals<>但只是引用一个外部变量;这通常是一个坏主意,但由于您的解析器不是递归/可重入的,您可以这样做

    Live On Coliru

    parsed = phrase_parse(iter, end, (
                   lexeme[+(alnum | '_')]
                >> uint_ [ phx::ref(dist_) = _1 ] >> (uint_ | attr(phx::ref(dist_)))
                >> (("//" >> lexeme[+char_]) | attr(""))
            ), space, result);
    
  3. 您可以充分利用 Boost Phoenix 并直接从语义操作中处理值

    Live On Coliru

    parsed = phrase_parse(iter, end, (
                   lexeme[+(alnum | '_')]
                >> uint_ >> (uint_ | attr(phx::at_c<1>(_val)))
                >> (("//" >> lexeme[+char_]) | attr(""))
            ), space, result);
    
  4. 您可以解析为 optional<uint8_t>并对信息进行后处理

    Live On Coliru

    std::string              name;
    uint8_t                  distance;
    boost::optional<uint8_t> travelDistance;
    std::string              comment;
    
    parsed = phrase_parse(iter, end, (
                   lexeme[+(alnum | '_')]
                >> uint_ >> -uint_
                >> -("//" >> lexeme[+char_])
            ), space, name, distance, travelDistance, comment);
    
    result = { name, distance, travelDistance? *travelDistance : distance, comment };
    

后记

我注意到这个有点晚了:

If possible, I'd like to isolate the parsing of the two integers to a separate parser rule, without giving up using the fusion struct.

嗯,当然可以:

rule<It, uint8_t(uint8_t)> def_uint8 = uint_parser<uint8_t>() | attr(_r1);

这立刻更加准确,因为它不会解析不适合 uint8_t 的无符号值。 。上面的混合搭配: Live On Coliru

关于c++ - 使用 boost::spirit 在解析为结构时将解析值默认为较早的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27693835/

相关文章:

boost - boost boost::spirit::qi以使用STL容器

c++ - cmake opencv : Parse error in command line argument: -D 错误

c++ - 为什么这个 boost::spirit::qi 规则不起作用?

c++ - Boost.Spirit.Qi 使用中的错误雪崩

c++ - 在运行时动态组合 Boost.Spirit.Qi 规则(任意数量的备选方案)

c++ - boost qi::phrase_parse 只读取第一个元素

c++ - 一段时间后获取 std::bad_alloc

c++ - 在 Visual Studio 中使用 C++ 构建 Linux .so 库需要什么

c++ - 为什么 fmt 将 0.5 四舍五入到 0(小数点后为零)?

c++ - Spirit X3 没有抛出期望失败