How could I tell different rules with the same prefix? #332
-
if the input is 1.2, it will match integer rule, namely just match 1 I've got 2 ways to address this. Is there any simpler way to solve this problem? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Given the deterministic semantics of PEGs, which is a fancy way of saying that Now in general it's a good idea to factor out common prefixes, so instead of
you might prefer sticking to your version, though with a couple of "renamed" intermediate rules you can make it clearer which role each set of digits is playing and simplify attaching your actions:
https://github.com/taocpp/PEGTL/blob/main/doc/Rule-Reference.md#if_then_else-r-s-t- |
Beta Was this translation helpful? Give feedback.
Given the deterministic semantics of PEGs, which is a fancy way of saying that
sor<>
goes from left to right and stops at the first sub-rule that matched (even if a later rule would be a "better" match), it is normal and expected to carefully order the sub-rules to avoid this prefix problem you encountered.Now in general it's a good idea to factor out common prefixes, so instead of
sor< seq< A, B >, seq< A, C >
you'd writeseq< A, sor< B, C > >
, however with your rule it is not quite that simple and while you could rewrite it asif_then_else< one< '.' >, plus< digit >, seq< plus< digit >, opt< one< '.' >, plus< digit > > > >
you might prefer sticking to your version, though with a couple…