How to analyze records followed by a semicolon or a new line (boost :: spirit)?

In Boost :: Spirit, how can I parse records followed by either a semicolon or a new line with an optional semicolon?

An example input where each entry is an int and a double:

12 1.4;
63 13.2
2423 56.4 ; 5 8.1

Here is an example of code that simply parses records followed by spaces:

#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;
}

Now, to parse what I want (each record, followed by a semicolon or a new line with an optional semicolon), I replaced this:

    entries = +entry;

:

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

where the operator boost::spirit ||means: (a follows additionally b) or b. But it gives an error if there is a space after in this input example 1.4:

12 1.4
63 13.2

It makes sense that the space does not match due no_skip, but I could not find a solution.

+5
2

.

  • , 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;
}
+6

, , :

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
+1

All Articles