The Difference between Compiler and Interpreter

The Difference between Compiler and Interpreter

8 mins read4.8K Views Comment
clickHere
Jaya
Jaya Sharma
Senior Executive Content
Updated on Aug 25, 2023 10:15 IST

Both compiler and interpreter are responsible for converting source code into machine code. However, compiler converts the code before execution whereas interpreter converts the code after execution. The two also differ in terms of speed and efforts.

2022_09_MicrosoftTeams-image-27-1.jpg

Let us understand the difference between compiler and interpreter in terms of their functionality through this article.

Table of Contents

  1. Difference between Compiler and Interpreter
  2. What is Compiler?
  3. Types of Compiler
  4. Phases of Compiler
  5. What is Interpreter?
  6. Types of Interpreter
  7. Similarities between Interpreter and Compiler

Difference Between Compiler and Interpreter

Let us learn about the differences between interpreter and compiler:

Parameter Compiler  Interpreter
Code conversion Before running the program While running the program
Suitable for C and C++ Web environments
Working Translates entire source code into machine code Translates the code line by line
Basis Translation linking-loading model Interpretation method
Error fixing Part of the program containing the error is reorganized The entire program is reorganized
Localization of error Difficult Easy
Functionality Generates stand-alone machine code Performs actions described by high-level programs

Explore free C programming courses and free C++ courses

What is a compiler?

A compiler is a computer program used for translating computer codes from source programming language to the target language. Compiler is essential for a computer since it can only understand the machine language. The source code is converted into object code so that the CPU can understand it. The final goal is to create an executable program.

As an analogy you can understand it like a human translator who translated from English to Hindi. Similarly, a computer compiler is used to translate your code (readable by human) into the binary language of 0s and 1s for computer to understand and perform the action.

Explore programming courses

Types of Compilers

Following are the different types of compilers that are in use:

  1. Source to source compiler/ transcompiler: This type of compiler takes the source code of one programming language as input to convert it into another programming language. This takes place between those programming languages that operate at almost same level of abstraction, unlike traditional compilers. 
  2. Cross compiler: This compiler creates executable codes for platforms other than on which it is running. It is used for compiling codes for various platforms from a single development host. The objective of cross compiler is to distinguish the build environment from the target environment. These can be used for compiling on server farm and bootstrapping to a new platform.
  3. Hardware compiler/silicon compiler: It takes hardware description language as an input and convert it into logic as description. The source of compilation is either the netlist or a high-level description. 
  4. Just-in-time compiler: It is a feature of run-time interpreter compiles bytecode into machine code instructions of running machine rather than interpreting byte code when method is invoked. In this type, compilation occurs during execution of a program at run time. 

Phases of Compiler

Compiler operates in different phases where in each phase the source program transforms from one representation to another. Each phase takes in input from the previous stage and feeds the output to the next phase of the compiler. In a compiler, there are six phases as given below:

1. Lexical analysis

It is the initial phase where compiler scans source code. The character steam from source program is grouped into meaningful sequences by identifying tokens. This turns entry of corresponding tickets into symbol table and passes the token to the next phase. At this stage, the lexical units are identified in source code and are then classified into classes. These are then entered in different tables. Then tokens that are not the part of the language are ignored.  

Explore free online courses with certificates

2. Syntax analysis

In this phase, the structure in the code is determined. This analysis also determines the structure and syntax of source language. It is assessed whether a text follows the expected format. There are rules involved in this phase that are based on specific programming languages. This is done by constructing parse trees using tokens. During this phase, tokens are obtained from lexical analyzer. Then it is verified if the expression is syntactically correct. Any errors are then reported. 

3. Semantic analysis

This phase analyzes the code for its semantic consistency with the help of symbol table. Syntax tree and symbol table from the previous phase are picked. Type information is stored and saved in the syntax tree. This allows type checking. At this phase, semantic errors can also be identified in case of type mismatch. 

4. Intermediate code generator

In this phase, the compiler generates intermediate code for target machine that represents a program from abstract machine. The code is generated in a way so that it can be translated to target machine code. This code should be produced from semantic representation of source program and should hold values computed during translation process.

5. Code optimizer 

This is the phase of intermediate code or code optimization that removes unnecessary code lines. It arranges sequence of statements for expediting the execution of program. The aim is to produce code that runs faster and takes up less space. This phase also helps in improving the runtime of the target program.  

6. Code generator 

It is the final phase of the compiler where it fetches input from the previous phase and generates page or object code. This phase allocates memory location for the variable and generates relocatable machine code. The intermediate code is converted into target language or machine code at this point.

Check out free C++ courses

Types of Compiler

The following are the different types of compilers that are used:

  1. Just-in-time (JIT) compiler: It compiles programs as they are executed. It is faster than traditional compilers and helps in reducing program size by elimination of redundant code. This reduced the size of the program and make it more efficient. This helps in performance improvement. 
  2. Cross compiler: This is a technology to allow developers to compile and run codes on various platforms. This type of compiler is useful while working on several versions of code for ensuring that all platforms are being supported. This is useful while working on a new platform to verify whether the code is working on this platform. 
  3. Bytecode compiler: It is a compiler that translates high level language into machine code which is executable on target machine. Such compilers allow developers to write codes in a high-level language and compile them into machine code. Through this compiler, developers write concise and comprehensible codes. These compilers should be written in high-level language. They are not suitable for developing low-level code.
  4. Source-to-source compiler: This software tool translates the source code into executable code. Such compilers are used to translate source code written in multiple programming languages. The translation process can be completed in both manual and automatic methods. Compilers translate source code into machine code which is executed by a target machine.
  5. Binary compiler: This compiler translates the source code file into binary format. This type of format stores the program information in a compact form that is easily read by computer. Developers use compilers for network programming, database administration, and web development.
  6. Hardware compiler: Such compilers compile the source code into machine code for transforming source code into machine code. Post that, the computer executes this code. Such compilers are used in operating systemsembedded systems, and computer games. Assembler is a type of hardware compiler.

Explore operating system courses

What is an Interpreter?

Interpreters are programs that directly execute instructions written in a scripting language without the need to have them previously compiled into machine language programs. It consists of lists of commands or instructions that are in the order in which the programmer wants to execute them. These are used in microcomputers to help the programmers in finding out errors and rectifying before the control moves over to the next statement.

Explore machine language certifications

Types of Interpreter

The following are the different types of interpreters:

1. Threaded code interpreters 

These interpreters use pointers wherein every instruction is a word pointing towards a function or instruction sequence. These are followed by parameters. They loop fetching instructions and call the function that they point to. Every instruction sequence will end with fetch and jump to the next instruction. 

2. Abstract syntax tree interpreters

Here the aim is to transform the source code in to the optimized abstract syntax tree (AST) and execute the program following this structure. Here, every sentence is parsed at once. It allows system to perform analysis in a better way during the run tine. Unline bytecode, it retains the global program structure and relationship among statements intact. When compressed, it provides more compact representation. 

3. Template Interpreter

This is one of the special interpreters. It maintains a large arrangement of bytecode that is directly mapped to the corresponding to native machine instructions to be hosted on the host hardware as key value pairs known as ‘template’. When a specific code segment is executed, the template interpreter loads or jumps to the opcode mapping in template. It then runs on the hardware directly. It is much faster than other interpreter. This is due to template’s simple design where it directly passes calls to the hardware instead of implementing the calls. 

Difference Between Primary Memory and Secondary Memory
Difference Between Primary Memory and Secondary Memory
Computer memory is simply the computer’s brain where data and information are stored for easy retrieval. Memory is the computer’s storage space that temporarily or permanently stores data or programs....read more
What is the Difference Between LAN and WAN?
What is the Difference Between LAN and WAN?
A LAN is like a small neighborhood of connected devices, usually in one building or home. It’s fast and uses things like Wi-Fi. On the other hand, a WAN is...read more
Difference Between System Software and Application Software
Difference Between System Software and Application Software
Software’s are set of program, procedure, and routine that tells computer what to do. They are broadly classified into system software and application software. In this article, we will discuss...read more

Similarities between Compiler and Interpreter

Once you have learnt the difference between compiler and interpreter, you should also be aware of the similarities between the two.

  • Both compiler and interpreter convert programs written into high-level to machine code.
  • The two also must perform lexical and syntactic analysis.
  • Generally, both compiler and interpreter turn source code (text files) into the tokens.
  • They may also generate immediate instructions.

FAQs

How does an interpreter work?

An interpreter reads, translates, and executes the source code one statement or line at a time during runtime. It does not create a separate executable file. Instead, it directly processes the source code, making it slower compared to a compiled program.

Which one is faster: a compiled program or an interpreted program?

Compiled programs are faster than interpreted ones because the translation from source code to machine code happens only once, before execution. On the other hand, interpreted programs translate source code during runtime, causing some overhead.

What is the difference between compiled and interpreted language?

In a compiled language, the program is directly translated by the target machine. Whereas, in an interpreted language, source code is not directly changed by target machine.

About the Author
author-image
Jaya Sharma
Senior Executive Content

Jaya is a writer with an experience of over 5 years in content creation and marketing. Her writing style is versatile since she likes to write as per the requirement of the domain. She has worked on Technology, Fina... Read Full Bio

Comments