[antlr-interest] AST nodes in v3

Kay Roepke kroepke at classdump.org
Mon Oct 9 03:42:11 PDT 2006


On 9. Oct 2006, at 12:16 Uhr, David Holroyd wrote:

> On Mon, Oct 09, 2006 at 12:01:40PM +0200, Michiel Vermandel wrote:
>>
>> directive_statement:
>>    AXI_DIRECTIVE identifier
>>    { ## = #([DIRECTIVE_STATEMENT, 'directive_statement'], ##); }
>>
>> with DIRECTIVE_STATEMENT in my token section.
>>
>> How do I add such a node to the AST in Antlr v3?
>
> directive_statement
> 	:	AXI_DIRECTIVE identifier
> 		-> ^(DIRECTIVE_STATEMENT AXI_DIRECTIVE identifier)
> 	;

To elaborate a bit:

You can use any combination of
	* copy the information from another token to the imaginary token
	* just set the text of an imaginary token

Suppose you have the rule

directive_statement
	:	ad=AXI_DIRECTIVE identifier
	;

and want to

1) Just copy the info (type, line, column):
	-> (DIRECTIVE_STATEMENT[$ad] AXI_DIRECTIVE identifier)
	(this isn't too useful in this example, as you could just make the  
AXI_DIRECTIVE the root node, but you'd use it to set the type of the  
imaginary token to the type of a different real token, e.g. the  
opening '{' of a block)
2) Just set the text but keep the token type of DIRECTIVE_STATEMENT  
(I guess this is what you actually wanted to know ;):
	-> (DIRECTIVE_STATEMENT["directive statement"] AXI_DIRECTIVE  
identifier)
3) Set both:
	-> (DIRECTIVE_STATEMENT[$ad, "directive statement"] AXI_DIRECTIVE  
identifier)

HTH,
-k





More information about the antlr-interest mailing list