parboiled icon indicating copy to clipboard operation
parboiled copied to clipboard

Check for "FirstOf alternative prefix of later one" problem

Open sirthias opened this issue 15 years ago • 5 comments

The mistake of having a FirstOf alternative be a prefix of a subsequent one is common and sometimes hard to see. It'd be great if parboiled could automatically detect these grammar problems and report them. Actually this would likely be a byproduct of implementing #19

sirthias avatar Apr 22 '11 20:04 sirthias

Are you saying that the user should be advised by the parboiled to reorder choices with a leading substring in a particular manner ? I often thought that if you have a grammar like foo and foobar; the user should declare foobar first to avoid backtracking.

javouhey avatar Apr 25 '11 11:04 javouhey

If you have a rule like this in your grammar:

FirstOf("foo", "foobar")

The second alternative will never match! Ever! In fact this rule is equivalent to one just directly matching "foo". Backtracking is not going to help (see the parboiled mailing list for a recent discussion on this). So parboiled can and should warn you, when you write things like this. It is almost always an error.

sirthias avatar Apr 25 '11 13:04 sirthias

This would be a very helpful improvement. I would like to see validation of a grammar for:

  • left recursion
  • *a a ; +a a #=> always fails
  • optional(a) | optional(b) #=> never matches the alternative
  • *ANY x #=> this only makes sense if x == EOI i think
  • a | a
  • *optional(a) ; +optional(a) #=> endless loop?
  • probably more

Ragmaanir avatar Jun 23 '11 10:06 Ragmaanir

Yes, all these things would be nice if they were available. However, IMHO the "FirstOf alternative prefix of later one" error is the most important one, since it can be hard to "see" and difficult to find, whereas all the ones you listed are somewhat easier to spot and fix manually...

sirthias avatar Jun 23 '11 12:06 sirthias

Maybe a LongestOf()?

I have a similar problem with CSS where, for instance, font-size has "small" as a possible value, and font-variant has "small-caps". My plan was to implement LongestOf() somehow, I just don't know how :/

fge avatar Jan 01 '12 16:01 fge