正在加载图片...
4 CHAPTER 1.COMPUTERS AND PROGRAMS 1.5 Programming Languages 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. 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. Computer scientists have gotten around this problem by designing notations for expressing computa- tions 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,unambiguous syntax and semantics. 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. 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 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 Os and Is). 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. 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.Figure 1.2 shows 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.Figure 1.3 illustrates the process 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 lan- guage:portability.The machine language of a computer is created by the designers of the particular CPU.4 CHAPTER 1. COMPUTERS AND PROGRAMS 1.5 Programming Languages 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. 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. Computer scientists have gotten around this problem by designing notations for expressing computa￾tions 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. 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. 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 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. 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. Figure 1.2 shows 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. Figure 1.3 illustrates the process. 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 lan￾guage: portability. The machine language of a computer is created by the designers of the particular CPU
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有