So, is it correct that I have to put always the longest option in this cases? Are there other approaches?
Not necessarily, you put first what has the highest priority.
You are right. I should rephrase my question in: Is it correct to put always the longest option when the others are a subset of it? :)
Yes. In your case the subset (or sub-langugage) relationship is quite clear, but generally that can be less obvious.
For example in the Smalltalk grammar
identifier / 'true' asParser
does not work, because 'true' is also an identifier. Reordering to
'true' asParser / identifier
alone does not solve the problem, because the identifier 'truehearted' would succeed as 'true' and 'hearted' would be tried on the following parser (the identifier parser would never be touched). To make it work you need something like
('true' asParser , #word asParser not) / identifier
what means that 'true' not directly followed by another identifier-character.
#not is a predicate (as well as #and) that does not consume input but that can provide an unlimited lookahead. This makes PetitParser able to parse more languages than a typical LR/LALR grammar.
Lukas