用简单的英语来说,Python、Perl 和 Ruby 在将它们翻译成另一种语言时有什么区别?

发布于 2024-10-07 06:31:52 字数 616 浏览 3 评论 0原文

更新:收到很多有用的信息,谢谢——根据一些答案,再次阅读我的问题,指出可以执行 XYZ,但这不是问题。问题的核心是这样的:“这个问题的主要焦点是用简单的英语找到答案,解释为什么 Python、Perl 和 Ruby 在将它们移植到另一种语言时是这样的。”


一直在研究 Perl、Python 和 Ruby 之间的差异——对我来说,这是迄今为止我发现的最有趣的差异之一,因为它似乎可以 将 Python 转录为 C,但无法转录 Perl 转换为任何语言。还没有找到有关 Ruby 的任何内容,但猜测 99.99% 的事物都是对象,这可能会使转录为另一种语言变得困难(如果不是不可能的话)。

这个问题的主要焦点是用简单的英语找到答案,解释为什么 Python、Perl 和 Ruby 在将它们移植到另一种语言时是这样的。

UPDATE: Received a lot of great info, thanks -- went back to read my question again, based on some of the answers stating that it's possible to do XYZ, which is not the question. the core of the question is this: "Main focus of this question is finding an answer in plain English of why Python, Perl, and Ruby are what they are when it comes to porting them to another language."


Been looking at the differences between Perl, Python, and Ruby -- and this to me has been one of the most interesting differences I've found so far, that being that it appears it's possible to transcribe Python into C, but it's not possible to transcribe Perl into any language. Haven't found anything about Ruby, but guessing that it's approach to 99.99% of everything is a object might make this hard if not impossible to do a transcription to another language.

Main focus of this question is finding an answer in plain English of why Python, Perl, and Ruby are what they are when it comes to porting them to another language.

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

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

发布评论

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

评论(4

青芜 2024-10-14 06:31:52

并不是说不可能翻译它们,因为任何图灵完备语言都可以模拟任何其他语言图灵完备的语言。问题在于生成高效翻译,因为某些语言支持的功能在另一种语言中模拟起来要么非常困难,要么非常复杂。

It isn't that it's impossible to translate them, since any Turing-complete language can emulate any other Turing-complete language. The problem is generating an efficient translation, since some languages support features that are either very difficult or very complex to emulate in another.

雪化雨蝶 2024-10-14 06:31:52

你的问题没有什么意义(如果有的话),特别是考虑到你所链接的问题。

阅读 为什么不将 Perl 翻译成 C? 可能会帮助你理解生命、宇宙和一切。

Your question makes little —if any— sense, especially considering the questions you linked to.

Reading Why not translate Perl to C? might help you make sense of life, universe and everything.

酒废 2024-10-14 06:31:52

任何图灵完备语言都可以翻译成任何其他图灵完备语言,只是需要很多时间的努力,可能看起来很丑陋。 Perl 很特别,因为它的解析器充满了特殊情况,但这并不意味着将 Perl 翻译成任何东西都是不可能的,只是非常困难。 Python 有非常好的语法并且很容易解析。 Ruby 具有 Perl 的一些特性,但仍然相对干净。

可以这样想:每种语言迟早都会在 CPU 上执行代码。您始终可以获取此机器代码并将其“反编译”为任何更高级的语言,但根据代码的复杂性,您的结果可能对人类理解不是很有用。

Any Turing-complete language can be translated into any other turing-complete language, it just takes a lot of effort and may look very ugly. Perl is special because its parser is full of special cases, but that does not make translating Perl to anything impossible, just very hard. Python has a pretty nice grammar and is easily parsed. Ruby has some peculiarities from Perl, but is still comparatively clean.

Think of it like this: Every language executes code on your CPU sooner or later. You can always take this machine code and "decompile" it into any higher level language, but depending on the code complexity, your results might just not be very useful for humans to understand.

冷月断魂刀 2024-10-14 06:31:52

我明白你在问什么,但这不是正确的问题(我稍后会讨论这个问题)。

答案是因为 Perl 不是由正式的语言定义定义的。 Perl 有点不常见,因为该语言的引用是解释器的实现。其他语言(例如 Python 和 C)也有语言定义。该定义是参考,编译器/解释器的实现是为了符合该定义。

此参考允许某人对一种语言采取词法分析器/解析器步骤,并将其反向应用到另一种语言的语法树。

即。 Python 词法分析器 ->蟒蛇帕森 ->
AST-> C 解析器 -> C词法分析器

这使您可以轻松创建一个将 Python 转录为 C 的程序。当然这并不那么容易,但您明白了。

对于 Perl,除了 Perl 解释器之外没有其他参考。因为有参考,所以可以创建一个翻译代码的程序,但是与 BNF(例如)。

您想问的问题是:

为什么这么难
自动将 Perl 翻译为 C 并
反之亦然,比 Python 到 C 更好吗?

I understand what you asking, but that's not the right question (I'll get to that in a second).

Answer is because Perl is not defined by a formal language definition. Perl is somewhat in uncommon in that the reference for the language IS the implementation of the interpreter. Other languages such as Python, and C have a language definition. This definition is the reference and the compiler/interpreter is implemented to conform to that definition.

This reference allows someone to take the lexer/parser steps for one language and apply it in reverse to the syntax tree of another language.

ie. Python Lexer -> Python Parson ->
AST -> C Parser -> C Lexer

This allows you it easily create a program to transcribe Python to C. Of course it's not that easy, but you get the idea.

For Perl there is no reference other than the Perl interpreter. Because there is a reference, it is possible to create a program that will translate the code, HOWEVER it would be very difficult compared to a language that had their reference in BNF (for example).

The question you wanted to ask is:

Why is it so much more difficult to
automatically translate Perl to C and
vice-versa, than Python to C?

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