[antlr-interest] Tree grammar confusion

Junkman j at junkwallah.org
Wed Aug 25 12:31:55 PDT 2010


Piper, Martin wrote:
> Greetings, 
> 
...

> 
> My main confusion is that when generating a tree 
> 
> ^(rule_1 A B) 
> rule_1: TOKEN C D->^(TOKEN C D)
> produces a tree (TOKEN C D A B)
> 
> but the same when walking the tree seems to expect to find ((TOKEN C D) A B)
> 

Hi Martin,

You ran into the same issue I did a while back (and whined about it here
:-).

Given an expression "^(LEAF)", the (tree-generating) parser will
generate a single LEAF node, but the tree parser will expect the node
sequence LEAF-DOWN-UP - tree parser takes ^() expression bit more literally.

A simple but crude workaround is to refactor your tree grammar so that
the root term of an ^() expression is not a subrule that matches another
^() expression.  For your simple example, the simple fix is to inline
rule_1 into ^() expression referencing rule_1 as its root term.

This inlining business wasn't so bad for my tree grammar, but I suspect
it could be a pain for others.

Perhaps others can chime in with better solutions.

Jay



More information about the antlr-interest mailing list