Interpreter
An interpreter is a program that directly executes the instructions in a
high-level language, without converting it into machine code. In
programming, we can execute a program in two ways. Firstly,
through compilation and secondly, through an interpreter. The
common way is to use a compiler.
Strategies of an Interpreter
It can work in three ways:
Execute the source code directly and produce the output.
Translate the source code in some intermediate code and then
execute this code.
Using an internal compiler to produce a precompiled code. Then,
execute this precompiled code.
Types of an Interpreter in Programming
1. Bytecode Interpreters
The source code is firstly converted to bytecode.
Bytecode is a compressed and optimized representation of source
code. But, it is not the machine code.
The bytecode interpreters then execute this compiled code
Compiler and interpreter both are used hence, the name
‘compreters’.
Each instruction starts with a byte. Therefore, they have up to 256
instructions.
2. Threaded Code Interpreters
Similar to bytecode interpreters but, they use pointers.
Each instruction is a word acting as a pointer. This pointer points
to a function or instruction sequence.
There is no restriction on the number of instructions. Considering
that, memory and address space is available.
3. Abstract Syntax Tree Interpreters
It converts the source code to an abstract syntax tree (AST).
Then it executes the program according to this tree.
Each sentence is parsed just once.
The program structure and the relation between statements remain
the same.
Provides better analysis during run time
4. Self Interpreters
These are a special type of interpreters.
They are programming language interpreters written in a
programming language that can interpret itself.
An example can be a BASIC interpreter written in BASIC.
Self interpreters are created in case if no compiler exists for a
language.
Their creation requires the implementation of that language in a
host language. This host language can be another programming
language.
Browse more Topics under System Software
Operating System
Compiler
Assembler
Difference Between Compilers and Interpreters
Sr.No Compilers Interpreters
It converts the whole program into
1. It translates only one statement at a time.
machine code at once.
It takes more time to analyze the source
It comparatively takes less time to analyze the source
code. In other words, compile time is
2. code. In other words, compile time is less. However,
more. However, the overall execution
The overall execution time is more.
time is less.
It generates an intermediate object It does not generate any intermediate object code.
3.
code. Therefore, more memory is used. Hence it is memory efficient.
The whole program is compiled and
It stops the compilation if any error occurs. Hence,
4. then it shows all the errors together.
debugging is easier.
Therefore, debugging is difficult.
Programming languages like Python, Ruby, PHP, etc.
Programming languages like C, C++,
5. use an interpreter. These interpreted languages are
Java, etc use compiler.
also called scripting languages.
Advantages and Disadvantages of Interpreter
Advantages
Executes line by line. Hence, debugging is easy.
No intermediate code hence uses the memory efficiently.
Disadvantages
Execution time is more.
Interpreters of Different Programming Languages
Java- HotSpot, OpenJ9, JRockIt
Python- PyPy, Stackless Python
Ruby- YARV, CRuby
A pure interpreter keeps the original source program, while an impure
interpreter converts the source program into an intermediate
representation.
Explanation
Pure interpreter
Keeps the source program in its original form
Analyzes the source text and executes it as it
goes
Can be slow because it spends time analyzing
strings of characters
Impure interpreter
Converts the source program into an intermediate
representation
Eliminates most analysis during interpretation
Speeds up interpretation by reducing analysis
overhead
Requires re-preprocessing after any modifications