Re: rule's attribute captures too much?

Joel de Guzman <[email protected]>
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <[email protected]>
On 09/11/2016 4:44 PM, [email protected] wrote:
> Hello,
>
> Trying to parse "0/1/x" with
>
> qi::ascii::digit >> +(qi::char_('/') >> qi::ascii::digit)
>
> I had expected the synthesized attributes value to be "0/1".
>
> But he following code results in "0/1/", where the suffix is "/x". Is this the correct behavior?

It might not be intuitive, but I think it is the correct behavior. Qi
is greedy and will eat as much as it can. '/' is valid at that point,
while digit, after it, will fail and thus exit the + loop. The other
interpretation will be very inefficient requiring backtracking and
attribute saving and try-commit semantics.

If you want the behavior you expect, use syntactic predicates or
some other means to test first before committing the attribute.
Example:

   digit >> +(('/' >> &digit) >> attr('/') >> digit)

Regards,
-- 
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/


------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.