[antlr-interest] Direct access and manipulation of tokens in existing trees

Johannes Luber jaluber at gmx.de
Wed Feb 13 09:39:22 PST 2008


Mark Heieis schrieb:
> Is there a way to get direct access to tokens of existing trees and then 
> maintain the reference points to those tokens so that they can be changed?
> 
> The output of the tree is a formula which has placeholder tokens (eg X0) 
> for the input values. What I would like to do is replace a placeholder 
> token  with actual values. Because I have to iterate through lots of 
> values using the same formula (tree), rather than reprocess the whole 
> input formula over and over, it would seem to make more sense to just 
> use a placeholder token and replace that before sending it on for eval. 
> This is assuming the overhead to find and change a token directly is 
> more efficient than just updating the raw input and reprocessing from 
> the beginning again.
> 
> I've looked at TreeWizard, which seems to have what I need, but can't 
> seem to figure out how to get a TreeAdapter from an existing tree which 
> is needed to create a TreeWizard, and whether changes to it will be 
> reflected in the original tree.
> 
> Hopefully, that all makes sense. Any pointers would be much appreciated.

You have to create a special token class, which is basically a 
self-dereferencing pointer. Any access to the actual value is delegated 
towards the real token. In other words, there is one place alone where 
changes have to be made. It should be a good idea to create a registry, 
which retrieves a token based on a number.

Johannes


More information about the antlr-interest mailing list