Boost logo

Boost Users :

Subject: Re: [Boost-users] Spirit: how to force backtracking
From: Larry Evans (cppljevans_at_[hidden])
Date: 2017-11-03 18:23:15


On 11/03/2017 05:20 AM, Stian Zeljko Vrba via Boost-users wrote:
> I have the following grammar rule:
>
>
> value = (int_ % ',') | *ascii::print;
>
> The intention is to parse either a comma-separated list of integers or a string. The rule as such fails in cases like "123ABC", which should be parsed as string.
> From the description of the | operator, I would expect the parser to backtrack and try the other alternative when it sees that the first alternative does not match the input. However, I get a parse error instead (this rule is a part of a larger grammar), I guess because the first alternative matches the single integer but the rest of the grammar cannot be matched.
>
> How to rewrite the grammar to resolve this ambiguity, or force spirit to backtrack if the first alternative of 'value' cannot be matched?
>
What happens when you reverse the order of the alternatives:

   value = *ascii::print | (int_ % ',');


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net