Re: Get line numbers with Antlr parser

Kai Koehne <>
18 May 2006 23:52:26 -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: Kai Koehne <>
Date: 18 May 2006 23:52:26 -0400
Organization: Compilers Central
References: 06-05-058
Keywords: PCCTS
Posted-Date: 18 May 2006 23:52:26 EDT

Ulrich Hobelmann schrieb:
> 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.
> 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).
> [...]


antlr creates copies of all node objects in a tree if you have
buildAST=true in your parser/TreeParser. It seems that the information
is lost during that copying. To prevent that, create your own node class
and copy the relevant information in the initialize() methods:

public class MyNode extends CommonAST {
        public void initialize(Token t)
                line = t.getLine();
                column = t.getColumn();

        public void initialize(AST t)
                line = t.getLine();
                column = t.getColumn();

You have to register this class in all parsers/treewalkers that
manipulate the tree:

MyParser p;
MyTreeWalker w;
// ...

BTW. the antlr-interest mailing list
( might be a
better place for such questions.


Kai Koehne

Post a followup to this message

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