[antlr-interest] v3 & templates question

Terence Parr parrt at cs.usfca.edu
Thu Sep 28 11:26:11 PDT 2006


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