1

I'm trying to get into boost spirit 2, but the following code does not work as expected:

template<typename Iterator>
struct my_grammar : qi::grammar<Iterator, std::string()>
{
    my_grammar() : my_grammar::base_type(time_literal)
    {
        using ascii::char_;
        using ascii::digit;

        time_literal = digit >> -digit >> char_(':') >> digit >> digit >> -(char_(':') >> digit >> digit);
    }

    qi::rule<Iterator, std::string()> time_literal;
};

void main()
{
    my_grammar<std::string::iterator> g;
    std::string input("01:02:03");
    std::string::iterator begin = input.begin();
    std::string::iterator iter = begin;
    std::string::iterator end = input.end();
    std::string result;
    bool matched = phrase_parse(iter, end, g, ascii::space, result);
    std::cout << (matched ? "matched "+std::string(begin, iter) : "no match") << std::endl;
    if (iter != end)
        std::cout << "remaining: " << std::string(iter, end) << std::endl;
    else
        std::cout << "result: " << result << std::endl;
    std::cout << std::endl;
}

This prints:

matched: 01:02:03
result: 01:02:

But I expected to see:

matched: 01:02:03
result: 01:02:03

So where did those last two digits go and how can I get them back?

  • 1
    Worked as you expected for me with boost 1.51.0. What version are you using? – pelletjl Jan 11 '13 at 22:43
  • I'm still using boost 1.46.1 and since backwards compatibility has been an issue with the boost libraries in the past, upgrading will not be an option for the near future. – Alexander Tobias Bockstaller Jan 12 '13 at 11:49
  • 1
    Your code works correctly for me on boost 1.49.0. Your spirit version is 2.4.2 whereas both pelletj and I are on spirit 2.5.1. You could try breaking the rule up into hours, minutes, seconds and see if that fixes it. Also, try switching on `BOOST_SPIRIT_DEBUG` and friends to see if that delivers any insight. – FatalFlaw Jan 23 '13 at 09:19

0 Answers0