[antlr-interest] How to duplicate nodes using a subrule when doing AST rewriting?

Stefan Mätje Stefan.Maetje at esd-electronics.com
Tue Dec 20 07:47:05 PST 2011


Hi,

I would like to duplicate AST nodes as it is described in chapter 7 of the  
ANTLR
reference book.

The following simplified rules are part of my grammar. The "oneIdOrList" rule
collects one or multiple identifiers into one list. At some points in my  
grammar
I want to generate/duplicate a subtree for each identifier found. But using the
"oneIdOrList" rule does not work (see NOT WORKING variant of "structComponent"
below).

To get the "structComponent" rule working I have to write the
"( ID | '(' ID (',' ID )* ')' )" directly into the "structComponent" rule.

Is there a way to use (and or change) the "oneIdOrList" rule in the
"structComponent" rule and still get the wanted node and tree duplication?

Or to I have to replace the "oneIdOrList" rule anywhere in my grammar with the
direct code to get the duplication working.

Anybody there to give me a hint to solve this problem?

Thanks,
	Stefan

-------------8<-------------8<-------------8<-------------8<-------------8<----
/** "OneIdentifierOrList"
*/
oneIdOrList:
	( ID | '(' ID (',' ID )* ')' )	-> ID+
	;

/** "TypeStructure"
	*/
typeStruct
	:	'STRUCT'^ LBRACK! structComponent ( ','! structComponent )*  
RBRACK!
	;

/** "StructureComponent"
	*/
// WORKING variant
structComponent
	:	( ID | '(' ID (',' ID )* ')' ) oneIdOrList simpleType	
		-> ^(FIELD_DCL ID simpleType)+
	;
// NOT WORKING variant
structComponent
	:	oneIdOrList simpleType	
		-> ^(FIELD_DCL oneIdOrList simpleType)+
	;
-------------8<-------------8<-------------8<-------------8<-------------8<----

Example input
-------------8<-------------8<-------------8<-------------8<-------------8<----
MODULE ( structsmpl );

PROBLEM;
     DCL toast   STRUCT [
         (zing, zang, zong)  FIXED,
         (ring, rang)  CLOCK
     ] GLOBAL;
MODEND;
-------------8<-------------8<-------------8<-------------8<-------------8<----

Pretty printed AST result of NOT WORKING variant using the "oneIdOrList" rule:
-------------8<-------------8<-------------8<-------------8<-------------8<----
(MODULE structsmpl
     (PROBLEM
         (VAR_DCL
             (STRUCT
                 (FIELD_DCL zing zang zong FIXED 15)
                 (FIELD_DCL ring rang CLOCK)
             )
             (MOD_LIST GLOBAL)
             toast
         )
     )
)
-------------8<-------------8<-------------8<-------------8<-------------8<----

Pretty printed AST result of the WORKING variant using "ID" directly in the
"structComponent" rule:
-------------8<-------------8<-------------8<-------------8<-------------8<----
(MODULE structsmpl
     (PROBLEM
         (VAR_DCL
             (STRUCT
                 (FIELD_DCL zing FIXED 15)
                 (FIELD_DCL zang FIXED 15)
                 (FIELD_DCL zong FIXED 15)
                 (FIELD_DCL ring CLOCK)
                 (FIELD_DCL rang CLOCK)
             )
             (MOD_LIST GLOBAL)
             toast
         )
     )
)
-------------8<-------------8<-------------8<-------------8<-------------8<----



More information about the antlr-interest mailing list