[antlr-interest] Store Line Number in AST node

Terry tangfc2001 at yahoo.com
Thu Apr 7 12:15:18 PDT 2005


Hi,
Firstly I consider your suggestion using getLine in
AST class directly, then I can call
trav_tree(parser.getAST()) to traverse the AST and
print the line number of node.

public static void trav_tree( AST top )
{
  if (top != null) {
          System.out.println(top.getLine()+" ");

    String  str;
    str = top.getText();
   System.out.println(str);
    if (top.getFirstChild() != null) {
      System.out.print("kid: ");
      trav_tree( top.getFirstChild());
    }
    if (top.getNextSibling()!=null) {
      System.out.print("sib: ");
      trav_tree( top.getNextSibling());
    }
  }
}

But since AST did not contain the line number of
token, therefore, the code prints line number always
0.

So I want to subclass AST to my own AST class which
stores linenumber as my last email.  I added
parser.setASTNodeClass("PNode"); to my code, but I
still have ClassCastException. 

Do you think own ASTFactory is required in creating
own AST class? Is there any example in creating own
ASTFactory?

thanks

Terry


--- Martin Probst <mail at martin-probst.com> wrote:
> 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
> > 
> 
> 


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Personals - Better first dates. More second dates. 
http://personals.yahoo.com



More information about the antlr-interest mailing list