SlideShare a Scribd company logo
2
Most read
3
Most read
4
Most read
Akhil Kaushik
Asstt. Prof., CE Deptt.,
TIT Bhiwani
Lexical Analysis
Lexical Analysis
• 1st phase of compiler.
• Part of front end of
compiler.
• It is usually implemented
as subroutine or co-
routine of parser.
Lexical Analysis
• Reads the source program as a file of characters and
divide them up into tokens.
• Tokens are recognized using Finite Automata or
Transition Diagrams.
Lexical Analysis
• The vital functions of Lexical Analyzer are:-
– Separate tokens from program
– Insert tokens into symbol table
– Eliminates whitespaces, comments, newline characters,
etc. from string.
– Give tokens to parser (pass their integer no./code to
parser)
– Keep track of line numbers (for error handling).
– Pre-processing of Macros.
Lexical Analysis
• It reads characters from the input, groups them into
lexeme and
• Then, passes the tokens formed by the lexemes,
together with their attribute value, to the parser.
• In some situations ,it has to read some characters
ahead before it can decide on the token to be
returned to the parser.
Lexical Analysis
• Ex: lexical analyzer for C must read ahead after it
sees the character ‘>’.
• If the next character is = ,then the character
sequence >= is the lexeme forming the token for
the “Greater than or equal to ” operator.
• Other wise > is the lexeme forming “Greater than ”
operator ,and the lexical analyzer has read one
character too many.
Lexical Analysis
• The extra character has to be pushed back on to the
input, because it can be the beginning of the next
lexeme in the input.
• The lexical analyzer and parser form a producer-
Consumer pair.
• Produced tokens can be held in a token buffer until
they are consumed.
Input Buffering
• The interaction b/w the two is constrained only by
the size of the buffer, because:-
– lexical analyzer can not proceed when the buffer is full.
– parser can not proceed when the buffer is empty.
• Commonly,the buffer holds just one token.
• Lexical Analyzer can be a procedure called by a
parser, returning tokens on demand.
Input Buffering
• The implementation of reading and pushing back
character is usually done by setting up an input buffer.
• A block of character is read into the buffer at a time.
• A pointer keeps track of the portion of the input that
has been analysed.
• Pushing back a character is implemented by moving
the pointer.
Input Buffering
• Source program is read from HDD.
• LA has to access secondary memory everytime it
needs to identify tokens.
• It may be time consuming, hence an input buffer
is used.
• LA scans input string from left to right one char. at
a time to identify tokens.
Input Buffering
• It uses 2 pointers:
– Begin Pointer (bptr) – points to begin of string.
– Look-ahead Pointer (lptr) – moves ahead to search for
end of token.
• Ex: int a, b;
Input Buffering
Input Buffering
Tokens, Lexemes & Patterns
• Connected with lexical analysis are three
important terms with similar meaning.
• Lexeme
• Token
• Patterns
Tokens, Lexemes & Patterns
• A token is a pair consisting of a token name and an
optional attribute value.
• Token names:
– Keywords,
– operators,
– identifiers,
– constants,
– literal strings,
– punctuation symbols(such as commas, semi-colons)
Tokens, Lexemes & Patterns
A lexeme is a sequence of characters in the
source program that matches the pattern for a
token and is identified by the lexical analyzer as
an instance of that token.
Ex: Relation Operators {<,<=,>,>=,==,<>}
Tokens, Lexemes & Patterns
• A pattern is a description of the form that the
lexemes of token may take.
• It gives an informal or formal description of a token.
Ex: identifier
• Gives a precise description/ specification of tokens.
• Used to automatically generate a lexical analyzer
Tokens, Lexemes & Patterns
Tokens
Non-Tokens
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
CONTACT ME AT:
Akhil Kaushik
akhilkaushik05@gmail.com
9416910303
THANK YOU !!!

More Related Content

PPTX
Compiler Design Unit 2
PPT
1.Role lexical Analyzer
PPT
Lexical Analysis
PPTX
Lexical analysis - Compiler Design
PPT
Lecture 1 - Lexical Analysis.ppt
PPTX
A Role of Lexical Analyzer
PDF
Lexical Analysis - Compiler design
Compiler Design Unit 2
1.Role lexical Analyzer
Lexical Analysis
Lexical analysis - Compiler Design
Lecture 1 - Lexical Analysis.ppt
A Role of Lexical Analyzer
Lexical Analysis - Compiler design

What's hot (20)

PPTX
Role-of-lexical-analysis
PDF
Lecture 01 introduction to compiler
PPT
Intermediate code generation (Compiler Design)
PPTX
Syntax Analysis in Compiler Design
PDF
Algorithms Lecture 1: Introduction to Algorithms
PDF
Code optimization in compiler design
PPTX
Phases of Compiler
PPTX
Algorithm Introduction
PPTX
Types of Parser
PPTX
Parsing in Compiler Design
PPTX
Type checking compiler construction Chapter #6
PDF
Syntactic analysis in NLP
PPTX
Compilers
PPTX
Input-Buffering
PPTX
Code Optimization
PDF
Syntax Directed Definition and its applications
PPTX
COMPILER DESIGN
PPTX
Lexical analyzer generator lex
PPTX
Recognition-of-tokens
PPT
Compiler Design
Role-of-lexical-analysis
Lecture 01 introduction to compiler
Intermediate code generation (Compiler Design)
Syntax Analysis in Compiler Design
Algorithms Lecture 1: Introduction to Algorithms
Code optimization in compiler design
Phases of Compiler
Algorithm Introduction
Types of Parser
Parsing in Compiler Design
Type checking compiler construction Chapter #6
Syntactic analysis in NLP
Compilers
Input-Buffering
Code Optimization
Syntax Directed Definition and its applications
COMPILER DESIGN
Lexical analyzer generator lex
Recognition-of-tokens
Compiler Design
Ad

Similar to Lexical Analysis - Compiler Design (20)

PDF
role of lexical parser compiler design1-181124035217.pdf
PPTX
LexicalAnalysis chapter2 i n compiler design.pptx
PPTX
Ch03-LexicalAnalysis chapter2 in compiler design.pptx
PPTX
Compiler Construction ( lexical analyzer).pptx
PPTX
Cd ch2 - lexical analysis
PDF
Lexical Analysis.pdf
PPTX
Compiler Lexical Analyzer to analyze lexemes.pptx
PPT
52232.-Compiler-Design-Lexical-Analysis.ppt
PDF
Ch03-LexicalAnalysis in compiler design subject.pdf
PPT
Chapter-2-lexical-analyser and its property lecture note.ppt
PPT
Compiler Design ug semLexical Analysis.ppt
PPT
Lecturer-05 lex anylser (1).pptrjyghsgst
PDF
COMPILER DESIGN.pdf
PPT
Data design and analysis of computing tools
PPTX
5490ce2bf23093de242ccc160dbfd3b639d.pptx
PDF
Compiler lec 8
PPTX
Chapter 2.pptx compiler design lecture note
PPT
atc 3rd module compiler and automata.ppt
PPT
LexicalAnalysis in Compiler design .pt
PPTX
Chahioiuou9oioooooooooooooofffghfpterTwo.pptx
role of lexical parser compiler design1-181124035217.pdf
LexicalAnalysis chapter2 i n compiler design.pptx
Ch03-LexicalAnalysis chapter2 in compiler design.pptx
Compiler Construction ( lexical analyzer).pptx
Cd ch2 - lexical analysis
Lexical Analysis.pdf
Compiler Lexical Analyzer to analyze lexemes.pptx
52232.-Compiler-Design-Lexical-Analysis.ppt
Ch03-LexicalAnalysis in compiler design subject.pdf
Chapter-2-lexical-analyser and its property lecture note.ppt
Compiler Design ug semLexical Analysis.ppt
Lecturer-05 lex anylser (1).pptrjyghsgst
COMPILER DESIGN.pdf
Data design and analysis of computing tools
5490ce2bf23093de242ccc160dbfd3b639d.pptx
Compiler lec 8
Chapter 2.pptx compiler design lecture note
atc 3rd module compiler and automata.ppt
LexicalAnalysis in Compiler design .pt
Chahioiuou9oioooooooooooooofffghfpterTwo.pptx
Ad

More from Akhil Kaushik (19)

PPT
Symbol Table, Error Handler & Code Generation
PPTX
Context Free Grammar
PPTX
Error Detection & Recovery
PPTX
Symbol Table
PPTX
Lexical Analyzer Implementation
PPTX
NFA & DFA
PPTX
File Handling Python
PPTX
Regular Expressions
PPTX
Algorithms & Complexity Calculation
PPTX
Intro to Data Structure & Algorithms
PPTX
Decision Making & Loops
PPTX
Basic programs in Python
PPTX
Python Data-Types
PPTX
Introduction to Python Programming
PPT
Compiler Design Basics
PPTX
Bootstrapping in Compiler
PPTX
Compiler construction tools
PPTX
Phases of compiler
PPTX
Introduction to Compilers
Symbol Table, Error Handler & Code Generation
Context Free Grammar
Error Detection & Recovery
Symbol Table
Lexical Analyzer Implementation
NFA & DFA
File Handling Python
Regular Expressions
Algorithms & Complexity Calculation
Intro to Data Structure & Algorithms
Decision Making & Loops
Basic programs in Python
Python Data-Types
Introduction to Python Programming
Compiler Design Basics
Bootstrapping in Compiler
Compiler construction tools
Phases of compiler
Introduction to Compilers

Recently uploaded (20)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Basic Mud Logging Guide for educational purpose
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Business Ethics Teaching Materials for college
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
Pre independence Education in Inndia.pdf
PDF
RMMM.pdf make it easy to upload and study
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Classroom Observation Tools for Teachers
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Institutional Correction lecture only . . .
PDF
O7-L3 Supply Chain Operations - ICLT Program
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Basic Mud Logging Guide for educational purpose
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Pharma ospi slides which help in ospi learning
Business Ethics Teaching Materials for college
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Pre independence Education in Inndia.pdf
RMMM.pdf make it easy to upload and study
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Classroom Observation Tools for Teachers
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O5-L3 Freight Transport Ops (International) V1.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Insiders guide to clinical Medicine.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Institutional Correction lecture only . . .
O7-L3 Supply Chain Operations - ICLT Program

Lexical Analysis - Compiler Design

  • 1. Akhil Kaushik Asstt. Prof., CE Deptt., TIT Bhiwani Lexical Analysis
  • 2. Lexical Analysis • 1st phase of compiler. • Part of front end of compiler. • It is usually implemented as subroutine or co- routine of parser.
  • 3. Lexical Analysis • Reads the source program as a file of characters and divide them up into tokens. • Tokens are recognized using Finite Automata or Transition Diagrams.
  • 4. Lexical Analysis • The vital functions of Lexical Analyzer are:- – Separate tokens from program – Insert tokens into symbol table – Eliminates whitespaces, comments, newline characters, etc. from string. – Give tokens to parser (pass their integer no./code to parser) – Keep track of line numbers (for error handling). – Pre-processing of Macros.
  • 5. Lexical Analysis • It reads characters from the input, groups them into lexeme and • Then, passes the tokens formed by the lexemes, together with their attribute value, to the parser. • In some situations ,it has to read some characters ahead before it can decide on the token to be returned to the parser.
  • 6. Lexical Analysis • Ex: lexical analyzer for C must read ahead after it sees the character ‘>’. • If the next character is = ,then the character sequence >= is the lexeme forming the token for the “Greater than or equal to ” operator. • Other wise > is the lexeme forming “Greater than ” operator ,and the lexical analyzer has read one character too many.
  • 7. Lexical Analysis • The extra character has to be pushed back on to the input, because it can be the beginning of the next lexeme in the input. • The lexical analyzer and parser form a producer- Consumer pair. • Produced tokens can be held in a token buffer until they are consumed.
  • 8. Input Buffering • The interaction b/w the two is constrained only by the size of the buffer, because:- – lexical analyzer can not proceed when the buffer is full. – parser can not proceed when the buffer is empty. • Commonly,the buffer holds just one token. • Lexical Analyzer can be a procedure called by a parser, returning tokens on demand.
  • 9. Input Buffering • The implementation of reading and pushing back character is usually done by setting up an input buffer. • A block of character is read into the buffer at a time. • A pointer keeps track of the portion of the input that has been analysed. • Pushing back a character is implemented by moving the pointer.
  • 10. Input Buffering • Source program is read from HDD. • LA has to access secondary memory everytime it needs to identify tokens. • It may be time consuming, hence an input buffer is used. • LA scans input string from left to right one char. at a time to identify tokens.
  • 11. Input Buffering • It uses 2 pointers: – Begin Pointer (bptr) – points to begin of string. – Look-ahead Pointer (lptr) – moves ahead to search for end of token. • Ex: int a, b;
  • 14. Tokens, Lexemes & Patterns • Connected with lexical analysis are three important terms with similar meaning. • Lexeme • Token • Patterns
  • 15. Tokens, Lexemes & Patterns • A token is a pair consisting of a token name and an optional attribute value. • Token names: – Keywords, – operators, – identifiers, – constants, – literal strings, – punctuation symbols(such as commas, semi-colons)
  • 16. Tokens, Lexemes & Patterns A lexeme is a sequence of characters in the source program that matches the pattern for a token and is identified by the lexical analyzer as an instance of that token. Ex: Relation Operators {<,<=,>,>=,==,<>}
  • 17. Tokens, Lexemes & Patterns • A pattern is a description of the form that the lexemes of token may take. • It gives an informal or formal description of a token. Ex: identifier • Gives a precise description/ specification of tokens. • Used to automatically generate a lexical analyzer
  • 18. Tokens, Lexemes & Patterns
  • 21. Akhil Kaushik akhilkaushik05@gmail.com 9416910303 CONTACT ME AT: Akhil Kaushik akhilkaushik05@gmail.com 9416910303 THANK YOU !!!