SlideShare una empresa de Scribd logo
1 de 12
Descargar para leer sin conexión
Lexical Analysis
Lecture 3
Lexical Analysis
Introduction
• For an English sentence:
– Read the sentence string
– Recognize its parts of speech – noun, verb, adjective,
etc…
– Match these parts of speech against a grammar
• For computer languages:
– Extract the words (lexemes) of the input string
– Classify them according to their roles (parts of speech)
• This is tokenization – finding token classes
– Lexical analyzer recognizes the string by matching
these tokens against a grammar
8-Jan-15 2CS 346 Lecture 3
• Lexical Structure of a programming language
consists of a set of token classes
– Operators, whitespaces, keywords, identifiers,
numbers, (, ), ;, = etc…
• Lexical Analysis
Introduction
• Lexical Analysis
– Classify the program substrings into various token
classes and communicate them to the parser.
• Goal – Given the specification for token classes
of a language, automatically construct a lexical
analyzer
8-Jan-15 3CS 346 Lecture 3
Regular Expressions
• Each token class consists of a set of strings.
• A distinct set of strings constitute a language over an alphabet .
– Alphabet: A finite set of symbols by which the strings of the language
may be formed
• They are regular languages (recognized by FA)
• Regular languages – defined by regular expressions (RE)
• RE’s denote the set of strings recognized by a language by defining
their pattern
• Automation steps:
– RE NFA DFA Table-Driven Implementation
8-Jan-15 4CS 346 Lecture 3
• Two basic RE’s
– Single character RE – eg: ‘B’: A language containing
one string which is the single character ‘B’
– Epsilon RE – ɛ : A language which contains one
string, the empty string.
Regular Expressions
• Three compound RE’s
– Union – eg.: A + B or A | B (either A or B)
– Concatenation – eg.: AB {ab | a Є AΛ b Є B}
– Iteration – eg.: A* – 0 or more occurrences of A
• A0 = ɛ
• (A) has the same meaning as A
Regular Language (Set)
• The regular expression over alphabet Σ are the
smallest set of expressions including
R = ɛ
| ‘c’ for c Є Σ (alphabet)| ‘c’ for c Є Σ (alphabet)
| R + R (union)
| RR (concatenation)
| R* (iteration)
• Operator precedence: (A), A*, AB, A|B
– So ab*c|d is parsed as ((a(b*))c)|d
• Describe the languages denoted by the following
REs when alphabet (Σ) = {0,1}
– 1*; (1+0)1; 0*+1*; 01(01)*, (0+1)*, 11(0+1)*
Regular Expressions
– 1*; (1+0)1; 0*+1*; 01(01)*, (0+1)*, 11(0+1)*
• Short-hands:
– Character range: [a-d] for a | b | c | d;
– Atleast one: r+ for rr*;
– Option: r? for r | e
– Excluded range: [^a-z] = Complement of [a-z]
• Write RE’s for:
– Strings of 0’s and 1’s containing 00 as a substring
Regular Expressions
• Write RE’s that:
– Recognizes our email addresses at IITG
– Recognizes a number consisting of: One or more
digits, followed by an optional fractional part,
followed by an optional exponent.
Lexical Specification of a Language
1. Write REs for the lexemes of each token class:
Number = digit+
Keyword = ‘if’ + ‘else’ + …
Identifier = letter (letter + digit)*Identifier = letter (letter + digit)*
…
2. Construct R, matching all lexemes for all tokens:
R = Keyword + Identifier + Number + …
= R1 + R2 + …
1. Let input be x1…xn
For 1 <= i <= n check
x1…xi Є L(R)
Lexical Specification of a Language
2. If success, then we know that:
x1…xi Є L(Rj) for some j
3. Remove x1…xi from input and go to (3)
• How much of the input to use?
– x1 …xi Є L(R)
– x1…xj Є L(R), i ≠ j
Solution: “Maximal Munch”
• What if more than one token matches?
Lexical Specification of a Language
• What if more than one token matches?
– x1 …xi Є L(Rx)
– x1…xi Є L(Ry)
– Solution: Priority ordering – Choose the one listed first
• What to do when no rule matches?
– Solution: Make special token class / classes for all error
strings. Put it last in the priority list.
Lecture3 lexical analysis

Más contenido relacionado

La actualidad más candente (20)

Lexical Analysis - Compiler design
Lexical Analysis - Compiler design Lexical Analysis - Compiler design
Lexical Analysis - Compiler design
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
A Role of Lexical Analyzer
A Role of Lexical AnalyzerA Role of Lexical Analyzer
A Role of Lexical Analyzer
 
2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt2_2Specification of Tokens.ppt
2_2Specification of Tokens.ppt
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
4 lexical and syntax analysis
4 lexical and syntax analysis4 lexical and syntax analysis
4 lexical and syntax analysis
 
Lexical
LexicalLexical
Lexical
 
Compiler lec 8
Compiler lec 8Compiler lec 8
Compiler lec 8
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
 
Lexical analysis
Lexical analysisLexical analysis
Lexical analysis
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
Token, Pattern and Lexeme
Token, Pattern and LexemeToken, Pattern and Lexeme
Token, Pattern and Lexeme
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Lecture4 lexical analysis2
Lecture4 lexical analysis2Lecture4 lexical analysis2
Lecture4 lexical analysis2
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
Lex
LexLex
Lex
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 

Destacado

13. Constantin Orasan (UoW) Natural Language Processing for Translation
13. Constantin Orasan (UoW) Natural Language Processing for Translation13. Constantin Orasan (UoW) Natural Language Processing for Translation
13. Constantin Orasan (UoW) Natural Language Processing for TranslationRIILP
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionSarmad Ali
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compilerMahesh Kumar Chelimilla
 
Symbolic Automata = Automata + SMT solvers at ExCape14
Symbolic Automata = Automata + SMT solvers at ExCape14Symbolic Automata = Automata + SMT solvers at ExCape14
Symbolic Automata = Automata + SMT solvers at ExCape14Loris D'Antoni
 
DFA minimization algorithms in map reduce
DFA minimization algorithms in map reduceDFA minimization algorithms in map reduce
DFA minimization algorithms in map reduceIraj Hedayati
 
Nltk:a tool for_nlp - py_con-dhaka-2014
Nltk:a tool for_nlp - py_con-dhaka-2014Nltk:a tool for_nlp - py_con-dhaka-2014
Nltk:a tool for_nlp - py_con-dhaka-2014Fasihul Kabir
 
نموذج مسار هندسة الأفكار
نموذج مسار هندسة الأفكارنموذج مسار هندسة الأفكار
نموذج مسار هندسة الأفكارHani Al-Menaii
 
هندسة الأفكار
هندسة الأفكارهندسة الأفكار
هندسة الأفكارHani Al-Menaii
 

Destacado (17)

Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
13. Constantin Orasan (UoW) Natural Language Processing for Translation
13. Constantin Orasan (UoW) Natural Language Processing for Translation13. Constantin Orasan (UoW) Natural Language Processing for Translation
13. Constantin Orasan (UoW) Natural Language Processing for Translation
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Lecture6 syntax analysis_2
Lecture6 syntax analysis_2Lecture6 syntax analysis_2
Lecture6 syntax analysis_2
 
Lecture5 syntax analysis_1
Lecture5 syntax analysis_1Lecture5 syntax analysis_1
Lecture5 syntax analysis_1
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
 
تعرف إلى-الهندسة المعلوماتية
تعرف إلى-الهندسة المعلوماتيةتعرف إلى-الهندسة المعلوماتية
تعرف إلى-الهندسة المعلوماتية
 
Symbolic Automata = Automata + SMT solvers at ExCape14
Symbolic Automata = Automata + SMT solvers at ExCape14Symbolic Automata = Automata + SMT solvers at ExCape14
Symbolic Automata = Automata + SMT solvers at ExCape14
 
ف 2 الدرس الأول والثانى والثالث
ف 2 الدرس الأول والثانى والثالثف 2 الدرس الأول والثانى والثالث
ف 2 الدرس الأول والثانى والثالث
 
Forouzan atm
Forouzan atmForouzan atm
Forouzan atm
 
Lecture1 introduction compilers
Lecture1 introduction compilersLecture1 introduction compilers
Lecture1 introduction compilers
 
DFA minimization algorithms in map reduce
DFA minimization algorithms in map reduceDFA minimization algorithms in map reduce
DFA minimization algorithms in map reduce
 
DFA Minimization
DFA MinimizationDFA Minimization
DFA Minimization
 
optimization of DFA
optimization of DFAoptimization of DFA
optimization of DFA
 
Nltk:a tool for_nlp - py_con-dhaka-2014
Nltk:a tool for_nlp - py_con-dhaka-2014Nltk:a tool for_nlp - py_con-dhaka-2014
Nltk:a tool for_nlp - py_con-dhaka-2014
 
نموذج مسار هندسة الأفكار
نموذج مسار هندسة الأفكارنموذج مسار هندسة الأفكار
نموذج مسار هندسة الأفكار
 
هندسة الأفكار
هندسة الأفكارهندسة الأفكار
هندسة الأفكار
 

Similar a Lecture3 lexical analysis

Regular expressions h1
Regular expressions h1Regular expressions h1
Regular expressions h1Rajendran
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdfkenilpatel65
 
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
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler DesignKuppusamy P
 
Theory of computing
Theory of computingTheory of computing
Theory of computingRanjan Kumar
 
01-Introduction&Languages.pdf
01-Introduction&Languages.pdf01-Introduction&Languages.pdf
01-Introduction&Languages.pdfTariqSaeed80
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Compiler Design ug semLexical Analysis.ppt
Compiler Design ug semLexical Analysis.pptCompiler Design ug semLexical Analysis.ppt
Compiler Design ug semLexical Analysis.pptssuser6ba09a
 
Compiler Designs
Compiler DesignsCompiler Designs
Compiler Designswasim liam
 
compiler Design course material chapter 2
compiler Design course material chapter 2compiler Design course material chapter 2
compiler Design course material chapter 2gadisaAdamu
 

Similar a Lecture3 lexical analysis (20)

Syntax
SyntaxSyntax
Syntax
 
Regular expressions h1
Regular expressions h1Regular expressions h1
Regular expressions h1
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
7645347.ppt
7645347.ppt7645347.ppt
7645347.ppt
 
Control structure
Control structureControl structure
Control structure
 
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
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
SS UI Lecture 4
SS UI Lecture 4SS UI Lecture 4
SS UI Lecture 4
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Lisp and scheme i
Lisp and scheme iLisp and scheme i
Lisp and scheme i
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
01-Introduction&Languages.pdf
01-Introduction&Languages.pdf01-Introduction&Languages.pdf
01-Introduction&Languages.pdf
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Compiler Design ug semLexical Analysis.ppt
Compiler Design ug semLexical Analysis.pptCompiler Design ug semLexical Analysis.ppt
Compiler Design ug semLexical Analysis.ppt
 
Compiler Designs
Compiler DesignsCompiler Designs
Compiler Designs
 
L_2_apl.pptx
L_2_apl.pptxL_2_apl.pptx
L_2_apl.pptx
 
compiler Design course material chapter 2
compiler Design course material chapter 2compiler Design course material chapter 2
compiler Design course material chapter 2
 

Más de Mahesh Kumar Chelimilla (14)

Lecture11 syntax analysis_7
Lecture11 syntax analysis_7Lecture11 syntax analysis_7
Lecture11 syntax analysis_7
 
Lecture10 syntax analysis_6
Lecture10 syntax analysis_6Lecture10 syntax analysis_6
Lecture10 syntax analysis_6
 
Lecture9 syntax analysis_5
Lecture9 syntax analysis_5Lecture9 syntax analysis_5
Lecture9 syntax analysis_5
 
Lecture8 syntax analysis_4
Lecture8 syntax analysis_4Lecture8 syntax analysis_4
Lecture8 syntax analysis_4
 
Lecture7 syntax analysis_3
Lecture7 syntax analysis_3Lecture7 syntax analysis_3
Lecture7 syntax analysis_3
 
Transportlayer tanenbaum
Transportlayer tanenbaumTransportlayer tanenbaum
Transportlayer tanenbaum
 
Network layer tanenbaum
Network layer tanenbaumNetwork layer tanenbaum
Network layer tanenbaum
 
Forouzan x25
Forouzan x25Forouzan x25
Forouzan x25
 
Forouzan ppp
Forouzan pppForouzan ppp
Forouzan ppp
 
Forouzan isdn
Forouzan isdnForouzan isdn
Forouzan isdn
 
Forouzan frame relay
Forouzan frame relayForouzan frame relay
Forouzan frame relay
 
Forouzan data link_2
Forouzan data link_2Forouzan data link_2
Forouzan data link_2
 
Forouzan data link_1
Forouzan data link_1Forouzan data link_1
Forouzan data link_1
 
Datalinklayer tanenbaum
Datalinklayer tanenbaumDatalinklayer tanenbaum
Datalinklayer tanenbaum
 

Último

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01KreezheaRecto
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdfKamal Acharya
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfRagavanV2
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Bookingroncy bisnoi
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 

Último (20)

Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
NFPA 5000 2024 standard .
NFPA 5000 2024 standard                                  .NFPA 5000 2024 standard                                  .
NFPA 5000 2024 standard .
 
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance BookingCall Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
Call Girls Wakad Call Me 7737669865 Budget Friendly No Advance Booking
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 

Lecture3 lexical analysis

  • 2. Introduction • For an English sentence: – Read the sentence string – Recognize its parts of speech – noun, verb, adjective, etc… – Match these parts of speech against a grammar • For computer languages: – Extract the words (lexemes) of the input string – Classify them according to their roles (parts of speech) • This is tokenization – finding token classes – Lexical analyzer recognizes the string by matching these tokens against a grammar 8-Jan-15 2CS 346 Lecture 3
  • 3. • Lexical Structure of a programming language consists of a set of token classes – Operators, whitespaces, keywords, identifiers, numbers, (, ), ;, = etc… • Lexical Analysis Introduction • Lexical Analysis – Classify the program substrings into various token classes and communicate them to the parser. • Goal – Given the specification for token classes of a language, automatically construct a lexical analyzer 8-Jan-15 3CS 346 Lecture 3
  • 4. Regular Expressions • Each token class consists of a set of strings. • A distinct set of strings constitute a language over an alphabet . – Alphabet: A finite set of symbols by which the strings of the language may be formed • They are regular languages (recognized by FA) • Regular languages – defined by regular expressions (RE) • RE’s denote the set of strings recognized by a language by defining their pattern • Automation steps: – RE NFA DFA Table-Driven Implementation 8-Jan-15 4CS 346 Lecture 3
  • 5. • Two basic RE’s – Single character RE – eg: ‘B’: A language containing one string which is the single character ‘B’ – Epsilon RE – ɛ : A language which contains one string, the empty string. Regular Expressions • Three compound RE’s – Union – eg.: A + B or A | B (either A or B) – Concatenation – eg.: AB {ab | a Є AΛ b Є B} – Iteration – eg.: A* – 0 or more occurrences of A • A0 = ɛ • (A) has the same meaning as A
  • 6. Regular Language (Set) • The regular expression over alphabet Σ are the smallest set of expressions including R = ɛ | ‘c’ for c Є Σ (alphabet)| ‘c’ for c Є Σ (alphabet) | R + R (union) | RR (concatenation) | R* (iteration)
  • 7. • Operator precedence: (A), A*, AB, A|B – So ab*c|d is parsed as ((a(b*))c)|d • Describe the languages denoted by the following REs when alphabet (Σ) = {0,1} – 1*; (1+0)1; 0*+1*; 01(01)*, (0+1)*, 11(0+1)* Regular Expressions – 1*; (1+0)1; 0*+1*; 01(01)*, (0+1)*, 11(0+1)* • Short-hands: – Character range: [a-d] for a | b | c | d; – Atleast one: r+ for rr*; – Option: r? for r | e – Excluded range: [^a-z] = Complement of [a-z]
  • 8. • Write RE’s for: – Strings of 0’s and 1’s containing 00 as a substring Regular Expressions • Write RE’s that: – Recognizes our email addresses at IITG – Recognizes a number consisting of: One or more digits, followed by an optional fractional part, followed by an optional exponent.
  • 9. Lexical Specification of a Language 1. Write REs for the lexemes of each token class: Number = digit+ Keyword = ‘if’ + ‘else’ + … Identifier = letter (letter + digit)*Identifier = letter (letter + digit)* … 2. Construct R, matching all lexemes for all tokens: R = Keyword + Identifier + Number + … = R1 + R2 + …
  • 10. 1. Let input be x1…xn For 1 <= i <= n check x1…xi Є L(R) Lexical Specification of a Language 2. If success, then we know that: x1…xi Є L(Rj) for some j 3. Remove x1…xi from input and go to (3)
  • 11. • How much of the input to use? – x1 …xi Є L(R) – x1…xj Є L(R), i ≠ j Solution: “Maximal Munch” • What if more than one token matches? Lexical Specification of a Language • What if more than one token matches? – x1 …xi Є L(Rx) – x1…xi Є L(Ry) – Solution: Priority ordering – Choose the one listed first • What to do when no rule matches? – Solution: Make special token class / classes for all error strings. Put it last in the priority list.