SlideShare ist ein Scribd-Unternehmen logo
1 von 25
WELCOME TO A
JOURNEY TO
CS419
Dr. Hussien Sharaf
Computer Science Department
dr.sharaf@from-masr.com
FINITE AUTOMATA FA
 Its goal is to act as a recognizer for specific
language/pattern.
 Any problem can be presented in form of
decidable problem that can be answered
by Yes/No.
 Hence FA (machine with limited memory)
can solve any problem.
 FA used to be hard wired devices like
controller of a lift.
Dr. Hussien M. Sharaf 2
IMPLEMENTATION OF FINITE
AUTOMATA IN CODE
 Possible ways:
1. Use the position in the code (nested within
tests) to maintain the state implicitly.
2. Use a variable to maintain the current state
and write transitions as doubly nested case
statements inside a loop, where the first
case statement tests current state and the
nested second level tests the input
character, given the state or vice versa.
Dr. Hussien M. Sharaf 3
EXAMPLE FOR METHOD 1
The position in the code to maintain the state
Implicitly.
Dr. Hussien M. Sharaf 4
CODE 1
Dr. Hussien M. Sharaf 5
EXAMPLE FOR METHOD 2
 A variable to maintain the current state.
Dr. Hussien M. Sharaf 6
CODE 2
Dr. Hussien M. Sharaf 7
 There is a fixed number of states and the DFA can
only be in one state at a time.
DFA = “a 5-tuple “ (Q, Σ, , q0, F)
Q: {q0, q1, q2, …} is set of states.
Σ: {a, b, …} set of alphabet.
(delta): A transition function, which is a total
function from Q x Σ to Q, this function:
 Takes a state and input symbol as arguments.
 Returns a single state.
: Q x Σ→Q
q0 Q is the start state.
F Q is the set of final/accepting states.
DETERMINISTIC FINITE AUTOMATA DFA
Dr. Hussien M. Sharaf 8
 : Q x Σ→Q
 Maps from domain of (states, letters) to
range of states.
TRANSITION FUNCTION 
(q0, a)
(q2, b)
(q1, b)
q1
q2
q3
Dr. Hussien M. Sharaf 9
 Lets name it Conditional link instead of transition
 λ: Q x Q → Σ
 Maps from domain of (states, states) to range
of letters.
RENAMING FUNCTION  TO λ (LAMDA)
Σ
(q0, q1)
(q2, q3)
(q1, q3)
a
Dr. Hussien M. Sharaf 10
b
c
Allows more than one link
from domain to codomain
Not recommended
EXAMPLE1.1
 Build an FA that accepts only aab
 L = {aab}
S1
-
S3
a
S2
a b
+
S4
S5
b
b
a
a,b
a b
S1 S2 S5
S2 S3 S5
S3 S5 S4
S4 S5 S5
Dr. Hussien M. Sharaf 11
a, b
EXAMPLE1.2
 Build a DFA that accepts only aab
 DFA that is not well defined.
S1
-
S3
a
S2
a b
+
S4
a b
S1 S2 ?
S2 S3 ?
S3 ? ?
S4 ? ?
Dr. Hussien M. Sharaf 12
EX2
 (a+b)*
a, b
±
Dr. Hussien M. Sharaf 13
FA ACCEPTING NOTHING
1. FA with no final states
a
-
a,b
b
2. FA with disconnected graph. Start state does not
have a path to the final state.
a
-
a,b
b
+
b
Dr. Hussien M. Sharaf 14
EX3
 All words with even count of letters.
((a+b)(a+b))*
1± 2
a, b
a, b
Dr. Hussien M. Sharaf 15
EX4.1
 All words that start with “a”
a(a+b)*
1-
2
b
a 3 + a,b
1-
2b
a 3 +
a,b
a,b
Does not accept all inputs
Dr. Hussien M. Sharaf 16
EX4.2
 All words that start with “a”
a(a+b)*
4+
1-
2b
a
3 +
a,b
a,b
a,b
Special accept state for string “a”, might give
better performance in hardware implementation
Dr. Hussien M. Sharaf 17
ASSIGNMENT2
 Compilers_DFA_Sheet_2.pdf
 Deadline is 7 March-2013
Dr. Hussien M. Sharaf 18
EX5
 All words that start with triple letter
(aaa+bbb)(a+b)*
1-
2a 3
a,b
4
b 5
b
6+
b
a a
Dr. Hussien M. Sharaf 19
EX6
{aa, ba, baba, aaaa, bbba, abba, aaabaa, …}
All words with even count of letters and ends with
“a”. (a+b)a ((a+b)a (b(a+b)a)* )*
-
a,b
+
a,b
5
b
a
b
Dr. Hussien M. Sharaf 20
EX7
{aa, ba, baba, aaaa, ab, bb, bababa, aaba, …}
All words with even count of letters having “a” in
an even position from the start, where the first
letter is letter number one. (a+b)a((a+b)a)*
-
a,b
Dr. Hussien M. Sharaf 21
EX8 COMPILERS CONSTRUCTION PAGE 50
 Consider the following FA:
•Accepts identifiers where
symbols are not acceptded with
error handling.
•An identifier must start with a
letter.
letter [a-zA-Z]
Other =~letter
Other# = ~(letter|digit)
—
letter
digit
letter
error
other
Other#
any
Dr. Hussien M. Sharaf 22
EX9 COMPILERS CONSTRUCTION PAGE 52
 Integer constants
—
digit
error
- digit
digit
other
other
Dr. Hussien M. Sharaf 23
EX10 COMPILERS CONSTRUCTION PAGE 52
 Decimal/floating constants
— +
digit
error
- digit
digit
other
other
+
digit.
digit
other
Dr. Hussien M. Sharaf 24
THANK YOU
Dr. Hussien M. Sharaf 25

Weitere ähnliche Inhalte

Was ist angesagt?

Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFAMaulik Togadiya
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expressionAnimesh Chaturvedi
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsMesut Günes
 
Regular expressions in Ruby and Introduction to Vim
Regular expressions in Ruby and Introduction to VimRegular expressions in Ruby and Introduction to Vim
Regular expressions in Ruby and Introduction to VimStalin Thangaraj
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMRajendran
 
Materi 4 Regular Expression
Materi 4   Regular ExpressionMateri 4   Regular Expression
Materi 4 Regular Expressionahmad haidaroh
 
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping LemmaTheory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping LemmaRushabh2428
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsEran Zimbler
 
REGULAR EXPRESSION TO N.F.A
REGULAR EXPRESSION TO N.F.AREGULAR EXPRESSION TO N.F.A
REGULAR EXPRESSION TO N.F.ADev Ashish
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++Anjesh Tuladhar
 
Regular Expression
Regular ExpressionRegular Expression
Regular ExpressionBharat17485
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsRaj Gupta
 
Processing Regex Python
Processing Regex PythonProcessing Regex Python
Processing Regex Pythonprimeteacher32
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
 

Was ist angesagt? (20)

Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFA
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expression
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
Ch02
Ch02Ch02
Ch02
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
 
Regular expressions in Ruby and Introduction to Vim
Regular expressions in Ruby and Introduction to VimRegular expressions in Ruby and Introduction to Vim
Regular expressions in Ruby and Introduction to Vim
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEM
 
Materi 4 Regular Expression
Materi 4   Regular ExpressionMateri 4   Regular Expression
Materi 4 Regular Expression
 
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping LemmaTheory of Computation Regular Expressions, Minimisation & Pumping Lemma
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
REGULAR EXPRESSION TO N.F.A
REGULAR EXPRESSION TO N.F.AREGULAR EXPRESSION TO N.F.A
REGULAR EXPRESSION TO N.F.A
 
The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++The Power of Regular Expression: use in notepad++
The Power of Regular Expression: use in notepad++
 
Regular Expression
Regular ExpressionRegular Expression
Regular Expression
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Processing Regex Python
Processing Regex PythonProcessing Regex Python
Processing Regex Python
 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
 
Lex analysis
Lex analysisLex analysis
Lex analysis
 
Matlab
MatlabMatlab
Matlab
 
Theory of computation / Post’s Correspondence Problems (PCP)
Theory of computation / Post’s Correspondence Problems (PCP)Theory of computation / Post’s Correspondence Problems (PCP)
Theory of computation / Post’s Correspondence Problems (PCP)
 

Ähnlich wie Cs419 lec5 lexical analysis using dfa

Ähnlich wie Cs419 lec5 lexical analysis using dfa (20)

Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Automata Theory
Automata TheoryAutomata Theory
Automata Theory
 
Automata theory introduction
Automata theory introductionAutomata theory introduction
Automata theory introduction
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
aho corasick paper study note
 aho corasick paper study note aho corasick paper study note
aho corasick paper study note
 
flat unit1
flat unit1flat unit1
flat unit1
 
Nondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfNondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdf
 
Finite automata
Finite automataFinite automata
Finite automata
 
CS 162 Fall 2015 Homework 1 Problems September 29, 2015 Timothy Johnson 1. Ex...
CS 162 Fall 2015 Homework 1 Problems September 29, 2015 Timothy Johnson 1. Ex...CS 162 Fall 2015 Homework 1 Problems September 29, 2015 Timothy Johnson 1. Ex...
CS 162 Fall 2015 Homework 1 Problems September 29, 2015 Timothy Johnson 1. Ex...
 
Hwsoln03 toc
Hwsoln03 tocHwsoln03 toc
Hwsoln03 toc
 
Atomata assignment 1 solution
Atomata  assignment 1 solutionAtomata  assignment 1 solution
Atomata assignment 1 solution
 
1.3.2 non deterministic finite automaton
1.3.2 non deterministic finite automaton1.3.2 non deterministic finite automaton
1.3.2 non deterministic finite automaton
 
1.3.1 deterministic finite automaton
1.3.1 deterministic finite automaton1.3.1 deterministic finite automaton
1.3.1 deterministic finite automaton
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
Theory of computation and automata
Theory of computation and automataTheory of computation and automata
Theory of computation and automata
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
 
TOC Solutions-Adi.pdf
TOC Solutions-Adi.pdfTOC Solutions-Adi.pdf
TOC Solutions-Adi.pdf
 
TOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdfTOC_Solutions-Adi.pdf
TOC_Solutions-Adi.pdf
 

Mehr von Arab Open University and Cairo University

Mehr von Arab Open University and Cairo University (20)

Infos2014
Infos2014Infos2014
Infos2014
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Theory of computation Lec6
Theory of computation Lec6Theory of computation Lec6
Theory of computation Lec6
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Cs419 lec12 semantic analyzer
Cs419 lec12  semantic analyzerCs419 lec12  semantic analyzer
Cs419 lec12 semantic analyzer
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Compilers midterm spring 2013 model answer
Compilers midterm spring 2013   model answerCompilers midterm spring 2013   model answer
Compilers midterm spring 2013 model answer
 
Cs419 Compiler lec1&2 introduction
Cs419 Compiler lec1&2  introductionCs419 Compiler lec1&2  introduction
Cs419 Compiler lec1&2 introduction
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 

Kürzlich hochgeladen

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 

Kürzlich hochgeladen (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 

Cs419 lec5 lexical analysis using dfa

  • 1. WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 2. FINITE AUTOMATA FA  Its goal is to act as a recognizer for specific language/pattern.  Any problem can be presented in form of decidable problem that can be answered by Yes/No.  Hence FA (machine with limited memory) can solve any problem.  FA used to be hard wired devices like controller of a lift. Dr. Hussien M. Sharaf 2
  • 3. IMPLEMENTATION OF FINITE AUTOMATA IN CODE  Possible ways: 1. Use the position in the code (nested within tests) to maintain the state implicitly. 2. Use a variable to maintain the current state and write transitions as doubly nested case statements inside a loop, where the first case statement tests current state and the nested second level tests the input character, given the state or vice versa. Dr. Hussien M. Sharaf 3
  • 4. EXAMPLE FOR METHOD 1 The position in the code to maintain the state Implicitly. Dr. Hussien M. Sharaf 4
  • 5. CODE 1 Dr. Hussien M. Sharaf 5
  • 6. EXAMPLE FOR METHOD 2  A variable to maintain the current state. Dr. Hussien M. Sharaf 6
  • 7. CODE 2 Dr. Hussien M. Sharaf 7
  • 8.  There is a fixed number of states and the DFA can only be in one state at a time. DFA = “a 5-tuple “ (Q, Σ, , q0, F) Q: {q0, q1, q2, …} is set of states. Σ: {a, b, …} set of alphabet. (delta): A transition function, which is a total function from Q x Σ to Q, this function:  Takes a state and input symbol as arguments.  Returns a single state. : Q x Σ→Q q0 Q is the start state. F Q is the set of final/accepting states. DETERMINISTIC FINITE AUTOMATA DFA Dr. Hussien M. Sharaf 8
  • 9.  : Q x Σ→Q  Maps from domain of (states, letters) to range of states. TRANSITION FUNCTION  (q0, a) (q2, b) (q1, b) q1 q2 q3 Dr. Hussien M. Sharaf 9
  • 10.  Lets name it Conditional link instead of transition  λ: Q x Q → Σ  Maps from domain of (states, states) to range of letters. RENAMING FUNCTION  TO λ (LAMDA) Σ (q0, q1) (q2, q3) (q1, q3) a Dr. Hussien M. Sharaf 10 b c Allows more than one link from domain to codomain Not recommended
  • 11. EXAMPLE1.1  Build an FA that accepts only aab  L = {aab} S1 - S3 a S2 a b + S4 S5 b b a a,b a b S1 S2 S5 S2 S3 S5 S3 S5 S4 S4 S5 S5 Dr. Hussien M. Sharaf 11 a, b
  • 12. EXAMPLE1.2  Build a DFA that accepts only aab  DFA that is not well defined. S1 - S3 a S2 a b + S4 a b S1 S2 ? S2 S3 ? S3 ? ? S4 ? ? Dr. Hussien M. Sharaf 12
  • 13. EX2  (a+b)* a, b ± Dr. Hussien M. Sharaf 13
  • 14. FA ACCEPTING NOTHING 1. FA with no final states a - a,b b 2. FA with disconnected graph. Start state does not have a path to the final state. a - a,b b + b Dr. Hussien M. Sharaf 14
  • 15. EX3  All words with even count of letters. ((a+b)(a+b))* 1± 2 a, b a, b Dr. Hussien M. Sharaf 15
  • 16. EX4.1  All words that start with “a” a(a+b)* 1- 2 b a 3 + a,b 1- 2b a 3 + a,b a,b Does not accept all inputs Dr. Hussien M. Sharaf 16
  • 17. EX4.2  All words that start with “a” a(a+b)* 4+ 1- 2b a 3 + a,b a,b a,b Special accept state for string “a”, might give better performance in hardware implementation Dr. Hussien M. Sharaf 17
  • 18. ASSIGNMENT2  Compilers_DFA_Sheet_2.pdf  Deadline is 7 March-2013 Dr. Hussien M. Sharaf 18
  • 19. EX5  All words that start with triple letter (aaa+bbb)(a+b)* 1- 2a 3 a,b 4 b 5 b 6+ b a a Dr. Hussien M. Sharaf 19
  • 20. EX6 {aa, ba, baba, aaaa, bbba, abba, aaabaa, …} All words with even count of letters and ends with “a”. (a+b)a ((a+b)a (b(a+b)a)* )* - a,b + a,b 5 b a b Dr. Hussien M. Sharaf 20
  • 21. EX7 {aa, ba, baba, aaaa, ab, bb, bababa, aaba, …} All words with even count of letters having “a” in an even position from the start, where the first letter is letter number one. (a+b)a((a+b)a)* - a,b Dr. Hussien M. Sharaf 21
  • 22. EX8 COMPILERS CONSTRUCTION PAGE 50  Consider the following FA: •Accepts identifiers where symbols are not acceptded with error handling. •An identifier must start with a letter. letter [a-zA-Z] Other =~letter Other# = ~(letter|digit) — letter digit letter error other Other# any Dr. Hussien M. Sharaf 22
  • 23. EX9 COMPILERS CONSTRUCTION PAGE 52  Integer constants — digit error - digit digit other other Dr. Hussien M. Sharaf 23
  • 24. EX10 COMPILERS CONSTRUCTION PAGE 52  Decimal/floating constants — + digit error - digit digit other other + digit. digit other Dr. Hussien M. Sharaf 24
  • 25. THANK YOU Dr. Hussien M. Sharaf 25