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.

Working of compiler and interpreter

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.

Difference Between Compilers and Interpreters

Sr.NoCompilersInterpreters
1.It converts the whole program into machine code at once.It translates only one statement at a time.
2.It takes more time to analyze the source code. In other words, compile time is more. However, the overall execution time is less.It comparatively takes less time to analyze the source code. In other words, compile time is less. However, The overall execution time is more.
3.It generates an intermediate object code. Therefore, more memory is used.It does not generate any intermediate object code. Hence it is memory efficient.
4.The whole program is compiled and then it shows all the errors together. Therefore, debugging is difficult.It stops the compilation if any error occurs. Hence, debugging is easier.
5.Programming languages like C, C++, Java, etc use compiler.Programming languages like Python, Ruby, PHP, etc. use an interpreter. These interpreted languages are 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

Frequently Asked Questions (FAQs)

Q1. What is an interpreter?

A1. An interpreter is a software that executes the program directly without converting it to machine code.

Q2. What is the difference between a compiler and an interpreter?

A2. The major difference is that the compiler compiles the whole program at once whereas the interpreter executes it line by line.

Q3. List the types of interpreters.

A3. There are four types of interpreters:

  • Bytecode Interpreters
  • Threaded code Interpreters
  • Abstract Syntax Tree Interpreters
  • Self Interpreters

Read More

error: Content is protected !!