[antlr-interest] Basic AST tree generation

Martin Probst mail at martin-probst.com
Sun May 7 03:21:58 PDT 2006


Hi,

> I'd rather not factor operator into expr (because I use operator
> in other places, not just expr), and I'm not sure how to get any
> meaningful tree construction from the term rule.

Well, if it was my grammar, I'd certainly factor. But if you don't  
like that, you can go with something like this:

expr: term ( o:operator! term { ## = #(#o, ##); })*

## means current tree, the ! tells ANTLR not to add "operator" to the  
tree (because we do it manually), #( head, child1, child2, ... )  
means "construct a new subtree with head "head" and children 1, 2  
etc. - if you pass an AST list to it ("##" here) it will add all the  
elements.

HTH,
Martin


More information about the antlr-interest mailing list