310 likes | 328 Views
Learn about Regular Expressions, Deterministic Finite Automata (DFA), their examples, and exercises. Understand the conversion of RE to DFA and the rules governing them. Practice exercises to strengthen your knowledge.
E N D
Example-1 • Regular expression:
Example-2 • DFA for C-type comment:
Exercise 1 • Let ∑ be a finite set of symbols • ∑ = {10, 11}, ∑* = ? Answer: ∑* = {є, 10, 11, 1010, 1011, 1110, 1111, …}
Exercises 2 • L1 = {10, 1}, L2 = {011, 11}, L1L2 = ? • L1L2 = {10011, 1011, 111}
Exercises 3 • Write RE for • All strings of 0’s and 1’s • (0|1)* • All strings of 0’s and 1’s with at least 2 consecutive 0’s • (0|1)*00(0|1)* • All strings of 0’s and 1’s beginning with 1 and not having two consecutive 0’s • (1+10)*
More Exercises • All strings of 0’s and 1’s ending in 011 (0|1)*011 • any number of 0’s followed by any number of 1’s followed by any number of 2’s 0*1*2* • strings of 0,1,2 with at least one of each symbol 00*11*22*
More Exercise • The set of all strings whose number of 0’s is divisible by 3 • (1+01*01*0)*
DFA • Deterministic Finite Automata (DFA) is given by M = (Q, Σ, qo, F, δ), Where- • Q - Set of states • Σ - Set of input symbols • qo - Start state • F - set of final states • δ - Transition function (mapping states to input symbol); δ :Q x Σ → Q
DFA and NFA • Non-deterministic Finite Automata (NFA) • More than one transition occurs for any input symbol from a state. • Transition can occur even on empty string (Ɛ). • Deterministic Finite Automata (DFA) • For each state and for each input symbol, exactly one transition occurs from that state.
Exercise: DFA • Construct a DFA for “Odd number of a’s” • What is the DFA for Σ = { 0 ,1} and strings that have an odd number of 1’s and any number of 0’s?
Exercise: DFA • What is the DFA for Σ = { 0 ,1} and strings that have an odd number of 1’s and any number of 0’s? • What is the DFA for Σ = { a, b } that accepts any string with 1) ‘aab’ as a substring, 2) ‘aababb’ as a substring? (draw two DFA’s)
Theory of DFAs and REs • RE. Concise way to describe a set of strings. • DFA. Machine to recognize whether a given string is in a given set. • Duality: for any DFA, there exists a regular expression to describe the same set of strings; for any regular expression, there exists a DFA that recognizes the same set.
Rules for RE to DFA • Regular expression can be converted into DFA by the following methods: • Thompson's subset construction • Given regular expression is converted into NFA • Resultant NFA is converted into DFA • Direct Method • In direct method, given regular expression is converted directly into DFA. • Ɛ - Closure is the set of states that are reachable from the state concerned on taking empty string as input. It describes the path that consumes empty string (Ɛ) to reach some states of NFA.
Duality Example • DFA for multiple of 3 b’s: • RE for multiple of 3 b’s:
RE to DFA: Problem 1 • Make a DFA that accepts the strings in the language denoted by regular expression ab*a
RE to DFA: Problem 2 • Write the RE for the following automata: • a(a|b)*a
Another: Example • Construct DFA for (1+0)*(00+11)(0+1)*
Cont… • Construct DFA for (1+0)*(00+11)(0+1)*
References • Another approach: from Mishra book • http://ecomputernotes.com/compiler-design/convert-regular-expression-to-dfa