[antlr-interest] use of TreeWizard

Terence Parr parrt at cs.usfca.edu
Tue Feb 23 12:35:10 PST 2010


Hi, just a reminder in case you folks haven't used the TreeWizard that it's useful in some situations. For example, when creating the implicitly generated lexers pulled from combined grammars, I need to build lexer rules for keywords you reference in the grammar. Once I have a wizard

TreeWizard wiz = new TreeWizard(adaptor,ANTLRParser.tokenNames);

Then I can create trees without having to do all of the manual "add child" stuff:

GrammarAST litRule = (GrammarAST)
	wiz.create("(RULE ID["+rname+"] (BLOCK (ALT STRING_LITERAL["+lit+"])))");

If you have a tree and you want to pull out information from it, you can use a scanf like method:

Map nodes = new HashMap();
boolean isLitRule =
	wiz.parse(r, "(RULE %name:ID (BLOCK (ALT %lit:STRING_LITERAL)))", nodes);

then get the node pointers from the map you pass in:

GrammarAST litNode = (GrammarAST)nodes.get("lit");
GrammarAST nameNode = (GrammarAST)nodes.get("name");

It's really making my life much easier.

Ter


More information about the antlr-interest mailing list