jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question How can I include line numbers in automatically generated ASTs?
Topics Tools:ANTLR:Intermediate forms:Automatic AST construction, Tools:ANTLR:Intermediate forms:Node types, Tools:ANTLR:Intermediate forms:Trees
Author Fondement Frederic
Created Jun 7, 2000 Modified Jun 7, 2000


Answer
Tree parsers are often used in type checkers. But useful error messages need the offending line number. So I have written:
import antlr.CommonAST;
import antlr.Token;
public class CommonASTWithLines extends CommonAST {
     private int line = 0;
     public void initialize(Token tok) {
          super.initialize(tok);
          line=tok.getLine();
     }
     public int getLine(){
          return line;
     }
}

initialize is automatically called when an AST is created from a Token object, which has its own line variable.

Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 1



Comments and alternative answers

Comment on this FAQ entry

Doesn't help when a different form of initialize() is called.
Charles Duffy, Aug 12, 2003
In prticular, I'm trying to figure out how to get a useful line number for initialize(int, string) calls; as initialize isn't called with a token, the snipper herein simply doesn't Work For Me.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  


Ask A Question



 
Related Links

ANTLR FAQ

ANTLR Forum

Download now!

Resources

Documentation

Book in progress

ANTLR Consulting

Wish List
Features
About jGuru
Contact Us

 



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers