在ANTLR4中,“not after”可以使用“not”和“MANY”的组合来实现。具体方法是,在需要匹配的模式前加上“not”,并在它后面加上“MANY”的表达式。下面是一个代码示例,其中匹配任何不以“x”开头的单词。
grammar NotAfterExample;
options { language = Java; }
@lexer::members { boolean notX() { int pos = _input.index(); boolean match = true; try { match(input.LA(1)); } catch (RecognitionException e) { match = false; } _input.seek(pos); return !match; } }
@parser::members { boolean notX() { return _input.LT(1).getText().charAt(0) != 'x'; } }
notXChar : ~[x] ;
notXWord : notX notXChar+ ;
WORD : [a-zA-Z]+ ;
SPACE : [ \t\r\n]+ -> skip ;
上一篇:ANTLR4歧义 - 如何解决