[antlr-interest] Where to put general intialisation logic for a parser?

Johannes Luber jaluber at gmx.de
Tue Apr 10 14:55:08 PDT 2007


David Holroyd wrote:
> How about putting a (dirty dirty!) instance initialiser block into
> @members?
> 
> i.e.,
> 
>   @members {
>     {
>       ...init statements here...
>     {
>   }

Compared to the extra constructor suggestion of Terrence it has the
definitive advantage, that one doesn't need a dummy argument to
distinguish between the the normal variant and the modified one.
Unfortunately it isn't convertible to C# (though I use Java until C# has
been finished), so I'll use constructor overloading.

But this causes a small problem - is the class name always "GrammarName"
+ "Parser"? What if one changes the base class? It looks to me, as if a
special variable $ctor available in @members and filled with the current
class name would be a good idea. To use this variable would immunize the
code against grammar name changes, too. Bugs caused by innocent changes
are the worst to find. :(

> That'll confuse anyone who looks at the grammar :)

Guaranteed! I had to look up, what instance initialisers were because I
haven't used them myself. I can understand that the syntax is simply a
static initialiser without static, but it is cryptic nonetheless and a
violation of the expectation that no {} can come up without being in a
function (or associated with a class).

Thanks for your tips,
Johannes Luber


More information about the antlr-interest mailing list