本文介绍了如何使用boost :: spirit将单词序列解析为向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习boost::spirit.例如,我正在尝试将单词序列解析为vector<string>.我尝试过:

I'm trying to learn boost::spirit. As an example, I'm trying to parse a sequence of words into a vector<string>. I tried this:

#include <boost/spirit/include/qi.hpp>
#include <boost/foreach.hpp>

namespace qi = boost::spirit::qi;

int main() {

  std::vector<std::string> words;
  std::string input = "this is a test";

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

  BOOST_FOREACH(std::string str, words) {
    std::cout << "'" << str << "'" << std::endl;
  }
}

这给了我这个输出:

'thisisatest'

但是我想要以下输出,其中每个单词分别匹配:

but I wanted the following output, where each word is matched separately:

'this'
'is'
'a'
'test'

如果可能的话,我希望避免为这种简单情况定义自己的qi::grammar子类.

If possible, I'd like to avoid having to define my own qi::grammar subclass for this simple case.

推荐答案

您从根本上误解了跳过解析器的目的(或至少滥用了).用作跳过解析器的qi::space用于使解析器的空白不可知,从而使a bab之间没有区别.

You're fundamentally misunderstanding the purpose of (or at least misusing) a skip parser – qi::space, used as a skip parser, is for making your parser whitespace agnostic such that there is no difference between a b and ab.

在您的情况下,空格 很重要,因为您希望它分隔单词.因此,您不应该跳过空格,而要使用qi::parse而不是qi::phrase_parse:

In your case, the whitespace is important, as you want it to delimit words. Consequently, you shouldn't be skipping whitespace, and you want to use qi::parse rather than qi::phrase_parse:

#include <vector>
#include <string>
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>

int main()
{
    namespace qi = boost::spirit::qi;

    std::string const input = "this is a test";

    std::vector<std::string> words;
    bool const result = qi::parse(
        input.begin(), input.end(),
        +qi::alnum % +qi::space,
        words
    );

    BOOST_FOREACH(std::string const& str, words)
    {
        std::cout << '\'' << str << "'\n";
    }
}

(现在已用G. Civardi的修复程序进行了更新.)

(Now updated with G. Civardi's fix.)

这篇关于如何使用boost :: spirit将单词序列解析为向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 11:01