[antlr-interest] Checking whether an optional rule matched or not

Mark Wright markwright at internode.on.net
Fri Aug 21 22:46:15 PDT 2009


Hi Achint,

Maybe something like:

first
@init { bool second_rule_matched = false; }
  : (second)? { second_rule_matched = true; } third
{
    if(second_rule_matched)
      cout<<" Second rule matched"<<endl;
}
;

Or p. 126 of the Antlr book describes how to do it with optional tokens,
something like (assuming 'second' is a token):

first
  : (second_rule_matched='second')? third
{
    if($second_rule_matched != 0)
      cout<<" Second rule matched"<<endl;
}
;

Regards, Mark

On Fri, 21 Aug 2009 23:34:05 -0400
Achint Mehta <achintmehta at gmail.com> wrote:

> Hi All,
> 
> If I have a option rule in my grammar, how can I check whether that rule
> matched or not.
> e.g. if I have a rule in my grammar
> 
> first: (second)? third
> {
>     if(/* second rule matched */)
>       cout<<" Second rule matched"<<endl;
> }
> 
> How do I write the if condition (in C/C++) ? (I am using version 3.1.3 of
> the antlr)
> 
> Thanks!
> 
> Regards,
> Achint


-- 


More information about the antlr-interest mailing list