[antlr-interest] adding node to AST

Donal Murtagh donalmurtagh at yahoo.co.uk
Fri Dec 2 08:24:37 PST 2005


Hi ,

Thanks very much for your response, however I don't think I can use your suggestion "as is".
Here's the subscriptionManager rule reproduced verbatim (I'd previosuly simplified it to avoid
confusion):

subscriptionManager[ChargeFile cf]
{ boolean oldSubMgr = true; }
	:	x:"SubscriptionManager"! (":"! sm:INT! { oldSubMgr = false; } )?
	{ if(oldSubMgr) {
		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);	  	
	  	
	  cf.addSubscriptionManager(sm);
	}
		LBRACE!
			(subscriptionLevel[cf.getSubscriptionManager()])*			
		RBRACE!
	;


As you can see if the token is in the correct format (SubscriptionManager:N), then we call 

cf.addSubscriptionManager(N);

The argument N is of type Token. If I treat the SubscriptionManager:N as a single token (as you
suggested), then I guess I could create this argument by stripping off the trailing integer and
calling:

Token N = new Token(Token.MIN_USER_TYPE, strippedInt);

Any other comments would be very welcome!

Cheers,
Don
--- Bryan Ewbank <ewbank at gmail.com> wrote:

> Hi Donal,
> 
> Since you're calling it a *token* rather than a sequence of tokens, it
> seems you should be gathering these in the lexer - or is arbitrary
> whitespace between the elements allowed, like this:
>                SubscriptionManager
>                :                            2345
>                { }
> 
> ... ... ...
> Assuming that this is a *token*, rather than a sequence of tokens,
> something like the following will work:
> 
>     // somewhat abstract lex syntax; sorry...
>     SubscriptionManager:[0-9][0-9]*     { return SUBSCRIPTION_MANAGER; }
> 
> This way, every token that starts with SubscriptionManager: and has a
> trailing integer component will be folded into one abstract token, and
> allows your grammar to look like this:
> 
>     subscriptionManager!
>         x:SUBSCRIPTION_MANAGER^ LBRACE! (subscriptionLevel)* RBRACE!
>         | "SubscriptionManager"
>             {
>                 // choke and die here!;
>             }
>     ;
> 
> This results in a parent node of SUBSCRIPTION_MANAGER, containing the
> appropriate string, and the trees produced by the <subscriptionLevel>
> elements are the children:
>     [SUBSCRIPTION_MANAGER,"SubscriptionManager:01"]
>         [SUBSCRIPTION_LEVEL, ... ]
>         [SUBSCRIPTION_LEVEL, ... ]
>         [SUBSCRIPTION_LEVEL, ... ]
> 
> 
> Hope this helps,
> - Bryan Ewbank
> 



		
___________________________________________________________ 
Does your mail provider give you FREE antivirus protection? 
Get Yahoo! Mail http://uk.mail.yahoo.com


More information about the antlr-interest mailing list