[antlr-interest] AST construction - adding extra children via semantic action

Jim Idle jimi at temporal-wave.com
Wed Aug 11 13:29:37 PDT 2010


Bill,

As you are using action code anyway, then you can just use the standard Java
methods for tree building in your code. However, it is good practice to move
such code into external methods of some helper class so that your grammar is
not cluttered with unreadable inline code. 

You can look at the generated code to see how to do that via the tree
adaptor. 

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Bill Andersen
> Sent: Wednesday, August 11, 2010 12:17 PM
> To: antlr-interest
> Subject: [antlr-interest] AST construction - adding extra children via
semantic
> action
> 
> Hi Folks
> 
> Here's the relevant fraction of a tree grammar I'm working on...
> 
> spec
>   : ^(SPEC rule+)
>   ;
> 
> rule
>   : ^(RULE_DECL ID alts)
>   ;
> 
> It's a grammar that recognizes a language, the output of which is another
> ANTLR grammar.
> 
> I want the first rule to look like this:
> 
> spec
>   :	^(SPEC rule+)
>         { < some action here > }
>         -> ^(SPEC < augmented list of rules >)
>   ;
> 
> In other words, I want to add some extra RULE_DECL trees on to the end of
> whatever rule+ accumulates, based on semantic information gathered during
> the tree parse.
> 
> Is there a straightforward way to do this?
> 
> Thanks
> 
> 	.bill
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address



More information about the antlr-interest mailing list