0% found this document useful (0 votes)
402 views17 pages

Compiler Design

The document discusses the phases of a compiler: 1. The compiler takes a source program written in a high-level language and converts it into an equivalent program in a target language like assembly or machine code. It also reports any errors. 2. The phases of compilation are lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. 3. Each phase performs specific tasks like breaking the source code into tokens, checking syntax, generating optimized intermediate code, and producing the final target code. Symbol tables and error handling are also used throughout the compilation process.

Uploaded by

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

Compiler Design

The document discusses the phases of a compiler: 1. The compiler takes a source program written in a high-level language and converts it into an equivalent program in a target language like assembly or machine code. It also reports any errors. 2. The phases of compilation are lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. 3. Each phase performs specific tasks like breaking the source code into tokens, checking syntax, generating optimized intermediate code, and producing the final target code. Symbol tables and error handling are also used throughout the compilation process.

Uploaded by

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

UCSC002 - COMPILER DESIGN

Phases of Compiler

PREPARED BY,

Ms. M.P.GEETHA
Assistant Professor
Department of Computer Science and Engineering
Sri Ramakrishna Institute of Technology

Compiler Design / SRIT 1


Compilers
– Translation of a program written in a source
language into a semantically equivalent program
written in a target language.
– Error Reporting

Source Program Target Program


High level language
Compiler
Assembly or Machine
Level Language

Error messages

Compiler Design / SRIT 2


Phases of Compiler

• Six phases:
– Lexical Analyzer
– Syntax Analyzer
– Semantic Analyzer
– Intermediate code generation
– Code optimization
– Code Generation

• Symbol table and error handling

Compiler Design / SRIT 3


SOURCE PROGRAM

PHASES OF COMPILER Lexical Analyzer

Syntax Analyzer

Semantic Analyzer

Symbol Table Error Handler


Manager
Intermediate Code
Generator

Code Optimizer

Code Generator
TARGET PROGRAM

Compiler Design / SRIT 4


LEXICAL ANALYSIS
- Lexical Analyzer or Linear Analyzer or scanning
reads the characters in the source program breaks the
group of strings into tokens.
- Token represents a sequence of characters, such as,
An identifier, A keyword, A punctuation character, A
Number.
- The character sequence forming a token is called the
lexeme for the token.
Example:
position = initial + rate * 60

Compiler Design / SRIT 5


Example :-

position = initial + rate * 60

Would be grouped into the following tokens:

1. Identifier position.
2. Assignment symbol =
3. Identifier initial.
4. Operator +.
5. Identifier rate.
6. Multiplication sign. *
7. Number 60

gets converted to  id1 = id2 +id3*60

Compiler Design / SRIT 6


SYNTAX ANALYSIS
Syntax analysis is also called PARSING.
It takes the token produced by lexical analysis as input
and generates a parse tree (or syntax tree).
It checks the code syntax using CFG (Context Free
Grammar),the set of rules .
Parse Tree:

id1 = id2 +id3*60

Compiler Design / SRIT 7


SEMANTIC ANALYSIS
The semantic analysis phase checks source program for
semantic errors and gathers type information and type
compatibility for the subsequent code-generation phase.
Type checking - where compiler checks that each operator
has matching operands.
It uses syntax tree and symbol table to check the source
program for semantic consistency.

Compiler Design / SRIT 8


INTERMEDIATE CODE GENERATION

- The Intermediate representation should have two


important properties:

• It should be easy to produce.


• And easy to translate into target program.

- Intermediate representation can have a variety of forms


like Quadruple, Triple, Three Address Code.

Compiler Design / SRIT 9


INTERMEDIATE CODE GENERATION
- One of the forms is: three address code; which is
like the assembly language for a machine in which
every location can act like a register.
- Three address code consists of a sequence of
instructions, each of which has at most three
operands.
Example:
temp1=inttoreal(60)
temp2= id3*temp1
temp3=id2+temp2
id1=temp3

Compiler Design / SRIT 10


CODE OPTIMIZATION
The code optimization phase attempts to improves the
intermediate code, so that faster-running machine code
result.
Optimized Code is

temp1=inttoreal(60)
temp1=id3*60.0 temp2= id3*temp1
id1=id2+temp1 temp3=id2+temp2
id1=temp3

Code Optimization significantly improve the running


time of the target program with out slowing down the
compilation too much.

Compiler Design / SRIT 11


CODE GENERATION
• The Final phase of the compiler is the generation of the
target code
• The Intermediate code instructions are converted into the
re-locatable machine code or assembly code.

MOVF id3, R2 temp1=id3*60.0


MULF #60.0, R2 id1=id2+temp1
MOVF id2, R1
ADDF R2, R1
MOVF R1, id1

Compiler Design / SRIT 12


Symbol Table Management
- Symbol table is a data structure used to store
information about the identifier
- its type, ( semantic and intermediate code)
- its scope, ( semantic and intermediate code)
- storage allocation, ( code generation)

Position id1 &Attributes

Initial id2 &Attributes

Rate id3 &Attributes

Compiler Design / SRIT 13


Error Handler

- Each phase encounters errors.


- Lexical phase determine the input that do not
form token.
- Syntax phase determine the token that violates
the syntax rule.
- Semantic phase detects the type mismatch kind of
error.

Compiler Design / SRIT 14


Example

Scanner [Intermediate Code Generator]


[Lexical Analyzer]

Parser
[Syntax Analyzer]
Code Optimizer

Semantic Process Code Generator


[Semantic analyzer]

Compiler Design / SRIT 15


REFERENCES

1.Alfred V Aho, Monica S.Larn Ravi Sethi and Jeffrey D Ullman,


"Compilers - Principles, Techniques and Tools", 2nd Edition, Pearson
Education, 2007.
2.Randy Allen, Ken Kennedy, “Optimizing Compilers for Modern
Architectures: A Dependence-based Approach”, Morgan Kaufmann
Publishers, 2002.
3.Steven S. Muchnick, “Advanced Compiler Design and Implementation,
“Morgan Kaufmann Publishers - Elsevier Science, India, Indian Reprint
2003.
4.Keith D Cooper and Linda Torczon, “Engineering a Compiler”, Morgan
Kaufmann Publishers Elsevier Science, 2004.
5.Charles N. Fischer, Richard. J. LeBlanc, “Crafting a Compiler with C”,
Pearson Education, 2008.
 

Compiler Design / SRIT 16


WEB RESOURCES

http://web.stanford.edu/class/cs143/handouts/introlex.pdf
http://dragonbook.stanford.edu/lecture-notes.html
http://www.nptel.ac.in/courses/106104072/3

Compiler Design / SRIT 17

You might also like