[antlr-interest] adding node to AST

Donal Murtagh donalmurtagh at yahoo.co.uk
Fri Dec 2 07:13:35 PST 2005


Thanks very much for your response. Sorry, but I wasn't clear about what I'm trying to do in my
original mail. The block I'm trying to process looks like this

SubscriptionManager:2
{
 /* Stuff not relevant to this discussion */
}

What I want is to add the *entire* token as a node e.g. "SubscriptionManager:2", but I need to
check if the token is just "SubscriptionManager", so I can print out a particular message if it
is.

That's why I was trying things like 

x:"SubscriptionManager" (":" sm:INT { oldSubMgr = false; } )?^

I've almost figured out how to do this, my rule now looks like this:

subscriptionManager!
{ boolean badFormat = true; }
	:	x:"SubscriptionManager" (":" sm:INT { badFormat = false; } )?
	{ if(badFormat) {
		ChargeFileProblem problem = new ChargeFileProblem(x, 
			"'SubscriptionManager' block is no longer legal.");
		throw new ChargeFileException(problem);
	  }
	  
	  String nodeText = "SubscriptionManager:" + sm.getText();
	  // add the AST node
	  #subscriptionManager = #(#[SUBSCRIPTION_MANAGER, nodeText],
	  		#subscriptionManager);	  	
	  	
	}
		LBRACE
			(subscriptionLevel)*			
		RBRACE
	;


This does add SubscriptionManager:X as a node, but any nodes that are added in the
subscriptionLevel rule are not added as children of SubscriptionManager:X (which is what I want),
but as siblings of SubscriptionManager:X.

If I turn construction on (and put '!' after the elements I don't want added to the AST), the
result is still the same.

Thanks Again,
Don


> > I need to check that the SubscriptionManager token *does* end with :INT, and add it as a node.
> 
> So why is the (":" INT) block optional then? Better leave syntax
> analysis and errors to ANTLR.

Basically so that I can print out a friendly error message.

> 
> > subscriptionManager!
> > { boolean oldSubMgr = true; }
> > 	:	x:"SubscriptionManager" (":" sm:INT { oldSubMgr = false; } )?
> > 	{ 
> > 	  #subscriptionManager = #x; 
> > 		
> > 	  if(oldSubMgr) {		
> > 		throw new Exception("bad format");
> > 	  }
> > 	}
> > 		LBRACE!
> > 		RBRACE!
> > 	;
> 
> Well, with the "!" no AST construction takes place in the rule, so the
> ASTs for ":" and "sm:INT" are not attached anywhere. If you want them,
> you have to add them manually in this case:
> > x:"SubscriptionManager" col:":" sm:INT
> > { #subscriptionManager = #(#x, #col, #sm); }
> 
> or, preferrably without action code 
> (and with construction on, e.g. no "!"):
> > x:"SubscriptionManager"^ col:":" sm:INT
> 
> This will give you
>     x
>   /   \
>  col  sm
> 
> (Do you really need the ":"?)
> 
> > I previously tried to add the node in the usual way, using '^' (with the '!' removed from the
> rule
> > name and the line "#subscriptionManager = #x;" omitted).
> 
> The above should work.
> 
> > I tried all of the following, but no luck:
> > x^:"SubscriptionManager" (":" sm:INT { oldSubMgr = false; } )?
> > x:"SubscriptionManager" (":" sm:INT { oldSubMgr = false; } )?^
> > x:("SubscriptionManager" (":" sm:INT { oldSubMgr = false; } )?)^
> 
> I think those are not syntactically correct in ANTLR, are they? There is
> a section in the manual about tree construction, though it's quite
> hidden.
> 
> Martin
> 
> 



		
___________________________________________________________ 
WIN ONE OF THREE YAHOO! VESPAS - Enter now! - http://uk.cars.yahoo.com/features/competitions/vespa.html


More information about the antlr-interest mailing list