[antlr-interest] TreeWalker to optimize AST under certain conditions

Jim Idle jimi at temporal-wave.com
Wed Apr 1 09:56:56 PDT 2009


Tilman Bender wrote:
> Hi everybody,
>
> I am trying to make a simple compiler for a language that does simple 
> arithmetic expression and read/write operations.
> I have a parser generating an AST. Now I would like to walk that AST 
> an alter/rewrite it by removing superflous subtrees.
>
> e.g:
> When I get a subtree for an addition saying:
> ^(ADD 0 expr ) or ^(ADD expr 0) the subtree should be replaced by just 
> expr
>
> The same applies to multiplications with one or zero: remove the 
> subtree and replace it by one of the operands or zero.
>
> How could I achieve this with a tree-grammar?
Download the examples on the download page and use the polydiff example 
as a guide. This isn't your homework is it?

Jim


More information about the antlr-interest mailing list