[antlr-interest] If statement in action code

Stephanie stephanie.balzer at gmail.com
Mon Oct 11 13:35:37 PDT 2010


Hello,

I wonder whether I can use if statements in action code of rules. Here is an
example of a rule for which I would need such an if statement:

x[Object o]
: lp='(' y? ')'
-> ^( NodeX[$lp, "NodeX"] { ((BaseTree) $o).getChild(0) } { ((BaseTree)
$o).getChild(1) } y? )

In above rule, I create a NodeX with three children. The first child is the
left child of the tree passed as argument, the second child is the right
child of the tree passed as argument, and the optional third child is y. The
action code works perfectly.

However, I would like to change the rule as follows:

If the tree passed as argument has only one child, then NodeX will have a
NodeThis as first child and the only child of the argument tree as second
child.
If the tree passed as argument has two children, then I want to keep above
action code.

Is there action syntax that allows me to express this? And if yes, how would
I express this?

Thanks for your help,

Stephanie


More information about the antlr-interest mailing list