非片段词法分析器规则x可以匹配空字符串



下面的鹿茸lexer有什么问题?

我得到一个错误

warning(146): MySQL.g4:5685:0: non-fragment lexer rule VERSION_COMMENT_TAIL can match the empty string

附源代码

VERSION_COMMENT_TAIL:
{ VERSION_MATCHED == False }? // One level of block comment nesting is allowed for version comments.
    ((ML_COMMENT_HEAD MULTILINE_COMMENT) | . )*? ML_COMMENT_END { self.setType(MULTILINE_COMMENT); }
| { self.setType(VERSION_COMMENT); IN_VERSION_COMMENT = True; }

,

您正在尝试将我的ANTLR3语法转换为MySQL的ANTLR4?删除词法分析器中的所有注释规则,并插入以下内容:

// There are 3 types of block comments:
// /* ... */ - The standard multi line comment.
// /*! ... */ - A comment used to mask code for other clients. In MySQL the content is handled as normal code.
// /*!12345 ... */ - Same as the previous one except code is only used when the given number is a lower value
//                   than the current server version (specifying so the minimum server version the code can run with).
VERSION_COMMENT_START: ('/*!' DIGITS) (
  {checkVersion(getText())}? // Will set inVersionComment if the number matches.
  | .*? '*/'
) -> channel(HIDDEN)
;
// inVersionComment is a variable in the base lexer.
MYSQL_COMMENT_START: '/*!' { inVersionComment = true; setChannel(HIDDEN); };
VERSION_COMMENT_END: '*/' {inVersionComment}? { inVersionComment = false; setChannel(HIDDEN); };
BLOCK_COMMENT: '/*' ~[!] .*? '*/' -> channel(HIDDEN);
POUND_COMMENT: '#' ~([nr])*  -> channel(HIDDEN);
DASHDASH_COMMENT: DOUBLE_DASH ([ t] (~[nr])* | LINEBREAK | EOF) -> channel(HIDDEN);

你需要一个本地inVersionComment成员和一个函数checkVersion()在你的词法分析器中(我在基本词法分析器中有它,生成的词法分析器从中派生),它返回true或false,这取决于当前服务器版本是否等于或高于给定版本。

对于你的问题:你不能在替代方案中有行动。操作只能出现在整个规则的末尾。

最新更新