[antlr-interest] Can someone send a simple working C# StringTemplate example? (I'm frustrated)

Michael Bedward michael.bedward at gmail.com
Thu Oct 27 20:57:59 PDT 2011


I don't use C# so can't help you there, but regarding StringTemplate
vs ST: the former is the class in version 3 of the library:
http://www.antlr.org/wiki/display/ST/StringTemplate+3+Documentation

while the latter is the new, improved, shiny class in the completely
re-written version 4:
http://www.antlr.org/wiki/display/ST4/StringTemplate+4+Documentation

Also, just a general comment on the frustration that you express.
Writing docs for any open source project of reasonable size is hard.
Keeping them up to date is harder. Worse still, while OS code
development is sometimes funded, docs are usually a labour of love.
I'm not involved in ANTLR or StringTemplate development but do work on
other OS projects and see similar complaints or expressions of anguish
about docs being out of date or contradictory all the time, but
getting people to actually help with the docs is nigh on impossible.
So... hint, hint :)

Michael


On 28 October 2011 14:38, Voelkel, Andy <andy.voelkel at plantronics.com> wrote:
> Hi,
>
> I've moved on from trying to get the C# version of Antlr working (successfully - with help from this list!) to trying to get StringTemplate working in the C# version. I've tried to "port" this example from Terence's book:
>
> import org.antlr.stringtemplate.*;
> ...
> StringTemplate hello = new StringTemplate("Hello <name>" );
> hello.setAttribute("name" , "World" );
> System.out.println(hello.toString());
>
> Seems easy enough, right? Well, given the version of the C# runtime I've got, StringTemplate seems to be named Template, and doesn't have a setAttribute method (or a SetAttribute method), and displays nonsensical output from the toString (actually ToString) method.
>
> I looked on the StringTemplate website, and the examples there don't even refer to a StringTemplate or a Template, they talk about an "ST" (for both Java and C#).
>
> Can someone point me at a simple example of a StringTemplate example (such as the above) that works with the C# runtime Antlr4.StringTemplate dll (version v2.0.50727)? I can probably go from there, using the forensic techniques I learned trying to figure out how the Java version of Antlr maps to the C# version.
>
> - Andy
>
> P.S. Editorial comment. These are really powerful tools, but the amount of out of date or missing documentation and examples on the websites is maddening. Thank god for this list, or I would have given up two weeks ago!
>
>
>
> -----Original Message-----
> From: Michael Bedward [mailto:michael.bedward at gmail.com]
> Sent: Thursday, October 27, 2011 5:18 PM
> To: Voelkel, Andy
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] What's the right way to do this?
>
> Hello Andy,
>
> Here is an example of doing this with stringtemplate v3:
>
> // A function call with comma-delimited list of args
> call(name, args) ::= <<
> <name>(<args; separator=", ">)
>>>
>
> You can see this usage on the stringtemplate cheat sheet:
> http://www.antlr.org/wiki/display/ST/StringTemplate+cheat+sheet
>
> Michael
>
>
> On 28 October 2011 09:26, Voelkel, Andy <andy.voelkel at plantronics.com> wrote:
>> Hi,
>>
>> I'm developing tools for generating various C source files from a simple DSL. I've got the lexing/parsing/tree-creation part of it working to my satisfaction, and am comfortable with my level of understanding (though I will continue to work my way all the way through Terence's book). But I'm confused about the best approach to generating one of the output files I need, which should look like this:
>>
>> toplevel name = {
>>  { // begin group1
>>    { // begin block1
>>      item 1,
>>      item 2
>>    }, // end block1
>>    { // begin block2
>>      item 1,
>>      item 2
>>    } // end block2
>>  }, // end group1
>>  { // begin block3
>>    item 1,
>>    item 2,
>>    item 3
>>  }, // end block3
>>  { // begin group2
>>    { // begin block4
>>      item 1,
>>      item 2
>>    }, // end block4
>>    { // begin block5
>>      item 1,
>>      item 2
>>    } // end block5
>>  } // end group2
>> };
>>
>> The problem is putting the "," characters on all elements in the nested structure except for the last one at that level, and I'm wondering what the best approach to the problem is. It seems like I could pass a "last one" parameter down to subrules when the item invoking the subrule is the last in the list of a rule, and use that as a way to avoid printing the ",". Alternatively, I get the feeling that this could be handled by the right StringTemplate magic, but I haven't found the right StringTemplate tutorials to get comfortable with this problem, which is obviously recursive, and where the items in a list are not all of the same type. The toplevel structure can contain groups or blocks, and groups can contain other groups:
>>
>> group: 'group'^ ID '{'! (block | group)+ '}'! ;
>> block: 'block'^ ID '{'! (assign)+ '}'! ;
>>
>> What would be the right way to get the output I'm looking for?
>>
>> - Andy
>>
>> ________________________________
>>
>> CONFIDENTIALITY NOTICE: This e-mail transmission, and any documents, files or previous e-mail messages attached to it, may contain information that is confidential and/or legally privileged. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, please DO NOT disclose the contents to another person, store or copy the information in any medium, or use any of the information contained in or attached to this transmission for any purpose. If you have received this transmission in error, please immediately notify the sender by reply email or at privacy at plantronics.com, and destroy the original transmission and its attachments without reading or saving in any manner.
>>
>> For further information about Plantronics - the Company, its products, brands, partners, please visit our website www.plantronics.com.
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>
>
> ________________________________
>
> CONFIDENTIALITY NOTICE: This e-mail transmission, and any documents, files or previous e-mail messages attached to it, may contain information that is confidential and/or legally privileged. If you are not the intended recipient, or a person responsible for delivering it to the intended recipient, please DO NOT disclose the contents to another person, store or copy the information in any medium, or use any of the information contained in or attached to this transmission for any purpose. If you have received this transmission in error, please immediately notify the sender by reply email or at privacy at plantronics.com, and destroy the original transmission and its attachments without reading or saving in any manner.
>
> For further information about Plantronics - the Company, its products, brands, partners, please visit our website www.plantronics.com.
>


More information about the antlr-interest mailing list