Compiler or Interpreter

We generally write a computer program using a high-level language. A high-level language is one which is understandable by us humans. It contains words and phrases from the English (or other) language. But a computer does not understand high-level language. It only understands program written in 0's and 1's in binary, called the machine code. A program written in high-level language is called a source code. We need to convert the source code into machine code and this is accomplished by compilers and interpreters. Hence, a compiler or an interpreter is a program that converts a program written in high-level language into machine code understood by the computer.

High-level code (source code) is made up of instructions written in high-level languages such as Visual Basic, C++, Delphi and Java. The main purpose of such languages is to allow programmers to write instructions that can be easily understood by humans, but can still be translated into machine code so the CPU can execute them. High level source code will make use of some or all of the following:

Converting High Level Languages to Machine Code

High level languages are fairly close to normal written language, which is great for developers, but not so great for the CPU! An application is needed to convert the fairly abstract high level language into a precise set of machine codes that the CPU can understand and run. This application is called the Translator.

A translator converts high level commands into a set of machine codes.

One high level command can translate into many individual machine instructions.

Another advantage is that the same piece of high level code can be converted into the machine code of a number of different CPUs by using different translators.

Developers only need to write high level software once and yet it will run on a number of different CPUs. This is called portable code.

The translation of high-level source code into machine code is carried out by two types of program:

The difference between an interpreter and a compiler is given below:

Interpreter Compiler
Translates program one statement at a time. Scans the entire program and translates it as a whole into machine code.
It takes less amount of time to analyze the source code but the overall execution time is slower. It takes large amount of time to analyze the source code but the overall execution time is comparatively faster.
No intermediate object code is generated, hence are memory efficient. Generates intermediate object code which further requires linking, hence requires more memory.
Continues translating the program until the first error is met, in which case it stops. Hence debugging is easy. It generates the error message only after scanning the whole program. Hence debugging is comparatively hard.
Programming language like Python, Ruby use interpreters. Programming language like C, C++ use compilers.

Summary

Both compilers and interpreters are used to translate high level programming languages to machine code. Compilers do this by converting all of the high level code into machine code before running the program. This allows programs to run quickly, since the entire program is ready to be run immediately. However, it can be difficult to locate problems with the program because the compiler does not identify which line is causing the problem.

Interpreters however, translate high level code one line at a time, and run each line before translating the next one. This allows the bugs in the program to be easily identified, since the program will only stop when it reaches a line that causes a problem. However, since it runs one line at a time, an interpreter runs slower than a compiler.