[antlr-interest] request for comments on language implementation

Terence Parr parrt at cs.usfca.edu
Wed Feb 25 14:08:59 PST 2009


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


More information about the antlr-interest mailing list