GoalExplain why programs need a translator and compare a compiler with an interpreter.
New words
source code · a program written by a person in a high-level languagemachine code · binary patterns that the processor understands directlycompiler · a translator that deals with the whole program at onceinterpreter · a translator that deals with one line at a time
Explanation
People write source code in a high-level language, with English-like words such as IF and WHILE. A processor cannot follow such words. It follows only machine code, which is made of binary patterns, and each pattern means one command. So a translator, which is system software, turns source code into machine code. A compiler translates the whole program in one go. If it finds a syntax error, nothing runs. If there is none, it makes an executable file, which can be run again later without the compiler. An interpreter works through the program line by line, translating and running each line in turn. It keeps going until it meets an error, then stops. It makes no executable file, so the interpreter is needed every time the program is run. Operating systems, utilities and translators are all system software.
Source code in a high-level language→Translatortranslates→Machine code in binary→CPU carries it out
Worked examples
Question
Compiler
Interpreter
How much is translated at a time?
the whole program
one line
Is a translator needed every time the program runs?
no, the executable file is enough
yes
Is an executable file made?
yes
no
Compiler or interpreter? The table compares them on three questions. The answers explain why a compiled program can be sent to people who do not own the compiler, while an interpreted program needs the interpreter on the computer each time.
Pseudocode
1OUTPUT "Start"2total ← 4 + 53OUTPUT total
4OUTPT "End"
An error on line 4. The four lines below are written in an imagined language, and OUTPT on line 4 is a spelling mistake. A compiler reads the whole program first, finds the mistake and runs nothing, so nothing is printed. A line-by-line interpreter runs lines 1 to 3, so it prints Start and 9, and then stops at line 4 with an error message. We use an imagined language because Python checks a whole program for spelling mistakes of this kind before it runs any line, so Python would print nothing, like a compiler. The lines are only a picture of the idea: a computer never runs pseudocode, and real translators differ in detail.
Situation
Better choice
Reason
a finished game is sent to thousands of players
compiler
the executable file runs without a translator
a learner tests a short program after each change
interpreter
it runs straight away and stops where the error is
a company wants a program to start at once, every time
compiler
it is translated only once
Choosing a translator. Each situation gives a reason for one kind. A finished game for thousands of players should be an executable file that runs without any translator. A learner who changes a line and runs the program again and again is helped by an interpreter, because it runs at once and stops at the line with the problem.
Class activity
Two ways to translate (no computer). Invent four symbols and what they mean, for example ▲ = clap, ■ = stamp, ● = turn round, ◆ = jump. Write a secret program of eight symbols on paper, with one symbol that is not on the key sheet as the “syntax error”, in the fifth place. Your partner has the key sheet. Round 1, compiler: the partner first translates all eight symbols into words on a new sheet, finds the error and does nothing. Round 2, interpreter: the partner translates one symbol, does the action, then the next, until the error stops them. Count how many actions were done in each round.
Practice
1
Write compiler or interpreter for each description.
Description
Translator
(a) It translates the whole program before anything runs.
?
(b) It has already run the earlier lines when it meets an error.
The program below, written in an imagined language, has a mistake on line 3. Write what is printed when it is run by a compiler and by a line-by-line interpreter.
Compiler: nothing is printed, because the error is found before the program runs. Interpreter: it prints Hello and 6, then stops at line 3 with an error and does not run lines 4 and 5.
3
Why can a CPU not follow source code that uses English-like words directly? What fixes this?
A CPU understands only machine code, made of binary patterns, not English-like words. A translator, a compiler or an interpreter, turns the source code into machine code.
4
Write operating system, utility software or translator for each job.
Job
Kind of system software
(a) scanning a downloaded file for malware
?
(b) giving each running program its share of RAM
?
(c) turning source code into machine code
?
(d) making a new microphone work with the computer