[stringtemplate-interest] Translation using Recursive Templates

Terence Parr parrt at cs.usfca.edu
Fri Aug 29 13:21:44 PDT 2008


Heh, that's cool. Looks fine.
Ter
On Aug 29, 2008, at 1:18 PM, Jones, Larry wrote:

>
> Oops. I think user error has betrayed me once again.
>
> The following template does what I want (when I don't try to get the
> first of the first of the rest)
> max(args) ::= <<
> <if(first(rest(rest(args))))>
> MAX(<first(args)>, <max(rest(args))>)
> <else>
> MAX(<first(args)>, <first(rest(args))>)
> <endif>
>>>
>
> Sorry for the noise.
>
> -----Original Message-----
> From: stringtemplate-interest-bounces at antlr.org
> [mailto:stringtemplate-interest-bounces at antlr.org] On Behalf Of Jones,
> Larry
> Sent: Friday, August 29, 2008 3:07 PM
> To: Terence Parr
> Cc: stringtemplate-interest at antlr.org
> Subject: Re: [stringtemplate-interest] Translation using Recursive
> Templates
>
>
> Ter,
>
> I thought I had my problem licked, but I either:
> - Iterate over all the children producing a linear list.
> - Recurse one too many times.
>
> For example, my source language has expressions like max(1, 2, 3, 4,  
> 5).
> I want to translate this to max(1, max(2, max(3, max(4, 5))))  
> because my
> target language only takes two arguments for max().
>
> I currently use the following template. A and B are a debugging device
> to indicate which branch the template engine takes. The conditional is
> attempting to determine if I have two arguments left.
> max(args) ::= <<
> <if(first(first(first(rest(args)))))>
> A MAX(<first(args)>, <max(rest(args))>)
> <else>
> B MAX(<first(args)>, <first(rest(args))>)
> <endif>
>>>
>
> The result of rendering the template is:
> a = A MAX(1, A MAX(2, A MAX(3, A MAX(4, B MAX(5, )))))
>
> Notice that the "B" branch is taken when rest(args) is empty.
>
> -----Original Message-----
> From: Terence Parr [mailto:parrt at cs.usfca.edu]
> Sent: Friday, August 29, 2008 12:34 PM
> To: Jones, Larry
> Cc: stringtemplate-interest at antlr.org
> Subject: Re: [stringtemplate-interest] Translation using Recursive
> Templates
>
>
>
> 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
>
> This e-mail and any attachments are intended only for use by the
> addressee(s) named herein and may contain legally privileged and/or
> confidential information. If you are not the intended recipient of  
> this
> e-mail, you are hereby notified any dissemination, distribution or
> copying of this email, and any attachments thereto, is strictly
> prohibited. If you receive this email in error please immediately  
> notify
> the sender and permanently delete the original copy and any copy of  
> any
> e-mail, and any printout thereof.
> _______________________________________________
> stringtemplate-interest mailing list stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
>
> This e-mail and any attachments are intended only for use by the  
> addressee(s) named herein and may contain legally privileged and/or  
> confidential information. If you are not the intended recipient of  
> this e-mail, you are hereby notified any dissemination, distribution  
> or copying of this email, and any attachments thereto, is strictly  
> prohibited. If you receive this email in error please immediately  
> notify the sender and permanently delete the original copy and any  
> copy of any e-mail, and any printout thereof.



More information about the stringtemplate-interest mailing list