用于标记化的 Antlr 词法分析器规则 float/double

Antlr lexer rule to tokenize float/double

我正在尝试使用 this regex 来标记 Antlr 中的双精度数,但显然 Antlr 无法理解此正则表达式。

Number: (-?)(0|([1-9][0-9]*))(\.[0-9]+)?;

错误:

syntax error: '-' came as a complete surprise to me 
syntax error: '?' came as a complete surprise to me
syntax error: '0' came as a complete surprise to me
syntax error: '\' came as a complete surprise to me 
syntax error: '\' came as a complete surprise to me 
syntax error: '-' came as a complete surprise to me 
syntax error: '?' came as a complete surprise to me
syntax error: '0' came as a complete surprise to me
syntax error: '\' came as a complete surprise to me 
syntax error: '\' came as a complete surprise to me

ANTLR Lexer 规则有点类似于 REGEX,但绝对不是 REGEX。

尝试:

Number: '-'?('0'|[1-9][0-9]*)('.'[0-9]+)?;

字符需要用'号括起来(集合符号除外([]之间的字符)