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

How to parse entries followed by semicolon or newline (boost::spirit)?

本文关键字:boost spirit 换行符 何解析      更新时间:2023-10-16

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

示例输入,其中每个条目都是 int 和双精度

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,空间不匹配是有道理的,但我无法找到解决方案。

这是我

的看法。

  • 您可能想知道qi::blank(除qi::eol外,qi::space(。这将消除对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;
}

好的,我发现这工作正常:

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

所以眼前的问题解决了。

但是,如果选项卡出现在1.4中,它仍然会失败

12 1.4
63 13.2

这会更好,但它不会编译:

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

错误:

error: invalid static_cast from type ‘const std::pair<int, double
>’ to type ‘int’