Learning Objectives
  • Describe how an interpreter translates and executes high-level code.
  • Explain how an interpreter reports errors.
  • Compare compiler and interpreter operation accurately.
  • Explain why interpreters are useful during program development.
  • Choose between a compiler and interpreter for a given scenario.
Key Terms
Interpreter
A translator that translates and executes high-level source code one statement at a time.
Line-by-line translation
Processing a source statement and executing it before moving to the next statement.
Run-time
The period during which a program is executing.
Source code
The program text written in a high-level language.
Error
A problem that prevents correct translation or execution.
Development
The process of designing, writing, testing and correcting a program.
Compiler
A translator that translates a complete source program before execution.
Executable
A translated file that can be run without translating the original source line by line.
Testing cycle
The repeated process of changing code, running it and checking the result.
Summary diagram
Summary Of The Main Ideas In This Lesson
How An Interpreter Operates

An interpreter translates and executes a high-level program one statement at a time. It reads a source statement, translates it into an action the computer can perform and executes that statement before moving to the next one.

No separate executable containing the whole translated program is normally produced by this process. The source code and interpreter are required whenever the program is run in the interpreted form.

The line-by-line process makes the relationship between source statements and observed behaviour immediate. A programmer can change a statement and run the program again without first completing a separate whole-program compilation stage.

Error Reporting By An Interpreter

An interpreter stops when it reaches an error that prevents the current statement from being translated or executed. It reports the error at that point, allowing the programmer to inspect and correct the relevant source statement.

Because execution stops at the first encountered error, later errors are not reported until the earlier error is corrected and the program is run again. This can require repeated test-and-correct cycles.

The immediate stop can be helpful during development because the programmer sees which statement was being processed when the problem occurred. As with a compiler, an interpreter cannot automatically identify every logic error.

Compiler And Interpreter Compared

A compiler translates the whole source program before execution and can produce an executable file. An interpreter translates and executes one statement at a time and normally does not produce a separate final executable in the same way.

A compiler can provide an error report for the whole program after analysis. An interpreter stops execution when an error is found, so later source statements are not processed in that run.

Compiled code can be run repeatedly without repeating source translation each time. Interpreted code requires the interpreter and source, and translation occurs during execution. The correct comparison must describe both translators rather than giving an isolated fact about only one.

Choosing During Development And Final Use

An interpreter is mostly used during development because changes can be tested immediately. The programmer can write a section, run it, observe the result, correct an error and run again without waiting for full compilation of a final program.

A compiler is commonly used to translate a final program because the produced executable can be distributed and run many times. This does not mean interpreted programs cannot be useful products; it is the syllabus-based reason for the usual development-versus-final-program distinction.

In a scenario, identify the priority. Choose an interpreter when rapid testing and immediate error location are important. Choose a compiler when a stable completed program will be executed repeatedly or distributed without supplying the source interpreter environment.

Compiler And Interpreter Comparison
Feature Compiler Interpreter
Translation unit Whole program. One statement at a time.
Execution Occurs after successful translation. Occurs immediately after each statement is translated.
Output Produces object code or an executable. Normally no separate complete executable is produced.
Error reporting Reports errors found across the complete code. Stops when an error is found.
Typical use Translating the final program. Developing and testing a program.
Later runs Executable can run without source translation each time. Source is translated again while running.
Choosing A Translator
Situation Better Fit Reason
Programmer frequently tests small changes Interpreter Immediate line-by-line translation supports rapid testing.
Final application distributed to many users Compiler A completed executable can be run repeatedly.
Need errors from the whole source in one report Compiler It analyses the complete program.
Need execution to stop at the first faulty statement Interpreter It reports the error when that statement is reached.
Worked Examples
Finding An Error During Development

Question: A programmer wants execution to stop at the first statement containing a translation error. Which translator is appropriate and why?

  1. An interpreter translates and executes one statement at a time.
  2. When it reaches a faulty statement, it cannot continue normally.
  3. It stops and reports the error at that point.

Answer: An interpreter is appropriate because it stops when it reaches the error, which helps locate the faulty source statement during development.

Distributing A Finished Program

Question: A completed program will be installed on many users’ computers. Explain why a compiler may be preferred.

  1. The compiler translates the complete final source program.
  2. It produces object code or an executable for the target system.
  3. The executable can be distributed and run without translating each source line during every use.

Answer: A compiler is preferred because it creates a final executable that can be run repeatedly by users.

Examination Guidance
  • Use the exact comparison: compiler translates the whole program; interpreter translates and executes line by line.
  • State that an interpreter stops when an error is found.
  • State that a compiler provides an error report for the whole code if errors are detected.
  • Link interpreters to development and compilers to translating the final program, as required by the syllabus guidance.
  • Do not use “compiler is always faster” as a substitute for describing how the translators operate.
Common Mistakes
  • Saying an interpreter translates the whole program and then executes it.
  • Claiming an interpreter produces the same type of standalone executable as a compiler.
  • Saying a compiler stops at the first source line containing an error.
  • Claiming either translator detects all logic errors.
  • Giving only advantages without explaining the operational difference.
Knowledge Check

1. What does an interpreter do?

Answer: It translates and executes high-level source code one statement at a time.

2. What happens when an interpreter finds an error?

Answer: Execution stops and the error is reported at that point.

3. What is the main translation difference from a compiler?

Answer: A compiler translates the whole program before execution, while an interpreter processes statements line by line during execution.

4. Why is an interpreter useful during development?

Answer: Changes can be tested immediately and execution stops at the faulty statement.

5. Why is a compiler useful for a final program?

Answer: It can produce an executable that is distributed and run repeatedly.