0% found this document useful (0 votes)
86 views5 pages

What Is The Phases of Compiler

Uploaded by

sadaf abid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views5 pages

What Is The Phases of Compiler

Uploaded by

sadaf abid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

What is a Compiler?

A compiler is a computer program that decodes computer code composed in one programming
language into another language. Or we can say that the compiler helps in translating the source
code composed in a high-level programming language into the machine code. You can also dive
deep into the difference between Compiler and Interpreter to get more familiar with this topic.

Explore the Phases of Compiler


The 6 phases of a compiler are:

1. Lexical Analysis
2. Syntactic Analysis or Parsing
3. Semantic Analysis
4. Intermediate Code Generation
5. Code Optimization
6. Code Generation
1.Lexical Analysis: Lexical analysis or Lexical analyzer is the initial stage or phase of the compiler.
This phase scans the source code and transforms the input program into a series of a token.
A token is basically the arrangement of characters that defines a unit of information in the source
code.
NOTE: In computer science, a program that executes the process of lexical analysis is called a
scanner, tokenizer, or lexer.
You can gain in-depth knowledge of lexical analysis to get a better understanding.

Roles and Responsibilities of Lexical Analyzer


 It is accountable for terminating the comments and white spaces from the source program.
 It helps in identifying the tokens.
 Categorization of lexical units.
2. Syntax Analysis:
In the compilation procedure, the Syntax analysis is the second stage. Here the provided input string
is scanned for the validation of the structure of the standard grammar. Basically, in the second
phase, it analyses the syntactical structure and inspects if the given input is correct or not in terms of
programming syntax

It accepts tokens as input and provides a parse tree as output. It is also known as parsing in a
compiler.
Roles and Responsibilities of Syntax Analyzer

 Note syntax errors.


 Helps in building a parse tree.
 Acquire tokens from the lexical analyzer.
 Scan the syntax errors, if any.
3. Semantic Analysis: In the process of compilation, semantic analysis is the third phase. It scans
whether the parse tree follows the guidelines of language. It also helps in keeping track of identifiers
and expressions. In simple words, we can say that a semantic analyzer defines the validity of the
parse tree, and the annotated syntax tree comes as an output.
Roles and Responsibilities of Semantic Analyzer:

 Saving collected data to symbol tables or syntax trees.


 It notifies semantic errors.
 Scanning for semantic errors.
4. Intermediate Code Generation: The parse tree is semantically confirmed; now, an intermediate
code generator develops three address codes. A middle-level language code generated by a
compiler at the time of the translation of a source program into the object code is known as
intermediate code or text.
Few Important Pointers:

 A code that is neither high-level nor machine code, but a middle-level code is an
intermediate code.
 We can translate this code to machine code later.
 This stage serves as a bridge or way from analysis to synthesis.

Roles and Responsibilities:

 Helps in maintaining the priority ordering of the source language.


 Translate the intermediate code into the machine code.
 Having operands of instructions.
5. Code optimizer: Now coming to a phase that is totally optional, and it is code optimization. It is
used to enhance the intermediate code. This way, the output of the program is able to run fast and
consume less space. To improve the speed of the program, it eliminates the unnecessary strings of
the code and organizes the sequence of statements.

Roles and Responsibilities:

 Remove the unused variables and unreachable code.


 Enhance runtime and execution of the program.
 Produce streamlined code from the intermediate expression.
6. Code Generator: The final stage of the compilation process is the code generation process. In
this final phase, it tries to acquire the intermediate code as input which is fully optimized and map it
to the machine code or language. Later, the code generator helps in translating the intermediate
code into the machine code.
Roles and Responsibilities:

 Translate the intermediate code to target machine code.


 Select and allocate memory spots and registers.

You might also like