Re: Get line numbers with Antlr parser

Kai Koehne <java.3.kkoehne@spamgourmet.org>
18 May 2006 23:52:26 -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: Kai Koehne <java.3.kkoehne@spamgourmet.org>
Newsgroups: comp.compilers.tools.pccts,comp.compilers
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).
>
> [...]


Hi,


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)
        {
                super.initialize(t);
                line = t.getLine();
                column = t.getColumn();
        }


        public void initialize(AST t)
        {
                super.initialize(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;
// ...
p.setASTNodeClass("MyNode");
w.setASTNodeClass("MyNode");




BTW. the antlr-interest mailing list
(http://www.antlr.org:8080/mailman/listinfo/antlr-interest) might be a
better place for such questions.


Regards,


Kai Koehne


Post a followup to this message

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