没有特定字符序列的 Antlr 字符串标记

Antlr string token without a certain character sequence

我正在尝试定义一个词法分析器语法来匹配不包含特定字符序列的字符串标记。例如“AB”

我要捕获的字符串示例

""

"asda A rewr A"

"asda A"

"asdas B ad"

但不是

"asdas AB fdsdf"

我尝试了一些东西,但我似乎总是漏掉一些案例

可以用一点模式魔法来完成:当你在第一个string-mode时遇到一个AB,你直接推入第二个string-mode:

lexer grammar MyLexer;

QUOTE      : '"'        -> more, pushMode(MODE_1);
SPACES     : [ \t\r\n]+ -> skip;

mode MODE_1;
STR_1      : '"'        -> popMode;
AB         : 'AB'       -> more, pushMode(MODE_2);
CONTENTS_1 : ~["]       -> more;

mode MODE_2;
STR_2      : '"'        -> popMode, popMode;
CONTENTS_2 : ~["]+      -> more;

Java 演示:

String source = "\"\"\n" +
    "\"asda A rewr A\"\n" +
    "\"asdas AB fdsdf\"\n" +
    "\"asda A\"\n" +
    "\"asdas B ad\"\n";

Lexer lexer = new MyLexer(CharStreams.fromString(source));
CommonTokenStream stream = new CommonTokenStream(lexer);
stream.fill();

System.out.println(source);

for (Token t : stream.getTokens()) {
  System.out.printf("%-20s `%s`%n",
      MyLexer.VOCABULARY.getSymbolicName(t.getType()),
      t.getText().replace("\n", "\n"));
}

将打印以下内容:

""
"asda A rewr A"
"asdas AB fdsdf"
"asda A"
"asdas B ad"

STR_1                `""`
STR_1                `"asda A rewr A"`
STR_2                `"asdas AB fdsdf"`
STR_1                `"asda A"`
STR_1                `"asdas B ad"`