An interpreter is a type of computer program that directly executes instructions written in a programming or scripting language, without requiring them to be compiled into a machine language program. Interpreters are often used to execute code interactively, allowing users to enter commands one at a time and have them immediately executed.
Interpreters are used to execute programming languages, such as Python, Ruby, and JavaScript. They are also used for scripting languages, such as Bash, PowerShell, and batch files.
An interpreter reads and executes the source code, line by line, and performs any actions that are specified in the code. This differs from a compiler, which reads the source code and generates a machine language program that can be executed without further interpretation.
Interpreters can be used in two ways:
Interpreters can also be used to execute code from a remote machine. This is useful for running programs on web servers, or for running programs on a computer that does not have the necessary software installed.
The first interpreter was developed in the 1950s for the FORTRAN programming language. Since then, interpreters have been developed for many other programming languages, including BASIC, Lisp, and Python.
Interpreters have several advantages over compilers:
Here is an example of an interpreter in action:
$ python
Python 3.7.4 (default, Aug 13 2019, 15:17:50)
[Clang 4.0.1 (tags/RELEASE_401/final)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, world!")
Hello, world!
In this example, the user has invoked the Python interpreter and entered the command print("Hello, world!")
, which prints the string "Hello, world!" to the screen.
Interpreters have several advantages over compilers:
However, interpreters also have some disadvantages:
Compilers: Compilers are programs that read source code and generate a machine language program that can be executed without further interpretation.
Virtual Machines: Virtual machines are software programs that emulate a physical computer, allowing programs written for one type of computer to be executed on another.
Interpreters are often used to execute code interactively, allowing users to enter commands one at a time and have them immediately executed. This can be useful for testing and debugging programs, as well as for exploring the features of a language.
Interpreters can also be used to execute code from a remote machine. This is useful for running programs on web servers, or for running programs on a computer that does not have the necessary software installed.