Language translators and IDEs · 语言翻译器与 IDE
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| compiler/kəmˈpaɪlə/ | 编译器 | biān yì qì |
| source code/sɔːs kəʊd/ | 源代码 | yuán dài mǎ |
| translator/trænˈsleɪtə/ | 翻译器 | fān yì qì |
| machine code/məˈʃiːn kəʊd/ | 机器码 | jī qì mǎ |
| assembler/əˈsemblə/ | 汇编器 | huì biān qì |
| assembly language/əˈsemblɪ ˈlæŋɡwɪdʒ/ | 汇编语言 | huì biān yǔ yán |
| object code/ˈɒbdʒekt kəʊd/ | 目标代码 | mù biāo dài mǎ |
| executable/ɪɡˈzekjʊtəbl/ | 可执行文件 | kě zhí xíng wén jiàn |
| interpreter/ɪnˈtɜːprɪtə/ | 解释器 | jiě shì qì |
| bytecode/ˈbaɪtkəʊd/ | 字节码 | zì jié mǎ |
| portable/ˈpɔːtəbl/ | 可移植的 | kě yí zhí de |
| virtual machine/ˈvɜːtʃuːəl məˈʃiːn/ | 虚拟机 | xū nǐ jī |
| just-in-time compilation/dʒʌst ɪn taɪm ˌkɒmpɪˈleɪʃn/ | 即时编译 | jí shí biān yì |
| integrated development environment/ˈɪntɪɡreɪtɪd dɪˈveləpmənt enˈvaɪrənmənt/ | 集成开发环境 | jí chéng kāi fā huán jìng |
| context-sensitive prompts/ˈkɒntekst ˈsensɪtɪv prɒmpts/ | 上下文相关提示 | shàng xià wén xiāng guān tí shì |
| auto-complete/ˈɔːtəʊ kəmˈpliːt/ | 自动补全 | zì dòng bǔ quán |
| dynamic syntax checks/daɪˈnæmɪk ˈsɪntæks tʃeks/ | 动态语法检查 | dòng tài yǔ fǎ jiǎn chá |
| prettyprint/ˈpretɪprɪnt/ | 代码美化 | dài mǎ měi huà |
| breakpoints/ˈbreɪkpɔɪnts/ | 断点 | duàn diǎn |
| single stepping/ˈsɪŋɡl ˈstepɪŋ/ | 单步执行 | dān bù zhí xíng |
| report window/rɪˈpɔːt ˈwɪndəʊ/ | 报告窗口 | bào gào chuāng kǒu |
Nobody believed the computer could write its own program
- In 1952 Grace Hopper built the first compiler 编译器, a program that turned a programmer's notation into machine instructions. "Nobody believed that," she said later. "I had a running compiler and nobody would touch it. They told me computers could only do arithmetic."
- Every language you have used since rests on that idea: you write source code 源代码 for people, and a translator produces the machine code 机器码 the processor runs.
- There are three kinds of translator and one famous hybrid, and the exam wants you to say which to use when, and why.
- This lesson is the translators, the trade-offs between compiling and interpreting, Java's mixture of both, and the tools an IDE puts around them.
没人相信计算机能写自己的程序
- 1952 年,Grace Hopper 造出了第一个编译器(compiler)——一个把程序员的记号变成机器指令的程序。"没人相信,"她后来说。"我有一个能运行的编译器,却没人愿意碰它。他们告诉我计算机只能做算术。"
- 你此后用过的每一种语言都建立在那个想法上:你为人写源代码(source code),翻译器生成处理器运行的机器码(machine code)。
- 有三种翻译器和一个著名的混合体,考试要你说出何时用哪一种,以及为什么。
- 这一课讲翻译器、编译与解释之间的权衡、Java 对两者的混合,以及 IDE 围绕它们提供的工具。
Source code, machine code and the assembler
- The processor executes only machine code. A translator 翻译器 converts a program written for people into it.
- An assembler 汇编器 translates assembly language 汇编语言 into machine code: each mnemonic instruction,
LDD,ADD,JMP, becomes exactly one machine-code instruction, and labels and symbolic addresses are replaced by real addresses. - Assembly is used where the programmer needs direct control of the hardware: device drivers, embedded systems, the innermost loops of a game.
源代码、机器码和汇编器
- 处理器只执行机器码。翻译器(translator)把为人编写的程序转换成机器码。
- 汇编器(assembler)把汇编语言(assembly language)翻译成机器码:每条助记符指令——
LDD、ADD、JMP——恰好变成一条机器码指令,标号和符号地址被替换成真实地址。 - 汇编用在程序员需要直接控制硬件的地方:设备驱动、嵌入式系统、游戏最内层的循环。
Match each translator to what it does. · 把每个翻译器与它做的事配对。
Assembler: assembly→machine code; compiler: all-at-once to an executable; interpreter: line by line at run time. · 汇编器:汇编 → 机器码;编译器:一次性到一个可执行文件;解释器:运行时逐行。
The compiler
- A compiler translates the whole high-level program into machine code, object code 目标代码, before it is run. It produces an executable 可执行文件 file and reports all the syntax errors together, as a list, at the end of translation.
- It does not run the program. Once the code is clean, the executable runs without the compiler installed, as many times as you like.
- Fast at run time, since nothing is translated while running; but the machine code is tied to one processor and operating system.
编译器
- 编译器在程序运行之前把整个高级语言程序翻译成机器码——目标代码(object code)。它生成一个可执行文件(executable),并在翻译结束时把所有语法错误列成一张清单一起报告。
- 它不运行程序。代码没有错误后,可执行文件无需安装编译器即可运行,想运行多少次都行。
- 运行时快,因为运行中不做翻译;但机器码绑定于一种处理器和操作系统。
The compiler route: source to running program · 编译器路线:从源代码到运行中的程序
Step through how a compiler works — translating the whole program once, before it runs. Contrast it with an interpreter, which translates and runs one line at a time. · 逐步走过一个编译器如何工作——在程序运行前把整个程序翻译一次。把它与一个解释器对比,后者一次一行地翻译并运行。
A compiler differs from an interpreter because it: · 一个编译器与一个解释器不同,因为它:
A compiler translates all at once into an executable that runs without the compiler. An interpreter runs line by line. · 一个编译器一次性翻译成一个不需要编译器就能运行的可执行文件。一个解释器逐行运行。
Put the compiler route in order, from writing code to running it. · 把编译器路线按顺序排列,从写代码到运行它。
A compiler translates the whole program once into an executable; that file then runs without the compiler. · 一个编译器把整个程序翻译一次成一个可执行文件;那个文件然后不需要编译器就运行。
The interpreter
- An interpreter 解释器 translates one statement of the high-level program at a time and executes it immediately before moving to the next.
- No executable is produced. It stops at the first error it meets and reports it, with the line, so you fix it and run again.
- Both the source code and the interpreter must be present every time the program runs, and each run re-translates, so execution is slower; but the same source runs wherever an interpreter exists.
Translate once and ship, or translate as you go
解释器
- 解释器(interpreter)一次翻译高级语言程序的一条语句并立即执行它,然后再处理下一条。
- 不生成可执行文件。它在遇到的第一个错误处停下并报告——带行号——你修好再运行。
- 每次运行都必须同时有源代码和解释器,而且每次运行都重新翻译,所以执行更慢;但同一份源代码在任何有解释器的地方都能运行。

翻译一次然后发布,或者边走边翻译
When does an interpreter report an error? · 一个解释器何时报告一个错误?
An interpreter runs line by line, so it reports an error at the moment it reaches that line — handy during development. · 一个解释器逐行运行,所以它在到达那一行的时刻报告一个错误——在开发期间很方便。
An interpreter produces no stand-alone executable, so the interpreter itself must be installed to run the program. · 一个解释器不产生独立的可执行文件,所以解释器本身必须被安装才能运行程序。
An interpreter translates and runs line by line each time — there is no separate executable to ship, unlike a compiler. · 一个解释器每次逐行翻译并运行——没有单独的可执行文件可分发,不像一个编译器。
Benefits and drawbacks, as the mark scheme lists them
| compiler | interpreter | |
|---|---|---|
| execution speed | fast, already machine code | slower, translated on every run |
| what the user needs | only the executable; the source stays private | the source code and the interpreter |
| finding errors | all listed at once after translation | each reported at its line, as you develop |
| changing the code | recompile the whole program | edit and run again immediately |
| portability | one platform; recompile for each | runs wherever an interpreter exists |
好处与坏处,按评分标准的列法
| 编译器 | 解释器 | |
|---|---|---|
| 执行速度 | 快,已是机器码 | 较慢,每次运行都翻译 |
| 用户需要什么 | 只要可执行文件;源代码保持私密 | 源代码和解释器 |
| 发现错误 | 翻译后一次全部列出 | 开发时在各自的行报告 |
| 修改代码 | 重新编译整个程序 | 编辑后立即再运行 |
| 可移植性 | 一个平台;每个平台重新编译 | 有解释器的地方都能运行 |
You should choose a compiler when: · 你应该在以下时候选择一个编译器:
Compiled executables are fast and stand-alone. Interpreters suit quick development and portable scripts. · 编译的可执行文件快且独立。解释器适合快速开发和可移植的脚本。
Which points favour using an interpreter while developing a program? Select all · 所有 that apply. · 哪些点支持在开发程序时使用解释器?选出所有适用的。
Fast edit-run cycles and errors at their line are the interpreter's strengths. Run-time speed is the compiler's. · 快速的编辑—运行循环和按行报告错误是解释器的长处。运行时速度是编译器的长处。
Worked example: an interpreter while writing, a compiler when finished
- A developer uses an interpreter while writing a program and a compiler when it is finished. Explain how each is used. [4]
- During development the interpreter runs the partly written program at once, without waiting for a complete translation. When it meets an error it reports the line, so the developer fixes it and runs again immediately: a fast edit-run cycle that makes debugging easier.
- When the program is finished, the compiler translates the whole program into an executable that runs faster, needs no translator on the user's computer, and keeps the source code private.
- Two marks for each tool: what it does, and why that suits that stage.
例题:写的时候用解释器,写完用编译器
- 一名开发者在编写程序时使用解释器,完成后使用编译器。解释各自怎样使用。[4]
- 开发期间,解释器立即运行只写了一部分的程序,不必等待完整翻译。遇到错误时它报告行号,开发者修好后立即再运行:一个快速的编辑—运行循环,让调试更容易。
- 程序完成后,编译器把整个程序翻译成运行更快的可执行文件,用户的计算机上不需要翻译器,源代码也保持私密。
- 每个工具两分:它做什么,以及为什么适合那个阶段。
Java: partially compiled, partially interpreted
- Java source is compiled into bytecode 字节码, a compact, portable 可移植的 intermediate form that is not machine code for any real processor.
- A virtual machine 虚拟机, the JVM, then interprets the bytecode on whatever machine it runs on, or uses just-in-time compilation 即时编译 to turn the busiest parts into native code while the program runs.
- The compile stage catches syntax errors early; the interpret stage lets one bytecode file run anywhere a JVM exists. Java in console mode, a text program run from the command line, is the syllabus's example.
Write once, run anywhere: one bytecode file, one JVM per platform
Java:部分编译,部分解释
- Java 源代码被编译成字节码(bytecode)——一种紧凑、可移植的(portable)中间形式,不是任何真实处理器的机器码。
- 然后虚拟机(virtual machine)——JVM——在它运行的任何机器上解释字节码,或用即时编译(just-in-time compilation)在程序运行中把最繁忙的部分变成本机代码。
- 编译阶段早早抓住语法错误;解释阶段让一个字节码文件在任何有 JVM 的地方运行。控制台模式下的 Java——从命令行运行的文本程序——是大纲的例子。

一次编写,到处运行:一个字节码文件,每个平台一个 JVM
Java compiles to bytecode that runs on a JVM. The main benefit is: · Java 编译成在 JVM 上运行的字节码。主要好处是:
Bytecode is platform-independent; any JVM can run it (with JIT giving near-native speed for hot code). · 字节码是平台无关的;任何 JVM 都能运行它(JIT 给热点代码接近本机的速度)。
Java's compiler produces platform-independent , which a virtual machine then interprets. · Java 的编译器生成与平台无关的,再由虚拟机解释。
Bytecode is an intermediate form: compiled once, then run by the JVM on any platform. That is what makes Java partially compiled and partially interpreted. · 字节码是中间形式:编译一次,然后由任何平台上的 JVM 运行。这就是 Java 部分编译、部分解释的原因。
Worked example: why compile to bytecode and then interpret?
- Explain why Java is compiled to bytecode rather than straight to machine code.
- A compiler produces machine code for one processor and operating system, so a program compiled on one machine will not run on another.
- Java's compiler targets a virtual machine instead, so the bytecode is identical everywhere. Each platform supplies its own JVM to interpret it.
- The cost is speed: interpreting bytecode is slower than running native code, which is why the JVM adds just-in-time compilation for the hot parts.
例题:为什么先编译成字节码再解释?
- 解释 Java 为什么编译成字节码而不是直接编译成机器码。
- 编译器生成的机器码只针对一种处理器和操作系统,所以在一台机器上编译的程序在另一台上无法运行。
- Java 的编译器改为以一台虚拟机为目标,所以字节码在任何地方都相同。每个平台提供自己的 JVM 来解释它。
- 代价是速度:解释字节码比运行本机代码慢,所以 JVM 对热点部分加入即时编译。
The IDE and its four groups of features
- An integrated development environment 集成开发环境 (IDE) is one application for writing, translating, running and debugging a program. The syllabus groups its features in four.
- For coding: context-sensitive prompts 上下文相关提示 pop up the identifiers, keywords or parameters that fit at that point; auto-complete 自动补全 finishes the name.
- For initial error detection: dynamic syntax checks 动态语法检查 underline a mistake as you type, before translation. For presentation: prettyprint 代码美化 shows keywords, identifiers, strings and comments in different colours with consistent indentation; expand and collapse hides the body of a block so you see the outline.
- For debugging: breakpoints 断点 pause the program at a marked line; single stepping 单步执行 runs one line at a time; a window shows the values of variables and expressions; the report window 报告窗口 lists errors, warnings and output.
Coding, error detection, presentation, debugging
IDE 及其四组功能
- 集成开发环境(integrated development environment,IDE)是一个用于编写、翻译、运行和调试程序的应用。大纲把它的功能分成四组。
- 编码:上下文相关提示(context-sensitive prompts)弹出在该位置合适的标识符、关键字或参数;自动补全(auto-complete)替你补完名字。
- 初始错误检测:动态语法检查(dynamic syntax checks)在你输入时、翻译之前就给错误画下划线。呈现:代码美化(prettyprint)用不同颜色显示关键字、标识符、字符串和注释,并保持一致的缩进;展开与折叠隐藏代码块的主体,让你看到大纲。
- 调试:断点(breakpoints)让程序在标记的行暂停;单步执行(single stepping)一次运行一行;一个窗口显示变量和表达式的值;报告窗口(report window)列出错误、警告和输出。

编码、错误检测、呈现、调试
Which feature is part of an IDE's debugger? · 哪个功能是一个 IDE 的调试器的一部分?
A debugger lets you set breakpoints, step through code line by line, and inspect variable values. · 一个调试器让你设置断点、逐行步进代码,并检查变量值。
Match each IDE feature to its syllabus group. · 把每个 IDE 功能与它在大纲中的分组配对。
Coding, initial error detection, presentation, debugging: the four groups the syllabus names, one feature each here. · 编码、初始错误检测、呈现、调试:大纲点名的四组,这里各一个功能。
Worked example: finding the bug in Calculate()
- A function
Calculate()returns an unexpected value. Describe how the debugging features of an IDE help find the cause. [4] - Set a breakpoint on the first line of
Calculate(), so the program pauses there instead of running through. - Single-step through the function one line at a time. After each step, read the values of the variables and of any expression you have asked the IDE to watch, and compare them with what you expected.
- The first line after which a value is wrong is where the logic error is. The report window shows any run-time error messages and output along the way.
Pause, step, inspect
例题:找出 Calculate() 里的 bug
- 函数
Calculate()返回一个意外的值。描述 IDE 的调试功能怎样帮助找到原因。[4] - 在
Calculate()的第一行设一个断点,让程序在那里暂停而不是一直跑完。 - 单步执行函数,一次一行。每步之后,读出变量和你让 IDE 监视的任何表达式的值,与你期望的比较。
- 第一个让某个值出错的行之后,就是逻辑错误所在。报告窗口沿途显示任何运行时错误信息和输出。

暂停、单步、检查
Put the steps of finding a logic error with a debugger in order. · 把用调试器找出逻辑错误的步骤按顺序排列。
Pause, step, inspect, compare, locate. Each is one of the debugging features the syllabus lists. · 暂停、单步、检查、比较、定位。每一步都是大纲列出的调试功能之一。
Marks that slip away
- A compiler does not run the program. It translates the whole of it and produces an executable.
- An interpreter stops at the first error; a compiler lists them all. Do not swap these.
- "Faster" needs "at run time", and "portable" needs "the same source runs wherever an interpreter exists".
- Sort IDE features into the syllabus's four groups: a breakpoint is debugging, prettyprint is presentation, a context-sensitive prompt is coding. Bytecode is not machine code.
容易丢掉的分
- 编译器不运行程序。它翻译整个程序并生成可执行文件。
- 解释器在第一个错误处停下;编译器把错误全部列出。不要互换。
- "更快"要加"运行时","可移植"要加"同一份源代码在任何有解释器的地方运行"。
- 把 IDE 功能归入大纲的四组:断点是调试,代码美化是呈现,上下文相关提示是编码。字节码不是机器码。
You've got it
- assembler: one assembly instruction to one machine-code instruction · compiler: the whole program, before it runs, an executable, all errors listed · interpreter: one statement at a time, executed at once, stops at the first error, no executable
- compiler for speed, distribution and privacy of the source; interpreter for a fast edit-run cycle, portability and beginners
- Java compiles to portable bytecode that a JVM interprets or JIT-compiles: partially compiled, partially interpreted
- IDE features in four groups: coding (context-sensitive prompts), initial error detection (dynamic syntax checks), presentation (prettyprint, expand and collapse), debugging (breakpoints, single stepping, variables, report window)
你掌握了
- 汇编器:一条汇编指令对一条机器码指令 · 编译器:整个程序、运行前、一个可执行文件、列出所有错误 · 解释器:一次一条语句、立即执行、在第一个错误处停下、无可执行文件
- 编译器为了速度、分发和源代码的私密;解释器为了快速的编辑—运行循环、可移植性和初学者
- Java 编译成可移植的字节码,由 JVM 解释或即时编译:部分编译,部分解释
- IDE 功能四组:编码(上下文相关提示)、初始错误检测(动态语法检查)、呈现(代码美化、展开与折叠)、调试(断点、单步执行、变量、报告窗口)