Antlr 4产生与Python混合的Swift

发布于 2025-02-02 23:05:00 字数 2030 浏览 4 评论 0 原文

我正在尝试使用ANTLR 4来生成可以解析Python 3代码的Swift代码。 ANTLR 4在生成的Swift文件中生成Python和Swift的混合物。该问题在最新的Antlr4 jar和最新的MacOS上的最新Python3语法中非常简单地安装了该问题。

我正在使用最新的ANTLR 4进行Mac OS Monterey 12.4的安装。具体来说,我正在使用 antlr-4.10.1-complete.jar python3.g4 来自Master的负责人( 2228B048 )的文件 https://github.com/antlr/grammars-v4

我按照以下说明:

我正在生成Swift使用:

# grammars-v4 is a clean copy of https://github.com/antlr/grammars-v4 at HEAD of master (`2228b048`) 
cd grammars-v4
antlr4 -Dlanguage=Swift ./python/python3-py/Python3.g4 -o ../antlr-swift-python/

这将生成4个Swift文件。生成的 python3lexer.swift 文件包含Swift(解析语言)和Python(解析语言)的混合物。

该文件从一些Python开始,然后切换到Swift。

// Generated from Python3.g4 by ANTLR 4.10.1

from antlr4.Token import CommonToken
import re
import importlib

# Allow languages to extend the lexer and parser, by loading the parser dynamically
module_path = __name__[:-5]
language_name = __name__.split('.')[-1]
language_name = language_name[:-5]  # Remove Lexer from name
LanguageParser = getattr(importlib.import_module('{}Parser'.format(module_path)), '{}Parser'.format(language_name))

import Antlr4

open class Python3Lexer: Lexer {

    internal static var _decisionToDFA: [DFA] = {

请注意Python风格的导入,评论和代码,然后是Swift Import和Swift类定义。

这在整个文件中继续。这是另一个片段,证明了这个问题:

    public
    static let VOCABULARY = Vocabulary(_LITERAL_NAMES, _SYMBOLIC_NAMES)


    @property
    def tokens(self):
        try:
            return self._tokens
        except AttributeError:
            self._tokens = []
            return self._tokens

在这里,我们看到了迅速的属性,然后是Python方法。

  • 我刚刚安装了Antlr 4和Grammars-V4;我没有编辑任何文件。
  • 我能够成功地生成Swift运行时并构建该框架。

I'm trying to use Antlr 4 to generate Swift code that can parse Python 3 code. Antlr 4 generates a mixture of Python and Swift in the generated Swift files. The issue repros with a very simple install of a the latest Antlr4 jar and the latest Python3 grammar on latest MacOS.

I'm using the latest Antlr 4 on a nearly clean install of Mac OS Monterey 12.4. Specifically, I'm using antlr-4.10.1-complete.jar and the Python3.g4 file from HEAD of master (2228b048) of https://github.com/antlr/grammars-v4

I'm following these instructions:

https://github.com/antlr/antlr4/blob/master/doc/swift-target.md

I'm generating the Swift using:

# grammars-v4 is a clean copy of https://github.com/antlr/grammars-v4 at HEAD of master (`2228b048`) 
cd grammars-v4
antlr4 -Dlanguage=Swift ./python/python3-py/Python3.g4 -o ../antlr-swift-python/

This generates 4 Swift files. The generated Python3Lexer.swift file contains a mixture of Swift (the parsing language) and Python (the parsed language).

The file begins with some Python, then later switches to Swift.

// Generated from Python3.g4 by ANTLR 4.10.1

from antlr4.Token import CommonToken
import re
import importlib

# Allow languages to extend the lexer and parser, by loading the parser dynamically
module_path = __name__[:-5]
language_name = __name__.split('.')[-1]
language_name = language_name[:-5]  # Remove Lexer from name
LanguageParser = getattr(importlib.import_module('{}Parser'.format(module_path)), '{}Parser'.format(language_name))

import Antlr4

open class Python3Lexer: Lexer {

    internal static var _decisionToDFA: [DFA] = {

Note the Python-style imports, comments and code followed by Swift import and Swift class definition.

This continues throughout the file. Here's another snippet demonstrating the issue:

    public
    static let VOCABULARY = Vocabulary(_LITERAL_NAMES, _SYMBOLIC_NAMES)


    @property
    def tokens(self):
        try:
            return self._tokens
        except AttributeError:
            self._tokens = []
            return self._tokens

Here we see a Swift property followed by a Python method.

  • I've just installed Antlr 4 and grammars-v4; I haven't edited any of its files.
  • I was able to successfully generate the Swift runtime and build that framework.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨笨の傻瓜 2025-02-09 23:05:00

Python3语法似乎并未设置为迅速目标。

语义谓词和动作通常(几乎总是)以目标语言引入代码。进行语法“目标不可知”需要很多纪律,对于某些语言来说,这是不可能的(考虑到Python对凹痕的依赖,可能不可能创建目标不可能的语法版本的语法版本)。 Python3语法似乎将不得不使用基本类作为注入目标特定代码的钩子来促进多个目标,但是对于Swift来说,这些基本类别没有实现。

我看到Python,C#,Java和JavaScript的目标,但不是Swift的目标。

The python3 grammar doesn’t appear to be set up for a Swift target.

Semantic predicates and actions will, very often (almost always) introduce code in the target language. It takes a lot of discipline to make a grammar “target agnostic”, and for some languages, it’s just not possible (given Python’s reliance on indentation, it would probably be impossible to create a target agnostic version of the grammar). The python3 grammar seems to have going to lengths to facilitate multiple targets by using base classes as the hook to inject target specific code, but the is no implementation of those base classes for Swift.

I see targets for Python, C#, Java, and JavaScript, but not one for Swift.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文