[antlr-interest] Disregarding subrule ordering

Martin Probst mail at martin-probst.com
Fri Dec 2 06:05:42 PST 2005


Hi,

> In this particular case, if I declare a number of booleans (or a
> bitset) in my parser and do something like below:
> 
> object:
>     KW_OBJECT
>     IDENTIFIER
>     LBRACE { //set all bools to false here
>     (
>         { !param1bool }? param1 { param1bool = true; }
>         { !param2bool }? param2 { param2bool = true; }
>         { !param3bool }? param3 { param3bool = true; }
>         { !optParam1bool }? optParam1 { optParam1bool = true; }
>         { !optParam2bool }? optParam2 { optParam2bool = true; }
>     )*
>     RBRACE { if(! (param1bool && param2bool && param3bool) ) throw ....)
> 
> Haven't tested this yet so it might not even work, but other than pure
> design considerations; are there reasons for not doing this directly
> in the parser?

Both will work. Though many people consider it more elegant to do only
parsing and tree building in the parser, and everything that smells like
semantics is put in a later phase (separation of concerns a like, it
might make it easier to define a fixed tree structure, that is
independent of any optimisations etc.). It's mostly a matter of style.

In your case you forgot to add "|" between the single cases, otherwise I
don't see any error.

> I'm beginning to think I might just dodge this by enforcing strict
> ordering and be done with it. Since I'm designing the grammar for my
> own small script ground up I can do as I please... :) OTOH I really
> like the free ordering...

Well, that's always the question in language design, isn't it? How much
freedom to the user and how much enforcing certain coding styles. I'm
personally more a fan of a stricter style, at least for programming
languages (in contrast to XML formats).

Martin



More information about the antlr-interest mailing list