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

Chris Sekszczynska Development at ChrisSek.de
Sun Nov 9 08:24:07 PST 2008


Hi all,

After a long research, I ask you because I have no idea how to write  
directly to the AST-Stream from within an action. My parser remembers  
token-names in an ArrayList which must be written to the AST in an  
other rule. Here's my rule "program", where the names are ready to  
write to AST:

	program
	scope SymbolTable;
	@init {
		// initialize the scope for this block
		$SymbolTable::newGlobals = new ArrayList<String>();
	}
	@after {
		System.out.println("Number of globals: " +  
$SymbolTable::newGlobals.size());
	}
	    : sourceElement*
	    -> ^(PROGRAM sourceElement*)
	    ;

There, I like to achieve something like this:

		-> ^(PROGRAM ^(GLOBALS {«return the element of  
$SymbolTable::newGlobals»}) sourceElement*)

The GLOBALS-subtree should look like ^(GLOBALS i x y) when "i", "x"  
and "y" are the token-names in my "newGlobals" ArrayList.

Is it maybe possible to add a method like writeToAstStream(String  
root, String child ...) to the parser? This is, in general, no  
problem, but how can I write something to the tree from within such an  
method or an action?

Any ideas?

Thank you very much in advance,

Chris


More information about the antlr-interest mailing list