ANTLR4:lexer命令中无法识别的常数值 [英] ANTLR4: Unrecognized constant value in a lexer command
问题描述
我正在学习如何使用更多" lexer命令.我输入了ANTLR书第281页中显示的词法分析器语法:
I am learning how to use the "more" lexer command. I typed in the lexer grammar shown in the ANTLR book, page 281:
lexer grammar Lexer_To_Test_More_Command ;
LQUOTE : '"' -> more, mode(STR) ;
WS : [ \t\r\n]+ -> skip ;
mode STR ;
STRING : '"' -> mode(DEFAULT_MODE) ;
TEXT : . -> more ;
然后,我创建了这个简单的解析器以使用词法分析器:
Then I created this simple parser to use the lexer:
grammar Parser_To_Test_More_Command ;
import Lexer_To_Test_More_Command ;
test: STRING EOF ;
然后我打开DOS窗口并输入以下命令:
Then I opened a DOS window and entered this command:
antlr4 Parser_To_Test_More_Command.g4
产生此警告消息:
警告(155):Parser_To_Test_More_Command.g4:3:29:规则LQUOTE 包含一个无法识别的常数值的lexer命令;词法分析器 口译员可能会产生错误的输出结果
warning(155): Parser_To_Test_More_Command.g4:3:29: rule LQUOTE contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output
我在词法分析器或解析器中做错什么了吗?
Am I doing something wrong in the lexer or parser?
推荐答案
组合语法(即仅以grammar
开头的语法,而不是parser grammar
或lexer 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.
这篇关于ANTLR4:lexer命令中无法识别的常数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!