0% found this document useful (0 votes)
9 views9 pages

Introduction To Compiler

This is based on introduction to compiler. It is based on  WHAT IS COMPILER?  TYPES OF COMPILER  DIFFERENT PHASES OF COMPILER  WORKING OF DIFFERENT PHASES WITH EXAMPLE. Studnets can take it as reference or study material as well. I had to made this document for my CA1 exam.

Uploaded by

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

Introduction To Compiler

This is based on introduction to compiler. It is based on  WHAT IS COMPILER?  TYPES OF COMPILER  DIFFERENT PHASES OF COMPILER  WORKING OF DIFFERENT PHASES WITH EXAMPLE. Studnets can take it as reference or study material as well. I had to made this document for my CA1 exam.

Uploaded by

shahilshaikh5718
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Agenda

 WHAT IS COMPILER?

 TYPES OF COMPILER

 DIFFERENT PHASES OF COMPILER

 WORKING OF DIFFERENT PHASES WITH EXAMPLE


What is a Compiler?
 A compiler is a special program that translates code written in a high-level
programming language (like C, Java) into assembly level/machine lavel
language that a computer can understand if there is no error found.
 It helps the computer to run programs written by humans.
 Example: C code is compiled into .exe files using a compiler like GCC.
 Some compilers produces output in assembly level language. On other hand
some come with built in assembler that converts the assembly into machine
level language.
Types of Compiler:
❑ Single-pass Compiler:
➢ Translates the source code in one go.
➢ Faster but less optimization.
➢ Example: Early BASIC compilers.
❑ Multi-pass Compiler:
➢ Goes through the code multiple times for better analysis and
optimization.
➢ Example: GCC (GNU Compiler Collection).
❑ Just-In-Time (JIT) Compiler:
➢ Converts code into machine language during program execution rather
than before.
➢ Example: Java’s JVM compiler.
❑ Cross Compiler:
➢ Generates code for a different machine than the one it is running on.
➢ Example: Compiling for embedded systems.
Phase of Compiler
 A compiler operate in two different phases such as a phase in a logically
interrelated operation that takes source program in one representation &
produce output in other representation.
The two phases of compiler
1. Analysis Phase :
 It reads and understands the source code.
 Checks if the program is correct in terms of grammar and meaning.
 Converts high-level code into an intermediate form.
 It is language dependent Machine Independent.
 It consists of Lexical Analysis, Syntax Analysis and Semantic Analysis.
 Lexical Analysis: It breaks the source code into tokens like keywords, identifiers,
and symbols.
 Syntax Analysis: It checks if the arrangement of tokens follows the grammar rules
of the programming language.
 Semantic Analysis: It takes the output from syntax analyzer and verifies the
meaning of the code. If no meaning is derived then error is generated.
Phase of Compiler
1. Synthesis Phase :
 It generates machine code from intermediate code.
 Also improves and optimizes the code before final output.
 It is language independent Machine dependent.
 It consists of Intermediate Code Generation, Code Optimization and Target Code
Generation.
 Intermediate Code Generation: In this phase, the compiler converts the source
code into a machine-independent representation. This phase acts as a bridge
between analysis and synthesis phase.
 Code Optimization: This phase improves the intermediate code to make it run
more efficiently, reducing resource usage or increasing speed.
 Target Code Generation: The final phase where the optimized code is translated
into the target machine code or assembly language that can be executed on
the computer.
Working of Compiler Phases with Example
 Suppose we have this as an example: value = a+b*50
1. Lexical Analysis: It reads the sequence of characters and group them into something
meaningful known as lexeme. For each lexeme lexical analysis produces an output known as
token.
value => Identifier- (id, 1)
= => Operator - Assignment
A => Identifier- (id, 2)
+ => Operator - Binary Addition
b => Identifier- (id, 3)
* => Operator - Multiplication
50 => Constant - Integer

2. Syntax Analysis (Parsing): Checks if the token sequence


follows the grammar rules of the language. Constructs a
parse tree to reflect the structure of expressions like a + b *
50
S -> Id = E
E -> E+T | T
T -> T*F | F
F -> Id | Integer constant
Working of Compiler Phases with Example
3. Semantic Analysis: Ensures that operations are meaningful: correct data types,
declared variables, etc. Example: verifies a, b, and x are declared with
compatible types; shows error if missing or mismatched.

4. Intermediate Code Generation: Converts the parsed structure into a machine-


independent intermediate form (like three-address code).

t1 = b * 50
t2 = a + t1
x = t2
Working of Compiler Phases with Example
5. Code Optimization: Improves the intermediate code for better performance and reduced memory
usage. Techniques include eliminating unused variables, simplifying expressions, constant folding,
common subexpression elimination, etc.
t1 = b * 50
t2 = a + t1 (Before)
x = t2

t1 = b* 50.0 (After Optimizing)


x = a+ t1

6. Target Code Generator: It is the last phase and In this phase it can convert the final expression into
assembly code. so, that it will be easy to understand for the processor.

MOV R1, b
MUL R1, 50.0
MOV R2, a
ADD R2, R1
MOV x, R2
Conclusion

 The proper compilation is very essential. Each phase in compiler has


it’s own significance, which is crucial for the whole compilation
process. The final code generated it the assembly code or machine
code. If it’s assembly code then it’s parsed to assembler which
generates machine code which is directly executed by CPU.
Otherwise if the compiler is integrated with the assembler then the
compiler directly generates the machine language.

You might also like