[antlr-interest] How to write template for rules with optional node?

Jim Idle jimi at temporal-wave.com
Sun Jun 17 13:44:55 PDT 2007


Just use a different rewrite if there are no arumgents:

function_call :
  ID '.' '(' 
	(
         argument_list ')'
 	       ->    <<
 	       template(function_name={$ID.text},
 
function_args={$argument_list.st})
                   >>
        | ')'
             -> << something else >>
      )
 	      ;

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Yang Jiang
> Sent: Saturday, June 16, 2007 9:07 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] How to write template for rules with
optional
> node?
> 
> Hi list,
> 
> I'm trying to write a template for such a rule,
> 
> function_call :
> 		ID '.' '(' argument_list? ')'
> 	       ->
> 	      <<
> 	       template(function_name={$ID.text},
> function_args={$argument_list.st})
>                   >>
> 	      ;
> 
> this works fine for input such as "aSimpleMethod(arg1, arg2)",
> but since the argument_list is optional, for input like
> "aSimpleMethod()",
> the argument_list is null, and will cause a NullPointerException as
> antrl
> will try to access the "st" attribute of "argument_list".
> 
> So, how can I handle this problem?
> 
> Thanks,
> Yang



More information about the antlr-interest mailing list