计算机编程语言到底是什么?

大学计算机基础

Remember that a program is just a sequence of instructions telling a computer what to do. Obviously, we need to provide those instructions in a language that a computer can understand. It would be nice if we could just tell a computer what to do using our native language, like they do in science fiction movies. (“Computer,how long will it take to reach planet Alphalpha at maximum warp?”) Unfortunately, despite the continuing efforts of many top-flight computer scientists (including your author), designing a computer to understand human language is still an unsolved problem.

记住,程序就是一段告诉计算机要做什么的指令序列(有顺序的集合)。很明显,我们需要用语言精确描述的指令集给计算机,它们才能够理解或者明白。当然最理想,就是像科幻电影中计算机一样,直接能够听懂我们的人类语言(计算机,告诉我们如果全速飞行,去Alphalpha还需要多长时间?)。不幸的是,很多顶尖聪明的计算机科学家付出巨大的努力,但让计算机理解人类的语言还是遥遥无期的。

Even if computers could understand us, human languages are not very well suited for describing complex algorithms. Natural language is fraught with ambiguity and imprecision. For example, if I say: “I saw the man in the park with the telescope,” did I have the telescope, or did the man? And who was in the park? We understand each other most of the time only because all humans share a vast store of common knowledge and experience. Even then, miscommunication is commonplace.

即使计算机能够弄懂人类的语言,我们的语言也不适合来描述解释一些复杂的算法。人类自然语言的缺陷是多义性与精确度不高。打个比方,如果我说“I saw the man in the park with the telescope."  是我本人手里拿着望远镜,还是我看到的人?到底谁在公园里?生活在地球上的人类能够相互沟通的原因是,我们共享在海量的知识与经验。但是误解也是家常便饭,不然地球上就没有争端与战争了。

Computer scientists have gotten around this problem by designing notations for expressing computations in an exact, and unambiguous way. These special notations are called programming languages. Every structure in a programming language has a precise form (its syntax) and a precise meaning (its semantics). A programming language is something like a code for writing down the instructions that a computer will follow. In fact, programmers often refer to their programs as computer code, and the process of writing an algorithm in a programming language is called coding.

计算机科学家设计一套记号系统,通过精确无歧义的方法来描述计算过程。这套记号系统叫做编程语言。每一种编程语言由它的精确的形式(语法),精确的含义(语义)。编程语言是编写指令的代码,计算机能够执行的。事实上,程序员通常用编写的程序来特指计算机代码,用编程语言编写算法的过程叫编程。

Python is one example of a programming language. It is the language that we will use throughout this book. You may have heard of some other languages, such as C++, Java, Perl, Scheme, or BASIC. Although these languages differ in many details, they all share the property of having well-defined, unambiguoussyntax and semantics.

Python就是一门编程语言。本书使用Python为例进行展开的。大家可能听说过其它编程语言,如C++,Java,Perl, Scheme等。虽然这些语言在细节上有差异,有也很多的相似点,如定义优良,语法,语义无歧义。

All of the languages mentioned above are examples of high-level computer languages. Although they are precise, they are designed to be used and understood by humans. Strictly speaking, computer hardware can only understand very low-level language known as machine language.

上面提及的语言均为高级计算机语言。它们是精确的,能够被人们所理解并使用。严格说来,计算机只能理解并执行低级语言,我们称其为机器语言。

Suppose we want the computer to add two numbers. The instructions that the CPU actually carries out might be something like this.举例说来,我们让计算机把两个数累加,CPU执行的指令如下:

  • load the number from memory location 2001 into the CPU

  • load the number from memory location 2002 into the CPU

  • Add the two numbers in the CPU

  • store the result into location 2003

(存储器2001地址处的数转入到Cpu, 存储器2002地址处的数转入到CPU, 在CPU中把这两数累加,结果保存到存储器2003地址处)


This seems like a lot of work to add two numbers, doesn’t it? Actually, it’s even more complicated than this because the instructions and numbers are represented in binary notation (as sequences of 0s and 1s).

看起来,把两个数相加挺费事的!实际上,把指令与数用二进制序列的方式表征,可能情况更复杂。

In a high-level language like Python, the addition of two numbers can be expressed more naturally: c = a + b. That’s a lot easier for us to understand, but we need some way to translate the high-level language into the machine language that the computer can execute. There are two ways to do this: a high-level language can either be compiled or interpreted.

在Python中,两数相加,可以写成这样的代码:c=a+b。这个我们容易理解,但问题是我们得把这代码翻译成机器语言,计算机才能执行。一般有两种方法完成这个事情:编绎和解释。

A compiler is a complex computer program that takes another program written in a high-level language and translates it into an equivalent program in the machine language of some computer. 

编绎器是一个复杂的计算机程序,它的作用是把高级语言转化(翻译)成计算机能够理解的机器语言。

a block diagram of the compiling process. The high-level program is called source code, and the resulting machine code is a program that the computer can directly execute. The dashed line in the diagram represents the execution of the machine code.

下面图中会告诉你编绎过程,高级语言编写的程序叫源代码。结果机器代码是计算机能够直接执行的程序。图中的虚线部分表示转化机器代码的过程。


编译性高级语言

An interpreter is a program that simulates a computer that understands a high-level language. Rather than translating the source program into a machine language equivalent, the interpreter analyzes and executes the source code instruction by instruction as necessary. 

解释器也是一段程序,使得计算机能够理解高级语言.。 它不是把源代码翻译成相应的机器语言,它是通过解释器,边读取指令边分析并执行源代码。

The difference between interpreting and compiling is that compiling is a one-shot translation; once a program is compiled, it may be run over and over again without further need for the compiler or the source code. In the interpreted case, the interpreter and the source are needed every time the program runs. Compiled programs tend to be faster, since the translation is done once and for all, but interpreted languages lend themselves to a more flexible programming environment as programs can be developed and run interactively.

编绎与解释的差别是,编绎是一次性翻译,编绎工作完成,目标代码就可以反复执行,不需要编绎器与源代码文件了。而翻译型,解释器与源代码,程序每次执行时都需要的。编绎程序速度相对快,因为编绎工作是一劳永逸,但解释性语言,在一个更灵活的编程环境,使得程序可以开发与运行交互性的进行。

The translation process highlights another advantage that high-level languages have over machine language: portability. The machine language of a computer is created by the designers of the particular CPU.

翻译过程的一个更重要的优势是,高级语言可以在不同的计算机上运行,便捷性。机器语言的构造与CPU的设计有关。


解释性高级语言


Each kind of computer has its own machine language. A program for a Pentium CPU won’t run on a Macintosh that sports a PowerPC. On the other hand, a program written in a high-level language can be run on many different kinds of computers as long as there is a suitable compiler or interpreter (which is just another program). For example, if I design a new computer, I can also program a Python interpreter for it, and then any program written in Python can be run on my new computer。

不同种类的计算机有不同的机器语言。Intel奔腾系列程序不能在苹果Macintosh上运行,它支持PowerPC。另一方面,高级语言可以在不同种类的计算机运行,只要它有相应的编绎器或者解释器。比如我设计一台新的计算机,我可以做一个Python的解释器,那用Python写的代码就可以在这台新电脑上运行了。

文章推荐