[antlr-interest] Re: Constructors

Ric Klaren klaren at cs.utwente.nl
Mon Jul 8 03:26:16 PDT 2002


Hi,

On Mon, Jul 08, 2002 at 10:02:50AM -0000, pete_forman wrote:
> --- In antlr-interest at y..., Ric Klaren <klaren at c...> wrote:
> > On Sun, Jun 09, 2002 at 09:50:51PM -0000, johnclarke72 wrote:
> > > I know how to define extra methods within my grammar but how can I 
> > > write my own Constructors within a grammar. Essentially I want to add 
> > > two constructors (one that has no parameters and another that has 
> > > parameters).
> > 
> > Just add them:
> > 
> > header { // headerstuff }
> > 
> > { // for c++ this goes into the generated .cpp file
> > // extra constructor implementation
> > MyParser::MyParser( .. .. .. )
> > {
> > }
> > 
> > }
> > class MyParser extends Parser;
> > {
> > // for java the implementation goes here as well (i think)
> > MyParser( .. .. .. .. );
> > 
> > }
> 
> This fails for me (2.7.1 generating Java).  There already is a
> contructor with no arguments in the generated code, so my
> constructor gets a duplicate definition error when I compile.

Adding ones with different parameters than the default ones should work? Or
are you trying to add a new parameter less constructor? For that we would
need an option to suppress the default constructors.. but you are more
likely to run into trouble when upgrading antlr versions.

> There does not seem to be a way to augment the constructors
> generated by ANTLR.  I suppose that I could subclass from the 
> generated code.  Another workaround might be to add an init
> method but that seems an unnecessary burden on the clients.

Subclassing is probably the best option then with 2.7.1. Another options is
to do additional initializations in the first rule of the grammar, maybe
not an option in some situations...

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- klaren at cs.utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
     Innovation makes enemies of all those who prospered under the old
   regime, and only lukewarm support is forthcoming from those who would
               prosper under the new. --- Niccolò Machiavelli


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list