[antlr-interest] Re: ANTLR for Python

Terence Parr parrt at cs.usfca.edu
Wed Nov 17 09:38:49 PST 2004



On Nov 15, 2004, at 2:27 PM, Marq Kole wrote:
>>
>> 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.

Oh, so that's the problem.  Hmm...yeah that is a tough one.  I'm 
thinking of being more formal in ANTLR 3.0.  For example, we could have 
a generic "init" section that gets copied to the appropriate 
constructor(s).

> 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.

Hmm...yeah, I don't know python well enough to help :(

> 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:

The C++ parser uses

header "foo" {...}

I know so the infrastructure is there for sure.

> - 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.

Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!





 
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