如何解析分号后跟分号或换行符(boost :: spirit)?

时间:2012-05-19 22:29:05

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

在Boost :: Spirit中,如何解析后面跟分号或带有可选分号的换行符的条目?

示例输入,其中每个条目都是int和double:

12 1.4;
63 13.2
2423 56.4 ; 5 8.1

以下是解析条目后跟空格的示例代码:

#include <iostream>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/fusion/include/std_pair.hpp>
namespace qi = boost::spirit::qi;

typedef std::pair<int, double> Entry;

template <typename Iterator, typename Skipper>
struct MyGrammar : qi::grammar<Iterator, std::vector<Entry>(), Skipper> {
  MyGrammar() : MyGrammar::base_type(entries) {
    entry = qi::int_ >> qi::double_;
    entries = +entry;
  }
  qi::rule<Iterator, Entry(), Skipper> entry;
  qi::rule<Iterator, std::vector<Entry>(), Skipper> entries;
};

int main() {
  typedef boost::spirit::istream_iterator It;
  std::cin.unsetf(std::ios::skipws);
  It it(std::cin), end;

  MyGrammar<It, qi::space_type> entry_grammar;
  std::vector<Entry> entries;
  if (qi::phrase_parse(it, end, entry_grammar, qi::space, entries)
      && it == end) {
    BOOST_FOREACH(Entry const& entry, entries) {
      std::cout << entry.first << " and " << entry.second << std::endl;
    }
  }
  else {
    std::cerr << "FAIL" << std::endl;
    exit(1);
  }
  return 0;
}

现在,为了解析我想要的方式(每个条目后跟分号或带有可选分号的换行符),我将其替换为:

    entries = +entry;

由此:

 entries = +(entry >> (qi::no_skip[qi::eol] || ';'));

boost::spirit运算符||表示:( a后跟可选的b)或b。但是如果在此示例输入中1.4后面有空格,则会出错:

12 1.4
63 13.2

由于no_skip空间不匹配,但我无法找到解决方案。

2 个答案:

答案 0 :(得分:6)

这是我的看法。

  • 您可能想了解qi::blank(除qi::space之外的qi::eol)。这样就无需no_skip
  • 核心语法变为:

        entry = qi::int_ >> qi::double_;
        entries = entry % +qi::char_("\n;") >> qi::omit[*qi::space];
    
  • 使用BOOST_SPIRIT_DEBUG来了解解析失败的原因和原因(例如回溯)

输出:

12 and 1.4
63 and 13.2
2423 and 56.4
5 and 8.1

完整代码:

//#define BOOST_SPIRIT_DEBUG
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/fusion/include/std_pair.hpp>
namespace qi = boost::spirit::qi;

typedef std::pair<int, double> Entry;

template <typename Iterator, typename Skipper>
struct MyGrammar : qi::grammar<Iterator, std::vector<Entry>(), Skipper> {
    MyGrammar() : MyGrammar::base_type(entries) {
        entry = qi::int_ >> qi::double_;
        entries = 
            entry % +qi::char_("\n;")          // the data
            >> qi::omit[*qi::space] > qi::eoi; // trailing whitespace
        BOOST_SPIRIT_DEBUG_NODE(entry);
        BOOST_SPIRIT_DEBUG_NODE(entries);
    }
    qi::rule<Iterator, Entry(), Skipper> entry;
    qi::rule<Iterator, std::vector<Entry>(), Skipper> entries;
};

int main() {
    typedef boost::spirit::istream_iterator It;
    std::cin.unsetf(std::ios::skipws);
    It it(std::cin), end;

    MyGrammar<It, qi::blank_type> entry_grammar;
    std::vector<Entry> entries;
    if (qi::phrase_parse(it, end, entry_grammar, qi::blank, entries)
            && it == end) {
        BOOST_FOREACH(Entry const& entry, entries) {
            std::cout << entry.first << " and " << entry.second << std::endl;
        }
    }
    else {
        std::cerr << "FAIL" << std::endl;
        exit(1);
    }
    return 0;
}

答案 1 :(得分:1)

好的,我发现这很好用:

entries = +(entry >> (qi::no_skip[*qi::lit(' ') >> qi::eol] || ';'));

所以直接的问题就解决了。

但如果选项卡出现在{/ 1}}

中,它仍会失败
1.4

这会更好,但不会编译:

12 1.4
63 13.2

错误:

entries = +(entry >> (qi::no_skip[*qi::space >> qi::eol] || ';'));