-1

> qi::double_ v.s. >> qi::double_

I want to parse the following string

    "***: @a_-091 , *** 1"

to a struct defined as

    using type = boost::fusion::vector<char, int, double>;

When the parser

    *qi::omit[qi::char_ - '@'] >> '@' >> qi::char_ >> '_' >> qi::int_ >> *qi::omit[qi::char_ - qi::digit] >> qi::double_

is used, the result is OK. However, the result is totally different with the following parser

    *qi::omit[qi::char_ - '@'] >> '@' >> qi::char_ >> '_' >> qi::int_ >> *qi::omit[qi::char_ - qi::digit] > qi::double_

The following is the sample code.

    #include <vector>
    #include <sstream>
    #include <iostream>
    #include <boost/spirit/include/qi.hpp>
    #include <boost/spirit/include/qi_match.hpp>
    #include <boost/fusion/include/io.hpp>

    using type = boost::fusion::vector<char, int, double>;

    int main() {
        std::istringstream istr{
            "***: @a_-091 , *** 1"
        };

        std::vector<type> data;

        namespace qi = boost::spirit::qi;
        istr >> std::noskipws >> qi::match(*(
            *qi::omit[qi::char_ - '@'] >> '@' >> qi::char_ >> '_' >> qi::int_ >> *qi::omit[qi::char_ - qi::digit] > qi::double_
            ), data);
        // istr >> std::noskipws >> qi::match(*(
            *qi::omit[qi::char_ - '@'] >> '@' >> qi::char_ >> '_' >> qi::int_ >> *qi::omit[qi::char_ - qi::digit] >> qi::double_
            ), data);

        for (size_t i = 0; i != data.size(); ++i) {
            std::cerr << data[i] << "\n";
        }

        return 0;
    }

PS: The problem has been solved by the link proved by cv_and_he in the comment. It is caused by the mixed usage of ">>" (the sequence parser) and ">" (the expectation parser).

cqdjyy01234
  • 1,180
  • 10
  • 20
  • Two suggestions: one question per question, and code must be in the question. And describe the code, describe what it's doing (or not doing) and what you expected instead. (And comments go into the comment section, not in the question.) – Mat May 30 '15 at 10:29
  • @Mat Thanks. Look forward for further suggestions. – cqdjyy01234 May 30 '15 at 10:35
  • You talk about two things that do different stuff. Yet you show only one piece of code and describe neither. – Mat May 30 '15 at 10:40
  • @Mat I don't quite understand. As you suggested, I remove a problem asked. Now, the problem is the different behavior betwwen ">>" and ">". – cqdjyy01234 May 30 '15 at 10:44
  • Why would you expect the same thing when using two different parsers? What behavior exactly don't you understand? What results are you getting and what were you expecting? – Mat May 30 '15 at 10:49
  • @Mat Yes, they are different parsers. When ">>" is used, the double is parsed succesfully. Thus, ">" should successed as well. I think the only difference between the two parsers is that ">>" allows failure, but ">" does not (throw an exception instead). – cqdjyy01234 May 30 '15 at 10:53
  • Explain all that _in your question_. – Mat May 30 '15 at 10:56
  • 1
    I believe your problem is similar to the one described [here](http://boost.2283326.n4.nabble.com/Unexpected-behavior-using-versus-tp4662087p4662134.html). – llonesmiz May 30 '15 at 11:06
  • http://coliru.stacked-crooked.com/a/a88056650c51580b – llonesmiz May 30 '15 at 11:13
  • [This](http://coliru.stacked-crooked.com/a/705d921eaf86a031) also works, but it's awful. – llonesmiz May 30 '15 at 11:24

1 Answers1

0

As a quick hint, perhaps you can rewrite using >>.

To still have the expectation points you can perhaps write

a >> b > c

like

a >> b >> (c > eps)

I will test this later

sehe
  • 374,641
  • 47
  • 450
  • 633