Cambridge IGCSE · Topic 4.2
IGCSE Computer Science 0478 / Topic 4 / IGCSE

All at once, or line by line.

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.

The key idea

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.

Compiler and interpreter Compiler whole sourcecode translates ALLof it at once executable file:run any time source code,one line translates andruns that line output; then thenext line Interpreter errors: one report listing all of them, no executable made repeat line by line; stops at the first error
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.

TranslatorTranslatesHow
Assemblerassembly language (low-level)converts each mnemonic into its machine code instruction
Compilerhigh-level languagetranslates the whole program at once, before it runs, into an executable file
Interpreterhigh-level languagetranslates 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?
  1. The compiler tries to translate all five lines before anything is executed.
  2. It finds the invalid operator >== in line 2 and the missing closing quote in line 4, and carries on checking to the end.
  3. It produces one error report listing every error found, with line numbers.
  4. 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?
  1. Line 1 is translated and executed: the user is asked for a mark.
  2. It moves to line 2, finds the invalid operator and stops, so lines 3 to 5 are never reached.
  3. 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.

CompilerInterpreter
Translationwhole program at once, before executionline by line, during execution
Errorserror report for the whole code at the endstops at the first error found
Outputan executable fileno executable file
Running againfast: run the executable, no translation neededslower: translated again every time it runs
Distributingusers need only the executable; source code stays privateusers need the source code and the interpreter
Best forthe final, finished programdeveloping 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?
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.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Assemblers, compilers and interpreters: how each translates code, how errors are reported, and when to use each.