简体   繁体   English

ANTLR4特定位置的特定字符

[英]ANTLR4 specific characters in specific places

I'm using ANTLR4 for a project but I've been tasked with something I'm not quite grasping. 我正在为项目使用ANTLR4,但是我遇到的任务是我不太了解的东西。 I have have a lexer rule tokenize something that starts with either and underscore or a letter, can't start with 'xml' and can contain periods, dashes, letters, digits and underscores. 我有一个词法分析器规则,对以,下划线或字母开头的内容进行标记,不能以“ xml”开头,并且可以包含句点,破折号,字母,数字和下划线。 How would I make sure the beginning is a letter or an underscore and also be sure that it isn't 'xml'? 如何确定开头是字母或下划线,又确保它不是“ xml”?

The grammar: 语法:

grammar Expr;
prog: word+ EOF;
word : STRING ;
STRING : ~[xml]STRINGCHAR+;
WS : [ \t\r\n]+ -> skip;
fragment
STRINGCHAR : [_a-z] ;

matches "boy" and "_girl" but not "xmlboy" by using the explicit exclusion ~ operator in the definition of STRING. 通过在STRING的定义中使用显式排除〜运算符来匹配“ boy”和“ _girl”,但不匹配“ xmlboy”。

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

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