[antlr-interest] Matching String Templates to Tree Grammars

Simon Woods simon.woods at virgin.net
Thu Apr 3 02:12:12 PDT 2008


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






More information about the antlr-interest mailing list