[antlr-interest] v3 & templates question

Robert Hill rob.hill at blueyonder.co.uk
Thu Sep 28 12:34:00 PDT 2006


Ahh, there's the missing link right there...

             codegen.setTemplateLib(generator.templates);
   
I did look for some form of linking the templates to the treewalker, (im
using intelliJ) but the intellisense doesn't show that method, hence my
confusion. I saw the references to templates in the rewrite rules, but no
method to make the connection ..

Brilliant! I was hoping for something better than masses of setAttributes
this time round :)

V3 absolutely Rocks.

Cheers Ter!

Rob



-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terence Parr
Sent: 28 September 2006 19:26
To: Antlr List
Subject: Re: [antlr-interest] v3 & templates question


On Sep 28, 2006, at 8:58 AM, Robert Hill wrote:

> I may be missing something but how do I interface templates into a  
> V3 tree grammar?
>
> I've checked out the V3 Template Construction page, but it's not  
> making much sense - I cant see any other examples of a v3 grammar  
> using templates that already exist in group files.
>
> Do I have to use the templates the old way ie, populate them with  
> setAttribute calls inside the tree grammars actions? , or have I  
> missed a vitally important blog explaining how to do it?
>
>
>
> Any pointers to examples would be appreciated!

Hi Rob, please see:

http://www.linguamantra.org

Stuff like:

tree grammar CodeGenPhase;

options {
   output=template;
   tokenVocab = Mantra;
   ASTLabelType = MantraAST;
}

...

assignment
     :   // special case "a[i] = expr;"
         ^('=' ^(EXPR ^(INDEX a=expression i=expression))  
rhs=completeExpression)
         -> indexed_assignment(list={$a.st}, index={$i.st}, rhs= 
{$rhs.st})
     |   ^('=' lvalue completeExpression)
         -> assignment(
                 lhs={$lvalue.st},
                 rhs={$completeExpression.st})
     |   ^(assign_op lvalue completeExpression)
         -> assignment_with_op(
                 type={$assign_op.start.type.name},
                 op={$assign_op.text},
                 lhs={$lvalue.st},
                 rhs={$completeExpression.st})
     ;

THen I create with:

         CodeGenerator generator = new CodeGenerator();
         try {
             CommonTreeNodeStream nodeStream = new  
CommonTreeNodeStream(tree);
             CodeGenPhase codegen = new CodeGenPhase 
(nodeStream,                                                    tokens,
                                                     symtab,
                                                     generator);
                codegen.setTemplateLib(generator.templates);
             	codegen.compilationUnit();
         }
         catch (RecognitionException re) {
                 re.printStackTrace();
         }

Ter




More information about the antlr-interest mailing list