Re: Get line numbers with Antlr parser

Antoine Reilles <>
18 May 2006 23:51:18 -0400

          From comp.compilers

Related articles
Get line numbers with Antlr parser (Ulrich Hobelmann) (2006-05-16)
Re: Get line numbers with Antlr parser (Antoine Reilles) (2006-05-18)
Re: Get line numbers with Antlr parser (Kai Koehne) (2006-05-18)
Re: Get line numbers with Antlr parser (Ulrich Hobelmann) (2006-05-18)
| List of all articles for this month |

From: Antoine Reilles <>
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 <> 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) {
        line = tok.getLine();
        col = tok.getColumn();
    public void initialize (AST 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

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

Have fun,

Post a followup to this message

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