大蛋散
大蛋散

Reputation: 113

How to describe String that contains characters with range counts under ANTLR4 lexer rules?

There's a rule about the flight no(such as:CZ3102), which has 2 chars followed by 3-4 digits. And its Regular Expression should be: [A-Z]{2}[0-9]{3,4}.

Then how to write the lexer rule under ANTLR4?

One easy lexer rule is: [A-Z][A-Z][0-9][0-9][0-9][0-9]?

But that's not so elegant, and if the range is big, such as 1-255, it's not so easy the lexer rule.

Thanks

Upvotes: 2

Views: 849

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170308

But that's not so elegant, and if the range is big, such as 1-255, it's not so easy the lexer rule.

Tokenize just numbers, and validate the numerical value inside parser listener or visitor.

Related links:

Upvotes: 1

Related Questions