[antlr-interest] Checking for missing optional token in rule

Jim Idle jimi at temporal-wave.com
Mon Jul 23 18:15:17 PDT 2012


paren_delimited_numeric_unit_list
     : LPAREN number unit1=unit
       (COMMA number
         (unit2=unit
            {
                C code Action when unit2 is present;
            }
         )?
       )* RPAREN
         -> (number unit)+
     ;

If you want to perform a custom rewrite, then set a flag (@declarations
and @init) and use the conditional rewrite syntax.

If you use unit2= though, then unit2 will only ever see the last one.

Are you sure that you are not just trying to do something like this:

paren_delimited_numeric_unit_list
     : LPAREN n+=number u+=unit (COMMA n+=number (u+=unit)? )* RPAREN
         -> ^(LPAREN ($n $u)+)
     ;


Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Burton Samograd
> Sent: Monday, July 23, 2012 2:55 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Checking for missing optional token in rule
>
> See the comment below for an explanation:
>
> paren_delimited_numeric_unit_list
>     : LPAREN number unit1=unit (COMMA number unit2=unit? {
>             // I would like to check if unit2 was present in the input
> tokens
>             // and do something if it was not.
>         })* RPAREN
>         -> (number unit)+
>         ;
>
> In the tree walker we can say ($unit2 == NULL) to check for presence,
> but that technique does not work in the parser.  Is there a way to do
> what I would like to do?
>
> Thanks.
>
> --
> Burton Samograd
>
> ________________________________
> This e-mail, including accompanying communications and attachments, is
> strictly confidential and only for the intended recipient. Any
> retention, use or disclosure not expressly authorised by Markit is
> prohibited. This email is subject to all waivers and other terms at the
> following link: http://www.markit.com/en/about/legal/email-
> disclaimer.page
>
> Please visit http://www.markit.com/en/about/contact/contact-us.page?
> for contact information on our offices worldwide.
>
> 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