Why am I getting an error when assigning tokens to a channel?

痞子三分冷 提交于 2019-12-04 04:37:09

问题


I have the following code in my .g4 file.

@lexer::members{
public static final int WHITESPACE = 1;
public static final int COMMENTS = 2;
}


WS :  (' '|'\t'|'\f')+ -> channel(WHITESPACE)
   ;

COMMENT 
    :   '//' ~('\n'|'\r')* -> channel(COMMENTS)
    ;

LINE_COMMENT 
    :   '/*' .*? '*/' NEWLINE? -> channel(WHITESPACE)
    ;

I'm getting the following errors:

warning(155): Shiro.g4:239:34: rule 'WS' contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output

warning(155): Shiro.g4:243:38: rule 'COMMENT' contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output

warning(155): Shiro.g4:247:42: rule 'LINE_COMMENT' contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output

This is the technique described by Terrence in the ANTLR4 book to put tokens on separate channels. Why am I getting these warnings? Should I be concerned?


回答1:


You are not receiving an error; it is a warning. In particular, it is the UNKNOWN_LEXER_CONSTANT warning, which is new to ANTLR 4.2.

Compiler Warning 155.

rule 'rule' contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output

A lexer rule contains a standard lexer command, but the constant value argument for the command is an unrecognized string. As a result, the lexer command will be translated as a custom lexer action, preventing the command from executing in some interpreted modes. The output of the lexer interpreter may not match the output of the generated lexer.

The following rule produces this warning.

@members {
public static final int CUSTOM = HIDDEN + 1;
}

X : 'foo' -> channel(HIDDEN);           // ok
Y : 'bar' -> channel(CUSTOM);           // warning 155


来源:https://stackoverflow.com/questions/22027175/why-am-i-getting-an-error-when-assigning-tokens-to-a-channel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!