[antlr-interest] header sections (was: Re: ANTLR for Python)
Marq Kole
marq.kole at philips.com
Wed Nov 17 13:27:37 PST 2004
--- In antlr-interest at yahoogroups.com, Ric Klaren <ric.klaren at g...>
wrote:
> > Now that I have had a look at it, I am beginning to wonder: the
> > header section seems to be generic, so not tied to either a
lexer,
> > parser or treeparser in particular.
>
> Think you're right there. Hmmm. I guess you're te first to notice.
I never
> much needed the sections and always split lexer/parser/treeparser
so I've
> never run into it myself. Other than a bug in the antlr parser
with respect
> to them.
>
<SNIP>
> > Alternatively, we can do some clever processing on the named
header
> > files (this is just a proposal). If I want a named
> > section "__init__" - I'm going to the scheme devised for Python
now -
> > to be used only for the lexer class, I could provide a
> > name "<LexerClassName>.__init__" for the header. In the case
there
> > is also a parser in the file, that would not get the code in
this
> > header section. By using the actual name of the class instead of
a
> > name "lexer.__init__" it would even be possible to address this
> > issue with 2 lexer definitions in the .g file (if that is at all
> > possible).
>
> 2 lexers in one .g is not possible. Your idea will work I guess
although
> moving some stuff around might be prettier. Hmm might also need
some fixing
> in antlr.g.
Ric,
Moving things around in the .g files is going to be hard if you do
not want to break existing code. Maybe that is something to consider
when doing ANTLR 3.
In the meanwhile, the scheme using named headers that refer to the
classname works beautifully. I now have an implementation in the
Python code generator that can handle
header "InstrTreeParser.__init__" {
// do some treewalker stuff
}
Having defined earlier:
private static final String initHeaderAction = "__init__";
The simple solution is by replacing the code:
printHeaderAction(initHeaderAction);
by:
printHeaderAction(initHeaderAction);
printHeaderAction(grammar.getClassName() + "." +
initHeaderAction);
So in case there is just one grammar per file, you can use
the "__init__" header, if you have both Lexer and Parser (and/or
TreeParser) you can use the class-specific "__init__" header. You
can even use both to provide both shared and specific initialization
code.
This can be readily translated to any of the code generators for the
other languages.
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