[antlr-interest] Check if rule returns empty result

Jim Idle jimi at temporal-wave.com
Thu Nov 3 16:22:35 PDT 2011


Remember that your tree parser rule for the body should be:

method_body
    : ^(METHOD_BODY statement_list)
    | METHOD_BODY  // Empty body
    ;

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Christian
> Sent: Thursday, November 03, 2011 2:37 PM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Check if rule returns empty result
>
> Thank you for your explanation. And your solution works great, too.
>
> Christian
>
> Am 03.11.2011 21:31, schrieb Jim Idle:
> > You don't want that anyway as your nodes will not have token start
> and
> > stop information and you might need that.
> >
> > method_declaration
> >      : method_header method_body
> >      ;
> >
> > method_body
> > 	: (
> > 		  ';'!
> >      	| '{'!   statement_list?   '}'!
> >        )
> > 		-> ^(METHOD_BODY statement_list?)
> >      ;
> >
> > method_header
> > 	:  attributes ID etc
> >
> > 		->^(METHOD_HEADER attributes ID )
> > 	;
> >
> >
> > Jim
> >
> >> -----Original Message-----
> >> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> >> bounces at antlr.org] On Behalf Of Christian
> >> Sent: Thursday, November 03, 2011 1:01 PM
> >> To: antlr-interest at antlr.org
> >> Subject: [antlr-interest] Check if rule returns empty result
> >>
> >> Hi at all,
> >>
> >> I have the following rules:
> >>
> >> method_declaration
> >>     : method_header method_body  -> ^(METHOD_HEADER method_header)
> >> ^(METHOD_BODY method_body)
> >>     ;
> >>
> >> method_body:
> >>     ';'!
> >>     | '{'!   statement_list?   '}'!;
> >>     ;
> >>
> >> Of course, method_body is executed because it has no question mark.
> >> However, introducing rewrite rules and operators cause a problem. By
> >> using ^(METHOD_BODY method_body), method_body must return a non-
> empty
> >> value. However, statement_list? is optional and thus may not match
> at
> >> all. In this case, method_body returns nothing anymore and I get an
> >> RewriteEmpty exception.
> >>
> >> How can I solve this problem by only changing rewrite rules?
> >>
> >> Regards,
> >> Christian
> >>
> >> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >> Unsubscribe:
> >> http://www.antlr.org/mailman/options/antlr-interest/your-
> >> email-address
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> > http://www.antlr.org/mailman/options/antlr-interest/your-email-
> address
> >
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list