Every program has to become machine code before the processor can run it. Learn the three translators, how a compiler and an interpreter each translate high-level code and report errors, and when each one is the better choice.
A translator turns source code into machine code. An assembler translates assembly language. A compiler translates the whole high-level program at once before it is executed, producing an executable file; if there are errors it gives an error report for the whole code. An interpreter translates and executes the code line by line and stops when it finds an error. An interpreter is mostly used while developing a program; a compiler is used to translate the final program.
Fig. 1: a compiler translates the whole program into an executable file; an interpreter translates and runs one line at a time
Section 01
Three translators.
Which translator you need depends on the language the source code is written in.
Translator
Translates
How
Assembler
assembly language (low-level)
converts each mnemonic into its machine code instruction
Compiler
high-level language
translates the whole program at once, before it runs, into an executable file
Interpreter
high-level language
translates one line, executes it, then moves to the next line
Examiner trap
An assembler is never used for a high-level language, and a compiler or interpreter is never used for assembly language. Match the translator to the language.
Section 02
How errors are reported.
Here is a short program with mistakes on lines 2 and 4. Follow what each translator does.
01 INPUT Mark
02 IF Mark >== 50
03 THEN
04 OUTPUT "Pass
05 ENDIF
Worked example Compiler
What happens when the program above is compiled?
The compiler tries to translate all five lines before anything is executed.
It finds the invalid operator >== in line 2 and the missing closing quote in line 4, and carries on checking to the end.
It produces one error report listing every error found, with line numbers.
No executable file is produced, so none of the program runs until the errors are fixed.
Answer: An error report for the whole code; no executable; nothing runs.
Worked example Interpreter
What happens when the program above is interpreted?
Line 1 is translated and executed: the user is asked for a mark.
It moves to line 2, finds the invalid operator and stops, so lines 3 to 5 are never reached.
It reports only that error, so the programmer fixes it and runs the program again to find the next one.
Answer: Runs up to the first error, then stops and reports it.
Examiner trap
A compiler does not run the program while it is translating. It reports all the errors it can find, and only an error-free program becomes an executable file.
Stage 1 · Learn
Check what the sim just showed you
Four quick checks. Each correct answer earns XP and records this skill on your profile.
Quick check+10 XP
Which translator converts assembly language into machine code?
Quick check+10 XP
What does a compiler produce from error-free source code?
Quick check+10 XP
What does an interpreter do when it finds an error?
Quick check+10 XP
When is a compiler usually used?
Section 03
Compiler or interpreter?
Both have strengths. The syllabus point to remember: an interpreter is mostly used while a program is being developed, and a compiler is used to translate the final program.
Compiler
Interpreter
Translation
whole program at once, before execution
line by line, during execution
Errors
error report for the whole code at the end
stops at the first error found
Output
an executable file
no executable file
Running again
fast: run the executable, no translation needed
slower: translated again every time it runs
Distributing
users need only the executable; source code stays private
users need the source code and the interpreter
Best for
the final, finished program
developing and debugging: test parts as you write them
Your turn
A developer is still writing and testing a game. Which translator is more useful now, and why?
An interpreter: it runs the code line by line, stopping at an error, so the developer can test small parts straight away and see exactly where it fails.
Stage 2 · Exam
Exam-style questions
Unlocks once the four checks above are done. Worth more XP, written to IGCSE Paper 1 standard.
Finish the four checks above to unlock the exam questions
Exam style+20 XP
Which describes how a compiler translates code?
Exam style+20 XP
Which is an advantage of an interpreter?
Exam style+20 XP
Why might a company compile a program before selling it?
Exam style+20 XP
A program has three syntax errors. How does a compiler report them?
Skill unlocked
Translators, mastered.
This skill is now recorded on your profile. Keep the chain going.