【问题标题】:Using boost::spirit to match words使用 boost::spirit 匹配单词
【发布时间】:2016-10-10 00:16:29
【问题描述】:

我想创建一个解析器,它可以完全匹配字符串中的两个字母数字单词,例如:

message1 message2

然后将其保存到两个 std::string 类型的变量中。

我读过this previous answer,它似乎适用于无数次重复,它使用以下解析器:

+qi::alnum % +qi::space

但是当我尝试这样做时:

 bool const result = qi::phrase_parse(
            input.begin(), input.end(),
            +qi::alnum >> +qi::alnum,
            +qi::space,
            words
    );

words 向量包含不同字符串中的每个字母:

't'
'h'
'i'
's'
'i'
's'

这非常违反直觉,我不确定为什么会这样。有人可以解释一下吗?

另外,我可以填充两个预定义的字符串而不是 std::vector 吗?

最后说明:我想避免使用 using 语句,因为我想明确定义每个命名空间以帮助我理解 Spirit 的工作原理。

【问题讨论】:

    标签: c++ parsing boost boost-spirit


    【解决方案1】:

    是的,但船长会先忽略空格,然后您才能对其进行操作。

    使用lexeme控制船长:

    bool const result = qi::phrase_parse(
            input.begin(), input.end(),
            qi::lexeme [+qi::alnum] >> qi::lexeme [+qi::alnum],
            qi::space,
            words
    );
    

    注意船长应该是qi::space 而不是+qi::space

    另见Boost spirit skipper issues

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-12
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多