[stringtemplate-interest] Translation using Recursive Templates

Terence Parr parrt at cs.usfca.edu
Fri Aug 29 10:33:38 PDT 2008


On Aug 27, 2008, at 7:58 PM, Jones, Larry wrote:

> I am translating one language to another. In the source language, I  
> have expressions such as (SRC A B C D) where SRC is a function ID  
> and A..D are other expressions. This expression must translate to an  
> expression like (TGT A, (TGT B, (TGT C, D))) or (TGT(TGT(TGT A, B),  
> C, D))). (The value of the expression is independent of the order of  
> its arguments.) I have already parsed the source and created an AST.
>
> Because the output grammar is recursive, I attempted to recursively  
> invoke a template; however, I cannot determine how to terminate the  
> recursion correctly. For example: tgt(args) ::=  
> <<tgt_fun(<first(args)>, <rest(args):tgt()>)>> produces incorrect  
> results.
>
> Any thoughts on the template I need to produce this translation?

Hi Larry,

Generally recursion in ST will terminate when there's no work to do.  
For example,

walk(t) ::= <<
<t.payload>
<t.children:{c | <c:walk()>}>
 >>

Here, if there are no children it does not recurse.
Ter


More information about the stringtemplate-interest mailing list