ANTLR4中有Python3LexerBase python文件吗?
我在这里找到了Python3的ANTLR语法,下载了它,并使用 Pycharm 中的 ANTLR 插件(插件版本 1.17,包含 ANTLR 运行时 4.9.1)生成 ANTLR 识别器。
生成的模块之一是 Python3Lexer.py。该类在顶部附近导入了名为 Python3LexerBase 的东西(这可能是另一个 Python 模块),但我一直无法找到名为 Python3LexerBase 的 python 模块。
生成的 Python3Lexer 模块还包含如下函数:
def NEWLINE_sempred(self, localctx:RuleContext, predIndex:int):
if predIndex == 0:
return this.atStartOfInput()
this.atStartOfInput() 看起来非常 Javaish。
我找到了一个 Python3LexerBase.java 类这里,所以它似乎试图从Python调用Java类。
有一个关于Go中相同基类的非常相似的线程,其中指出Python 中存在或可能存在一个。
有谁知道:
- 为什么它(看起来)试图从Python调用Java代码?
- 某处是否有 Python3LexerBase 模块,或者我需要自己实现它?
I found an ANTLR grammar for Python3 here, downloaded it, and generated the ANTLR recognizers using the ANTLR plugin in Pycharm (plugin version 1.17, containing ANTLR runtime 4.9.1).
One of the generated modules is Python3Lexer.py. That class imports something called Python3LexerBase near the top (which presumably would be another Python module), but I have not been able to find a python module called Python3LexerBase.
The generated Python3Lexer module also contains functions like the following:
def NEWLINE_sempred(self, localctx:RuleContext, predIndex:int):
if predIndex == 0:
return this.atStartOfInput()
this.atStartOfInput() looks very Javaish.
I found a Python3LexerBase.java class here, so it seems to be trying to call a Java class from the Python.
There is a very similar thread about the same base class in Go, where it was indicated that there is, or might be, one in Python.
Does anybody know:
- Why is it (seemingly) trying to call Java code from Python?
- Is there a Python3LexerBase module somewhere, or do I need to implement it myself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
词法分析器和解析器的基类通常用生成文件的目标语言编写,因此它们不属于语法本身(如果不包含目标操作代码,则其本身与目标语言无关)。但通常附近至少有一个针对特定目标语言的实现,您必须自己将其移植到您的语言(此处为 Python),当然,除非这个实现符合您的需求。
如果一种语言被足够多的人使用,那么通常会随着时间的推移贡献一个基本实现,就像 Python 语法一样。但正如 @kaby76 已经提到的,对于作为目标的 Python 来说,这种情况还没有发生。
Base classes for lexers and parsers are usually written in the target language of the generated files, hence they do not belong to the grammar itself (which is target language agnostic per se, if no target action code is included). But usually there's at least one implementation nearby, for a specific target language and you have to port that to your language (here Python) yourself, unless this one implementation matches your needs, of course.
If a language is used by enough people then usually a base implementation is contributed over time as it happened for the Python grammar. But as @kaby76 already mentioned, this hasn't happened yet for Python as target.
您需要运行 transformGrammar.py 转换为正确的 python 代码的脚本。
这是来自 REAME 文件:
该目录包含特定于目标的解析器的基类代码。
TransformGrammar.py 脚本修改目标的语法。
You will need to run transformGrammar.py script to transform to proper python code.
This is from the REAME file:
This directory contains the base class code for a target-specific parser.
The transformGrammar.py script modifies the grammar for the target.