Compiler and Interpreter


We generally write a computer program using a high-level language. A high-level language is one that is understandable by us, humans. This is called source code.

However, a computer does not understand high-level language. It only understands the program written in 0’s and 1’s in binary, called the machine code.

To convert source code into machine code, we use either a compiler or an interpreter.

Both compilers and interpreters are used to convert a program written in a high-level language into machine code understood by computers. However, there are differences between how an interpreter and a compiler works.

Real-World Scenario Similar To Compiler and Interpreter

You might have noticed that when leaders of two countries meet and if they do not understand the language of one another, they are accompanied by an official called an Interpreter, whose job is to convert the conversation line by line. This is exactly what a computer interpreter does.

compiler and interpreter

Now consider another scenario. On a certain occasion, a leader delivers a speech in some language. Once she/he finishes, the whole speech is translated into other different languages and broadcasted and telecasted. This process is called compilation and this is what a computer compiler does.

compiler and interpreter

Compiler and Interpreter – What is a Compiler

A compiler is software that converts the source code to the object code. In other words, we can say that it converts the high-level language to machine/binary language. Moreover, it is necessary to perform this step to make the program executable. This is because the computer understands the only binary language.

Some compilers convert the high-level language to an assembly language as an intermediate step. Whereas some others convert it directly to machine code. This process of converting the source code into machine code is called compilation.

Some of the popular computer languages using compilers are C, C++, COBOL, Fortran, etc.

How Does a Compiler Work

The compilation process can be divided into four steps, i.e., Pre-processing, Compiling, Assembling, and Linking.

  • Preprocessor: The source code is the code that is written in a text editor and the source code file is given an extension “.c”. This source code is first passed to the preprocessor, and then the preprocessor expands this code. After expanding the code, the expanded code is passed to the compiler.
  • Compiler: The code which is expanded by the preprocessor is passed to the compiler. The compiler converts this code into assembly code. Or we can say that the C compiler converts the pre-processed code into assembly code.
  • Assembler: The assembly code is converted into object code by using an assembler. The name of the object file generated by the assembler is the same as the source file. The extension of the object file in DOS is ‘.obj,’ and in UNIX, the extension is ‘o’. If the name of the source file is ‘hello.c’, then the name of the object file would be ‘hello.obj’.
  • Linker: Mainly, all the programs written in C use library functions. These library functions are pre-compiled, and the object code of these library files is stored with the ‘.lib’ (or ‘.a’) extension. The main work of the linker is to combine the object code of library files with the object code of our program. Sometimes the situation arises when our program refers to the functions defined in other files; then linker plays a very important role in this. It links the object code of these files to our program. Therefore, we conclude that the job of the linker is to link the object code of our program with the object code of the library files and other files. The output of the linker is the executable file. 

Features of a Compiler

  • The features are as follows:
  • Compilation speed.
  • The correctness of machine code.
  • The meaning of code should not change.
  • Speed of machine code.
  • Good error detection.
  • Checking the code correctly according to grammar.

Compiler and Interpreter – What is an Interpreter

The software by which the conversion of the high-level instructions is performed line-by-line to machine-level language is known as an interpreter. If an error is found on any line, the execution stops till it is corrected. This process of correcting errors is easier as it gives line-by-line errors but the program takes more time to execute successfully.

It translates source code into some efficient intermediate representation and immediately executes it.

Source programs are compiled ahead of time and stored as machine-independent code, which is then linked at run-time and executed by an interpreter. An Interpreter is generally used in microcomputers. It helps the programmer to find out the errors and to correct them before the control moves to the next statement.

The interpreter system performs the actions described by the high-level program. For interpreted programs, the source code is needed to run the program every time. Interpreted programs run slower than the compiled programs.

Python one of the most popular languages uses an interpreter. Some of the common languages using interpreters are JavaScript, Perl, BASIC, etc.

Features of an Interpreter

  • The interpreter converts the source code line-by-line during RUN Time.
  • Interpret completely translates a program written in a high-level language into machine-level language.
  • The interpreter allows evaluation and modification of the program while it is executing.
  • Relatively less time spent for analyzing and processing the program
  • Program execution is relatively slow compared to the compiler

Following are the comparison between interpreter and compiler:

Translates program one statement at a time into machine code.Scans the entire program and translates it as a whole into machine code.
Interpreters usually take less time to analyze the source code. However, the overall execution time is comparatively slower than compilers.Compilers usually take more time to analyze the source code. However, the overall execution time is comparatively faster than interpreters.
No intermediate object code is generated, hence is memory efficient.Generates intermediate object code which further requires linking, hence requires more memory.
Programming languages like JavaScript, Python, Ruby use interpreters.Programming languages like C, C++, Java use compilers.

Advantages And Disadvantages Of Compiler And Interpreter

We saw what compilers and interpreters were and how they worked. But which is better and are there any advantages and disadvantages associated with them? Let’s see what are the advantages and disadvantages of compiler and interpreter.

Advantages Of Compiler

  • Runs Faster: It runs faster than the Interpreter since the source code is already compiled and we only need to execute the executable file is generated.
  • Optimized: Machine code of executable file generated with a compiler is often a native machine instruction for the targeted machine which is well optimized and runs faster.
  • Secure: Executable files generated from the compiler can be executed on any of your clients or other systems without the need for actual source code. This makes your program unhackable, secure and private.
  • No dependencies: Your client or anyone else doesn’t need any compiler, interpreter, or third party program to be installed in their system, for executing the shared executable file of your source code.

Disadvantages Of Compiler

  • Extra Memory: It occupies extra memory since it needs to generate a new file.
  • Extra Step: Unlike the interpreter, we can’t run our source code directly, we need to additionally run the executable file.
  • Incompatibility Issue: Since its code is optimized for the system it was executed on, which many times leads to system incompatibility issues even if the system is running on the same OS.
  • Harder to Debug: After reading the whole code it returns all errors at once if available, which makes it harder to navigate and fix the error.

Advantages Of Interpreter

  • Cross-Platform: In interpreted language, we directly share the source code which can run on any system without any system incompatibility issue.
  • Easier To Debug: Code debugging is easier in interpreters since it reads the code line by line, and returns the error message on the spot. Also, the client with the source code can debug or modify the code easily if they need to.
  • Less Memory and Step: Unlike the compiler, interpreters don’t generate new separate files. So it doesn’t take extra Memory and we don’t need to perform one extra step to execute the source code, it is executed on the fly.
  • Execution Control: The interpreter reads code line by line so you can stop the execution and edit the code at any point, which is not possible in a compiled language. However, after being stopped it will start from the beginning if you execute the code again.

Disadvantages Of Interpreter

  • Slower: The interpreter is often slower than the compiler as it reads, analyzes, and converts the code line by line.
  • Dependencies file required: A client or anyone with the shared source code needs to have an interpreter installed in their system, in order to execute the code.
  • Less Secure: Unlike compiled languages, an interpreter doesn’t generate any executable file so to share the program with others we need to share our source code which is not secure and private. So it is not good for any company or corporations who are concerned about their privacy.

Conclusion: Both compilers and interpreters are computer programs that convert a code written in a high-level language into a lower-level or machine code understood by computers. However, there are differences in how they work and when to use them.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}