SlideShare a Scribd company logo
1 of 39
1
Jeena Thomas, Asst Professor, CSE,
Ā» The scanning/lexical analysis phase of a
compiler performs the task of reading the
source program as a file of characters and
dividing up into tokens.
Jeena Thomas, Asst Professor, CSE,
Ā» Usually implemented as subroutine or co-
routine of parser.
Ā» Front end ofcompiler.
2
Ā» Each token is a sequence of characters that
represents a unit of information in the source
program.
Jeena Thomas, Asst Professor, CSE,
3
Ā» Keywords which are fixed string of letters .eg:
ā€œifā€, ā€œwhileā€.
Jeena Thomas, Asst Professor, CSE,
Ā» Identifiers which are user-defined
strings composed of letters and
numbers.
Ā» Special symbols like arithmetic
symbols.
4
Ā» Scanners perform pattern matching process.
Ā» The techniques used to implement lexical
analyzers can also be applied to other areas
such as query languages and information
retrieval systems.
Jeena Thomas, Asst Professor, CSE,
Ā» Since pattern directed programming is
widely useful, pattern action language
called Lex for specifying lexical
analyzers.
Ā» In lex , patterns are specified by regular
expressions, and a compiler for lex can
generate an efficient finite-automaton
recognizer for the regular expression.
5
Ā» A software tool that automates the
construction of lexical analyzers allows
people with different backgrounds to
use pattern matching in their own
areas.
Jeena Thomas, Asst Professor, CSE,
Ā» Jarvis[1976] Lexical analyzer generator to
create a program that recognizes
imperfections in
printed circuit boards.
Ā» The circuits are digitally scanned and
converted into ā€œstringsā€ of line
segments at different angles.
Ā» The ā€œlexical analyzerā€ looked for patterns
corresponding to imperfections in the
string of line segments.
6
Ā» It can utilize the best-known pattern-
matching algorithms and thereby create
efficient lexical analyzers for people who
are not experts in pattern-matching
techniques.
Jeena Thomas, Asst Professor, CSE,
7
Winter
2007
S
E
G
2
10
1
C
h
ap
8
8
Ā» Lexical analyzer is the first phase of a
compiler.Ā» Its main task is to read input characters and
produce as output a sequence of tokens that
parser uses for syntax analysis.
9
Jeena Thomas, Asst Professor, CSE,
Type Examples
ID foo n_14 last
NUM 73 00 517 082
REAL 66.1 .5 10. 1e67 5.5e-10
IF if
COMMA ,
NOTEQ !=
LPAREN (
RPAREN )
Type Examples
comment /* ignored */
preprocessor directive #include <foo.h>
#define NUMS 5, 6
macro NUMS
whitespace t n b
Ā» Separation of the input source code into
tokens.
Jeena Thomas, Asst Professor, CSE,
Ā» Stripping out the unnecessary white
spaces from the source code.
Ā» Removing the comments from the source
text.
Ā» Keeping track of line numbers while
scanning the new line characters. These line
numbers are used by the error handler to
print the error messages.
Ā» Preprocessing ofmacros.
12
Ā» There are several reasons for separating
the analysis phase of compiling into
lexical analysis and parsing:
Jeena Thomas, Asst Professor, CSE,
Ā» It leads to simpler design of the parser as
the unnecessary tokens can be eliminated by
scanne
r.
Ā» Efficiency of the process of compilation is
improved. The lexical analysis phase is most
time consuming phase in compilation. Using
specialized buffering to improve the speed
of compilation.
Ā» Portability of the compiler is enhanced as
the specialized symbols and
13
Ā» Connected with lexical analysis are three
important terms with similar meaning.
Jeena Thomas, Asst Professor, CSE,
Ā»
Lexeme
Ā» Token
Ā»
Patterns
14
Ā» A token is a pair consisting of a token name
and an optional attribute value. Token
name: Keywords, operators, identifiers, constants,
literal strings, punctuation symbols(such as
commas,semicolons)
Ā» 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. E.g.Relation
Jeena Thomas, Asst Professor, CSE,
{<.<=,>,>=,==,
<>}
15
Ā» A pattern is a description of the form
that the lexemes of token may take.
Jeena Thomas, Asst Professor, CSE,
Ā» It gives an informal or formal
description of a token.
Ā» Eg:identifier
Ā» 2purposes
Ā» Gives a precise description/
specification of tokens.
Ā» Used to automatically generate a
lexical analyzer
16
17
Ā» const pi = 3.1416;
Ā» The substring piis a lexeme for the
token
ā€œidentifier.ā€
Jeena Thomas, Asst Professor, CSE,
Ā»
x=x*(acc+123)
Jeena Thomas, Asst Professor, CSE,
18
19
Jeena Thomas, Asst Professor, CSE,
Ā» 1.) let us consider a statement ā€œfi(a==f)ā€. Here
ā€œfiā€ is a misspelled keyword. This error is
not detected in lexical analysis as ā€œfiā€ is
taken as an identifier. This error is then
detected in other phases of compilation.
Jeena Thomas, Asst Professor, CSE,
Ā» 2.) in case the lexical analyzer is not
able to continue with the process of
compilation, it resorts to panic mode
of error recovery.
ā€¢ Deleting the successive characters
from the remaining input until a token is
detected.
ā€¢ Deleting extraneous characters.
20
ā€¢ Inserting missing characters
ā€¢ Replacing an incorrect character by a
correct character.
ā€¢ Transposing two adjacent characters
Jeena Thomas, Asst Professor, CSE,
21
Ā» Is the strategy generally followed by the
lexical analyzer to correct the errors
in the lexemes.
Jeena Thomas, Asst Professor, CSE,
Ā» It is nothing but the minimum number of
the corrections to be made to convert
an invalid lexeme to a valid lexeme.
Ā» But it is not generally used in practice
because it is too costly to implement.
22
23
Jeena Thomas, Asst Professor, CSE,
Ā» Scanners are special pattern
matching processors.
Jeena Thomas, Asst Professor, CSE,
Ā» For representing patterns of strings of
characters, Regular Expressions(RE)
are used.
Ā» A regular expression (r) is defined by
set of strings that matches it.
Ā» This set is called as the language
generated by the regular expression and
is represented as L(r).
Ā» The set of symbols in the language is
called the
alphabet of the language is represented as
24
Ā» An alphabet is a finite set of
symbols.
Jeena Thomas, Asst Professor, CSE,
Ā» Example
Ā» A set of alphabetic characters is represented
as L={A,ā€¦,Z,a,ā€¦,z} and set of digits is
represented as D={0,1,ā€¦,9}.
Ā» LUD is alanguage.
Ā» Strings over LUD- Begin,Max1, max1, 123,
ā‚¬ā€¦
25
26
Jeena Thomas, Asst Professor, CSE,
Ā»
Intersection
Jeena Thomas, Asst Professor, CSE,
Ā» Lāˆ©M={ s|s is in L and S is in
M}
Ā» ExponentiationĀ» Li =L Li-
1
27
Ā» Choice among
alternates
Jeena Thomas, Asst Professor, CSE,
Ā»
Concatenation
Ā» Repetition
28
Ā» Indicated by metacharacter ā€˜|ā€™(vertical
bar)
Jeena Thomas, Asst Professor, CSE,
Ā» r|s
Ā» R.E that matches any string that is matched
either by r or s.
Ā» L(r|s)=L(r) U L(s)
29
Ā» Consider
L(r)={a},
Jeena Thomas, Asst Professor, CSE,
Ā» L(s)={b},
Ā» L(t)= {ā‚¬},
Ā» L(u)= {}.
Ā» What do the following R.E
represent?
Ā» (i)r|s
Ā» (ii)r|t
Ā» (iii)r|u
30
Ā»
rs
Jeena Thomas, Asst Professor, CSE,
Ā» It matches any string that is a concatenation
of 2 strings, the first of which matches r
and second of which matches s.
Ā» L(rs) = L(r)
L(s)
31
Ā» Consider L(r) ={a}, L(s)={b}, L(t) ={ā‚¬},
L(u)={ }, L(v)={c}. What do following R.E
represent?
Jeena Thomas, Asst Professor, CSE,
Ā» (i)rs
Ā» (ii)rt
Ā» (iii)ru
Ā» (iv)
(r|s)v
32
Ā» Also called Kleene
closureĀ» Represents any finite concatenation of
strings each matches strings from L(r).
Ā» r*
Ā» Let S={a}, then L(a*)={ā‚¬, a, aa,aaa,ā€¦}
Ā» S*={ā‚¬}USUSSUSSSUā€¦.=
Ā»
33
Jeena Thomas, Asst Professor, CSE,
Ā» Consider L(r)=
{a},
Jeena Thomas, Asst Professor, CSE,
Ā» L(s)={b}.
Ā» What do the following R.E
represent?
Ā» (i)r*
Ā» (ii) (rs)*
Ā» (iii)(r|s)*
Ā» (iv)(r|ss)*
34
Ā» Repetition--------------
(highest) left
associative
Ā» Concatenation
Ā» Choice-------------------
(lowest)
35
Jeena Thomas, Asst Professor, CSE,
Ā» One or more instances:
(r)+
Jeena Thomas, Asst Professor, CSE,
Ā» Zero of one instances:
r?
Ā» Zero or more instances:
r*
36
37
Jeena Thomas, Asst Professor, CSE,
38
Jeena Thomas, Asst Professor, CSE,
Ā» Regular Expression(RE): represents
pattern of string of characters.
Jeena Thomas, Asst Professor, CSE,
Ā» Language (L(r)): set ofstrings
Ā» Alphabet(āˆ‘): set of symbols
Ā» Meta character: is a special character (not a
part of āˆ‘) used in R.E eg: *, | etc
Ā» Basic R.E: R.E consisting of only one
character
Ā» Regular language: language defined by RE
39

More Related Content

What's hot

Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysisIffat Anjum
Ā 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
Ā 
Control structure
Control structureControl structure
Control structurebaran19901990
Ā 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzerkiran acharya
Ā 
Application of HCR's Rank Formula on color property of articles
Application of HCR's Rank Formula on color property of articlesApplication of HCR's Rank Formula on color property of articles
Application of HCR's Rank Formula on color property of articlesHarish Chandra Rajpoot
Ā 
3. Lexical analysis
3. Lexical analysis3. Lexical analysis
3. Lexical analysisSaeed Parsa
Ā 
Propositional logic(part 2)
Propositional logic(part 2)Propositional logic(part 2)
Propositional logic(part 2)SURBHI SAROHA
Ā 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxDrIsikoIsaac
Ā 
Finite state system or finite automata
Finite state system or finite automataFinite state system or finite automata
Finite state system or finite automataDr. ABHISHEK K PANDEY
Ā 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
Ā 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
Ā 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler designRiazul Islam
Ā 
Chapter2CDpdf__2021_11_26_09_19_08.pdf
Chapter2CDpdf__2021_11_26_09_19_08.pdfChapter2CDpdf__2021_11_26_09_19_08.pdf
Chapter2CDpdf__2021_11_26_09_19_08.pdfDrIsikoIsaac
Ā 
SS UI Lecture 4
SS UI Lecture 4SS UI Lecture 4
SS UI Lecture 4Avinash Kapse
Ā 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammarmeresie tesfay
Ā 

What's hot (20)

Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
Ā 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
Ā 
Control structure
Control structureControl structure
Control structure
Ā 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Ā 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ā 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
Ā 
Application of HCR's Rank Formula on color property of articles
Application of HCR's Rank Formula on color property of articlesApplication of HCR's Rank Formula on color property of articles
Application of HCR's Rank Formula on color property of articles
Ā 
Lexical
LexicalLexical
Lexical
Ā 
3. Lexical analysis
3. Lexical analysis3. Lexical analysis
3. Lexical analysis
Ā 
Propositional logic(part 2)
Propositional logic(part 2)Propositional logic(part 2)
Propositional logic(part 2)
Ā 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
Ā 
Finite state system or finite automata
Finite state system or finite automataFinite state system or finite automata
Finite state system or finite automata
Ā 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
Ā 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
Ā 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Ā 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
Ā 
Chapter2CDpdf__2021_11_26_09_19_08.pdf
Chapter2CDpdf__2021_11_26_09_19_08.pdfChapter2CDpdf__2021_11_26_09_19_08.pdf
Chapter2CDpdf__2021_11_26_09_19_08.pdf
Ā 
SS UI Lecture 4
SS UI Lecture 4SS UI Lecture 4
SS UI Lecture 4
Ā 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
Ā 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
Ā 

Similar to Lexical analysis

Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compilerSudhaa Ravi
Ā 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignAnujashejwal
Ā 
Compiler Design
Compiler Design Compiler Design
Compiler Design waqar ahmed
Ā 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzerPrincess Doll
Ā 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)bolovv
Ā 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical AnalyzerArchana Gopinath
Ā 
Lexical Analysis.pdf
Lexical Analysis.pdfLexical Analysis.pdf
Lexical Analysis.pdfBiswanathSethi2
Ā 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptxObsa2
Ā 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptxwoldu2
Ā 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler DesignKuppusamy P
Ā 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdfTANZINTANZINA
Ā 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysisSudhaa Ravi
Ā 
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...Reddyjanardhan221
Ā 
Using Static Analysis in Program Development
Using Static Analysis in Program DevelopmentUsing Static Analysis in Program Development
Using Static Analysis in Program DevelopmentPVS-Studio
Ā 

Similar to Lexical analysis (20)

Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
Ā 
Compiler construction
Compiler constructionCompiler construction
Compiler construction
Ā 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Ā 
Compiler Design
Compiler Design Compiler Design
Compiler Design
Ā 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ā 
Chapter Three(1)
Chapter Three(1)Chapter Three(1)
Chapter Three(1)
Ā 
Parsing
ParsingParsing
Parsing
Ā 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
Ā 
Lexical Analysis.pdf
Lexical Analysis.pdfLexical Analysis.pdf
Lexical Analysis.pdf
Ā 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptx
Ā 
Pcd question bank
Pcd question bank Pcd question bank
Pcd question bank
Ā 
Ch 2.pptx
Ch 2.pptxCh 2.pptx
Ch 2.pptx
Ā 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Ā 
3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf3a. Context Free Grammar.pdf
3a. Context Free Grammar.pdf
Ā 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
Ā 
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
WINSEM2022-23_CSI2005_TH_VL2022230504110_Reference_Material_II_22-12-2022_1.2...
Ā 
Adv. python regular expression by Rj
Adv. python regular expression by RjAdv. python regular expression by Rj
Adv. python regular expression by Rj
Ā 
Syntax
SyntaxSyntax
Syntax
Ā 
Using Static Analysis in Program Development
Using Static Analysis in Program DevelopmentUsing Static Analysis in Program Development
Using Static Analysis in Program Development
Ā 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
Ā 

Recently uploaded

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
Ā 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
Ā 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
Ā 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
Ā 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
Ā 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
Ā 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
Ā 
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”soniya singh
Ā 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
Ā 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
Ā 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
Ā 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
Ā 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
Ā 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
Ā 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
Ā 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
Ā 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
Ā 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
Ā 

Recently uploaded (20)

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
Ā 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
Ā 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
Ā 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Ā 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
Ā 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
Ā 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Ā 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
Ā 
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Model Call Girl in Narela Delhi reach out to us at šŸ”8264348440šŸ”
Ā 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
Ā 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
Ā 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Ā 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
Ā 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
Ā 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
Ā 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Ā 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
Ā 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
Ā 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
Ā 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Ā 

Lexical analysis

  • 1. 1 Jeena Thomas, Asst Professor, CSE,
  • 2. Ā» The scanning/lexical analysis phase of a compiler performs the task of reading the source program as a file of characters and dividing up into tokens. Jeena Thomas, Asst Professor, CSE, Ā» Usually implemented as subroutine or co- routine of parser. Ā» Front end ofcompiler. 2
  • 3. Ā» Each token is a sequence of characters that represents a unit of information in the source program. Jeena Thomas, Asst Professor, CSE, 3
  • 4. Ā» Keywords which are fixed string of letters .eg: ā€œifā€, ā€œwhileā€. Jeena Thomas, Asst Professor, CSE, Ā» Identifiers which are user-defined strings composed of letters and numbers. Ā» Special symbols like arithmetic symbols. 4
  • 5. Ā» Scanners perform pattern matching process. Ā» The techniques used to implement lexical analyzers can also be applied to other areas such as query languages and information retrieval systems. Jeena Thomas, Asst Professor, CSE, Ā» Since pattern directed programming is widely useful, pattern action language called Lex for specifying lexical analyzers. Ā» In lex , patterns are specified by regular expressions, and a compiler for lex can generate an efficient finite-automaton recognizer for the regular expression. 5
  • 6. Ā» A software tool that automates the construction of lexical analyzers allows people with different backgrounds to use pattern matching in their own areas. Jeena Thomas, Asst Professor, CSE, Ā» Jarvis[1976] Lexical analyzer generator to create a program that recognizes imperfections in printed circuit boards. Ā» The circuits are digitally scanned and converted into ā€œstringsā€ of line segments at different angles. Ā» The ā€œlexical analyzerā€ looked for patterns corresponding to imperfections in the string of line segments. 6
  • 7. Ā» It can utilize the best-known pattern- matching algorithms and thereby create efficient lexical analyzers for people who are not experts in pattern-matching techniques. Jeena Thomas, Asst Professor, CSE, 7
  • 8. Winter 2007 S E G 2 10 1 C h ap 8 8 Ā» Lexical analyzer is the first phase of a compiler.Ā» Its main task is to read input characters and produce as output a sequence of tokens that parser uses for syntax analysis.
  • 9. 9 Jeena Thomas, Asst Professor, CSE,
  • 10. Type Examples ID foo n_14 last NUM 73 00 517 082 REAL 66.1 .5 10. 1e67 5.5e-10 IF if COMMA , NOTEQ != LPAREN ( RPAREN )
  • 11. Type Examples comment /* ignored */ preprocessor directive #include <foo.h> #define NUMS 5, 6 macro NUMS whitespace t n b
  • 12. Ā» Separation of the input source code into tokens. Jeena Thomas, Asst Professor, CSE, Ā» Stripping out the unnecessary white spaces from the source code. Ā» Removing the comments from the source text. Ā» Keeping track of line numbers while scanning the new line characters. These line numbers are used by the error handler to print the error messages. Ā» Preprocessing ofmacros. 12
  • 13. Ā» There are several reasons for separating the analysis phase of compiling into lexical analysis and parsing: Jeena Thomas, Asst Professor, CSE, Ā» It leads to simpler design of the parser as the unnecessary tokens can be eliminated by scanne r. Ā» Efficiency of the process of compilation is improved. The lexical analysis phase is most time consuming phase in compilation. Using specialized buffering to improve the speed of compilation. Ā» Portability of the compiler is enhanced as the specialized symbols and 13
  • 14. Ā» Connected with lexical analysis are three important terms with similar meaning. Jeena Thomas, Asst Professor, CSE, Ā» Lexeme Ā» Token Ā» Patterns 14
  • 15. Ā» A token is a pair consisting of a token name and an optional attribute value. Token name: Keywords, operators, identifiers, constants, literal strings, punctuation symbols(such as commas,semicolons) Ā» 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. E.g.Relation Jeena Thomas, Asst Professor, CSE, {<.<=,>,>=,==, <>} 15
  • 16. Ā» A pattern is a description of the form that the lexemes of token may take. Jeena Thomas, Asst Professor, CSE, Ā» It gives an informal or formal description of a token. Ā» Eg:identifier Ā» 2purposes Ā» Gives a precise description/ specification of tokens. Ā» Used to automatically generate a lexical analyzer 16
  • 17. 17 Ā» const pi = 3.1416; Ā» The substring piis a lexeme for the token ā€œidentifier.ā€ Jeena Thomas, Asst Professor, CSE,
  • 19. 19 Jeena Thomas, Asst Professor, CSE,
  • 20. Ā» 1.) let us consider a statement ā€œfi(a==f)ā€. Here ā€œfiā€ is a misspelled keyword. This error is not detected in lexical analysis as ā€œfiā€ is taken as an identifier. This error is then detected in other phases of compilation. Jeena Thomas, Asst Professor, CSE, Ā» 2.) in case the lexical analyzer is not able to continue with the process of compilation, it resorts to panic mode of error recovery. ā€¢ Deleting the successive characters from the remaining input until a token is detected. ā€¢ Deleting extraneous characters. 20
  • 21. ā€¢ Inserting missing characters ā€¢ Replacing an incorrect character by a correct character. ā€¢ Transposing two adjacent characters Jeena Thomas, Asst Professor, CSE, 21
  • 22. Ā» Is the strategy generally followed by the lexical analyzer to correct the errors in the lexemes. Jeena Thomas, Asst Professor, CSE, Ā» It is nothing but the minimum number of the corrections to be made to convert an invalid lexeme to a valid lexeme. Ā» But it is not generally used in practice because it is too costly to implement. 22
  • 23. 23 Jeena Thomas, Asst Professor, CSE,
  • 24. Ā» Scanners are special pattern matching processors. Jeena Thomas, Asst Professor, CSE, Ā» For representing patterns of strings of characters, Regular Expressions(RE) are used. Ā» A regular expression (r) is defined by set of strings that matches it. Ā» This set is called as the language generated by the regular expression and is represented as L(r). Ā» The set of symbols in the language is called the alphabet of the language is represented as 24
  • 25. Ā» An alphabet is a finite set of symbols. Jeena Thomas, Asst Professor, CSE, Ā» Example Ā» A set of alphabetic characters is represented as L={A,ā€¦,Z,a,ā€¦,z} and set of digits is represented as D={0,1,ā€¦,9}. Ā» LUD is alanguage. Ā» Strings over LUD- Begin,Max1, max1, 123, ā‚¬ā€¦ 25
  • 26. 26 Jeena Thomas, Asst Professor, CSE,
  • 27. Ā» Intersection Jeena Thomas, Asst Professor, CSE, Ā» Lāˆ©M={ s|s is in L and S is in M} Ā» ExponentiationĀ» Li =L Li- 1 27
  • 28. Ā» Choice among alternates Jeena Thomas, Asst Professor, CSE, Ā» Concatenation Ā» Repetition 28
  • 29. Ā» Indicated by metacharacter ā€˜|ā€™(vertical bar) Jeena Thomas, Asst Professor, CSE, Ā» r|s Ā» R.E that matches any string that is matched either by r or s. Ā» L(r|s)=L(r) U L(s) 29
  • 30. Ā» Consider L(r)={a}, Jeena Thomas, Asst Professor, CSE, Ā» L(s)={b}, Ā» L(t)= {ā‚¬}, Ā» L(u)= {}. Ā» What do the following R.E represent? Ā» (i)r|s Ā» (ii)r|t Ā» (iii)r|u 30
  • 31. Ā» rs Jeena Thomas, Asst Professor, CSE, Ā» It matches any string that is a concatenation of 2 strings, the first of which matches r and second of which matches s. Ā» L(rs) = L(r) L(s) 31
  • 32. Ā» Consider L(r) ={a}, L(s)={b}, L(t) ={ā‚¬}, L(u)={ }, L(v)={c}. What do following R.E represent? Jeena Thomas, Asst Professor, CSE, Ā» (i)rs Ā» (ii)rt Ā» (iii)ru Ā» (iv) (r|s)v 32
  • 33. Ā» Also called Kleene closureĀ» Represents any finite concatenation of strings each matches strings from L(r). Ā» r* Ā» Let S={a}, then L(a*)={ā‚¬, a, aa,aaa,ā€¦} Ā» S*={ā‚¬}USUSSUSSSUā€¦.= Ā» 33 Jeena Thomas, Asst Professor, CSE,
  • 34. Ā» Consider L(r)= {a}, Jeena Thomas, Asst Professor, CSE, Ā» L(s)={b}. Ā» What do the following R.E represent? Ā» (i)r* Ā» (ii) (rs)* Ā» (iii)(r|s)* Ā» (iv)(r|ss)* 34
  • 35. Ā» Repetition-------------- (highest) left associative Ā» Concatenation Ā» Choice------------------- (lowest) 35 Jeena Thomas, Asst Professor, CSE,
  • 36. Ā» One or more instances: (r)+ Jeena Thomas, Asst Professor, CSE, Ā» Zero of one instances: r? Ā» Zero or more instances: r* 36
  • 37. 37 Jeena Thomas, Asst Professor, CSE,
  • 38. 38 Jeena Thomas, Asst Professor, CSE,
  • 39. Ā» Regular Expression(RE): represents pattern of string of characters. Jeena Thomas, Asst Professor, CSE, Ā» Language (L(r)): set ofstrings Ā» Alphabet(āˆ‘): set of symbols Ā» Meta character: is a special character (not a part of āˆ‘) used in R.E eg: *, | etc Ā» Basic R.E: R.E consisting of only one character Ā» Regular language: language defined by RE 39