如何防止Antlr规则中出现两个后续空格?

如何防止Antlr规则中出现两个后续空格?,antlr,grammar,antlr4,Antlr,Grammar,Antlr4,作为lexer规则,我希望根据以下规则匹配字符串: 不能包含制表符(\t)或换行符(\r\n) 不能包含两个后续空格 可以包含所有其他字符,包括单个空格 我想到了: STRING: ~[\t\r\n]* 但我不知道如何防止出现空格。这样就可以了: STRING: ( ~[\t\r\n ] // non-whitespace | ' ' ~[\t\r\n ] // or single space followed by non-whitespace

作为lexer规则,我希望根据以下规则匹配字符串:

  • 不能包含制表符(\t)或换行符(\r\n)
  • 不能包含两个后续空格
  • 可以包含所有其他字符,包括单个空格
我想到了:

STRING: ~[\t\r\n]*
但我不知道如何防止出现空格。

这样就可以了:

STRING: 
     (
        ~[\t\r\n ] // non-whitespace
     | ' ' ~[\t\r\n ] // or single space followed by non-whitespace
     )+
     ' '?  // may optionally end in a space (if desired, remote the line otherwise)
     ;

您的字符串是否没有像
”这样的分隔符?该字符串可能的分隔符是\t、\n(和/或\r)和后面的两个空格。请不要问为什么:字符串的前面和结尾是PBoth?可能包括一些示例(在)有效字符串中。这很有效,谢谢!然后我修改了您的建议并以
字符串:~[\t\r\n]+(“”[\t\r\n]+)*;
以防止前导空格和尾随空格(我问这个问题时不知道这是相关的)。