否定词法分析器规则和内部规则 [英] Negating inside lexer- and parser rules
问题描述
否定元字符~
如何在ANTLR的词法分析器规则中使用?
How can the negation meta-character, ~
, be used in ANTLR's lexer- and parser rules?
推荐答案
Negating can occur inside lexer and parser rules.
在lexer规则内部,您可以否定字符,而在解析器规则中,您可以否定标记(lexer规则).但是词法分析器和解析器规则只能分别否定单个字符或单个标记.
Inside lexer rules you can negate characters, and inside parser rules you can negate tokens (lexer rules). But both lexer- and parser rules can only negate either single characters, or single tokens, respectively.
几个例子:
要匹配一个或多个字符(小写的ascii字母除外),您可以执行以下操作:
To match one or more characters except lowercase ascii letters, you can do:
NO_LOWERCASE : ~('a'..'z')+ ;
(否定元字符~
的优先级高于+
,因此上述规则等于(~('a'..'z'))+
)
(the negation-meta-char, ~
, has a higher precedence than the +
, so the rule above equals (~('a'..'z'))+
)
请注意,'a'..'z'
与单个字符匹配(因此可以取反),但是以下规则无效:
Note that 'a'..'z'
matches a single character (and can therefor be negated), but the following rule is invalid:
ANY_EXCEPT_AB : ~('ab') ;
因为'ab'
(显然)匹配2个字符,所以不能取反.要匹配包含2个字符但不包含'ab'
的令牌,您必须执行以下操作:
Because 'ab'
(obviously) matches 2 characters, it cannot be negated. To match a token that consists of 2 character, but not 'ab'
, you'd have to do the following:
ANY_EXCEPT_AB
: 'a' ~'b' // any two chars starting with 'a' followed by any other than 'b'
| ~'a' . // other than 'a' followed by any char
;
解析器规则
在解析器规则中,~
取反某个令牌或多个令牌.例如,您定义了以下标记:
parser rules
Inside parser rules, ~
negates a certain token, or more than one token. For example, you have the following tokens defined:
A : 'A';
B : 'B';
C : 'C';
D : 'D';
E : 'E';
如果您现在想匹配除A
以外的任何令牌,请执行以下操作:
If you now want to match any token except the A
, you do:
p : ~A ;
如果要匹配除B
和D
之外的任何令牌,则可以执行以下操作:
And if you want to match any token except B
and D
, you can do:
p : ~(B | D) ;
但是,如果您要匹配除A
后跟B
的任意两个令牌,则 不能 :
However, if you want to match any two tokens other than A
followed by B
, you cannot do:
p : ~(A B) ;
就像词法分析器规则一样,您不能求反多个令牌.为此,您需要执行以下操作:
Just as with lexer rules, you cannot negate more than a single token. To accomplish the above, you need to do:
P
: A ~B
| ~A .
;
请注意,解析器规则中的.
(DOT)字符与词法分析器规则中的任何字符都 不 匹配.在解析器规则内部,它匹配任何令牌(在这种情况下为A
,B
,C
,D
或E
).
Note that the .
(DOT) char in a parser rules does not match any character as it does inside lexer rules. Inside parser rules, it matches any token (A
, B
, C
, D
or E
, in this case).
请注意,您不能否定解析器规则.以下是非法的:
Note that you cannot negate parser rules. The following is illegal:
p : ~a ;
a : A ;
这篇关于否定词法分析器规则和内部规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!