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

Three-Address Code

Three-Address Code If there is at most one operator on the right side of the instruction, then the instruction will be the three-address code so that no arithmetic expressions are permitted....

2 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.

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.

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.

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.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

LEX

LEX Lex is a tool/computer program that generates a Lexical analyzer. Lex is developed by Vern Paxson in C around 1987. Lex works together with the YACC parser generator. It allows...

3 minutes read.

S-attributed and L-attributed SDTs

S-attributed and L-attributed SDTs STD stands for Syntax Directed Translation. When we associate some informal notations called semantic rules and the grammar, they are known as STD. So we can say...

2 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.

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.

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.

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 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.

Stack Allocation of Space

Stack Allocation of Space Almost all compilers for languages that use procedure, functions, or methods manage their run-time memory as a stack. Whenever a procedure is called, the local variable's space...

2 minutes read.

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.

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.

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.

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.

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

3 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.