Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSCI 3130: Formal Languages and Automata Theory Tutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering.

Similar presentations


Presentation on theme: "1 CSCI 3130: Formal Languages and Automata Theory Tutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering."— Presentation transcript:

1 1 CSCI 3130: Formal Languages and Automata Theory Tutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering

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

3 3 DFA Minimization

4 4 We will show how to turn any DFA for L into the minimal DFA for L 0 1 … … … … qq qq qq q  q  q  q  q  q  q  q  0 1 0 1 0 1 1 1 1 1 0 1 qq qq qq qq 11 1 0 0 0 0

5 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 6 Indistinguishable States DFA example from lecture note Indistinguishable states example: {q 0, q 00 } Explain: next few slides 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1

7 7 Indistinguishable States On continuation string (further input) “0” q 0  Reject q 00  Reject 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1

8 8 Indistinguishable States On continuation string “11” q 0  Accept q 00  Accept 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1

9 9 Indistinguishable States On continuation string “01011” q 0  Accept q 00  Accept 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1

10 10 Indistinguishable States On all continuation strings, {q 0, q 00 } have the same outcome q 0, q 00 are indistinguishable  Can be merged 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1

11 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 12 Distinguishable States For continuation string = “1” q 01  Accept q 10  Reject Distinguishable 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1

13 13 Distinguishable States For continuation string = “  ” q 10  Reject q 11  Accept Distinguishable 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1

14 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 15 Repeat !! as long as you mark some pairs Finding (in)distinguishable states Rule 1: Rule 2: q q’ If q is accepting and q’ is rejecting Mark (q, q’) as distinguishable (x) x q1q1 q1’q1’ x x q2q2 q2’q2’ aa If (q 1, q 1 ’) are marked, Mark (q 2, q 2 ’) as distinguishable (x) Rule 3: Unmarked pairs are indistinguishable Merge them into groups

16 16 Example 1 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1 xxxxxx qq qq q  q  q  q  qq qq qq q  q   q 11 is distinguishable from all other states

17 17 Example 1 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1 xxxxxx qq qq q  q  q  q  qq qq qq q  q   Look at pair q , q 0 0 1 Neither (q 0, q 00 ) nor (q 1, q 01 ) are distinguishable

18 18 Example 1 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1 xxxxxx qq qq q  q  q  q  qq qq qq q  q   Look at pair q , q 1 0 1 (q 1, q 11 ) is distinguishable x

19 19 Example 1 x x x x x x xxxx qq qq q  q  q  q  qq qq qq q  q   After going thru the whole table once Now we make another pass xxx x 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1

20 20 Example 1 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1 x x x x x x xxxx qq qq q  q  q  q  qq qq qq q  q   In the second pass, nothing changes So we are ready to apply Rule 3 xxx x

21 21 Example 1 qq qq qq q  q  q  q  x x x x x x x x x x x x xx qq qq q  q  q  q  qq qq qq q  q   Merge unmarked pairs into groups Unmarked Cells  Pairs of Indistinguishable states Blue line links indistinguishable states

22 22 A B C Example 1 qq qq qq q  q  q  q  x x x x x x x x x x x x xx qq qq q  q  q  q  qq qq qq q  q   Merge unmarked pairs into groups A A A A A B A

23 23 Example 1 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1 A x A x A x x A x A x x B x x x A x x xx qq qq q  q  q  q  qq qq qq q  q  1 qq qq qCqC 1 1 0 0 0 minimized DFA: A B C

24 24 Example 2 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1 qq qq q  q  q  q  qq qq qq q  q  xxxxx  q 10 is distinguishable from all other states

25 25 Example 2 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1 qq qq q  q  q  q  qq qq qq q  q  xxxxx xxxx xxx x xx  After going thru the whole table several times (See white board for simulation)

26 26 Example 2 0 1 qq qq qq q  q  q  q  0 1 0 1 1 0 0 1 1 0 0 1 qq qq q  q  q  q  qq qq qq q  q  xxxxx xxxx xxx x xx  Merge unmarked pairs into groups

27 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 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 29 Context Free Grammar

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

31 31 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 S  AB | ba A  aA | a B  b CFG Example = Apply Production Rule  AB  aAB  aaB  aab S

32 32 L = {w = 0 n 1 n : 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 Why CFG? S  0S1 S  01

33 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 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 35 CFG Design (Con’t) Good Try: S  b S  bS | Sb S  abS | baS | bSa | aSb 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 However, cannot parse strings like “aabbbaa” Explaination makes sense. But there is a counter example.

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

37 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 38 Parse Tree How to parse “aab” in this grammar? (Previous example) S  AB | ba A  aA | a B  b CFG Example S  AB  aAB  aaB  aab Derivation

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

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

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

42 42 End Thanks for coming! Any questions?


Download ppt "1 CSCI 3130: Formal Languages and Automata Theory Tutorial 4 Hung Chun Ho Office: SHB 1026 Department of Computer Science & Engineering."

Similar presentations


Ads by Google