[antlr-interest] potentially stupid question

Martin Probst mail at martin-probst.com
Wed Oct 19 15:14:46 PDT 2005


Hi,

> How does one get the exact character position at a given moment in a
> parser? Is this even possible?

Well, there is no such thing - your code in the parser is in a given
rule, but line numbers/cols are related to single tokens, so you have to
specify of what you want to get the line number. You can get the exact
character position of a given AST/Token you have, e.g.:

rule: t:TOKEN { t.getLine(); }

Although you have to use an AST subclass to get line numbers. See the
ANTLR manual about that.

Martin



More information about the antlr-interest mailing list