[antlr-interest] Resetting an AST tree walker

Michael Bedward michael.bedward at gmail.com
Wed Apr 22 18:56:51 PDT 2009


Hi Steve,

I posted the message you're quoting.  Just to clarify - I call the
reset method too.  My problem was that prior to 3.1.3 reset worked
with CommonTreeNodeStream but with 3.1.3 the stream is empty after the
tree walker has used it once so reset is useless.

I think this should have been highlighted in the release notes for
3.1.3.  It seems like a fairly major change and it caused me a lot of
grief for a while !

Michael

2009/4/22 Steve Souza <steve at stevesouza.com>:
> 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
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>


More information about the antlr-interest mailing list