且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Antlr lexer语义谓词的替代方法

更新时间:2023-09-16 14:45:52

快速解决方案是检查以前的LA(-1)令牌是否不相等.并添加前导的可选.

Quick fix is to check the previous LA(-1) token if it is unequal . and add a leading optional DOT.

结果语法为:

grammar Test;
words: (WORD|SPACE|DOT)+;
WORD : DOT? (
       LD
       |{_input.LA(-1)!='.'}? DOT       
       ) +        ;
DOT: '.';
SPACE: ' ';
fragment LD: ~[.\n\r ];

玩得开心,享受ANTLR,这是一个很好的工具.

Have fun and enjoy ANTLR, it is a nice tool.