[antlr-interest] Re: ANTLR for Python

Marq Kole marq.kole at philips.com
Mon Nov 15 14:27:31 PST 2004



--- In antlr-interest at yahoogroups.com, Terence Parr <parrt at c...> 
wrote:
<SNIP>
> > I am looking for a few extensions in the way ANTLR handles Python
> > grammars. At this moment the header { } block is supported, but 
just
> > like the C++ runtime has the option to have different kinds of
> > headers, whose contents are placed in different parts of the
> > generated code, I would like to have something similar in 
Python. To
> > be more precise, I would like to have a header whose contents are
> > added to the __init__ method of the generated parser, and also a
> > header whose contents are added at the end of all generated 
code, at
> > the outer indentation level. That would allow one to add self-
> > testing code to the grammar, or add another class that uses the
> > creater scanner/parser/treeparser classes directly.
> >
> > Could someone give me a pointer to where in the ANTLR source code
> > this kind of stuff is handled so I can add the
> > abovementioned "headers"?
> 
> Well, I normally just add any action before the grammar rules to 
the 
> output class, which can include
> 
> {
> .... __init__ ...
> }
> 
> Then you don't need to mess with the header thing.
 
Ter,

Errr, that's just fine, except that the __init__() method for the 
parser class is already defined in the Python code generator, as it 
is required to perform initialization when constructing a new 
instance in Python. Also, Python does not support overloading, so I 
can only have one __init__() method. So I'm afraid your suggestion 
wouldn't fly.

Of course, there are alternatives: I can define empty __preinit__() 
and __postinit__() methods in the base parsers (LLkParser class) in 
the Python runtime, that are called from the __init__() method in 
that base parser.
This LLkParser.__init__() method is actually called from the 
generated parser, so when I override the __preinit__() and 
__postinit__() methods in the actions outside the grammar rules the 
initialization is augmented in the required way.

Problem: this approach is not portable to inherited parsers, as 
there is no requirement in Python that the superclass initializer is 
called when initializing an instance of a subclass. Also, the built-
in super() function does not normally do what you think it would do; 
it can be very misleading and unreliable.

Therefore my request for how to make specific headers:
- header "init" { } is needed, but I do not actually need separate
  preinit and postinit, I guess. Otherwise it would be:
- header "preinit" { } and
- header "postinit" { }
- header { } is necessary as well, but that's already available.
- header "post" { } would be needed to add any code after the
  generated parser/lexer/treeparser class, for instance for self
  testing.

Regards,
Marq






 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

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





More information about the antlr-interest mailing list