且构网

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

ANTLR4:lexer命令中无法识别的常数值

更新时间:2023-02-09 13:26:27

组合语法(即仅以grammar开头的语法,而不是parser grammarlexer grammar开头的语法)不能使用词法分析器模式.代替使用import功能¹,您应该使用这样的tokenVocab功能:

Combined grammars (which are grammars that start with just grammar, instead of parser grammar or lexer grammar) cannot use lexer modes. Instead of using the import feature¹, you should use the tokenVocab feature like this:

Lexer_To_Test_More_Command.g4 :

lexer grammar Lexer_To_Test_More_Command;

// lexer rules and modes here

Parser_To_Test_More_Command.g4 :

parser grammar Parser_To_Test_More_Command;

options {
  tokenVocab = Lexer_To_Test_More_Command;
}

// parser rules here

¹我实际上建议在ANTLR中完全避免使用import语句.我上面描述的方法几乎总是首选.

¹ I actually recommend avoiding the import statement altogether in ANTLR. The method I described above is almost always preferable.