Mike75
Mike75

Reputation: 524

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?

Upvotes: 1

Views: 84

Answers (1)

GRosenberg
GRosenberg

Reputation: 5991

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  ;

Upvotes: 1

Related Questions