Re: Get line numbers with Antlr parser

Antoine Reilles <reilles@loria.fr>
18 May 2006 23:51:18 -0400

          From comp.compilers

Related articles
Get line numbers with Antlr parser u.hobelmann@web.de (Ulrich Hobelmann) (2006-05-16)
Re: Get line numbers with Antlr parser reilles@loria.fr (Antoine Reilles) (2006-05-18)
Re: Get line numbers with Antlr parser java.3.kkoehne@spamgourmet.org (Kai Koehne) (2006-05-18)
Re: Get line numbers with Antlr parser u.hobelmann@web.de (Ulrich Hobelmann) (2006-05-18)
| List of all articles for this month |
From: Antoine Reilles <reilles@loria.fr>
Newsgroups: comp.compilers
Date: 18 May 2006 23:51:18 -0400
Organization: CIRIL, Nancy, France
References: 06-05-058
Keywords: PCCTS
Posted-Date: 18 May 2006 23:51:18 EDT

On 2006-05-16, Ulrich Hobelmann <u.hobelmann@web.de> wrote:
> Hi, I'm using the Java1.5 parser + tree parser by Mike Studman.
>
> My problem: I need to extract the line numbers from the parser.
You will have to build an AST class to be used by antlr for that,
replacing the standard one
>
> Funny thing: in one part of the parser it works, but in the tree parser
> it doesn't. In java15.g I added some printlns to print getLine() of
> some nodes, and they printed the lines. The very same nodes are also
> put into a tree structure to read by the java-tree parser (obviously).
This is not surprising: theline and colum information is not stored in
the tree being built
>
> What can / do I have to do, to get the line numbers to the high level,
> where I need it?
You can define for instance something like the following


public class MyAST extends CommonAST {
    private int col = 0,line = 0;
    public void initialize(Token tok) {
        super.initialize(tok);
        line = tok.getLine();
        col = tok.getColumn();
    }
    public void initialize (AST ast) {
        super.initialize(ast);
        if (ast instanceof MyAST){
            col = ((MyAST)ast).getColumn();
            line = ((MyAST)ast).getLine();
        }
    }
    public int getLine() { return line; }
    public int getColumn() { return col; }
}


and use it with
parser.setASTNodeClass("MyAST");


That way, antlr will build the tree using MyAST nodes, which store the
line and column information


Have fun,
antoine


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.