David James Ball
David James Ball

Reputation: 913

ANTLR Removing nodes on tree walk

I asked this on the ANTLR mailing list archives, but I didn't get a reply, so I'll try here.

Is there a problem with deleting a node/tree during a tree walk? I want to find any specific subtrees and get rid of them. I thought this rule would work...

attribute  : ^(ATTRIBUTE ID ATTR_VALUE) -> ;

But I get a NullPointerException.

Also, Can you write general rules to match trees with a range of root tokens?

tree : : ^(root attribute+ children+=.*) -> ^(root $children*);

fragment root : A | B;

I seem to be getting EmptyRewriteExceptions all the time :)

Upvotes: 2

Views: 415

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170178

Is there a problem with deleting a node/tree during a tree walk?

That can go wrong in some cases. For example, whenever attribute (you've just rewritten to be removed) becomes the root of some other tree. There might be other causes, but that is one of them. When attribute will always be a lead in the tree, I don't think it can do any harm. Consider posting an SSCCE if this doesn't answer your question (be sure to post an actual SSCCE!).

Also, Can you write general rules to match trees with a range of root tokens?

No, the operator += creates a List of Common[Token/Tree]s. You'll need to move the .* to another rule, something like this (untested!):

tree : ^(root attribute+ children) -> ^(root children);

children : .* ;

Upvotes: 1

Related Questions