Phases of Compiler

Phases of Compiler

The compilation process of a compiler goes through various phases. Each phase of the compiler takes the output from the previous as an input.

Here we will see all the phases of a compiler: 

Phases of Compiler

Linguistic/Lexical Analysis:

  • The first phase of the compiler is the Linguistic/Lexical Analysis.
  • It takes the source code as an input and changes them into meaningful lexemes.
  • This analyzer is also known as a scanner.
  • Lexical analyzer converts all the lexemes into a meaningful token. In the next tutorial, we will get to know more about tokens.

Syntax analysis:

  • The compiler's second phase is the syntax analyzer.
  • It takes the token produced by the first phase as an input, followed by producing a parse tree as an output. Sometimes it also is known as the parser. So, don't get confused.
  • In this phase, all the tokens are checked against source code grammar to check if the tokens are syntactically correct or not.

Semantic Analysis:

  • This is the third phase of the compiler.
  • The role of the semantic analyzer is to check if the generated parse tree is meaningful or not.
  • It keeps track of identifier, types and their expression. The Annotated syntax tree is the output of this compiler phase.
  • Also, type checking is an important part of this phase of the compiler.

Intermediate Code Generation:

  • In this phase, it generates intermediate code by taking source code as an input.
  • It is neither a high-level language nor a machine level language. It is somewhere in between.
  • It can easily produce the intermediate code so that it can be translated into the target machine code.

Code Optimization:

  • The next phase of the compiler is code optimization.
  • In this phase, intermediate source code is optimized in such a way that it doesn't alter the meaning of the code.
  • Optimization can be done by removing unnecessary code lines so that it takes low memory and less execution time.

Code Generator:

  • This is the last stage of the compiler.
  • It takes optimized code as an input and generates the target code for the machine.
  • When the target language is machine code, then for each of the variables used by the program, a register or memory location will be allocated.

Symbol Table:

The main function of the Compiler is to store the variable names used in the source program and collect information about various attributes of each name. These attributes provide various information about the variable used in the program. In order to store the information about variables, the compiler needs a data structure, which is why it needs a symbol table.

      It is the compiler's data structure that is being used to store all the identifiers with their name and types. With the help of the symbol table, it makes it easier to search and retrieve.

All the phases of the compiler are associated with the symbol table and error handler, as shown in the above image.


Related Topics

Run-Time Storage Management

Run-Time Storage Management Every executing program has its own logical address space. Logical address space is partitioned into: Code: It is responsible for storing the executable target code. Static: It is used to...

3 minutes read.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

6 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Run-Time Environments

Run-Time Environments Storage Organization Every target program has its own logical address, and an executable program runs in it. The logical address space has the location for each program value. The...

2 minutes read.

Intermediate-Code Generator Compiler Design

Intermediate-Code Generator The process of translating a source language into machine code for a given target machine is done by intermediate-code. It lies between the high-level language and the machine language....

2 minutes read.

Bottom-Up Parsing in Compiler Design

Bottom-Up Parsing A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). Bottom-up parsing is...

6 minutes read.

Boolean Expression in Compiler Design

Boolean Expression The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following: Boolean expressions...

3 minutes read.

Compiler Design Tutorial

Our compiler design tutorial will provide all the information about compiler from basic to advanced level. This compiler tutorial will help the student for their semester as well as for...

3 minutes read.

Data Flow Analysis in Compiler Design

Data Flow Analysis All the optimization techniques we have learned earlier depend on data flow analysis. DFA is a technique used to know about how the data is flowing in any...

3 minutes read.

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

3 minutes read.

Machine-Independent Optimizations in Compiler Design

Machine-Independent Optimizations The main aim of machine-independent optimization is to improve the generated intermediate code so that compiler can get better target code. Eliminating unwanted code from the object code or replacing...

8 minutes read.

Storage Allocation

Storage Allocation The storage allocation represents memory management. The allocation of memory can be done in the following ways: Static AllocationStack AllocationHeap Management Static Allocation: It is a procedure used for the allocation of...

1 minute read.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.

LALR 1 Parsing | Compiler Design

LALR (1) Parsing The LALR parsing refers to the "lookahead LR" that has many lesser steps than typical parsers based on LR(1) items. For constructing the LALR(1) parsing table, the canonical...

6 minutes read.

Shift Reduce Parsing in Compiler Design

Like the bottom-up parsing, the shift reduce parser also builds the parse tree from the leaves (bottom) to the root (up). The LR parser is a more versatile variation of...

4 minutes read.

Derivation and Parse Tree in Compiler Design

Derivation and Parse Tree In this article, we will learn Derivation and Parse Tree. Derivations The parse tree can be constructed by taking a derivational view in which production is treated as rewriting...

4 minutes read.

Optimization of Basic Blocks in Compiler Design

Optimization of Basic Blocks We can apply the optimization process on a basic block. While optimization, there is no need to change the set of expressions computed by the block. The basic...

3 minutes read.

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.

Finite State Machine

Finite State Machine A finite state machine is a simple machine to recognize patterns. It takes a string of symbols as the input and changes its state to another state, but...

3 minutes read.

Basic Blocks and Flow Graphs in Compiler Design

Basic Blocks and Flow Graphs In this section, we are going to learn how to work with basic block and flow graphs in compiler design. Basic Block The basic block is a set...

3 minutes read.