为什么不可能创建一个实用的 Perl 到 Python 源代码转换器?

发布于 2024-09-12 12:14:07 字数 795 浏览 2 评论 0原文

如果有一个程序可以自动将 Perl 代码转换为 Python 代码,使生成的 Python 程序与原始程序一样可读和可维护,更不用说以相同的方式工作了,那就太好了。

最明显的解决方案就是通过 Python utils 调用 perl

#!/usr/bin/python
os.exec("tail -n -2 "+__file__+" | perl -")
...the rest of file is the original perl program...

但是,生成的代码很难说是 Python 代码,它本质上是 Perl 代码。潜在的转换器应该将 Perl 结构和习惯用法转换为易于阅读的 Python 代码,它应该保留变量和子例程名称(即结果不应该看起来很混乱)并且不应该过多地破坏工作流程。

这样的转变显然是非常困难的。转换的难度取决于 Perl 功能和语法结构的数量,而 Perl 功能和语法结构没有易于阅读、清晰的 Py​​thon 等效项。我相信大量的此类功能使得这种自动转换实际上不可能实现(尽管理论上存在可能性)。

那么,您能否列出一些无法用 Python 表达得像原始 Perl 代码那样简洁的 Perl 习语和语法特征?

编辑:有些人将 Python-to-Perl 转换程序联系起来,并在此基础上推断,编写 Perl-to-Python 也应该很容易。然而,我确信将转换为Python的需求更大;然而这个转换器还没有被写出来——而相反的部分已经写好了!这只会让我对编写一个好的 Python 转换器的可能性更加坚定。

It would be nice if there existed a program that automatically transforms Perl code to Python code, making the resultant Python program as readable and maintainable as the original one, let alone working the same way.

The most obvious solution would just invoke perl via Python utils:

#!/usr/bin/python
os.exec("tail -n -2 "+__file__+" | perl -")
...the rest of file is the original perl program...

However, the resultant code is hardly a Python code, it's essentially a Perl code. The potential converter should convert Perl constructs and idioms to easy-to-read Python code, it should retain variable and subroutine names (i.e. the result should not look obfuscated) and should not shatter the wrokflow too much.

Such a conversion is obviously very hard. The hardness of the conversion depends on the number of Perl features and syntactical constructs, which do not have easy-to-read, unobfuscated Python equivalents. I believe that the large amount of such features renders such automatic conversion impossible practically (while theoretical possibility exists).

So, could you please name Perl idioms and syntax features that can't be expressed in Python as concise as in the original Perl code?

Edit: some people linked Python-to-Perl conventers and deduced, on this basis, that it should be easy to write Perl-to-Python as well. However, I'm sure that converting to Python is in greater demand; still this converter is not yet written--while the reverse has already been! Which only makes my confidence in impossibility of writing a good converter to Python more solid.

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

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

发布评论

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

评论(12

耳根太软 2024-09-19 12:14:07

你最好的 Perl 到 Python 转换器可能是 23 岁,刚刚大学毕业,正在找工作。

Your best Perl to Python converter is probably 23 years old, just graduated university and is looking for a job.

不喜欢何必死缠烂打 2024-09-19 12:14:07

为什么 Perl 不是 Python。

  1. Perl 具有 Python 或多或少完全缺乏的语句。虽然您可能可以设计匹配语句,但其语法与 Perl 完全不同,因此很难将其称为“翻译”。您确实必须编写一些奇特的 Python 内容,使其像原始 Perl 一样简洁。

  2. Perl 的运行时语义与 Python 非常不同,这使得翻译非常具有挑战性。下面我们只看一个例子。

  3. Perl 的数据结构与 Python 非常不同,翻译起来很困难。

  4. 默认情况下,Perl 线程不共享数据。只能共享选定的数据元素。 Python 线程有更常见的“共享一切”数据。

#2 的一个例子就足够了。

Perl:

do_something || die()

其中 do_something 是任何类型的任何语句。

要将其自动转换为 Python,您必须包装每个 || die() 中的语句 其中

try:
   python_version_of_do_something
except OrdinaryStatementFailure, e:
   die()
   sys.exit()

更常见的表述

Perl

do_something

会使用简单的——不假思索的——源代码翻译变成这样

try:
   python_version_of_do_something
except OrdinaryStatementFailure, e:
   pass

而且,当然,

Perl

do_this || do_that || die()

翻译成 Python 会更加复杂。

确实

Perl

do_this && do_that || die()

挑战了极限。我的 Perl 生疏了,所以我记不起这种东西的精确语义了。但你必须完全理解语义才能制定出 Pythonic 实现。

Python 示例不是好的 Python。要写出好的 Python 需要“思考”,这是自动翻译无法做到的。

每个 Perl 构造都必须像这样“包装”,以便将原始 Perl 语义转换为 Python 形式。

现在,对 Perl 的每个功能进行类似的分析。

Why Perl is not Python.

  1. Perl has statements which Python more-or-less totally lacks. While you can probably contrive matching statements, the syntax will be so utterly unlike Perl as to make it difficult to call it a "translation". You'd really have to cook up some fancy Python stuff to make it as terse as the original Perl.

  2. Perl has run-time semantics which are so unlike Python as to make translation very challenging. We'll look at just one example below.

  3. Perl has data structures which are enough different from Python that translation is hard.

  4. Perl threads don't share data by default. Only selected data elements can be shared. Python threads have more common "shared everything" data.

One example of #2 should be enough.

Perl:

do_something || die()

Where do_something is any statement of any kind.

To automagically translate this into Python you'd have to wrap every || die() statement in

try:
   python_version_of_do_something
except OrdinaryStatementFailure, e:
   die()
   sys.exit()

Where the more common formulation

Perl

do_something

Would become this using simple -- unthinking -- translation of the source

try:
   python_version_of_do_something
except OrdinaryStatementFailure, e:
   pass

And, of course,

Perl

do_this || do_that || die()

Is even more complex to translate into Python.

And

Perl

do_this && do_that || die()

really push the envelope. My Perl is rusty, so I can't recall the precise semantics of this kind of thing. But you have to totally understand the semantics to work out a Pythonic implementation.

The Python examples are not good Python. To write good Python requires "thinking", something an automatic translated can't do.

And every Perl construct would have to be "wrapped" like that in order to get the original Perl semantics into a Pythonic form.

Now, do a similar analysis for every feature of Perl.

私藏温柔 2024-09-19 12:14:07

只是为了扩展这里的其他一些列表,这些是一些在 python 中可能非常笨拙的 Perl 构造(如果可能的话)。

  • 动态范围(通过 local 关键字)
  • typeglob 操作(多个同名变量)
  • 格式(它们有自己的语法)
  • 可变变量上的闭包
  • pragmas
  • 左值子例程 (mysub() = 5; 类型代码)
  • 源过滤器
  • 上下文(列表与标量,以及调用代码的方式可以使用wantarray检查它)
  • 类型强制/动态类型
  • 任何使用字符串eval的程序

这个列表一直在继续,有人可能会尝试在所有类似的构造之间创建映射,但最终由于一个简单的原因而失败。

Perl 无法静态解析。 Perl 代码中的定义(特别是 BEGIN 块中的定义)改变了编译器解释其余代码的方式。因此,对于重要的程序,从 Perl 的转换 => Python 存在停机问题。

在程序完成运行之前,无法确切地知道所有程序将如何编译,并且理论上可以创建一个每次运行时都会进行不同编译的 Perl 程序。这意味着一个 Perl 程序可以映射到无数个 Python 程序,只有在 Perl 解释器中运行原始程序后才能知道其正确性。

Just to expand on some of the other lists here, these are a few Perl constructs that are probably very clumsy in python (if possible).

  • dynamic scope (via the local keyword)
  • typeglob manipulation (multiple variables with the same name)
  • formats (they have a syntax all their own)
  • closures over mutable variables
  • pragmas
  • lvalue subroutines (mysub() = 5; type code)
  • source filters
  • context (list vs scalar, and the way that called code can inspect this with wantarray)
  • type coercion / dynamic typing
  • any program that uses string eval

The list goes on an on, and someone could try to create a mapping between all of the analogous constructs, but in the end it will be a failure for one simple reason.

Perl can not be statically parsed. The definitions in Perl code (particularly those in BEGIN blocks) change the way the compiler is going to interpret the remaining code. So for non-trivial programs, conversion from Perl => Python suffers from the halting problem.

There is no way to know exactly how all of the program will be compiled until the program has finished running, and it is theoretically possible to create a Perl program that will compile differently every time it is run. Meaning that one Perl program could map to an infinite number of Python programs, the correct of which is only know after running the original program in the perl interpreter.

血之狂魔 2024-09-19 12:14:07

这并非不可能,只是需要大量的工作。

顺便说一句,有 Perthon,一个 Python 到 Perl 的转换器。似乎没有人愿意做一个相反的事情。

编辑:我想我可能已经找到了为什么 Python 到 Perl 转换器更容易实现的原因。这是因为 Python 允许你修改脚本的 AST。请参阅解析器模块。

It is not impossible, it would just take a lot of work.

By the way, there is Perthon, a Python-to-Perl translator. It just seems like nobody is willing to make one that goes the other way.

EDIT: I think I might I've found the reason why a Python to Perl translator is much easier to implement. It's because Python lets you fiddle with a script's AST. See parser module.

独木成林 2024-09-19 12:14:07

可以实验性地构建 Perl,以在 Perl 代码编译期间收集附加信息(例如注释),甚至将结果作为 XML 发出。源代码之外似乎没有任何相关文档,除了: http:// search.cpan.org/perldoc/perl5100delta#MAD

这对于构建翻译器应该很有帮助。我希望你能相当轻松地完成 80% 的任务,95% 的任务则需要很大的困难,而且没有比这更好的了。有太多的东西没有很好地映射。

Perl can experimentally be built to collect additional information (for instance, comments) during compilation of perl code and even emit the results as XML. There doesn't appear to be any documentation of this outside the source, except for: http://search.cpan.org/perldoc/perl5100delta#MAD

This should be helpful in building a translator. I'd expect you to get 80% of the way there fairly easily, 95% with great difficulty, and never much better than that. There are too many things that don't map well.

沐歌 2024-09-19 12:14:07

从根本上来说,这是两种不同的语言。从一种转换到另一种并使结果大部分可读意味着软件必须能够识别和生成代码习惯用法,并能够进行一些静态分析。

程序的含义可能由语言定义精确定义,但程序员不一定需要所有细节。 AC 程序员测试 printf() 返回的值是否为负数是为了检查错误条件,并且通常不关心确切的值。 if (printf("%s","...") < 0) exit(); 可以翻译成 Perl 作为 print "..." 或 die();。这些语句的含义可能并不完全相同,但它们通常是程序员的意思,并且要从惯用的 Perl 或 C 代码创建惯用的 C 或 Perl 代码,翻译人员必须考虑到这一点。

由于不同的计算机语言对于相似的事物往往有稍微不同的语义,因此通常不可能将一种语言翻译成另一种语言并以可读的形式得出完全相同的含义。为了创建可读的代码,翻译人员需要了解程序员想要做什么,这确实很困难。

此外,从 Python 转换为 Perl 比将 Perl 转换为 Python 更容易。 Python 是一种简单的语言,具有明确的标准方法来完成任务,而 Perl 是一种过于复杂的语言,其座右铭是“做事的方法不止一种”。将 Python 表达式翻译成无数对应的 Perl 表达式之一比弄清楚 Perl 程序员的意思并用 Python 表达它更容易。

Fundamentally, these are two different languages. Converting from one to another and have the result be mostly readable would mean that the software would have to be able to recognize and generate code idioms, and be able to do some static analysis.

The meaning of a program may be exactly defined by the language definition, but the programmer did not necessarily require all the details. A C programmer testing if the value a printf() returned is negative is checking for an error condition, and doesn't typically care about the exact value. if (printf("%s","...") < 0) exit(); can be translated into Perl as print "..." or die();. These statements may not mean exactly the same thing, but they'll typically be what the programmer means, and to create idiomatic C or Perl code from idiomatic Perl or C code the translator must take this into account.

Since different computer languages tend to have different slightly semantics for similar things, it's typically impossible to translate one language into another and come up with the exact same meaning in readable form. To create readable code, the translator needs to understand what the programmer was intending to do, and that's real difficult.

In addition, it would be easier to translate from Python to Perl rather than Perl to Python. Python is intended as a straightforward language with clear standard ways to do things, while Perl is an unduly complex language with the motto "There's More Than One Way To Do It." Translating a Python expression into one of the innumerable corresponding Perl expressions is easier than figuring out what the Perl programmer meant and expressing it in Python.

感性 2024-09-19 12:14:07
  • Python 作用域和命名空间与 Perl 不同。

  • 在Python中,一切都是对象。在 Perl 中,幕后的一切似乎都是列表/散列/标量/引用/函数。这导致了不同的设计方法和习惯用法。

  • Perl 具有匿名代码块,并且可以通过某些分支动态生成闭包。我非常确信这不是Python的特性。

我确实认为一个非常聪明的人可以静态分析大部分 Perl 并生成一个程序,该程序采用小型 Perl 程序并输出执行相同工作的 Python 程序。

我对大型和/或粗糙的 Perl 翻译的可行性更加怀疑。我们中的一些人有时会编写一些非常时髦的代码......:)

  • Python scope and namespace are different from Perl.

  • In Python, everything is an object. In Perl, everything under the hood seems to be a list/hash/scalar/reference/function. This induces different design approaches and idioms.

  • Perl has anonymous code blocks and can generate closures on the fly with some branches. I am pretty sure that is not a python feature.

I do think that a very smart chap could statically analyze the bulk of Perl and produce a program that takes small Perl programs and output Python programs that do the same job.

I am much more doubtful about the feasibility of large and/or gnarly Perl translation. Some of us write some really funky code at times.... :)

花间憩 2024-09-19 12:14:07

这是不可能的,因为您甚至无法正确解析 perl 代码。有关更多详细信息,请参阅Perl 无法解析:形式证明

This is impossible just because you can't even properly parse perl code. See Perl Cannot Be Parsed: A Formal Proof for more details.

鹊巢 2024-09-19 12:14:07

Malcolm Beattie 的 B 模块集将是这样的事情唯一理智的起点,尽管我有其他答案,因为这将是一个很难解决的问题。一般来说,将一种高级语言的含义翻译成另一种高级语言需要高级翻译人员,而目前这只能意味着人类。

对于任何语言来说,这个问题的困难在于所讨论的语言本质上的根本差异,例如运行时语义和常见习惯用法,更不用说库了。

The B set of modules by Malcolm Beattie would be the only sane starting point for something like this, though I'm with other answers in that this would be a difficult problem to solve. In general, translating the sense of one high-level language into another high-level language requires a high-level translator, and, for the time being, that can mean only a human.

The difficulty of this problem, for any pair of languages, is due to fundamental differences in the nature of the languages in question, such as runtime semantics and common idioms, not to mention libraries.

墟烟 2024-09-19 12:14:07

创建从一种高级语言到另一种高级语言的通用翻译器几乎不可能的原因是该程序仅描述如何而不是为什么(这就是原因查看源代码中的注释)。

为了用另一种高级语言创建有意义的程序,您(或翻译程序)需要知道为什么才能创建最好的程序。如果你做不到这一点,你所能做的就是为 Perl 程序的编译版本创建一个 Python 解释器。

换句话说,要正确地做到这一点,您需要跳出框框,这对于计算机来说非常困难。

The reason it is close to impossible to create a generic translator from one high-level language to another, is that the program only describe HOW and not WHY (this is the reason for comments in the source code).

In order to create a meaningful program in another highlevel language you (or the translator program) needs to know WHY to be able to create the best possible program. If you cannot do that, all you can do is essentially to create a Python interpreter for the compiled version of the Perl program.

In other words, to do this properly you need to go outside the box, and this is very hard for a computer.

梦里兽 2024-09-19 12:14:07

NullUserException 基本上概括了这一点 - 它当然可以完成;这样做需要付出巨大的努力。我见过一些语言转换实用程序编译为中间语言(例如 .NET 的 CIL),然后将其反编译为所需的语言。我还没有看到 Perl 到 Python 的任何内容。不过,您可以此处找到 Python 到 Perl 的转换器,尽管这对您来说可能没什么用处,除非您正在尝试创建自己的,在这种情况下它可能会提供一些有用的参考。

编辑:如果您只需要 Python 脚本中的确切功能,PyPerl 可能会有一些用处给你。

NullUserException basically summed it up - it certainly can be done; it would just be an enormous amount of effort to do so. Some language conversion utilities I've seen compile to an intermediate language (such as .NET's CIL) and then decompile that to the desired language. I have not seen any for Perl to Python. You can, however, find a Python to Perl converter here, though that's likely of little use to you unless you're trying to create your own, in which case it may provide some helpful reference.

Edit: if you just need the exact functionality in a Python script, PyPerl may be of some use to you.

沉溺在你眼里的海 2024-09-19 12:14:07

尝试我的 Pythonizer 版本:http://github.com/snoopyjc/pythonizer - 它做了一个体面的工作

Try my version of the Pythonizer: http://github.com/snoopyjc/pythonizer - it does a decent job

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