[antlr-interest] TreeRewrite bug or misuse?

John B. Brodie jbb at acm.org
Wed Mar 24 14:28:15 PDT 2010


Greetings!
On Wed, 2010-03-24 at 13:26 -0700, Kaleb Pederson wrote:
> 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?

use the += operator rather than the = operator in order to accumulate
lists of things on the left of the -> tree operator. thus:

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





More information about the antlr-interest mailing list