antlr4 - 为什么在将 token 分配给 channel 时出现错误?

标签 antlr4

我的 .g4 文件中有以下代码。

@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)
    ;

我收到以下错误:

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

这是 Terrence 在 ANTLR4 书中描述的将 token 放在单独 channel 上的技术。为什么我会收到这些警告?我应该担心吗?

最佳答案

您没有收到错误;这是一个警告。特别是,它是 UNKNOWN_LEXER_CONSTANT警告,这是 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

关于antlr4 - 为什么在将 token 分配给 channel 时出现错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22027175/

相关文章:

java - 如何创建动态 ANTLR4 词法分析器规则

antlr4 - 如何在antlr lexer中处理嵌套注释

javascript - Antlr4 基于事件的访客 (javascript)

java - ANTLR 不匹配输入 'foo(some_foo)' 期望 {'foo' }

java - 使用 Antlr4 解析简单模板

Antlr4 不匹配复合标记

java - 从 Java 源代码生成 AST 报告而不实际运行它

java - ANTLR标签异构替代品?

antlr - 否定内部词法分析器和解析器规则

java - ANTLR4 是否可以确定适用于某些位置的代币类型?