[antlr-interest] TreeRewrite bug or misuse?

Kaleb Pederson kaleb.pederson at gmail.com
Wed Mar 24 13:26:10 PDT 2010


I'm rewriting a tree in a way that I think follows the rules.  Here's
a sample input fragment:

"one" == "two" && "three" == "four" && "five" == "six"

The following rewrite works correctly, but then I have to iterate over
the list and match rhs-lhs pairs,a mere minor annoyance I suppose:

matchExpression
  : (object EQUALS object) (AND object EQUALS object)*
    -> ^(AST_MATCH object+)
  ;

This version gives the following tree:

(AST_MATCH "one" "two" "three" "four" "five" "six")

I'd prefer to have a list of the right-hand side and one of the
left-hand side, so I tried the following:

matchExpression
  : (rhs=object EQUALS lhs=object) (AND rhs=object EQUALS lhs=object)*
    -> ^(AST_MATCH ^(AST_MATCH_ARG $rhs+) ^(AST_MATCH_ARG $lhs+))
  ;

The above yields the following tree:

(AST_MATCH (AST_MATCH_ARG "five") (AST_MATCH_ARG "six"))

But I expected the following:

(AST_MATCH (AST_MATCH_ARG "one" "three" "five") (AST_MATCH_ARG "two"
"four" "six"))

As 'rhs' and 'lhs' are indeed present more than once, and being
matched more than once, shouldn't the rewritten tree contain all the
nodes or am I misunderstanding something?

Thanks.

--
Kaleb Pederson

Blog - http://kalebpederson.com
Twitter - http://twitter.com/kalebpederson


More information about the antlr-interest mailing list