[antlr-interest] accessing input tokens while parsing the tree

Dave Thorn dave.thorn at gmail.com
Fri Nov 4 07:41:06 PDT 2011


Hi,

The Book has this in chapter 7:

> Also as part of the final rule tree processing, the parser automatically
> computes and stores the range of tokens associated with the subtree
> created for that rule. This information is extremely useful later when
> generating code. Sometimes you want to replace sections of the input
> with a translation computed from the tree. To do that, you need to know
> the corresponding input tokens to replace. For example, consider the
> expression x + y (including the spaces). The root plus node will store
> token boundaries 0..4, assuming that the expression is the only input
...
> The AST nodes will have start and stop token index boundaries...

So assuming I am matching a tree like that:

expr
    :   ^('+' a=expr b=expr) {  System.out.println("???"); }
    ;

How do I access the original source tokens that went into producing this
tree?

It seems, to my reading, that I should be able to access
$<something>.index.  $a.index, $b.index.
$somethingMagical[$a.index..$b.index] == "4 + 5"

Perhaps I missed it, but after saying it's possible, the book doesn't seem
to say how to do it.

Thanks,

Dave


More information about the antlr-interest mailing list