- How an interpreter executes a program
Translate-and-run one statement at a time, with no saved translated file.
A high-level program cannot be run directly by the processor — it must be translated. An interpreter does this in a particular way:
An interpreter analyses and executes the source code one statement at a time, while the program is running. It does not produce a complete translated (object) file that is stored for later.
Working through a program, the interpreter:
- reads the next statement of the source code;
- analyses (translates) it;
- executes it immediately;
- moves on to the next statement.
Because nothing is saved, the program must be re-translated every time it is run, and the source code is needed each time.
Why this is useful (and where it is weak):
| Interpreter | Effect |
|---|---|
| Stops at the first error and reports it | great for debugging — fast edit/run cycle |
| No recompilation needed after an edit | quick feedback while developing |
| Re-translates every run | generally slower to run than compiled code |
| Source code must be supplied | the program is not a standalone executable |
Contrast this with a compiler, which translates the entire program in advance into an object/executable file that is saved and can be run many times without re-translating.
- Interpreter: translate AND execute statement by statement, at run time.
- No stored object/translated file is produced.
- Source is re-translated every time the program runs.
- Stops/reports at the first error → good for debugging.