0

I'd like to parse a sequence of integers into an std::vector<int>, using boost::spirit. The integers may be separated by a semicolon or a newline.

But this grammar doesn't compile:

typedef std::vector<int> IntVec;
template <typename Iterator, typename Skipper>
struct MyGrammar : qi::grammar<Iterator, IntVec(), Skipper> {
  MyGrammar() : MyGrammar::base_type(start) {
    start = +(qi::int_
              >> (";" | qi::no_skip(qi::eol)));
  }
  qi::rule<Iterator, IntVec(), Skipper> start;
};

To be clear, I want to parse the following input, for example,

1; 2; 3
4 ; 5

into one vector (1,2,3,4,5). How can I do that and why does my version not compile?

Can I somehow write the separator ("semicolon or newline") as its own rule? What would its return type be? Some kind of null value?

Frank
  • 64,140
  • 93
  • 237
  • 324

1 Answers1

2

It looks like the skipper is being applied when checking the semicolon, and so the skip characters (including newline) have already been consumed once qi::no_skip[qi::eol] is reached. The following is working for me, with the no_skip token first:

    start = qi::int_ % (qi::no_skip[qi::eol] | ';');

I'm using % so that the final integer does not need to be followed by a semicolon or end-of-line.

Dan Garant
  • 723
  • 5
  • 12