简体   繁体   English

ANTLR4中的令牌否定

[英]Negative of token in ANTLR4

I define LEGAL_ESCAPE token like this: 我这样定义LEGAL_ESCAPE令牌:

LEGAL_ESCAPE: '\\'('b'|'f'|'r'|'n'|'t'|'"')?;

Is there any way for me to use negative of this token to define another ILLEGAL_ESCAPE token? 我有什么办法可以使用此令牌的负数来定义另一个ILLEGAL_ESCAPE令牌?

Certainly: 当然:

LEGAL_ESCAPE    : ESC [bfrnt"] ;
ILLEGAL_ESCAPE  : ESC 
                   ( ~[bfrnt"]  // any char not in the set
                   | EOF        // it is a non char
                   )
                ;
fragment ESC    : '\\' ;

There are quite a few of these 'tricks' implicit in the various example grammars in the Antlr4 Grammar Repo . Antlr4语法仓库中的各种示例语法中隐含了很多“技巧”。 The abnf and less grammars are good starting points. abnf和较少的语法是很好的起点。 The Java8, Antlr4 and Python3 grammars demonstrate many of the more involved capabilities. Java8,Antlr4和Python3语法演示了许多更复杂的功能。

Note, the negation operator ~ works on characters, sets and ranges of characters, on single tokens and sets of single tokens. 注意,否定运算符~适用于字符,字符集和范围,单个标记和单个标记集。 But, 但,

ILLEGAL_ESCAPE  : ~LEGAL_ESCAPE ;

is unlikely what you were asking for. 不太可能是您要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM