0% found this document useful (0 votes)
24 views8 pages

Unit 1, 4

The document summarizes the main phases of a compiler: 1. Lexical analysis converts source code into tokens 2. Syntax analysis generates a parse tree from the tokens 3. Semantic analysis checks the parse tree for semantic correctness and builds a symbol table 4. Intermediate code generation outputs an abstract machine code 5. Code optimization improves the intermediate code 6. Code generation translates the optimized code into target machine instructions

Uploaded by

Pankaj Kumawat
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)
24 views8 pages

Unit 1, 4

The document summarizes the main phases of a compiler: 1. Lexical analysis converts source code into tokens 2. Syntax analysis generates a parse tree from the tokens 3. Semantic analysis checks the parse tree for semantic correctness and builds a symbol table 4. Intermediate code generation outputs an abstract machine code 5. Code optimization improves the intermediate code 6. Code generation translates the optimized code into target machine instructions

Uploaded by

Pankaj Kumawat
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
You are on page 1/ 8

Phases Of Compiler

Compiler Construction
CSE

By Himanshu Swarnkar
Engineering College Banswara
Source Program

Lexical Analyzer
Stream of token

Syntax Analyzer
Parse Tree

Semantic Analyzer
Symbol Table Parse tree
semantically verified Error Handler
Manager
Intermediate
Code Generator
Three address Code

Code
Optimization

Code Generator

Target Program
1 Lexical Analysis
The first phase of compiler is also known as Scanner. The scanner works as a text scanner.
This phase scans the source code as a stream of characters and converts it into meaningful lexemes.
Lexical analyzer represents these lexemes in the form of tokens as: <Token-name, attribute-value>
Example: Position := initial + rate*60 Lexical Analyzer

id1= id2+id3*60
2 Syntax Analysis
The next phase is called the Syntax Analysis or Parser. It takes the token produced by lexical analysis, as input and generates a
parse tree (or syntax tree).
In this phase, token arrangements are checked against the source code grammar, i.e., the parser checks if the expression made
by the tokens is syntactically correct or not.
Syntax Analyzer CFG(A →α )
S→id:=E
E→ E+T/T
:=
T→T*F/F
+ F →60
id 1

id 2 *

id 3 60

Semantic Analyzer
3 Semantic Analysis
✔ Semantic analysis checks whether the parse tree constructed thus Semantic Analyzer
follows the rules of language.
✔ For example, it checks type casting, type conversions issues and so :=
on. Also, the semantic analyzer keeps track of identifiers, their types
and expressions; whether identifiers are declared before use or not, id 1 +
etc.
id 2 *
✔ The semantic analyzer produces an annotated syntax tree as an
output.
id 3 Int to Real

Semantic Analyzer 60

Intermediate Code Generation


4 Intermediate Code Generation
✔ After semantic analysis, the compiler generates an intermediate code
of the source code for the target machine. temp1 := inttoreal(60)
✔ It represents a program for some abstract machine. temp2 := id3*temp1
✔ It is in between the high-level language and the machine language. temp3 := id2+temp2
✔ This intermediate code should be generated in such a way that it Id1:= temp3
makes it easier to be translated into the target machine code.
✔ The intermediate code may be a Three Address code or Assembly
code.
5 Code Optimization
✔ The next phase does code optimization, it is an optional phase.
✔ Optimization can be assumed as something that removes unnecessary code lines, and arranges the sequence of statements in
order to speed up the program execution without wasting resources like CPU, memory.
✔ The output of this phase is an optimized intermediate code
Code Optimization

temp1 := id3*60.0
id1:= id2*temp1

6 Code Generation In
✔ this phase, the code generator takes the optimized representation of the intermediate code and maps it to the target machine
language.
✔ The code generator translates the intermediate code into a sequence of re-locatable machine code.
✔ Sequence of instructions of machine code performs the task as the intermediate code would do.
Code Optimization

MOVF id3,R2
MULF #60.0,R2
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1
7 Symbol Table Symbol
✔ Table is also known as Book Keeping. It is a data-structure
maintained throughout all the phases of a compiler.
✔ All the identifiers‟ names along with their information like type, size,
etc., are stored here.
✔ The symbol table makes it easier for the compiler to quickly search
and retrieve the identifier's record.
✔ The symbol table is also used for scope management.

8 Error Hander
✔ A parser should be able to detect and report any error in the program.
✔ It is expected that when an error is encountered, the parser should be
able to handle it and do parsing with the rest of the inputs.
✔ Mostly it is expected from the parser to check for errors.
✔ But errors may be encountered at various stages of the compilation
process.
Bootstrapping
The process by which simple language is use to translate more complicated program, which turn many handle on even more
complicated program and so on, is known as Bootstrapping.
or
Bootstrapping is a process in which simple language is used to translate more complicated program which in turn may handle
for more complicated program.
This complicated program can further handle even more complicated program and so on.
To design a compiler bootstrapping process is use. (Any compiler is represented using tree language, S.L., T.L., I.L.)

SL TL
PL PL ML
C++ C++ M
CL CL ML
AL AL M
M

Cross Compiler: A compiler which runs on one machine but produce object code (Generally Machine code) for another
machine. Py C++ Py C++
C C M M
M
Any Query ?

Thanks

You might also like