[antlr-interest] Resetting an AST tree walker

Steve Souza steve at stevesouza.com
Tue Apr 21 07:38:34 PDT 2009


This question in the antlr archives is the same one I have:  "Is this now
the correct practice where a tree walker is to be invoked repeatedly ?"

http://www.antlr.org/pipermail/antlr-interest/2009-March/033823.html

The poster says that using BufferedTreeNodeStream instead of
CommonTreeNodeStream solved his problem.  This didn't work for me.  However,
calling the following method after each tree walk worked.  Is this the
preferred way to handle multiple tree walks or is there a better way?

Also is the only disadvantage of using BufferedNodeStream increased memory?
Does anyone know if this memory difference is significant.  fyi I am getting
about 4 times the performance on my test case with BufferedNodeStream.

             private void resetStream() {
                TreeNodeStream nodeStream=getTreeNodeStream();
                if (nodeStream instanceof BufferedTreeNodeStream)
                  ((BufferedTreeNodeStream)nodeStream).reset();
                else if (nodeStream instanceof CommonTreeNodeStream)
                  ((CommonTreeNodeStream)nodeStream).reset();

             }

On Mon, Apr 20, 2009 at 12:13 PM, Steve Souza <steve at stevesouza.com> wrote:

> I have an AST TreeWalker that inherits from TreeParser.  In the code below
> this class is called MyASTWalker.   The language I am creating takes a
> string like the following:  7*5+myvariable
>
> myvariable is determined at runtime.  The first call to calc() works (this
> calculates the answer), however subseqent calls cause an EOF error.  I could
> recreate MyASTWalker(nodes) for each call to calc(), however that seems
> inefficient.  Is there a way to accomplish the same thing by calling reset()
> on a particular object such as TreeParser or CommonTreeNodeStream?
>
> Also, what is the difference between CommonTreeNodeStream and
> BufferedTreeNodeStream?
>
> ...
>  CommonTree commonTree=(CommonTree)ast.getTree();
>  CommonTreeNodeStream nodes=new CommonTreeNodeStream(commonTree);
>  MyASTWalker walker=new MyASTWalker(nodes);
>
>
>                     for (int i=0;i<100;i++) {
>                         System.out.println(walker.calc());
>                     }
>
>
>
> antlr-interest at antlr.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090421/aab58412/attachment.html 


More information about the antlr-interest mailing list