Compiler and Interpreter in Python
What is a Compiler?
A compiler is a program that translates a source language or high-level programming language (for example, Java, C++) into a target machine code (binary bits – 1 and 0) that the CPU can process and understand. The program to be translated is written inside an editor and are known as source statements. The act of translating source code to machine or binary code is known as compilation.
A compiler that is suitable for the programming language is used in which the name of the file name containing the source statements is specified. During compilation, all the language statements will be parsed or analyzed to see if it is correct. If there is no error, the compiler would then convert the source code into machine code which is then ready to execute.
The output of the compilation is also known as object code or object module. However, it should not be confused with an object in object-oriented programming as it is not the same.
The task of a compiler is generally divided into several phases. The phases include lexical analysis, syntax analysis, sematic analysis, intermediate code generator, code optimizer and code generator. Each of these phases helps convert the source code by breaking it down into tokens, generating parse trees and optimizing the source code.
What is an Interpreter?
An interpreter is a program which also converts a high-level programming language (like Python, PHP, Perl) into machine code. Although similar to a compiler, the way that code is executed is different for both. Unlike a compiler that simply converts the source code to machine code, an interpreter can be run directly as an executable program. Contrary to a compiler, it converts source code to machine code when the program is running and not before the program runs.
Interpreters do not produce any intermediary object code like compilers. In interpreters, the source code is compiled and executed at the same time. It continues to translate the program until it encounters the first error after which it stops. Therefore, it is easy to debug. With interpreters, the source statements are executed line by line in contrast to a compiler that converts the whole program at once.
The interpreter also performs lexing, parsing and type checking which is similar to a compiler. However, interpreters directly process syntax tree rather than generating code from it.
Python is generally referred to as an interpreted language. This means that each line of code is executed one by one. However, it does involve the process of compilation. The reason why Python is termed as an interpreted language is that the compiler in Python does relatively less work than an interpreter or in a compiled language like C or Rust.
However, a programming language in itself has no said as to where it is compiled or interpreted. Rather, it is the property of the implementation that decides this. The Python interpreter is generally installed as /usr/local/bin/python if it happens to be available in the system.
In Python, compilation takes place where the code compiles into a simpler form called bytecode. The byte code is not really interpreted to machine code unless there is some kind of implementation like PyPy. Bytecodes are executed by Python Virtual Machine (PVM) which emulates a simplified execution environment.
The compilation process to bytecode is entirely implicit. This means that you never invoke the compiler. Instead, you simply run a .py file. The lack of an explicit compile step is why the Python executable is known as the Python interpreter.
The first step of an interpreter is to read a Python code or instruction. It then checks the syntax of each line and verifies if the instruction is well-formatted. The interpreter can display the errors of each line one by one.
An important feature of Python is it’s interactive prompt. A Python statement can be typed and immediately executed. Most of the compiled languages may not have this interactivity. Python is compiled to bytecode and this bytecode is immediately executed without an explicit compile step.
Different Python implementations
PyPy is a Python interpreter that is developed based on RPython – a subset of Python programming language. A lot of programmers prefer PyPy over other implementations as it offers optimal speed and performance. The interpreter can understand and interpret the Python source code very quickly and efficiently. It comes with a built-in Just-in-Time or JIT compiler. The JIT compiler is used to make PyPy interpret source codes much speeder than other interpreters.
PyPy can also be used by developers to efficiently execute long-running codes. Various studies show that Python is more memory hungry as compared to other programming languages. As such, developers try to find ways to reduce the consumption of memory without affecting the performance of the Python application. Since PyPy is way lighter than the interpreters that are written in C programming language like CPython, it reduces memory consumption.
CPython is the original implementation for Python and has been written in C programming language. CPython is an interpreter that provides a foreign function interface with C as well as other programming languages. It compiles a Python code into intermediate bytecode which is interpreted by the CPython virtual machine. It also acts as a compiler as it transforms Python code to bytecode before it is interpreted. CPython is best if you are writing an open-source code Python code and wish to reach a larger audience. CPython also offers the advantage of speeding up code. CPython also offers the highest level of compatibility with Python packages as well as C extension modules.
Jython is another implementation of Python which compiles a Python code to Java bytecode. Using Jython, we can run Python on any environment that supports and runs a Java Virtual Machine or JVM. Moreover, it has the ability to import and use any Java class like a Python module. Jython supports both static and dynamic compilation.
IronPython is an open-source implementation of Python and is firmly integrated with the .NET framework. IronPython can use both .NET frameworks as well as Python libraries. It supports dynamic compilation has come with an interactive console.
Byterun is a compact Python interpreter written in Python. It was originally created as a learning exercise and is easy to understand partly because it is written in a high-level language that many people can easily read and understand. One of the disadvantages of Byterun is its speed.