On Feb 25, 2009, at 2:02 PM, Andy Tripp wrote:

> Hi Andreas,
>
> Andreas Meyer wrote:
>> Hi!
>>
>> Your mail was addressed to Michael, but I hope it's ok to answer
>> nonetheless:
>>
>> I would consider hand-written code to walk an AST harmful. Maybe  
>> there
>> are cases where it is useful, but walking a dynamically typed tree  
>> like
>> this looks like a maintenance nightmare to me.
>
> I've found it much easier to do "hand-written" AST transformations.
> For example, to find cases like "x+0" and replace with just "x", you'd
> have something like:
>
> List<AST> pluses = root.findAncestorsOfType(MyTokenTypes.PLUS);
> for (AST ast: plusses) {
>        if (ast.getChildCount() == 2
>           && ast.getSecondChild().getType() == MyTokenTypes.INTEGER
>            && ast.getSecondChild().getText().equals("0")) {
>               ast.replaceMyself(ast.getFirstChild());  // replace "x+0" with  
> just "x"
>       }
> }
>
> Isn't this simpler than the ANTLR treewalker equivalent?

Only until the next release of ANTLR when you can do tree grammar  
filters, or tree pattern matchers:

add0 : ^('+' x=. INT {$INT.int==0}?) -> $x ;

:)

It's sitting in my development branch right now and is what the book  
is going to use all over the place.

Ter

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to