You are on page 1of 18

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Compiler Design
KCS502
by
DR PARUL YADAV

Institute of Engineering and Technology, Lucknow


Lecture 2:
LEXICAL ANALYSIS
Contents
• Phases of Compiler
• Example
• One Pass Compiler
• Multi Pass Compiler
Phases of Compiler
Phases of Compiler
1. Lexical Analysis
2. Syntax Analysis
3. Semantics Analysis
4. Intermediate Code Generation
5. Code Optimization
6. Code Generation

Lexeme: Group of characters into meaningful sequences

letter ( letter + digit )*

Token: < taken_name, attr_val >


Lexical Analysis

Main Task:
Read the input characters of the source program
Group them into lexemes
Produce as output a sequence of tokens for each lexeme
Lexical Analysis

Other Tasks:
• Stripping out comments and whitespaces
• Correlating error messages Group them into lexemes
• May make copy of the source program with error messages inserted at appropriate
positions
• Macro preprocessing etc.
Lexical Analysis versus Parsing
• Simplicity of design
• Compiler efficiency is improved
• Compiler portability is enhanced
Token, Pattern and Lexemes
Finite Automata for Identifier
Finite Automata for Operators
Finite Automata for Numbers
Examples of Tokens
Tokens
Example: Token for FORTRAN Statement
Input Buffer
Counting Tokens
Thank you

You might also like