[antlr-interest] Newbie Needs help... StringTemplate and Language Translation

siemsen at ucar.edu siemsen at ucar.edu
Thu May 22 08:30:58 PDT 2008


Kevina,

You don't "need" 2 or 3 grammars.  I have a useful ANTLR 3 translator  
that uses StringTemplate and just 1 grammar, with no AST.  The number  
of grammars you need has to do with the number of phases that you  
need.  For example, if your language has forward references (in one  
source file, definitions of functions can appear after calls to the  
functions), then you probably need 2 grammars, one that lexes/parses  
and collects the names of the defined functions.  Then in a later AST  
phase, you can check that function calls refer to functions that have  
been defined, and emit output using StringTemplate.

The ANTLR book has a good chapter on how to use StringTemplate, with  
examples.

-- Pete


On May 22, 2008, at 5:43 AM, Kevina Choolhun wrote:

> Hello again everybody...
>
> I  have been checking the Mantra Language and I am spell bound.
>
> I am a student and would like a simple example of how to use  
> StringTemplate to translate a language. So far, I have 2 grammars,  
> but i have been told I need three. I am working on it. But still  
> cannot get a hang of how to deal with the StringTemplate bit.
>
> Anybody can help me?
>
> Thank you
>
> Kevina
>
>
> On Wed, May 21, 2008 at 6:44 PM, Kevina Choolhun <kevina.choolhun at gmail.com 
> > wrote:
> Thanks again... Me too havent got that deep yet... But I'm going to  
> look into this...
>
> Thank you.
>
> Kevina
>
>
> On Wed, May 21, 2008 at 6:33 PM, Nikolas Everett <nik9000 at gmail.com>  
> wrote:
> I haven't gotten that deep into this as of yet, but the idea is to  
> have something like
> class VerificationCallback {
>  private final List<Error> errors;
>
>  public void import(ASTNode importedPackage) {//Do some checks, and  
> add errors to the list of errors}
>   public void referencedType(ASTNode nodeForType) {//Do some checks,  
> and add errors to the list of errors}
>
> }
>
> You'd create the grammar with a member for the callback, and then  
> you'd call it when processing the tree like so:
> import:  ^(IMPORT name=packageName) {this.callback.import(name);}
>
> --Nik
>
>
> On Wed, May 21, 2008 at 10:13 AM, Kevina Choolhun <kevina.choolhun at gmail.com 
> > wrote:
> Hello,
>
> Thanks! Could you please explain further what you mean by
>
> "1 grammar to verify the AST (tree grammar with no output, only  
> embedded actions calling a callback)"
>
> Maybe, you mean error messages?
>
> Thank you.
>
> Kevina
>
>
> On Wed, May 21, 2008 at 5:59 PM, Nikolas Everett <nik9000 at gmail.com>  
> wrote:
> I learned a good portion of what I know about antlr and  
> stringtemplate by going through the source code for the Mantra  
> compiler.  Its all here:  http://www.linguamantra.org/
>
> The long and short of it is that embedding actions is great for  
> checking things but is more cumbersome when actually generating  
> output.  Tree grammars that output templates are the way to go there.
>
> I tend to do:
> 1 grammar to lex and parse into an AST (output=AST)
> 1 grammar to verify the AST (tree grammar with no output, only  
> embedded actions calling a callback)
> 1 grammar to generate output (tree grammar with output=template)
>
> This is wonderfully flexible because the grammars become modules,  
> allowing you to test them individually and substitute them at will.   
> This is kind of a pain because you need to wrote three or more  
> grammars and sync up the output of the tree constructing grammar to  
> the input of the others.
>
>
> On Wed, May 21, 2008 at 9:45 AM, csanders <csanders at hoovers.com>  
> wrote:
> I'm also new to ANTLR and would like to know how StringTemplate is a  
> better solution, is there somewhere we can read about this ?
>
> Thanks,
> Charlie
>
> Kevina Choolhun wrote:
> Hello,
>
> I am new at ANTLR but with the help of The Definitive ANTLR  
> Reference have managed to set up a parser and a parser walker..
>
> Now for the next stage, I have tried my hand at embedding actions..  
> but Stringtemplate I hear is a much better solution.
>
> I am currently reading through stringtemplate.org <http://stringtemplate.org 
> >. I wanted to know if there are any nice tutorials about it which  
> could help me.. enhance my parser n lexer with StringTemplate
>
>
> Thank you for your help.
>
> -- 
> Kev
>
>
>
>
>
> -- 
> Kevina
>
>
>
>
> -- 
> Kevina
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080522/d0f32b2b/attachment.html 


More information about the antlr-interest mailing list