简体   繁体   English

如何“实现”编程语言?

[英]How can a programming language be “implemented”?

maybe this is just a little misunderstanding but how can a programming language be implemented ? 也许这只是一个小小的误解,但是如何实现编程语言呢?

I'm not talking about how to implement my own programming language but about the word "implemented"? 我不是在谈论如何实现我自己的编程语言,而是关于“已实现”这个词? I mean, you can implement a compiler or an interpreter, but a programming language? 我的意思是,你可以实现编译器或解释器,但是编程语言?

What does it mean if I read "C++ is implemented in C" or "Python was implemented in C"? 如果我读“C ++是用C实现”还是“Python用C实现”是什么意思?

I think a language is more sth. 我认为一种语言更为严肃。 like a protocol of how someone thinks about things should be implemented. 就像某人如何思考事情的协议一样。 For example, if he wants do display a messagebox he can say the command for this is ShowMessageBox(string) and implement a compiler who will translate this into something that works on a computer (aside from the selected programming paradigms he imagines). 例如,如果他想要显示一个消息框,他可以说这个命令是ShowMessageBox(字符串)并实现一个编译器,它将把它转换为在计算机上工作的东西(除了他想象的选定的编程范例)。

I think this question leads to the question "what is a programming language in reality"? 我认为这个问题导致了“现实中的编程语言是什么”的问题? A compiler, an interpreter or just a documented language standard about how things should be implemented in a language? 关于如何用语言实现事物的编译器,解释器或文档语言标准?

[EDIT] [编辑]

Answer: Languages are never implemented, only compilers/interpreters etc. It's this simple. 答:语言从未实现,只有编译器/口译员等。就这么简单。

Here's a very academic answer (from a longtime academic). 这是一个非常学术性的答案(来自长期学术)。 First I'll reframe the question: 首先,我将重新构建问题:

What does it mean for a programming language to be implemented ? 编程语言的实现意味着什么?

I'll start with "what is a programming language": 我将从“什么是编程语言”开始:

  1. A programming language is a formal language (a set of utterances we can characterize precisely through algorithmic rules) such that a sentence in the language has a computational meaning. 编程语言是一种形式语言(我们可以通过算法规则精确表征的一组话语),使得语言中的句子具有计算意义。 There are a variety of ways to give computation meaning; 有多种方法可以赋予计算意义; two of the most popular are that a computation stands for a function (from values to values, or from machine states to machine states) and that a computation stands for a machine that makes "state transitions" and interacts with the outside world. 两个最流行的是计算代表一个函数(从值到值,或从机器状态到机器状态),并且计算代表一个机器,它进行“状态转换”并与外界交互。

  2. A language is implemented when a means is provided to read in an utterance and perform the computation, that is, calculate the function or perform the behavior. 当提供用于读入话语并执行计算的装置,即计算功能或执行行为时,实现语言。 The means is the implementation. 手段是实施。

Typical implementations include 典型的实现包括

  • Direct interpretation of the language syntax. 直接解释语言语法。 This model is rare but FORTH probably comes closest to it. 这种模式很少见,但FORTH可能最接近它。

  • Translation of the syntax into virtual-machine code, also called bytecode, which is itself another language and which is interpreted. 将语法翻译成虚拟机代码,也称为字节码,它本身就是另一种语言并被解释。 It is popular to write bytecode interpreters in C. Lua, Perl, Python, and Ruby are implemented more or less this way. 在C语言中编写字节码解释器很受欢迎.Loa,Perl,Python和Ruby或多或少地以这种方式实现。

  • Translation of the syntax into hardware machine instructions, which is itself another language, and which is interpreted by your CPU. 将语法翻译成硬件机器指令,硬件机器指令本身是另一种语言,由CPU解释。 C and C++ are typically (but not always) implemented this way. C和C ++通常(但不总是)以这种方式实现。

  • Direct interpretation of the language in hardware. 硬件直接解释语言。 IA-32 machine code and AMD64 machine code are implemented this way. IA-32机器码和AMD64机器码以这种方式实现。

When a person says "Language X is implemented in Y", they are usually saying that a translator for X or an interpreter for X's bytecode is written in language Y. One of the great secrets of compiler writers is the ability to write the compiler for language X in language X itself. 当一个人说“语言X在Y中实现”时,他们通常会说X的翻译器或X字节码的翻译器是用Y语言编写的。编译器编写者的一个重要秘密就是编写编译器的能力。语言X本身就是语言X. If this interests you, get Andrew Appel's paper Axiomatic Bootstrapping: A Guide for Compiler Hackers . 如果您对此感兴趣,请获取Andrew Appel的论文Axiomatic Bootstrapping:Compiler Hackers Guide

Sometimes the answer to this question is not obvious. 有时这个问题的答案并不明显。 Squeak Smalltalk writes both a translator and a bytecode interpreter in Smalltalk, then translates the interpreter to C, which is translated to machine code. Squeak Smalltalk在Smalltalk中写入转换器和字节码解释器,然后将解释器转换为C,转换为机器代码。 What is Squeak implemented in? 什么是Squeak实施? Smalltalk. 短暂聊天。


Poke a professor; 戳一位教授; get a lecture. 得到一个演讲。

You are right, those statements don't make any sense. 你是对的,这些陈述没有任何意义。 It's pretty obvious that whoever made those statements doesn't understand the difference between a programming language and a compiler (or interpreter). 很明显,制作这些语句的人并不理解编程语言和编译器(或解释器)之间的区别。

This is a surprisingly common problem. 这是一个令人惊讶的常见问题。 For example, sometimes people talk about interpreted languages or compiled languages. 例如,有时人们谈论解释语言或编译语言。 That's the same thing: languages aren't interpreted or compiled, they just are . 这是一回事:语言不被解释或编译,它们就是这样 Interpretation and compilation are traits of the implementation not the language. 解释和编译是实施的特征,而不是语言。

Another goodie: Python has a GIL. 另一个好东西:Python有一个GIL。 No, it doesn't: one implementation of Python has a GIL, all the other implementations don't, and the Python Language itself certainly doesn't. 不,它没有:Python的一个实现有一个GIL,所有其他实现都没有,而Python 语言本身肯定没有。 Or: Ruby has green threads. 或者:Ruby有绿色线程。 Again, not true: Ruby has threads. 同样,不是这样:Ruby有线程。 Period. 期。 Whether any particular language implementation chooses to implement them as green threads, native threads, platform threads or whatever, is a trait of that particular implementation, not of Ruby. 任何特定的语言实现是否选择将它们实现为绿色线程,本机线程,平台线程或其他任何东西,都是该特定实现的特征,而不是Ruby的特征。 And of course my favorite: Ruby 1.9 is faster than Ruby 1.8. 当然我最喜欢的:Ruby 1.9比Ruby 1.8更快。 This doesn't even make sense: Ruby 1.9 and Ruby 1.8 are programming languages, ie a bunch of abstract mathematical rules. 这甚至没有意义:Ruby 1.9和Ruby 1.8是编程语言,即一堆抽象的数学规则。 You cannot run a programming language, therefore a programming language can never be "faster" or "slower" than another one. 您无法运行编程语言,因此编程语言永远不会比另一种语言“更快”或“更慢”。

The most blatant confusion about the difference between programming languages and implementations is the Computer Language Benchmark Game, which claims to benchmark languages against each other but in fact benchmarks implementations. 关于编程语言和实现之间差异的最明显的混淆是计算机语言基准游戏,它声称相互之间的语言基准,但实际上是基准实现。

All of these are just different expressions of the fact that apparently some people seem to be fundamentally incapable of grasping the concept of abstraction. 所有这些只是表达事实的不同表面,显然有些人似乎从根本上无法理解抽象概念。 Or at least the concept of having an abstract language and a concrete implementation of that language. 或者至少是拥有抽象语言和该语言的具体实现的概念。

If we go back to the statement that "Python is implemented in C", it should now be obvious that that statement is not just wrong. 如果我们回到“Python在C中实现”的陈述,那么现在很明显,该陈述不仅仅是错误的。 If the statement were wrong that would imply that the statement even makes sense, ie that there is some possible world out there, in which it could at least theoretically be right. 如果陈述是错误的,这意味着该陈述甚至有意义,即有一些可能的世界,其中至少在理论上它是正确的。 But that's not the case. 但事实并非如此。 The statement is neither wrong nor right, it simply doesn't make sense. 声明既不错也不正确,根本没有意义。 If English were a typed language, it would be a type error. 如果英语是一种打字语言,则会出现类型错误。

Python is a programming language. Python是一种编程语言。 Programming languages aren't implemented in anything. 编程语言没有任何实现。 They are just implemented. 他们刚刚实施。 Compilers and interpreters are implemented in languages. 编译器和解释器以语言实现。 But even if you interpret the statement this way, it isn't true: Jython is implemented in Java, IronPython is implemented in C#, PyPy is implemented in RPython and Python, Pynie is implemented in PGE, NQP and PIR. 但即使你用这种方式解释语句,也不是这样:Jython用Java实现,IronPython用C#实现,PyPy用RPython和Python实现,Pynie用PGE,NQP和PIR实现。 (Oh, and all of those implementations have compilers, so there goes your "Python is an interpreted language".) Similar with Ruby: Rubinius is implemented in Ruby and C++, JRuby and XRuby are implemented in Java, IronRuby and Ruby.NET are implemented in C#, HotRuby is implemented in ECMAScript, Red Sun is implemented in ActionScript, RubyGoLightly is implemented in Go, Cardinal is implemented in PGE, NQP and PIR, SmallRuby is implemented in Smalltalk/X, MagLev is implemented in GemStone Smalltalk and Ruby, YARI is implemented in Io. (哦,所有这些实现都有编译器,所以你的“Python是一种解释语言”。)类似于Ruby:Rubinius用Ruby和C ++实现,JRuby和XRuby用Java实现,IronRuby和Ruby.NET是在C#中实现,HotRuby在ECMAScript中实现,Red Sun在ActionScript中实现,RubyGoLightly在Go中实现,Cardinal在PGE,NQP和PIR中实现,SmallRuby在Smalltalk / X中实现,MagLev在GemStone Smalltalk和Ruby中实现, YARI在Io中实现。 And for C++: Clang (which is the C, C++ and Objective-C front-end for LLVM) is implemented in C++ (all three front-ends are implemented in C++). 对于C ++:Clang(它是LLVM的C,C ++和Objective-C前端)是用C ++实现的(所有三个前端都是用C ++实现的)。

"C++ is implemented in C". “C ++是用C语言实现的”。 I understand this as "C++ compiler is written in C language". 我理解为“C ++编译器是用C语言编写的”。 Quite simple, without too much philosophy. 很简单,没有太多的哲学。

Generally, C++ compiler can be written in any language, including C++ itself (except of the first compiler version). 通常,C ++编译器可以用任何语言编写,包括C ++本身(第一个编译器版本除外)。

"Python was implemented in C" means that at least one Python compiler (in this case the most commonly used one) is written using C. The developers of that implementation of Python made a deliberate decision not to use C++. “Python在C中实现”意味着至少有一个Python编译器(在这种情况下是最常用的编译器)使用C编写。该Python实现的开发人员故意决定不使用C ++。 As a statement it is incomplete as Python has also been implemented in Java, in C# and in Python. 作为一个声明它是不完整的,因为Python也已经用Java,C#和Python实现。

The main relevance is that it gives you some idea of the systems you might be able to port the language onto: anything targeted by a C compiler should (at least in theory) be capable of running the C implementation of Python, but if they'd chosen to use C++ there would be a smaller set of systems that could run it. 主要的相关性在于它让您对可能将语言移植到的系统有所了解:C编译器所针对的任何事物(至少在理论上)应该能够运行Python的C实现,但如果它们' d选择使用C ++会有一小组可以运行它的系统。

C++ usually isn't implemented in C these days: I believe it is usually implemented in C++. C ++现在通常没有在C中实现:我相信它通常是用C ++实现的。 It is quite common for languages to be implemented in the same language (or a subset of the language) as it means you are no longer dependent on some other unrelated language being available for the target. 语言以相同的语言(或语言的子集)实现是很常见的,因为这意味着您不再依赖于目标可用的其他一些不相关的语言。 To bootstrap onto a new system you cross compile from some other system. 要引导到新系统,您可以从其他系统进行交叉编译。

If you compile gcc for a new platform the build process involves compiling the source code once with whatever compiler is already available (perhaps an older gcc), then compiling it a second time with the newly compiled compiler, then compiling it a third time with the output from the second compilation. 如果为新平台编译gcc,构建过程涉及使用已有的任何编译器(可能是较旧的gcc)编译源代码一次,然后使用新编译的编译器再次编译它,然后第三次使用编译器编译它。第二次编译的输出。 If the second and third versions aren't identical you get a build error. 如果第二个和第三个版本不相同,则会出现构建错误。 If they are identical then you've got a pretty good indication that it compiled correctly. 如果它们完全相同,那么你就可以很好地指出它是否正确编译了。

A programming language is a standard. 编程语言是一种标准。 Its interpreter or compiler is an implementation of this standard. 它的解释器或编译器是该标准的实现。

To build a new language, you don't necessarily needs to do in in low level machine code (assembly for instance). 要构建一种新语言,您不一定需要在低级机器代码(例如程序集)中进行。 So, using another language to accomplish your goal (creating a new language here) is perfectly normal. 所以,使用另一种语言来实现你的目标(在这里创建一种新语言)是完全正常的。 So, when we say: Python was implemented in C, it just means that C was used to create that language. 所以,当我们说:Python是用C实现的时候,它只是意味着C被用来创建那种语言。 For instance, C can be complied on many different architecture, so the programmers doesn't have to take care of the different type of computers (portable). 例如,C可以在许多不同的架构上进行编译,因此程序员不必处理不同类型的计算机(便携式)。

A language is just a way to express yourself to the computer. 语言只是向计算机表达自己的一种方式。 Today, it can be done in various ways. 今天,它可以通过各种方式完成。 But when you use the same syntax as the language and create your own framework, it's called a library or framework. 但是当您使用与语言相同的语法并创建自己的框架时,它被称为库或框架。 A programming language is just a notation for writing program. 编程语言只是编写程序的符号。 If the notation change, you have a different language. 如果符号更改,则您使用其他语言。 Like French or Spanish comes from Latin. 像法语或西班牙语来自拉丁语。 (French is implemented in Latin ;) (法语用拉丁语实现;)

Why is there so many different languages? 为什么有这么多不同的语言? Because the goal of a language is to solve complex problems. 因为语言的目标是解决复杂的问题。 So, depending on what you want to try yo accomplish, choosing the appropriate language can be an important decision. 因此,根据您想要尝试的内容,选择合适的语言可能是一个重要的决定。

The statement "Language X is implemented in Language Y" makes sense and is true if and only if there exists a canonical implementation of Language X and that implementation is written in Language Y. In common usage, either the first or the most popular implementation is often assumed to be canonical. 声明“语言X在语言Y中实现”是有道理的,并且当且仅当存在语言X的规范实现并且实现是用语言Y编写时才是真实的。在通常的使用中,第一个或最流行的实现是通常被认为是规范的。

For example, Perl is one of the few languages with a definitive canon. 例如,Perl是少数具有权威佳能的语言之一。 "Python is implemented in C" makes sense if CPython is taken to be the canonical implementation of Python, and "C++ is implemented in C" is true for CFront, the original implementation of "C with classes" by Bjarne Stroustrup. 如果CPython被认为是Python的规范实现,那么“Python在C中实现”是有道理的,并且“C ++在C中实现”对于CFront来说是正确的,这是Bjarne Stroustrup的“C with classes”的原始实现。

The direct answer: Implementation in the context you are talking about just means written and language actually means compiler. 直接答案:在你所讨论的上下文中的实现只意味着书面和语言实际上意味着编译器。 The original C++ compiler was as I understand it written in C. There is nothing (apart from knowledge and time) to stop you from writing a C++ compiler in another language. 最初的C ++编译器就像我所理解的那样用C语言编写。除了知识和时间之外,什么都没有阻止你用另一种语言编写C ++编译器。

Implementation is the code that makes software work. 实现是使软件工作的代码。 Often we talk about the implementation of a function as in: "the function has not been implemented yet." 我们经常讨论函数的实现,如:“函数尚未实现。” eg void foo() 例如void foo()
{ {
//function has not been implemented yet //功能尚未实现
throw(); 扔();
} }

This often happens during the design phase of a program because the call needs to be there in order to write/debug/concept test the calling code but we haven't got round to implementing (writing the code to go insde the function) 这通常发生在程序的设计阶段,因为调用需要在那里进行编写/调试/概念测试调用代码但我们还没有实现(编写代码来实现函数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM