DEV Community

Cover image for Introduction to Programming - Compiler and Interpreter
Deepak Chhitarka
Deepak Chhitarka

Posted on

Introduction to Programming - Compiler and Interpreter

We have talked about what is programming, and we have also talked about different programming paradigms and some of the languages which follow those paradigms. Now, let us talk about how these programs are executed by the computer.

In the article Introduction to programming — What is programming?, I briefly explained that Programming languages are used to write code which is then translated into machine-level code which is understood by the computer. This translation is done with the help of tools such as compilers or interpreters.

Some languages use compilers while others use interprets for this task. Based on this, languages are often termed as compiled language and interpreted language. Let us discuss these both in detail.

COMPILER

A compiler is a translator that takes a high-level programming language such as Java as input and produces an output of low-level language (like an assembly or machine language).

It is basically a computer program used to transform codes written in a programming language into machine code (human-readable code to a binary 0 and 1 bits language for a computer processor to understand).

The computer then processes the machine code for performing the corresponding tasks.

The compiler takes the whole code, checks all types of errors, limits, and ranges, and compiles it into machine code which is then executed by the computer. If any changes are made to the code, then the whole code needs to be re-compiled only then will the changes reflect.

Note: The time required by the compiler to compile the code is called compile-time where as the time used while executing the code is termed as run-time.

In case of compiled languages, compile-time is relative longer while the run-time is shorter as the code is already translated into machine-level code and the computer just need to run that code.

Compiler stores the translated machine code and execute it every time the code is run. So, it requires more memory.

Java, C++, C#, Go etc., are some of the most widely used high-level programming languages using compiler for translation of their code to machine level code.

Compiled languages often termed as statically typed programming languages as Compilers are very difficult to implement because they can’t predict anything that happens during the turn time.

INTERPRETER

Interpreter is a program that functions for the translation of a programming language into a comprehensible one. It is a computer program used for converting high-level program statements into machine codes. It includes pre-compiled code, source code, and scripts.

Interpreter is similar to compiler as it also translates high-level programming languages into machine code but the difference is that unlike compiler, interpreter reads the code line by line, check for any error in that line and then convert that line or statement to machine code.

Interpreter does not store the machine level code, so it takes less memory but since the code is not stored, it is generated every time the code is run.

Also, since code is not compiled, it has no compile-time, but it’s run-time is very large compared to compiler as every time it converts the high-level programming language code into machine level code line by line or statement by statement.

We will not be able to find all the errors in our code at a time, as the interpreter will stop interpreting the code at the line where it finds any error. Until that error is not fixed, it will not move past that line.

Python, PHP, JavaScript, Ruby etc., are some of the most widely used high-level programming languages using interpreter for translation of their code to machine level code.

These languages are often called as dynamically typed languages as they support Dynamic Typing as interpreter executes code line by line, so at run time, it dynamically determines the type of variables used.

One major advantage of interpreter is that you can run just a single line of code and see it’s output where as in case of compiler you need to first compile the whole code and then run it.

If you liked the article, then please give it a thumb-up.

Read other articles of the series Introduction to programming

Top comments (0)