Thursday, April 27, 2023

Introduction of Compiler and its Phases

 

Introduction to Compiler

  • A compiler is a translator that converts the high-level language into the machine language.

  • High-level language is written by a developer and machine language can be understood by the processor.

  • Compiler is used to show errors to the programmer.

  • The main purpose of compiler is to change the code written in one language without changing the meaning of the program.

  • When you execute a program which is written in HLL programming language then it executes into two parts.

  • In the first part, the source program compiled and translated into the object program (low level language).

  • In the second part, object program translated into the target program through the assembler.

Compiler Introduction

Fig: Execution process of source program in Compiler

Compiler Phases

The compilation process contains the sequence of various phases. Each phase takes source program in one representation and produces output in another representation. Each phase takes input from its previous stage.

There are the various phases of compiler:

Compiler Phases

Fig: phases of compiler

Lexical Analysis:

Lexical analyzer phase is the first phase of compilation process. It takes source code as input. It reads the source program one character at a time and converts it into meaningful lexemes. Lexical analyzer represents these lexemes in the form of tokens.

Syntax Analysis

Syntax analysis is the second phase of compilation process. It takes tokens as input and generates a parse tree as output. In syntax analysis phase, the parser checks that the expression made by the tokens is syntactically correct or not.

Semantic Analysis

Semantic analysis is the third phase of compilation process. It checks whether the parse tree follows the rules of language. Semantic analyzer keeps track of identifiers, their types and expressions. The output of semantic analysis phase is the annotated tree syntax.

Intermediate Code Generation

In the intermediate code generation, compiler generates the source code into the intermediate code. Intermediate code is generated between the high-level language and the machine language. The intermediate code should be generated in such a way that you can easily translate it into the target machine code.

Code Optimization

Code optimization is an optional phase. It is used to improve the intermediate code so that the output of the program could run faster and take less space. It removes the unnecessary lines of the code and arranges the sequence of statements in order to speed up the program execution.

Code Generation

Code generation is the final stage of the compilation process. It takes the optimized intermediate code as input and maps it to the target machine language. Code generator translates the intermediate code into the machine code of the specified computer.

Example:

Compiler Phases 1

No comments:

Post a Comment