ANTLR4:词法分析器命令中无法识别的常量值 [英] ANTLR4: Unrecognized constant value in a lexer command

查看:39
本文介绍了ANTLR4:词法分析器命令中无法识别的常量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用更多"词法分析器命令.我输入了 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包含具有无法识别的常量值的词法分析器命令;词法分析器解释器可能会产生错误的输出

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 开头的语法,而不是以 parsergrammar 开头的语法>lexer 语法) 不能使用词法分析器模式.您应该使用 tokenVocab 功能,而不是使用 import 功能¹:

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:词法分析器命令中无法识别的常量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆