[antlr-interest] Store Line Number in AST node

Terry tangfc2001 at yahoo.com
Wed Apr 6 22:53:42 PDT 2005


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