括号内冒号的含义

Meaning of colon inside parenthesises

表达式 (: .) 在 ANTLR 规则中有什么意义

message
   : COLON (: .)*?
   ;

https://github.com/antlr/grammars-v4/blob/master/stacktrace/StackTrace.g4#L79

?

我不能在任何文档中引用规则中允许的冒号。在这种情况下,括号内的 : 不应该被替换为 COLON 吗?

更新:

这些案例是否也是移除 options 后遗留下来的?;

grammars-v4/antlr/antlr3/examples/Verilog3.g(548,13): Warning: ignoring colon with no effect, token `'else'` at offset 12136
grammars-v4/vhdl/vhdl.g4(696,18): Warning: ignoring colon with no effect, token `logical_operator` at offset 13302
grammars-v4/vhdl/vhdl.g4(700,17): Warning: ignoring colon with no effect, token `DOUBLESTAR` at offset 13359
grammars-v4/vhdl/vhdl.g4(1087,25): Warning: ignoring colon with no effect, token `identifier` at offset 20396
grammars-v4/vhdl/vhdl.g4(1232,9): Warning: ignoring colon with no effect, token `relational_operator` at offset 22963
grammars-v4/vhdl/vhdl.g4(1310,9): Warning: ignoring colon with no effect, token `shift_operator` at offset 24300
grammars-v4/vhdl/vhdl.g4(1351,32): Warning: ignoring colon with no effect, token `adding_operator` at offset 24999
grammars-v4/vhdl/vhdl.g4(1497,16): Warning: ignoring colon with no effect, token `multiplying_operator` at offset 28078
grammars-v4/pascal/pascal.g4(439,38): Warning: ignoring colon with no effect, token `ELSE` at offset 7347

没有意义,应该删除。我的猜测是语法是从 ANTLR3 转换而来的,其中以下内容在 v3 版本中:

message
   : COLON (options{greedy=false;}: .)*
   ;

他们删除了 options{greedy=false;} 部分,但没有删除 :。为了匹配 v4 中的 ungreedy,引入了 ?。您还可以删除括号:

message
   : COLON .*?
   ;

这应该是对你的警告:语法没有经过很好的测试,规则以 .*? 结尾的事实对我来说总是有点可疑(通常有更好的方法来匹配这样的一条规则)。这个语法还有很多我不喜欢的地方(不一定是错的,但我认为是不好的做法)。在生产环境中谨慎使用!