[antlr-interest] Store Line Number in AST node

Martin Probst mail at martin-probst.com
Thu Apr 7 10:35:53 PDT 2005


Hi,
you have to set up an ASTFactory to use your AST node type. You will
find information about that in the manual.

Also, did you actually read my response to your original question? You
quoted it in your first two emails to this list. It contains an
explanation how to call .getLine() on AST nodes to retrieve the line
number.

regards,
Martin

Am Mittwoch, den 06.04.2005, 22:53 -0700 schrieb Terry:
> 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