[antlr-interest] Tree construction from rules

Martin Probst mail at martin-probst.com
Tue Sep 28 07:06:26 PDT 2004


Hello,
I'm trying to construct trees from my grammar and have run into two problems:

a) rule: leftExpr ( ( operator1 | operator2 | operator3 ) rightExpr )?

I want to construct a tree from this with whatever results from operator
being the root node of the subtree. Note that operator1-3 are rules, so I
can't write "operator1^". Is there a simple way of solving this? I've
somewhat succeeded with declaring a local variable within the rule and
writing "(o1:operator1 { opnode = #o1; } | o2:operator2 { opnode = #o2 }
...)" and constructing the tree manually afterwards. But this doesn't seem
very clean to me. Is there a better solution?

b) rule: someExpr ( OPERATOR someExpr )*
I want to create a subtree which roots at the first someExpr and all other
someExpr should be a child of the preceding someExpr. I could drag down
the (OPERATOR ...)* rule to someExpr and recurse which would eliminate the
problem with a possibly infinite number of expressions on the right side:

| rule: someExpr
| someExpr: ( the usual stuff ) ( OPERATOR someExpr )?

( The drawback with this method is that the maximum call depth within my
gramamr would get increased by that a lot. I don't know if that will
really be a problem though.) But I don't really know how to get further
from here (I'm getting infinite loops with everything I try). Can anyone
help me with this?

Both of these problems could be easily solved by allowing the caret (^)
operator not only on tokens but also on grammar rules. Is there a specific
reason for not doing this?

Regards,
Martin


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list