user2192043
user2192043

Reputation: 11

ANTLR 4 java.g4 -> weird grammar rule

here is one of the rule I have seen in java.g4:

DecimalLiteral : ('0' | '1'..'9' '0'..'9'*) IntegerTypeSuffix? ;

Why not writing it this way:

DecimalLiteral : ('0'..'9'+) IntegerTypeSuffix? ;

Is there anything I'm missing ? Thank you for your feedback

Regards Philippe Frankson

Upvotes: 1

Views: 715

Answers (1)

Sam Harwell
Sam Harwell

Reputation: 99939

The intent is to have 0 be a DecimalLiteral, but all other integers starting with 0 be an OctalLiteral.

I would prefer to use a pair of rules like this:

OctalLiteral : '0'+ [1-7] [0-7]* IntegerTypeSuffix?;
DecimalLiteral : [0-9]+ IntegerTypeSuffix?;

And then defer validation of invalid octal integers (which this pair of rules would still accept as a DecimalLiteral) to a later step in the parsing process.

Upvotes: 4

Related Questions