Daniel
Daniel

Reputation: 4549

Is there a way to specify order-dependent "at least one of these" in antlr4?

I have a rule:

element
    : first
    | second
    | first second
    ;

I can shrink it down slightly:

element
    : first
    | first? second
    ;

Is there a better way to write this? Something like first <andor> second where <andor> is the magic I need?

Same question for lexer rules:

FP
    : [0-9]+ '.' [0-9]*
    | [0-9]* '.' [0-9]+

Upvotes: 1

Views: 33

Answers (1)

Mike Lischke
Mike Lischke

Reputation: 53317

There's no way to make this simpler. The only change I'd suggest is that you make the second part optional, not the first one. This should give you a (slight) performance improvement, because the prediction algorithm doesn't have to visit two paths (one with and one w/o the first optional part) to know if an alternative can match:

element
    : first second?
    | second
    ;

Upvotes: 1

Related Questions