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

Yang Jiang yang.jiang.z at gmail.com
Sat Jun 16 23:51:09 PDT 2007


This works.
Thanks a lot for help :-)


But what if there are more than one optional rule, say 

function_definition : ID '.' '(' argument_list? ')'  throw_clause? '{'
codeblock? '}'  -> << ..... >>

if I write an rewrite for every possibilities, I would have to handle 8
different cases, end up with 8 rewrites. 
Is there any simple way to do this? 

Thanks,
Yang

-----Original Message-----
From: Jim Idle [mailto:jimi at temporal-wave.com] 
Sent: Monday, June 18, 2007 4:45 AM
To: Yang Jiang; antlr-interest at antlr.org
Subject: RE: [antlr-interest] How to write template for rules with optional
node?

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