简体   繁体   中英

ANTLR label heterogenous alternatives?

I would like to crate a grammar to define alternatives for events.

This

    time:
             type='at ' date
         | type='before ' date 
         | type='vor ' date
    ;


    date:
        SIMPLEDATE
    ;

    DAY: ('0'[1-9]|[12][0-9]|'3'[01]);
    MONTH: ('0' [1-9]|'1'[012]);
    YEAR: [0-2] [890] NUMBER NUMBER;
    SIMPLEDATE: DAY  [- /.] MONTH [- /.] YEAR;

seems to works fine.

But I would like reduce the first alternative like that:

    time:
             type='' date
         | type='before' date
         | type='after' date  
    ;

I tried the empty string because no type is not accepted.

With this modification a simple date is not recognized anymore.

Is there a way to label the (heterogenous) more consistent?

Yes. Simply do not provide a label in the non-qualified alternative. Antlr will generate a type token variable in the time context; for the non-qualified alternative, the value of Token type will be null.

Or, better:

time: type=('before'|'after')? date  ;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM