Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost Spirit Qi - Duplicate last letter with stream-based parsing

This may be very obvious, but why does the stream-based parsing in boost duplicate the last letter? I must be doing something wrong:

#include <iostream>
#include <sstream>

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

namespace qi = boost::spirit::qi;

int main() {
    std::string input = "hello";
    std::stringstream ss(input);

    std::string r1, r2;
    boost::spirit::istream_iterator first(ss), last;

    qi::phrase_parse(input.begin(), input.end(), qi::lexeme[qi::alpha >> *qi::alnum], qi::space, r1);

    std::cout << r1 << std::endl; // prints "hello"

    qi::phrase_parse(first, last, qi::lexeme[qi::alpha >> *qi::alnum], qi::space, r2);

    std::cout << r2 << std::endl; // prints "helloo"
}

Tested with XCode 5.0 and Boost 1.54.0.

Edit: The problem seems to be specific to libc++. Anybody using Clang care to confirm?

like image 274
kloffy Avatar asked Oct 16 '13 10:10

kloffy


1 Answers

If I understood correctly, you're not supposed to use input iterators, because they can give problems with backtracking. Maybe it's just sheer luck/a difference in implementation that this sometimes works at all.

like image 124
ecotax Avatar answered Oct 17 '22 03:10

ecotax