Why doesn't this improve the :: spiritual match of foo123 to the grammar (+ alpha | + alnum)?

I have a more complex boost :: spirit grammar that doesn't match as I expected. I was able to break it down into this minimal example: http://ideone.com/oPu2e7 (does not compile there, but compiles with VS2010)

This is basically my grammar:

my_grammar() : my_grammar::base_type(start)
{
    start %=
        (+alpha | +alnum)
    ;
}
qi::rule<Iterator, std::string(), ascii::space_type> start;

      

It matches foobar, 123foo, but not foo123. What for? I expect it to fit all three.

+3


source to share


1 answer


PEG parsers match greedy ones, from left to right. That should be enough to explain.

But let's look at foo123

: it matches "1 or more +alpha

, so the first branch is taken. The second branch is not taken, so the number 123

is left unconsidered.



There is no "inherent" return on kleen operations. You / can / use rollback if you know eg. what you need to parse the full input:

 (+alpha >> eoi | +alnum >> eoi)

      

+2


source







All Articles