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

Achint Mehta achintmehta at gmail.com
Fri Aug 21 23:27:08 PDT 2009


Hi Mark,

Thanks for your response.

When I tried the first solution, it seems that the action associated with
the optional (second) rule is always executed whether the optional (second)
rule matched or not.
I am pasting the actual grammar that I made to test this:
-------------------------------------------------
grammar sample_parser;
options
{
 language = C;
}

first : (second)? {printf("\n Second is present\n"); } ' ' third;
second: 'SECOND';
third: 'THIRD';
NEWLINE: ('\r')? '\n';
------------------------------------------------------------
For an input of " THIRD" (without the quotes) or "SECOND THIRD" (without the
quotes), the output is always "Second is present".

Now if the optional (second) rule did not match and I try to access the text
of the second rule (as $second.text->chars or even $second.text), then the
program crashes in toStringTT().

In the release notes of ANTLR 3.1 (
http://www.antlr.org/wiki/display/ANTLR3/ANTLR+3.1+Release+Notes), the
following is mentioned in the section "Element properties":

"Made refs to rule/token properties use conditional operator ?: to avoid
null ptr exceptions. $label.st, for example, is now label!=null?label.st:
null not label.st. This is useful not only for optional rule/token refs, but
also during error recovery. If ID is not matched, $ID.text won't cause a
null ptr."

This made me believe that in ver 3.1.3, it should be possible to check
whether the rule text is null or not (as you specified in solution 2 for
token) but I could not make it work (compilable).

Would you have any idea on this ?

Thanks in advance.

Regards,
Achint

On Sat, Aug 22, 2009 at 1:46 AM, Mark Wright <markwright at internode.on.net>wrote:

> 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
>
>
> --
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090822/9bde359e/attachment.html 


More information about the antlr-interest mailing list