[antlr-interest] Again: Source code position

Matthias Kempka lists at mkempka.de
Sat Dec 3 05:25:34 PST 2005


Hello all,

I'm aware that I'm not the first one to ask this question, but neither
the FAQ nor the mailing list archive give me answers that I can work
with, so here's my problem:

I'm working with ANTLR 2.7.5 and the Java 1.5 Grammar by Michael Studmann.
What I need is the source code position of an element in the AST.

I see that the tokens that are created by the lexer have exactly the
information I need. So I subclassed the CommonAST with a class named
TokenPreservingAST. In the section Tokens{} in the Grammar I added the
information which tokens should be represented by a TokenPreservingAST
object.
So far, it worked. But the token is always null. The problem is, that my
AST objects are always initialized with a call to
initialize(int,String) and never with initialize(Token). This is due to
the corresponding create-method in the ASTFactory.

I couldn't find out how to tell the JavaRecognizer that it should call
the method create(Token) instead of create(int, String).
Can somebody help me with this?

The FAQ tells me of another way with a special input stream that is
given to the lexer. However, I don't understand how I could get the
information of a token by counting how many characters the lexer has
consumed. There, I'm missing the information how to combine the
informations.

Regards,
Matthias Kempka


More information about the antlr-interest mailing list