A compiler translates the whole program at once, reports every error, and makes an executable file. An interpreter translates and runs the program line by line and stops at the first error. You see how each one reports errors, and why an interpreter is mostly used while a program is developed and a compiler for the final program.
about 25 min1 simulation1 practice question
Lesson 2 of 3Show all lessonsHide
Try the code and the simulations beside the lesson.
Two ways to translate
Imagine a speech in a language the audience does not know. One way is to translate the whole speech in writing first, check it, and hand out the copies. Another way is to have someone stand beside the speaker and translate each sentence as it is said. A compiler works like the first way. An interpreter works like the second.
Both translate a high-level language into machine code that the processor can run.
Two rows. Top row, Compiler: a box Source code (the whole program) goes to a box Compiler, which goes to two boxes: Error report for the whole code, and Executable file, which then runs. Bottom row, Interpreter: a box Source code goes one line at a time to a box Interpreter: translate one line, execute it, then the next; an arrow labelled error leads to a box Stop and report the error.
The compiler
A compiler translates the whole code at once, before executing it.
It produces an executable file: the program in machine code, ready to run.
If there are errors, it gives an error report for the whole code, with every error it found. No executable file is made until they are fixed.
Once the executable file is made, it runs without the compiler and without the source code. It runs fast, because it is already translated.
The interpreter
An interpreter translates and executes the code line by line.
It stops when an error is found, and reports that error.
No executable file is made. The program is translated again every time it runs, so the interpreter is needed every time.
The lines before the error have already run, so you can see their output. That helps you find where the program went wrong.
Watch them work
The program below has five lines. Line 3 has a missing comma and line 5 has a missing bracket. The toy interpreter runs lines 1 and 2, then stops at line 3. The toy compiler checks every line, reports lines 3 and 5, and runs nothing.
Advantages and disadvantages
An interpreter is mostly used when a program is being developed.
It stops at the first error and shows where it is, so errors can be found and fixed one at a time.
The program can be run, and parts of it tested, without translating the whole program first.
But the program runs more slowly, because it is translated each time it runs. The interpreter is needed on every computer that runs it, and the source code must be given out.
A compiler is used to translate the final program.
The executable file runs fast, and runs without the compiler.
The source code does not need to be given to the users, so it is harder to copy or change.
But the error report comes only after the whole program is translated, so errors are harder to fix while the program is being written. The program must be compiled again after every change.
Run the same program many times, and the time saved by compiling once adds up.
In the exam. "Describe the operation of a compiler" needs: the whole code translated at once, before it is executed; an executable file is made; an error report for the whole code. For an interpreter: line by line, translate then execute each line; it stops at the first error.
Python on this site. The code boxes on this site run Python with an interpreter. Python reads the whole program for spelling mistakes before the first line runs, so a missing bracket stops it at once. A mistake that shows only while running, such as a division by zero, stops it at that line. You met Python's error messages in the course Say Hello to Python, in the Python Programming · Foundations book.
Practise with new numbers
Common mistakes
Saying the interpreter makes an executable file. It does not. The compiler does.
Saying the compiler stops at the first error. It reports every error it finds in the whole code.
Saying the interpreter translates the whole program first. It translates one line, executes it, then moves on.
Mixing up the uses. Interpreter while developing, compiler for the final program.
Remember these
A compiler translates the whole code at once, before it is executed, and produces an executable file.
A compiler gives an error report for the whole code. An interpreter stops when it finds an error.
An interpreter translates and executes the code line by line.
An interpreter is mostly used when developing a program, and a compiler to translate the final program.
Check yourself
Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
Which translator produces an executable file?
A program has errors on lines 4 and 9. How many errors does a compiler report?
The same program is run with an interpreter. What happens?
Why is an interpreter useful while a program is being written?
Give one reason for compiling the final program before it is given to users.
Enroll free in Programming Languages, Translators and IDEs
Every lesson is open to read. Enrolling also lets you:
Mark lessons done and carry on where you stopped.
Take the tests of this lesson and its course.
Count the course towards the certificate for its chapter.