Skip to content

How could I tell different rules with the same prefix? #332

Answered by ColinH
shuizaiku7 asked this question in Q&A
Discussion options

You must be logged in to vote

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 write seq< A, sor< B, C > >, however with your rule it is not quite that simple and while you could rewrite it as

if_then_else< one< '.' >, plus< digit >, seq< plus< digit >, opt< one< '.' >, plus< digit > > > >

you might prefer sticking to your version, though with a couple…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@shuizaiku7
Comment options

Answer selected by shuizaiku7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants