Interpreter
An interpreter translates and runs a high-level program one line at a time, every time the program runs. It does not produce an executable file, so the interpreter is needed whenever the program is run.
Key uses
- Rapid prototyping: trying out ideas and seeing the results straight away
- Scripting: short programs that automate tasks, e.g. Python or Bash scripts
- Web and teaching: JavaScript in web browsers, and Python for learning to code
What is a primary characteristic of an interpreter compared to a compiler?
Features of Interpreters
An interpreter works through a program while it runs, rather than translating it all first.
- Line by line: each statement is translated and run before the interpreter moves on to the next one.
- Stops at the first error: it reports the error, and the line it is on, straight away.
- No executable: no separate file is produced, so the source code and the interpreter are needed every time.
What advantage does line-by-line execution provide in programming?
Advantages of Interpreters
Interpreters make writing and testing code quick and flexible, especially while a program is being developed.
- Easier debugging: it stops at the line with the error, so problems are quick to find and fix.
- No waiting to compile: changes can be run straight away, which speeds up development.
- Portable: the same source code runs on any computer that has the interpreter installed.
What is a key benefit of using an interpreter for executing code?
Disadvantages of Interpreters
The flexibility of an interpreter comes at a cost when the finished program is run and shared.
- Slow to run: every line is translated each time it runs, including lines inside loops.
- Interpreter needed: users must have the interpreter installed to run the program.
- Source code exposed: the source code has to be shared, so it is easy to copy or change.
What is a disadvantage of using interpreters for executing code?
Bytecode Interpreters
A bytecode interpreter executes programs by reading and executing bytecode instructions one at a time, instead of compiling directly to machine code ahead of time.
Typical pipeline
Source code > Bytecode (platform-independent) > Interpreter executes bytecode
Example languages / runtimes
Python (CPython) → .pyc bytecode
Java → JVM bytecode
C# → IL (Intermediate Language)
Advantages of Bytecode Interpreters
Bytecode sits between source code and machine code, which gives bytecode interpreters some useful benefits.
- Portable: the same bytecode runs on any computer with the right virtual machine.
- Fast start-up: programs start running straight away, with no wait for full compilation.
- Simpler runtime: a bytecode interpreter is easier to build than a full compiler.
Disadvantages of Bytecode Interpreters
Running bytecode through an interpreter is still slower than running native machine code.
- Slower execution: each bytecode instruction is interpreted while the program runs.
- Repeated work: code that runs often, such as a loop, is interpreted again every time.
- Virtual machine needed: the right runtime, such as the Java Virtual Machine, must be installed.
Complete! Ready to test your knowledge?
Interpreters
- Interpreter
- Features of Interpreters
- Advantages of Interpreters
- Disadvantages of Interpreters
- Bytecode Interpreters
- Advantages of Bytecode Interpreters
- Disadvantages of Bytecode Interpreters