[antlr-interest] How to use an optional rule attribute in an action?

Jim Idle jimi at temporal-wave.com
Tue Jun 26 11:54:43 PDT 2007



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of troy runkel
> Sent: Tuesday, June 26, 2007 11:51 AM
> To: ANTLR mail-list
> Subject: [antlr-interest] How to use an optional rule attribute in an
> action?
> 
> Greetings,
> 
> I'm using ANTLR v3 with the C-target and have a situation like the
> following.  I have an optional rule called number and I want to access
> the text attribute of that rule if and only if the rule was applied.
> Using the C-target, $number is uninitialized if the rule wasn't
> applied.  Is there a way, in the action, to determine if $number.text
> is valid?
> 

create_procedure :
         'CREATE' ('PROC' | 'PROCEDURE') procedure_name ( ';' n=number
)?
         {
             if ($n != NULL)
		 {
		     printf("Found number '%s'\n", $n.text->chars);
                 someFunction($n.text);
		 }
         }

Makes most sense to me.

Jim


More information about the antlr-interest mailing list