[antlr-interest] Rewriting Text

Adam Augusta roxton at gmail.com
Sun Feb 13 05:49:22 PST 2011


Let's say I've got a tree with a series of nested SPANs with TEXT leaves.

Let's say I want to write a tree grammar that causes SPANs with a
"boo" TEXT child to become a SPAN with a "moo" TEXT child.

rule: ^(SPAN 'boo') -> ^(SPAN 'moo')

That clearly doesn't work, because 'boo' and 'moo' aren't tokens.

So I did the following:

tree grammar Boo2Moo;

options {
    ASTLabelType = CommonTree;
    tokenVocab = IntermediateTree;
    filter = true;
    output = AST;
}

bottomup:	spantag;
spantag:	^(parent=SPAN e=TEXT) {"boo".equals($e.getText())}?
{adaptor.setChild($parent, 0, adaptor.create(TEXT, "moo"));};

This works.  Is there a more elegant way, or is this a reasonable approach?

Thanks,
-Adam


More information about the antlr-interest mailing list