[antlr-interest] "Superclassing" ANTLR parsers

Bogdan Mitu bogdan_mt at yahoo.com
Wed Jul 10 05:08:17 PDT 2002


Hi,

Digging in antlr.g I found that is possible to specify a custom superclass
for generated parsers (lexers, tree parsers as well). The syntax is:

class XxxParser extends Parser(MyCustomParserSuperclass);

where MyCustomParserSuperclass should be a class extending antlr.Parser (but
normally one should use antlr.LLkParser).
You get an error message, but it works.

This feature is not documented and seems to be not widely known. There is
currently a thread about adding constructors with no arguments. A custom
superclass where the no-arg constructor call super() first could be a better
solution.

Best regards,
Bogdan


--- pete_forman <pete.forman at westerngeco.com> 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.
> 
> 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.
> -- 
> Pete Forman                -./\.-  Disclaimer: This post is originated
> WesternGeco                  -./\.-   by myself and does not represent
> pete.forman at westerngeco.com    -./\.-   opinion of Schlumberger, Baker
> http://petef.port5.com           -./\.-   Hughes or their divisions.
> 
> 
>  
> 
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 
> 
> 
> 

__________________________________________________
Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free
http://sbc.yahoo.com

 

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



More information about the antlr-interest mailing list