9550Re: Line number information for AST
- Oct 2, 2003<hiteshk25@y...> wrote:
> How to get line number, which says token of AST
> node belong to that line.
> with regards,
The key is this: in ANTLR, AST nodes are completely different from
tokens. ASTs are (normally) instances of antlr.CommonAST, while
tokens are (normally) instances of antlr.CommonToken. These are
completely different classes (no inheritance neither composition
between them). They just "look" similar, because both have a "type"
attribute and a "text" attribute. CommonType can even be initialized
with a Token, but don't let this dissapoint you: only type and text
are copied. This is where similarities end: AST nodes don't have
file line or columns!
Putting aside the design reasons for this implementation, let me
reformulate your question:
How do I *add* line and column to the AST nodes?
This is easy: you can subclass antlr.CommonAST and add your own line
and column attributes. Then override the initialize() methods so
they copy line and column from the tokens, and add get and set
Finally, you'll have to make your parser use your new AST node class
instead of antlr.CommonAST... but I'll let you guess how to do it :)
I was joking :D! Use options ASTLabelType
However, if you want to make your life easier, you can use my lib,
antlraux (http://antlraux.sf.net). It has a class, LexInfoAST
l) that will match your needs perfectly (it even includes filename
- << Previous post in topic