ANTLR 中是否存在逻辑 AND 和 NOT?
ANTLR 里没有逻辑吗?我基本上试图否定我所拥有的规则,并且想知道它是否可能,还有 AND 逻辑吗?
Is there NOT logic in ANTLR? Im basically trying to negate a rule that i have and was wondering if its possible, also is there AND logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@larsmans 已经提供了答案,我只是想举一个 ANTLR 规则中的法律否定的例子(因为它们经常发生错误)。
ANTLR 中的否定运算符是
~
(波形符)。在词法分析器规则中,~
否定单个字符:匹配除
'A'
之外的任何字符并且:匹配除小写 ASCII 字母之外的任何字符。 lats 的例子也可以写成:
只要你只求反一个字符,使用
~
就是有效的。这样做是无效的:因为你不能否定字符串
'aa'
。在解析器规则内,否定不适用于字符,但适用于标记。因此,
parse
规则:不匹配
'b'
之外的任何字符,但匹配B
之外的任何标记代码 > 令牌。因此它会匹配标记A
(字符'a'
)或标记C
(字符'c'
)。相同的逻辑适用于
.
(DOT) 运算符:\u0000..\uFFFF
中的任何字符;@larsmans already supplied the answer, I just like to give an example of the legal negations in ANTLR rules (since it happens quite a lot that mistakes are made with them).
The negation operator in ANTLR is
~
(tilde). Inside lexer rules, the~
negates a single character:matches any character except
'A'
and:matches any character except a lowercase ASCII letter. The lats example could also be written as:
As long as you negate just a single character, it's valid to use
~
. It is invalid to do something like this:because you can't negate the string
'aa'
.Inside parser rules, negation does not work with characters, but on tokens. So the
parse
rule:does not match any character other than
'b'
, but matches any token other than theB
token. So it'd match either tokenA
(character'a'
) or tokenC
(character'c'
).The same logic applies to the
.
(DOT) operator:\u0000..\uFFFF
;ANTLR 为上下文无关语言 (CFL) 生成解析器。在这种情况下,
not
将翻译为补码,and
将翻译为交集。但是,CFL 在补集和交集下并不闭合,即not(rule)
不一定是 CFG 规则。换句话说,不可能以合理的方式实现
not
和and
,因此它们不受支持。ANTLR produces parsers for context-free languages (CFLs). In that context,
not
would translate to complement andand
to intersection. However, CFLs aren't closed under complement and intersection, i.e.not(rule)
is not necessarily a CFG rule.In other words, it's impossible to implement
not
andand
in a sane way, so they're not supported.