How can I include line numbers in automatically generated ASTs?

Fondement Frederic

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.

Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact