[antlr-interest] Write a token to the AST within an action

Chris Sekszczynska Development at ChrisSek.de
Tue Nov 11 02:56:32 PST 2008


Hi Gavin,

> All the functions that ANTLR itself uses to
> construct ASTs are available to your code.

The following helper method works great, thanks a lot!

<snip>
private CommonTree createTreeByGlobals(List<String> symbols) {
	CommonTree globalTree = null;

	if (newGlobals.size() > 0) {
	    globalTree =  
(CommonTree)adaptor.becomeRoot(adaptor.create(GLOBAL,  
"GLOBAL"),adaptor.nil());
	    for (String name : newGlobals) {
	    	adaptor.addChild(globalTree, adaptor.create(Identifier, name));
	    }
	}

	return globalTree;
}

<snip>

: sourceElement*
  -> ^(PROGRAM  
{createTreeByGlobals 
(((SymbolTable_scope)SymbolTable_stack.peek()).newGlobals)}  
sourceElement*)

<snip>

Kind regards,

Chris


More information about the antlr-interest mailing list