0% found this document useful (0 votes)
8 views

Compiler Design

Uploaded by

Loshan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Compiler Design

Uploaded by

Loshan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NAME - ROHAN ROUTH

STREAM - CSE [B]

SUBJECT – COMPILER DESIGN

Topic – Different Phases of Compiler

UNIVERSITY ROLL NO. - 14200121128


Introduction to Compiler Design
Typical Phases of Compiler are :
Have you ever wondered how your code gets translated into
machine-readable instructions? This is where compilers come into
play. A compiler is a program that takes the source code written
in a programming language and translates it into machine code
that a computer can understand. Compiler design is a complex
process that involves several phases, each with its own set of
challenges and requirements. However, the end result is worth
the effort. By using a compiler, programmers can write efficient
and error-free code that runs smoothly on different platforms and
devices. In this presentation, we will explore the different phases
of compiler design and their importance in programming.
Here we are to discuss about different phases of compiler.

Phases of Compiler :
We basically have two phases of compilers, namely the Analysis
phase and Synthesis phase. The analysis phase creates an
intermediate representation from the given source code. While
the synthesis phase creates an equivalent target program from the
intermediate representation. 
1. Lexical Analysis: 2. Syntax analysis:
The first phase of a compiler is lexical analysis, also known as In syntax analysis, the compiler checks the
scanning. This phase reads the source code and breaks it into syntactic structure of the input string, i.e.,
a stream of tokens, which are the basic units of the whether the given string follows the grammar
programming language. The tokens are then passed on to the or not. It uses a data structure called a parse tree
next phase for further processing. or syntax tree to make comparisons. The parse
There are three important terms to grab – tree is formed by matching the input string with
• Tokens: A Token is a pre-defined sequence of characters the pre-defined grammar. If the parsing is
that cannot be broken down further. It is like an abstract successful, the given string can be formed by the
symbol that represents a unit. grammar, else an error is reported.
• Lexemes: A lexeme is a sequence of characters matched in
the source program that matches the pattern of a token.
• Patterns: A pattern is a set of rules a scanner follows to
match a lexeme in the input program to identify a valid
token. It is like the lexical analyzer's description of a token
to validate a lexeme.
3. Semantic analysis: This is the third phase in compiler design where by the semantics used in a
program are validated to ensure correctness.
Semantic analysis involves a collection of procedures which are called at
appropriate times by the parser as the grammar requires.
These semantics are clear and consistent with the way in which data types and
controls structures are supposed to be used.
A semantic analyzer will use information stored in the syntax tree and symbol table
to check the source program's semantic consistency according to the language
definition.
The output of this stage is a representation of the program in either object code in
the case of one pass compilers or intermediate representation of the program in the
case of other compilers.

4. Intermediate Code The fourth phase of a compiler is intermediate code generation. Intermediate
code is used to translate the source code into the machine code. Intermediate
Generation: code lies between the high-level language and the machine language.
If the compiler directly translates source code into the machine code without
generating intermediate code then a full native compiler is required for each
new machine. The intermediate code keeps the analysis portion same for all the
compilers that's why it doesn't need a full compiler for every unique machine.
Intermediate code generator receives input from its predecessor phase and
semantic analyzer phase. It takes input in the form of an annotated syntax tree.
5. Optimization: 6. Code Generation:
The fifth phase of a compiler is optimization. This The final phase of a compiler is code generation.
phase applies various optimization techniques to the This phase takes the optimized intermediate code
intermediate code to improve the performance of the and generates the actual machine code that can be
generated machine code. executed by the target hardware.
Compiler optimizing process should meet the Code generator converts the intermediate
following objectives : representation of source code into a form that can be
•The optimization must be correct, it must not, in any readily executed by the machine. A code generator is
way, change the meaning of the program. expected to generate the correct code. Designing of
•Optimization should increase the speed and the code generator should be done in such a way
performance of the program. that it can be easily implemented, tested, and
•The compilation time must be kept reasonable. maintained.
•The optimization process should not delay the overall
compiling process.
Conclusion
In conclusion, the different phases of compiler design play a crucial role in creating
efficient and error-free code. The lexical analysis phase breaks down the source code
into tokens, while the syntax analysis phase creates a parse tree from these tokens. The
semantic analysis phase ensures that the code follows the rules of the programming
language, and the code generation phase translates the parse tree into machine code.
Without these phases, programming would be a much more difficult and error-prone
task. By understanding the importance of compiler design, developers can create better
software faster and with fewer errors. So, let's continue to explore and learn more
about this fascinating field.

You might also like