[antlr-interest] newbie's question on writing template rewrite rule

srinivasan karthikeyan pitchai srinivasan.karthikeyan.pitchai at oracle.com
Thu Jun 21 20:50:21 PDT 2012


Hi Wu,
Is CLASS_TOP_LEVEL_SCOPE a virtual token, ie a token that was not part 
of the original source token stream, that you created to create the 
subtree?  If yes, then you have 2 choices, you can either base the 
virtual token on a real token that would have a text or you can give a 
text to the virtual token at  the time of construction.  This would 
ensure that $CLASS_TOP_LEVEL_SCOPE.text has some value that can be 
produced in the output.   Try replacing  $classScopeDeclarations.text  
with $classScopeDeclarations.st provided that you are already have a 
template rewrite rule for classScopeDeclarations rule.

-Vasan

On 6/22/2012 7:00 AM, woodzltc zhou wrote:
> Hello all,
>
> I am a newbie to ANTLR, a great tool in my opinion! Here is a question
> on how to write template rewrite rule for such kind of rule:
>
> classTopLevelScope
>     :   ^(CLASS_TOP_LEVEL_SCOPE classScopeDeclarations*)
>
> I want to insert something fixed between CLASS_TOP_LEVEL_SCOPE and
> classScopeDeclarations*, what should I do?
>
> I wrote something as follows:
>
> classTopLevelScope
>     :   ^(CLASS_TOP_LEVEL_SCOPE classScopeDeclarations*) ->
> embed(arg1={$CLASS_TOP_LEVEL_SCOPE.text},
> arg2={$classScopeDeclarations.text})
>
> embed(arg1, arg2) ::=<<
> $arg1$
> blabla
> blabla
> $arg2$
> It does insert what I want. But it also drop all the stuff at the two
> sides. I guess I am wrong with what I gave as arg1 and arg2. Anyone
> can give me some suggestions? Thanks a lot!
>
> Best
> - Wu
>
> 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