[antlr-interest] Allowing random order via grammar-only notation ...

Gavin Lambert antlr at mirality.co.nz
Thu Jan 29 01:54:27 PST 2009


At 21:15 29/01/2009, Avid Trober wrote:
>Can grammar syntax alone accomplish this?  Or, am I going to have 
>to rely on some additional code of my own such as actions? gated 
>predicates? other?
>
>1. A command, COMMAND, can be followed by three modifiers, M1, M2 
>and M3.
>
>2. Each modifier has its own cardinality:
>     M1?
>     M2*
>     M3+
[...]
>Modifiers are not simple tokens, they're subrules.  I realize 
>some synpreds are needed to clear up non-determinism there.  But, 
>I want to make sure I can't do this via a grammar notation before 
>resorting to more complexity.

I think the way to do that is to be more lenient in the grammar 
(accepting any of the three in any order and quantity), and then 
do some validation afterwards to detect illegal cases such as too 
many M1s or not enough M3s:

   command : 'COMMAND' (M1 | M2 | M3)* { /* some validation */ };

Besides, you can produce better error messages doing it this way, 
anyway ;)



More information about the antlr-interest mailing list