[antlr-interest] Matching String Templates to Tree Grammars

Adam Connelly adam.rpconnelly at googlemail.com
Thu Apr 3 04:28:17 PDT 2008


When you do l_treeWalker.TemplateLib = l_templateGroup you let the tree
walker know where to look for templates.  In your rules, in a template
output grammar (where you have output=template in the options for your
grammar) the bit after "->" specifies how to "create" the template for the
rule.

So a simple example that uses the template group you provided would be:

myRule: SOMETHING -> simpletemplate();

myOtherRule : forClause getClause -> translate(forClause={$forClause.text},
getClause={$getClause.text});

So basically it looks for the name of the template specified after the "->"
in the template group that you've specified for the walker.  You can also
write inline templates and so on.  Just so you know, the code I've written
above should be taken as pseudo-code since I've not verified that it works,
but it should be pretty close.

The following page has a reference for template construction
http://www.antlr.org/wiki/display/ANTLR3/Template+construction.

Cheers,
Adam

On 03/04/2008, Simon Woods <simon.woods at virgin.net> wrote:
>
> Hi
>
> New to all this so please bear with me ... I'm missing something (probably
> quite a lot, actually) in my understanding. (I'm awaiting delivery of the
>  Definitive Antlr guide ... )
>
> It may seem overkill for what I'm trying to do but ... I have
>
> ============================ tree grammar
>
> tree grammar QLWalker;
>
> options {
>   tokenVocab=QL;
>   ASTLabelType = CommonTree;
>       language = CSharp;
>       output=template;
>       rewrite=true;
> }
>
> mainstatement
>   : forstatement getstatement
>   ;
>  forstatement
>   : ^(FOR text)
>   ;
>
> getstatement
>   : ^(GET text (COMMA text)*)
>   ;    text
>   : ID -> template(name = {$ID.text}) "$name$"
>   ;
>
> ============================ String Template Group
>
> group QL2SQL;
>
> translate(forClause, getClause) ::= <<
> SELECT <getClause; separator=","> FROM <forClause>
> >>
>
> simpletemplate() ::= "This is the template"
>
> ============================ Test routine
>
> Here's the test I'm running (VB.Net)
>
>   Public Sub Template_CanSubstituteFromAST()
>
>       Dim l_templateGroup As Antlr.StringTemplate.StringTemplateGroup =
> CreateTemplateGroup(TemplateGroupFile)           Dim l_lexer As New
> QLLexer(New Antlr.Runtime.ANTLRStringStream("FOR myDatasource GET a,b,c"))
>
>       '???
>       Dim l_tokens As New Antlr.Runtime.TokenRewriteStream(l_lexer)
>       Dim l_parser As New QLParser(l_tokens)
>
>       '???
>       Dim l_nodes As New
> Antlr.Runtime.Tree.CommonTreeNodeStream(l_parser.mainStatement.Tree)
>
>       Dim l_treeWalker As New QLWalker(l_nodes)
>       l_treeWalker.TemplateLib = l_templateGroup
>
>       Assert.AreEqual("SELECT a,b,c FROM myDatasource",
> l_treeWalker.mainstatement.ToString())
>
>   End Sub
>
> (I've left ??? in as I'm just copying from
> http://aaronfeng.com/articles/2007/04/26/translation-part-2-via-abstract-syntax-tree - though I'm not sure why I need to do this at the moment)
>
> Now when I step the code, I check the template variable in the
> mainstatement class and it is not set. So I figured this has something to do
> with my problem (of the thing not working!). But I'm wondering how the
> treeWalker is meant to know which template to use. Do I have to tell it in
> some way?
>
> Thanks in advance for any help,
>
> Simon
>
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080403/bb3af345/attachment.html 


More information about the antlr-interest mailing list