[antlr-interest] Tree construction - Adding subtrees

Stephen Tuttlebee themightystephen at googlemail.com
Wed Mar 9 15:17:17 PST 2011


Hi

I have a tree grammar that takes an AST and I want to rewrite/modify the 
AST slightly.

Here is the rule we're concerned with (with some unrelated actions removed):
componentDefinition
         :  ^(COMPONENT_DEF IDENTIFIER ^(BOUNDARY_DECLS 
boundaryDeclaration*) ^(FIELD_DECLS fieldDeclaration*) 
runMethodDeclaration* ^(HANDLER_DECLS handlerDeclaration*) 
^(METHOD_DECLS methodDeclaration*))
         ;

Now, under the ^(HANDLER_DECLS ...) subtree in the rule above I want to 
add extra 'handlerDeclaration' subtrees in the rewrite rule, each of 
which have the following form (you can ignore the details of the other 
rules referenced in it):
     ^(HANDLER_DECL IDENTIFIER type boundaryDirection IDENTIFIER 
handlerBlock)

I would like be able to have a tree rewrite rule like this for the 
componentDefinition rule. I know it's not valid ANTLR syntax but it 
hopefully gives the idea of what I want to achieve (it's broken up into 
many lines and indented appropriately for clarity) (also read the 
comments in the code):

componentDefinition
         :  ^(COMPONENT_DEF IDENTIFIER ^(BOUNDARY_DECLS 
boundaryDeclaration*) ^(FIELD_DECLS fieldDeclaration*) 
runMethodDeclaration* ^(HANDLER_DECLS handlerDeclaration*) 
^(METHOD_DECLS methodDeclaration*))
         { ... some actions here you can ignore... }
          -> ^(COMPONENT_DEF IDENTIFIER ^(BOUNDARY_DECLS 
boundaryDeclaration*) ^(FIELD_DECLS fieldDeclaration*) runMethodDeclaration*
                                                           
^(HANDLER_DECLS handlerDeclaration*
                                                               {
                                                               
for(String boundaryName : boundariesWithNoHandler) {
                                                                 // .... 
calculate/get values to put in subtree ...

                                                                 /* I 
want to be able to use ANTLR's friendly tree construction syntax here 
somehow whilst in the middle of this action.
                                                                      
Every iteration of the for loop creates a new subtree --- if I can't do 
it this way, what is the tree construction code I would need here? */
                                                                 
^(HANDLER_DECL {$boundaryName} ^(IDENT {$b.getType}) 
{$b.getDirectionString()} ^(BLOCK BLOCK_STATEMENT))
                                                               } // end for
                                                               }
                                                            )
                                                           
^(METHOD_DECLS methodDeclaration*))


So in the above tree rewrite, the tree is untouched except the 
^(HANDLER_DECLS ...) subtree which will have new ^(HANDLER_DECL ...) 
subtrees being added to it as children. I want to be able to use ANTLR's 
friendly tree construction syntax in the middle of the loop. Is there 
any way I can do this or another approach that might work?

Thanks for your time and effort,
Stephen


More information about the antlr-interest mailing list