An interpreter is a software program that directly executes the code written in a high-level language. Continue reading this article to learn about an interpreter and its function.
This is where the role of a language processor comes into play. A language processor is a software program that converts the code written in a high-level programming language into machine code or any intermediate code. This makes it easy for computer systems to process the code and provide the desired output.
Three major types of language processors are compiler , interpreter, and assembler. In this blog post, we shall discuss what an interpreter is, how it functions, and its advantages and disadvantages.
What is Interpreter?
An interpreter is a software program that executes the code or program written in a high-level language line by line. Unlike a compiler, an interpreter directly executes the code without the need for the compilation process. It reads one line of the code, converts it into the machine code or intermediate form, and executes it immediately.
The line-by-line code execution of the interpreter makes debugging easier because it stops execution where it finds an error. Developers/programmers can directly visit that line causing the error to fix it. However, this line-by-line execution is time-consuming.
Strategies Interpreter Uses to Execute a Program
An interpreter might use one of the following ways to execute a program:
- Analyze the source code and directly execute it to generate the desired result. Examples include minicomputer and microcomputer BASIC dialects and early versions of LISP programming languages.
- Translate each line of the source code into an efficient machine code or intermediate code and execute it immediately. Examples include Programming languages like Perl, Raku, MATLAB, Python and Ruby .
- Use a compiler to translate the code into machine language and execute the precompiled code. UCSD Pascal is an example.
The use of an interpreter dates back to the year 1952. It was initially used to simplify and smoothen programming due to the limitations of computers back then. Some of these limitations include no support for floating-point numbers and a shortage of storage space.
In addition, they were used to translate the code between low-level languages. At that time, the code was written for machines under development and executed on existing machines.
The first interpreted high-level programming language was Lisp. Steve Russell implemented Lisp first on an IBM 704 computer. Further, he got the idea to implement Lisp’s eval function in the machine code. This resulted in a List interpreter capable of running Lisp programs.
The Need for an Interpreter
The foremost need for an interpreter is to make the code written in a high-level programming language executable by a computer. This implies translating it into machine code or any intermediate format, like bytecode.
Now, you might have a question - why use an interpreter if a compiler exists for the same purpose? Well, the question is absolutely right.
However, it is important to note that a compiler comes with a few setbacks. For instance, consider a particular source code that is extremely lengthy. It may take hours for a compiler to compile it into the machine code.
In such a scenario, an interpreter comes in handy. As it eliminates the compilation process, an interpreter can save significant time required to compile a lengthy source code into the machine code. It reads each line of code at a time, translates it into the intermediate code, and executes it immediately.
Types of Interpreters
Here are the different types of interpreters you must know:
1. Bytecode Interpreters
A bytecode is a computer object code, which is a series of sequential instructions ready for an interpreter to execute. Simply put, a bytecode is a compromised form of the source code but not the machine code.
Bytecode interpreters convert the source code into an intermediate code called the bytecode. Further, the interpreter processes this bytecode on a virtual machine in a runtime environment and provides output.
2. Threaded Code Interpreters
Threaded code interpreters leverage pointers. Each instruction of the source code is a word that acts as a pointer. The pointer points to a function or an instruction sequence.
These interpreters iterate two actions – fetching instructions and calling a function to which instructions point. Each instruction sequence finishes with jumping to the next instruction sequence.
3. Abstract Syntax Tree (AST) Interpreters
These interpreters convert the source code into the abstract syntax tree (AST) and follow the same structure to execute a program. They parse each instruction in the source code only once. As a result, computers are able to analyze the source code efficiently during runtime.
4. Self Interpreters
Self interpreters are interpreters specific to programming languages. For instance, the Lisp interpreter can run only Lisp programs. Such interpreters are designed only if a specific programming language has no compiler.
The following are interpreted programming languages and corresponding interpreters:
- Python - CPython, PyPy, Stackless Python, IronPython
- Java - HotSpot, OpenJ9, JRockIt
- Ruby - YARV, Ruby MRI (CRuby)
- Kotlin - JariKo
Advantages and Disadvantages of an Interpreter
Let us now throw light on the benefits and drawbacks of an interpreter.
- Easy Debugging: An interpreter makes debugging very easy since it executes the program line-by-line.
- Consumes Less Space: It uses less memory to execute a program because it does not generate a new separate executable file like a compiler. It executes the program on the go.
- Instant Output: It comes in handy when we want to run a program to test the outputs.
- Easy to Use: Most interpreters have simple and beginner-friendly interfaces, making them easy to use even for beginners.
- Detailed Error Messages: An interpreter provides more detailed error messages than a compiler, making it easy for programmers to understand and fix them quickly.
- Slow Speed: The biggest disadvantage of interpreters is their slow execution speed. They execute each line at a time rather than an entire program in one go.
- Not Secure: Interpreters are not secure since they do not create a separate file of a program. Therefore, you need to share the source code directly.
- Limited Optimization: Interpreters do not analyze the entire source code in one go. As a result, they lack optimizing the code as compilers do.
Difference Between an Interpreter and a Compiler
The following table highlights the difference between an interpreter and a compiler:
A compiler converts an entire source code into machine code in one go.
An interpreter reads the source code line by line, converts each line into the intermediate code, and executes it immediately.
It creates an executable file after compiling the source code. Hence, it consumes more memory space.
It does not create an executable file, as it eliminates the process of compilation. Hence, it is memory-efficient.
Debugging is difficult.
Debugging is easier.
It displays all errors at once.
It displays errors detected in a single line.
C, C++, C#, and Java use a compiler
Python, Ruby, Perl, and PHP use an interpreter.
Check out the differences in detail here .
An interpreter executes the program on the fly without converting it into machine code. It eliminates the need for compiling the source code. It executes any program line-by-line and stops the execution if there is an error, which makes debugging easy.
We hope this article has helped you understand what an interpreter is, along with its advantages and disadvantages.
People are also reading: