How Does Compiler Work?

How Does Compiler Work?

how does compiler work

Hello Programmer,

Today we will discuss What is Compiler? How does Compiler work?

As a programmer, we all know that we always write our code in High-level Language. In High-level language, we write code in the English language which is understandable by another programmer but our computer or machine can not understand high-level language code.

Now the most important question is how the computer is capable to understand our code. How he displays the result even he can not capable to understand high-level language code.

Now compiler concept comes out... 

What is a Compiler?





A compiler is computer software that translates or compiles the source code written in a high-level language into a machine language instruction that can be understood by a computer.

A compiler is a large software which helps a computer to understand high-level language code and then computer give an appropriate result based on high-level language code.

A compiler is a very large program which checks all Syntax and Semantic of code. Some compiler translates high-level language code into an intermediate assembly language, which is then translated into machine code by an assembly program or assembler.

Some compiler directly converts high-level code into machine code.

How Do Compilers Work?

1) Lexical Analysis:- This phase scans the source code as a stream of characters and converts it into meaningful lexemes. It also removes white-space and comments written in the source code.




A program that performs the lexical analysis is called lexical analyzers or lexers. A lexer contains tokenizer or scanner. If the lexical analyzer detects that the token is invalid, it generates an error. It reads character streams from the source code, checks for legal tokens, and pass the data to the syntax analyzer when it demands.


2) Syntax Analysis:- This is the second phase of the compiler. Syntax analysis or parsing takes the token produced by lexical analysis as input and generate a parse tree or syntax tree. 

Syntax error can be detected at this level if the input is not in accordance with the grammar. 

The parse tree is developed with the help of pre-defined grammar of the language. The syntax analyzer also checks whether a given program fulfils the rules implied by a context-free grammar. If it satisfies, the parser then creates the parse tree of that program. Otherwise, it will display error messages.

3) Semantic Analysis:- This is the third phase of the Compiler. It checks whether the constructed parse tree follows the rule of language. 

Semantic analysis is the task of ensuring that the declarations and statements of a program are semantically correct, i.e, that their meaning is clear and consistent with the way in which control structures and data types are supposed to be used.

Also, keeps track of identifiers, their types and expressions; whether identifiers are declared before use, etc.

What Does Semantic Analysis Involve?

  • Type Checking:- Data types are used in a manner that is consistent with their definition.
  • Label Checking:- Labels references in a program must exist.
  • Flow control checks:- Control structures must be used in their proper fashion.


4) Intermediate Code:- During the compilation of source code into the object code for a target machine, a compiler may generate a middle-level language code, which is known as intermediate code. 

Intermediate Code Representation...

Intermediate code can be represented in two ways.


  1. High-Level Intermediate Code: High-level intermediate code can be represented as source code. To enhance the performance of source code, we can easily apply code modification, But to optimize the target machine, it is less prefered.
  2. Low-Level Intermediate Code: Low-level intermediate code is close to the target machine, which makes it suitable for register and memory allocation etc. It is used for machine-dependent optimizations.

5) Code Optimization: Code optimization means transforms the code so that it consumes fewer resources and produces more speed. The meaning of the code being transformed is not altered. 

Compiler optimizing process should meet the following objectives:
  • The optimization must be correct, it must not, in any way, change the meaning of the program.
  • Optimization should increase the speed and performance of the program. 
  • The compilation time must be kept reasonable.
  • The optimization process should not delay the overall compiling process.
When to Optimize?
Optimization of the code is often performed at the end of the development stage since it reduces readability and adds code that is used to increase the performance.

Advantage:
  • Optimized code has faster execution speed.
  • Optimized code utilizes the memory efficiently.
  • Optimized code gives better performance.

6) Code Generation:- In compiler code generation is the process by which a compiler's code generator converts some intermediate representation of source code into a machine code that can be readily executed by a machine.

Code generation can be considered as the final phase of compilation.



Sharing is Caring...

Comments :

Post a Comment