ANTLR v3 has built-in support for constructing StringTemplate templates. there are two forms: Special symbols in actions and rewrite rules similar to AST construction. I am including a number of rules from the mantra example.
Sometimes you just need a string to become a template:
The following tree grammar rule illustrates some of the basic rewrite rules:
And here are some more complicated examples:
When you need to append multiple strings or templates into a another template use the += operator for a rule's return value (former use of toTemplates no longer required). For example adding variable declarations inside a struct template :
and the templates for a struct declaration may be something like this :
More on string templates can be found here: String Template