Lecture 2- Phases of a compiler (1)
Lecture 2- Phases of a compiler (1)
Compiler construction
Phases of a Compiler
A compiler can broadly be divided into two phases
based on the way they compile.
Compiler construction
Phases of a Compiler
Structure of a compiler
• The output of the
front end is the
intermediate code
Compiler construction
Phases of a Compiler
Compiler Front End
• The front end of a compiler is responsible for
understanding and analyzing the source code
written in a high-level programming language
(e.g., C, Java, Python). The primary role of the
front end is to check the code for correctness and
prepare it for further processing in the back end.
Key Stages of the Compiler Front End:
• Lexical Analysis(tokenization)-The compiler reads
the raw source code and converts it into tokens
(e.g., keywords, operators, identifiers). Each
token represents a meaningful unit of the code.
• Syntax Analysis (Parsing) -The tokens are checked
to ensure they follow the correct grammatical
structure of the programmingCompiler
language. The
construction
Phases of a Compiler
Compiler Front End
Key Stages of the Compiler Front End:
Semantic Analysis
• The compiler checks for semantic errors—errors
that aren't about the structure but the meaning of
the code.
• E.g. checking if variables are declared before use,
verifying data types in expressions, or ensuring
that functions are called with the correct number
of arguments.
Intermediate Code Generation
• The front end generates an intermediate
representation (IR) of the code. This IR is easier to
optimize and is independent of anyconstruction
Compiler specific
Phases of a Compiler
Compiler Front End
Key Stages of the Compiler Back End:
Code optimization
• The back end applies various optimizations to
the intermediate code to make it run more
efficiently.
Code Generation:
• The optimized intermediate representation is
converted into machine code (or assembly code),
which is specific to the target architecture (e.g.,
x86, ARM).
• This is the actual code that the hardware will
execute.
Compiler construction
Phases of a compiler-details
• 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 Compiler construction
Phases of a compiler-details
• 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.
Compiler construction
Phases of a compiler-details
• 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
Compiler construction