SlideShare a Scribd company logo
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

More Related Content

PPTX
Cs419 lec6 lexical analysis using nfa
PPTX
Cs419 lec3 lexical analysis using re
PPTX
Cs419 lec4 lexical analysis using re
PDF
Regular expression
PPTX
Regular expressions
PPTX
theory of computation lecture 02
Cs419 lec6 lexical analysis using nfa
Cs419 lec3 lexical analysis using re
Cs419 lec4 lexical analysis using re
Regular expression
Regular expressions
theory of computation lecture 02

What's hot (20)

PPT
Regular expression with DFA
PDF
Regular language and Regular expression
PPT
Regular Languages
PDF
Ch02
PPTX
Regular Expression (Regex) Fundamentals
PDF
Regular expressions in Ruby and Introduction to Vim
PPT
POST’s CORRESPONDENCE PROBLEM
PPTX
Materi 4 Regular Expression
PDF
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
PPTX
Regular expressions
PPTX
REGULAR EXPRESSION TO N.F.A
PPT
The Power of Regular Expression: use in notepad++
PPT
Regular Expression
PPT
Regular Expressions
PPT
Regular expressions
PPTX
Processing Regex Python
ODP
Regex Presentation
PDF
Lex analysis
PPTX
PPTX
Theory of computation / Post’s Correspondence Problems (PCP)
Regular expression with DFA
Regular language and Regular expression
Regular Languages
Ch02
Regular Expression (Regex) Fundamentals
Regular expressions in Ruby and Introduction to Vim
POST’s CORRESPONDENCE PROBLEM
Materi 4 Regular Expression
Theory of Computation Regular Expressions, Minimisation & Pumping Lemma
Regular expressions
REGULAR EXPRESSION TO N.F.A
The Power of Regular Expression: use in notepad++
Regular Expression
Regular Expressions
Regular expressions
Processing Regex Python
Regex Presentation
Lex analysis
Theory of computation / Post’s Correspondence Problems (PCP)
Ad

Similar to Cs419 lec5 lexical analysis using dfa (20)

PPTX
Theory of computation Lec3 dfa
DOCX
Automata theory introduction
PPTX
Lec1.pptx
DOCX
Regular Expression .docx
PDF
Automata theory
PPTX
aho corasick paper study note
PDF
flat unit1
PDF
Nondeterministic Finite Automata AFN.pdf
PDF
Finite automata
PPTX
Theory of Automata(Formal Language) Lecture 3.pptx
PDF
CS 162 Fall 2015 Homework 1 Problems September 29, 2015 Timothy Johnson 1. Ex...
PDF
Hwsoln03 toc
PDF
Atomata assignment 1 solution
PPTX
TOC-L#05.pptx Theory of Computation Lecturwe
PPTX
1.3.2 non deterministic finite automaton
PPTX
1.3.1 deterministic finite automaton
PDF
Finite Automata
PPT
finitw automata2, Computer theory computure science
PPTX
Regular Language and Finite Automata, RE
Theory of computation Lec3 dfa
Automata theory introduction
Lec1.pptx
Regular Expression .docx
Automata theory
aho corasick paper study note
flat unit1
Nondeterministic Finite Automata AFN.pdf
Finite automata
Theory of Automata(Formal Language) Lecture 3.pptx
CS 162 Fall 2015 Homework 1 Problems September 29, 2015 Timothy Johnson 1. Ex...
Hwsoln03 toc
Atomata assignment 1 solution
TOC-L#05.pptx Theory of Computation Lecturwe
1.3.2 non deterministic finite automaton
1.3.1 deterministic finite automaton
Finite Automata
finitw automata2, Computer theory computure science
Regular Language and Finite Automata, RE
Ad

More from Arab Open University and Cairo University (20)

PDF
File Organization & processing Mid term summer 2014 - modelanswer
PDF
Model answer of compilers june spring 2013
PDF
Model answer of exam TC_spring 2013
PPTX
Theory of computation Lec6
PPTX
Theory of computation Lec2
PPTX
Theory of computation Lec1
PPTX
Theory of computation Lec7 pda
PPTX
PPTX
Cs419 lec8 top-down parsing
PPTX
Cs419 lec11 bottom-up parsing
PPTX
Cs419 lec12 semantic analyzer
PPTX
Cs419 lec9 constructing parsing table ll1
PPTX
Cs419 lec10 left recursion and left factoring
DOCX
Compilers Final spring 2013 model answer
PDF
Compilers midterm spring 2013 model answer
PPTX
Cs419 Compiler lec1&2 introduction
PDF
Final Exam OS fall 2012-2013 with answers
PDF
CS215 - Lec 8 searching records
PDF
CS215 - Lec 7 managing records collection
File Organization & processing Mid term summer 2014 - modelanswer
Model answer of compilers june spring 2013
Model answer of exam TC_spring 2013
Theory of computation Lec6
Theory of computation Lec2
Theory of computation Lec1
Theory of computation Lec7 pda
Cs419 lec8 top-down parsing
Cs419 lec11 bottom-up parsing
Cs419 lec12 semantic analyzer
Cs419 lec9 constructing parsing table ll1
Cs419 lec10 left recursion and left factoring
Compilers Final spring 2013 model answer
Compilers midterm spring 2013 model answer
Cs419 Compiler lec1&2 introduction
Final Exam OS fall 2012-2013 with answers
CS215 - Lec 8 searching records
CS215 - Lec 7 managing records collection

Recently uploaded (20)

PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
01-Introduction-to-Information-Management.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Cell Types and Its function , kingdom of life
PDF
Computing-Curriculum for Schools in Ghana
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PPTX
Institutional Correction lecture only . . .
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Basic Mud Logging Guide for educational purpose
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial disease of the cardiovascular and lymphatic systems
01-Introduction-to-Information-Management.pdf
Anesthesia in Laparoscopic Surgery in India
Microbial diseases, their pathogenesis and prophylaxis
VCE English Exam - Section C Student Revision Booklet
O7-L3 Supply Chain Operations - ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
STATICS OF THE RIGID BODIES Hibbelers.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Cell Types and Its function , kingdom of life
Computing-Curriculum for Schools in Ghana
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPH.pptx obstetrics and gynecology in nursing
Institutional Correction lecture only . . .
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Basic Mud Logging Guide for educational purpose
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
FourierSeries-QuestionsWithAnswers(Part-A).pdf

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