[antlr-interest] Tree grammars and references to attributes of the calling rule

Jim Idle jimi at temporal-wave.com
Thu Sep 18 09:03:32 PDT 2008


On Thu, 2008-09-18 at 16:00 +0200, Dominique de Waleffe wrote:
> I am exploring the use of tree grammars...
> 
> I exepcted to be able to write something like the following:
> 
> tree grammar PipelineWalker;
> 
> options {
>     language = Java;
>     tokenVocab=Pipeline;
>     ASTLabelType=CommonTree;
> }
> @header{
> //  import Pipeline;
> }
> pipeline[Pipeline p]:  ^(PIPE header body) ;
> 
> header  : ^('pipeline'  name) {
>   System.out.println($pipeline::p.getName());
>   $pipeline::p.setName($name.text);}
>   ;
> 
> Where the intent is to get my 'Pipeline' object from the top level and
> modifying it while going down the tree.
> 
> 
> The problem is that the references I make to $pipeline::p in my header rule seem to go untranslated into the generated code:
> 
> 

You need to use a scope to do this;

pipeline [Pipeline pIn]

scope {
 Pipeline p;
}
@init
{
  $pipeline::p = $pIn;
}

Then make sure that everything starts with the pipeline rule.

However, I think a better way is:

@members {

   protected Pipeline p;
   public void setPipeline(Pipeline p) { this.p = p; }
   public Pipeline getPipeline() { return this.p; }
}

Then just use p directly in your actions code. After you create the tree
parser, call setPipeline(myPipeline) on it; then invoke the walker. I
suggest a better name than just 'p' though ;-)

Jim

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


More information about the antlr-interest mailing list