[antlr-interest] header sections (was: Re: ANTLR for Python)

Ric Klaren ric.klaren at gmail.com
Wed Nov 17 08:06:53 PST 2004


Hi,

On Wed, Nov 17, 2004 at 09:22:54AM -0000, Marq Kole wrote:
> Thanks for the explanation, I got it working in one go.

Good! :)

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

> The ANTLR manual as it is did 
> seem to acknowledge that impression. So if I have a file with both a 
> lexer and a parser, a named header section will be added to both 
> generated classes, that is: the same named header. If you do 
> anything else than including headers in such a header section (for 
> instance, defining auxillary classes or functions, or global 
> variables) it will result in duplicated code.

Not pretty indeed. Would require some shoveling around of code to fix. Move
the hashmap with the action tokens from the behavior class to the grammar
subclasses (if I recall right on the last one)

> So would we need to put a warning in the manual sections on Cpp and 
> Python runtime (don't know if C# uses those) that if you use named 
> headers you'd better make separate grammar files for your lexer, 
> parser and treeparser.

Or that...

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

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893755  ----
-----+++++*****************************************************+++++++++-------
  "I don't have anything against Java - it's the best damn implementation
              of UCSD Pascal ever released..." --- Larry Smith



 
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