ANTLR 努力解析整数与引用字符串

ANTLR struggling to parse integer vs quoted string

我正在尝试使用 ANTLR 创建一种语言,其中每一行都包含一条指令,其中一条指令是一个操作码和任意数量的操作数,如下所示:

aaa "str1" "str2" 123
bbb 123 "str" 456
ccc
ddd

我的字符串似乎工作正常,但整数似乎解析不正确。

这是我的完整语法文件:

grammar Insn;

prog: (line? NEWLINE)+;

line: instruction;
instruction: instruction_name instruction_operands?;

instruction_name: IDENTIFIER;
instruction_operands: instruction_operand instruction_operand*;
instruction_operand: ' '+ (operand_int | operand_string);

operand_int: INT;
operand_string: QSTRING;

NEWLINE : [\r\n]+;
IDENTIFIER: [a-zA-Z0-9_\-]+;
INT: '-'?[0-9]+;
QSTRING: '"' (~('"' | '\' | '\r' | '\n') | '\' ('"' | '\'))* '"';
COMMENT: ';' ~[\r\n]* -> channel(HIDDEN);

我已经尝试了多个不同的 INT 定义,例如 INT: '-'?('0'..'9')+;INT: '2'; 使输入中的所有 INT 2,总是导致类似于 [=15= 的错误], 行号、列和 123 整数替换为正在解析的任何内容。

这是在 ANTLR getting-started.md 文档中使用的由 ANTLR 工具生成的解析树。

我是 ANTLR 的新手,不熟悉很多术语,所以请对我保持简单。

问题是 123 被识别为 IDENTIFIER 因为它是一个有效的标识符(所有 INT 都是)。两者必须是可区分的。 IDENTIFIER 应该是这样的 IDENTIFIER: [a-zA-Z][a-zA-Z0-9_\-]*;