[antlr-interest] Store Line Number in AST node
Bharath Sundararaman
Bharath.Sundararaman at starthis.com
Thu Apr 7 05:13:39 PDT 2005
Hi Terry,
Add this line of code:
parser.setASTNodeClass("MyAST");
Then, call your top most parser rule as usual:
parser.CompilationUnit();
NOTE:
If you do not properly mention the package name, for eg,
Parser.setASTNodeClass("myproject.MYAST"); Or else you will get an error
saying Can't find/access AST Node type MyAST"
Hope this helps.
Bharath.
-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terry
Sent: Thursday, April 07, 2005 12:54 AM
To: ANTLR Interest
Subject: [antlr-interest] Store Line Number in AST node
Hi,
I am thinking to create my own AST class which stores
line number of parsing code.
public class MyAST extends antlr.CommonAST {
private int lineNumber;
...
public void initialize( antlr.Token t )
{
super.initialize(t);
setLine(t.getLine() );
}
...
// my extensions
public void setLine(int l_)
{
lineNumber = l_;
}
public int getLine( )
{
return lineNumber;
}
}
And I add
option{
...
ASTLabelType = "MyAST";
}
in grammar file which is borrowed from
antlr-2.7.3\examples\java\java\.
I use my new defined AST class as:
JavaLexer lexer = new JavaLexer(r);
JavaRecognizer parser = new JavaRecognizer(lexer);
parser.compilationUnit();
MyAST p= (MyAST)parser.getAST();
Then I have the run-time error:
java.lang.ClassCastException at
JavaRecognizer.modifer, the source code at that line
is: tmp34_AST = (MyAST)astFactory.create(LT(1)); I
wonder why I can't cast it into my new AST class.
Thanks
Terry
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail
More information about the antlr-interest
mailing list