1 / 31

Regular Expressions and DFAs

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.

Download Presentation

Regular Expressions and DFAs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Regular Expressions and DFAs

  2. DFA

  3. Example-1 • Regular expression:

  4. Example-2 • DFA for C-type comment:

  5. RE Examples

  6. Exercise 1 • Let ∑ be a finite set of symbols • ∑ = {10, 11}, ∑* = ? Answer: ∑* = {є, 10, 11, 1010, 1011, 1110, 1111, …}

  7. Exercises 2 • L1 = {10, 1}, L2 = {011, 11}, L1L2 = ? • L1L2 = {10011, 1011, 111}

  8. 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)*

  9. 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*

  10. More Exercise • The set of all strings whose number of 0’s is divisible by 3 • (1+01*01*0)*

  11. 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

  12. 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.

  13. 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?

  14. 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)

  15. 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.

  16. 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.

  17. Rules: RE ->Automata (DFA)

  18. RE ->Automata

  19. Duality Example • DFA for multiple of 3 b’s: • RE for multiple of 3 b’s:

  20. RE to DFA: Problem 1 • Make a DFA that accepts the strings in the language denoted by regular expression ab*a

  21. RE to DFA: Problem 2 • Write the RE for the following automata: • a(a|b)*a

  22. RE ->Automata

  23. RE ->Automata

  24. RE ->Automata

  25. Another: Example • Construct DFA for (1+0)*(00+11)(0+1)*

  26. Cont… • Construct DFA for (1+0)*(00+11)(0+1)*

  27. Eliminate all the €-move from FNA

  28. Final DFA

  29. Home Work: RE to DFA

  30. References • Another approach: from Mishra book • http://ecomputernotes.com/compiler-design/convert-regular-expression-to-dfa

More Related