否定词法分析器和解析器规则 [英] Negating inside lexer- and parser rules

查看:32
本文介绍了否定词法分析器和解析器规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 ANTLR 的词法分析器和解析器规则中使用否定元字符 ~?

How can the negation meta-character, ~, be used in ANTLR's lexer- and parser rules?

推荐答案

否定可能发生在 词法分析器和解析器规则.

在词法分析器规则中,您可以否定字符,在解析器规则中,您可以否定标记(词法分析器规则).但是词法分析器规则和解析器规则只能分别否定单个字符或单个标记.

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 ;

如果你想匹配除 BD 之外的任何标记,你可以这样做:

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) 字符匹配任何字符,就像它在词法分析器规则中一样.在解析器规则中,它匹配任何标记(ABCDE,在这种情况下).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆