1 / 42

CSCI 3130: Formal Languages and Automata Theory Tutorial 4

CSCI 3130: Formal Languages and Automata Theory Tutorial 4. Hung Chun Ho Office: SHB 1026. Department of Computer Science & Engineering. Agenda. DFA Minimization Algorithm 2 Examples Context Free Grammar (CFG) Design Parse Tree. DFA Minimization. DFA Minimization. 0. 1. 1. 1. 1.

jumper
Download Presentation

CSCI 3130: Formal Languages and Automata Theory Tutorial 4

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. CSCI 3130: Formal Languages and Automata TheoryTutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering

  2. Agenda • DFA Minimization • Algorithm • 2 Examples • Context Free Grammar (CFG) • Design • Parse Tree

  3. DFA Minimization

  4. DFA Minimization 0 1 1 1 1 q0 q1 q2 q3 0 0 0 0 0 q000 1 0 q00 1 q001 q0 1 … 0 q01 … qe 1 q101 q10 0 … 1 q1 … 1 q11 1 q111 1 We will show how to turn any DFAfor L into the minimal DFAfor L

  5. DFA Minimization • After we design a DFA, there may be some redundant states • They have the same “function” • They are indistinguishable • No matter what happens in the future (inputs), they will have the same fate (accept/reject)! • We can merge indistinguishable states into groups

  6. 0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • DFA example from lecture note • Indistinguishable states example: {q0, q00} • Explain: next few slides

  7. 0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • On continuation string (further input) “0” • q0 Reject • q00  Reject

  8. 0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • On continuation string “11” • q0 Accept • q00  Accept

  9. 0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • On continuation string “01011” • q0 Accept • q00  Accept

  10. 0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Indistinguishable States • On all continuation strings, {q0 , q00} have the same outcome • q0 , q00 are indistinguishable Can be merged

  11. How to find? • Infinitely many continuation states • A bit difficult to find indistinguishable states directly • We do the reverse: • To recognize distinguishable states • Pair of states, s.t. there is a continuation string, causing them have different “fates” • (De Morgan’s Law, still remember?)

  12. 0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Distinguishable States • For continuation string = “1” • q01 Accept • q10  Reject • Distinguishable

  13. 0 0 q00 q0 1 0 1 q01 0 qe 1 0 q10 0 1 1 q1 0 1 q11 1 Distinguishable States • For continuation string = “e” • q10 Reject • q11  Accept • Distinguishable

  14. Distinguishable States • 2 states are distinguishable, if there is a continuation string, that bring them different fate!! (Accept / Reject) • Try all continuation strings? How? • Idea: • For any pair of {Accept, Reject} states, they are distinguishable • For these pairs, walk k transitions backward, mark the obtained pairs as distinguishable

  15. Finding (in)distinguishable states Repeat !! as long as you mark some pairs If q is accepting and q’ is rejectingMark(q, q’) as distinguishable (x) Rule 1: x q q’ x q1 q1’ If (q1, q1’) are marked,Mark(q2, q2’) as distinguishable (x) a a Rule 2: x q2 q2’ Unmarked pairs are indistinguishable Merge them into groups Rule 3:

  16. Example 1 0 q0 0 q00 q1 q0 1 0 1 q00 q01 0 qe 1 0 q01 q10 0 1 1 q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1  q11 is distinguishable from all other states

  17. Example 1 0 q0 0 q00 0 q1 1 q0 1 0 1 q00 q01 0 qe 1 0 q01 q10 0 1 1 q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1  Look at pair qe, q0 Neither (q0, q00) nor (q1, q01) are distinguishable

  18. Example 1 0 q0 0 q00 x q1 q0 1 0 1 q00 q01 0 qe 1 0 1 0 q01 q10 0 1 1 q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1  Look at pair qe, q1 (q1, q11) is distinguishable

  19. Example 1 0 q0 0 q00 q1 x x q0 1 0 1 q00 x q01 0 qe 1 0 x x x q01 q10 0 1 1 x x q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1  After going thru the whole table once Now we make another pass

  20. Example 1 0 q0 0 q00 q1 x x q0 1 0 1 q00 x q01 0 qe 1 0 x x x q01 q10 0 1 1 x x q10 q1 0 1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10 1  In the second pass,nothing changes So we are ready to apply Rule 3

  21. Example 1 q0 q00 q1 x x q0 q00 x q01 qe x x x q01 q10 x x q10 q1 q11 q11 x x x x x x qe q0 q1 q00 q01 q10  Merge unmarked pairs into groups Unmarked Cells  Pairs of Indistinguishable states Blue line links indistinguishable states

  22. Example 1 q0 A q00 q1 x x q0 A q00 A A x q01 qe x x B x q01 q10 A A x A x q10 q1 B q11 q11 x x x x x x C qe q0 q1 q00 q01 q10  Merge unmarked pairs into groups

  23. Example 1 0 q0 A 0 q00 q1 x x q0 1 A 0 1 q00 A A x q01 0 qe 1 0 x x B x q01 q10 0 1 1 A A x A x q10 q1 0 1 B q11 q11 x x x x x x C qe q0 q1 q00 q01 q10 1 0 1 1 minimized DFA: qA qB qC 1 0 0

  24. Example 2 0 q0 0 q00 q1 q0 1 0 1 q00 q01 0 qe 1 0 q01 q10 0 1 1 x x x x x q10 q1 0 1 q11 q11 qe q0 q1 q00 q01 q10 1  q10 is distinguishable from all other states

  25. Example 2 0 q0 0 q00 q1 x x q0 1 0 1 q00 x q01 0 qe 1 0 x x x q01 q10 0 1 1 x x x x x q10 q1 0 1 q11 q11 x x x x qe q0 q1 q00 q01 q10 1  After going thru the whole table several times (See white board for simulation)

  26. Example 2 0 q0 0 q00 q1 x x q0 1 0 1 q00 x q01 0 qe 1 0 x x x q01 q10 0 1 1 x x x x x q10 q1 0 1 q11 q11 x x x x qe q0 q1 q00 q01 q10 1  Merge unmarked pairs into groups

  27. Why it works? • For a pair of states, there is a continuation string that bring them different fates • Distinguishable • Idea: For any pair of {Accept, Reject} states, they are distinguishable, mark them • These pairs have a length-0 continuation string that distinguishes them • 1 step backward, mark them as distinguishable • These pairs have a length-1 continuation string that distinguishes them

  28. Why it works? • 1 further step backward, mark • These pairs have a length-2 continuation string that distinguishes them • 1 further step backward, mark • These pairs have a length-3 continuation string that distinguishes them • So-on …

  29. Context Free Grammar

  30. Another Terminal 4) Start Variable 3) Production Rule Another Production Rule 1) Variables 2) Terminal Context-Free Grammar (Recap) • A context free grammar is consisted of S  AB | ba A  aA | a B  b

  31. = Apply Production Rule CFG Example S  AB | ba A  aA | a B  b S • AB • aAB • aaB • aab Context-Free Grammar (Recap) • A string is said to belong to the language (of the CFG) if it can be derived from the start variable Derivation Therefore, aab belongs to the language

  32. S  0S1 S  01 Why CFG? • L = {w = 0n1n : n is an positive integer} • L is not a regular language • Proved by “Pumping Lemma” • A Context-Free Grammar can describe it • Thus, CFG is more general than regular expression • Recall: NFA  Regular Expression  DFA

  33. CFG Design • Given a context-free language, design the CFG • L = { ab-string, w : Number of a’s < Number of b’s } • Some time for you to get into think… 1 min S  ? …

  34. CFG Design (Con’t) • Idea: Bottom-up • Shortest string in L : “b” • Given a string in L, we can expand it, s.t. it is still in L • i.e., Add terminals, while not violating the constraints

  35. After adding 1 “b”, number of “b” is still greater than that of “a” Adding 1 “a” and 1 “b”, the difference between the numbers of “a” and “b” keep constant CFG Design (Con’t) Good Try: S  b S  bS | Sb S  abS | baS | bSa | aSb However, cannot parse strings like “aabbbaa” Explaination makes sense.But there is a counter example.

  36. Base Case #b still > #a 1st S 2nd S ‘a’ : #b ≥ #a + 1 : #b ≥ #a + 1 : #a = 1 SUM  #b ≥ #a + 2 - 1 CFG Design (Con’t) Consider the answer: S  b S  SS S  SaS | aSS | SSa Explaination makes sense.But, is the grammar is correct?

  37. A set of strings CFG Design (Con’t) • After designing the grammar, G, you may have to prove (if required) that the language of this grammar is equivalent to the given language • i.e., Prove that L(G) = L • Proof: Part 1) L(G) ⊂ L Part 2) L ⊂ L(G) • Due to time limit, I will not do this part

  38. Derivation S • AB • aAB • aaB • aab Parse Tree • How to parse “aab” in this grammar? (Previous example) CFG Example S  AB | ba A  aA | a B  b

  39. S A B b a A a Parse Tree (Con’t) • Idea: Production Rule = Node + Children • Should be very intuitive to understand Derivation S • AB • aAB • aaB • aab

  40. S S S - S S - S - 2 3 S - A S S 3 1 1 2 3 – (1 – 2) S S  S - S  1 | 2 | 3 Different parse trees!The grammar is ambiguous! Parse Tree (Con’t) • Ambiguity: String: 3 - 1 - 2 CFG: 3 – 1 – 2

  41. Parse Tree (Con’t) • Useful in programming language • CSC3180 • Useful in compiler • CSC3120

  42. End • Thanks for coming! • Any questions?

More Related