CS375: Logic nd Theory of Computing Fuhu (Frnk) Cheng Deprtment of Computer Science University of Kentucky 1
Tle of Contents: Week 1: Preliminries (set lger, reltions, functions) (red Chpters 1-4) Weeks 3-6: Regulr Lnguges, Finite Automt (Chpter 11) Weeks 7-9: Context-Free Lnguges, Pushdown Automt (Chpters 12) Weeks 10-12: Turing Mchines (Chpter 13) 2
Tle of Contents (conti): Weeks 13-14: Propositionl Logic (Chpter 6), Predicte Logic (Chpter 7), Computtionl Logic (Chpter 9), Algeric Structures (Chpter 10) 3
Regulr lnguges cn e chrcterized y regulr expressions, y DFAs, y NFAs, s well s y regulr grmmrs. RECALL: wht is grmmr? set of rules used to define the structure of the strings in lnguge, usully represented s 4-tuple G = (N, T, S, P) N : lphet of nonterminls (uppercse letters) T : lphet of terninls (lowercse letters) S : strt symol (nonterminl) P : set of productions 4
Exmple. N = {S}, T = {,, c}, S: strt symol, P : S S S S S S cs then the grmmr cn e represented y the 4-tuple G = ( {S}, {,, c}, S, P ) 5
Bck to regulr grmmrs - A regulr grmmr is grmmr whose productions tke the following form: where w is string of terminls. A wb or A w So we cn hve productions like: A Ʌ A w (w: not empty) A B (A nd B cn e the sme) A wb (w: not empty) 6
Exmple. Construct regulr grmmr for the lnguge of { Ʌ,,,,,,,, n, n, } Would S Ʌ S S work? 7
Exmple. Write regulr grmmr for {, c, cc, ccc, cccc, }. Solution: A regulr expression for the lnguge is c*. A regulr grmmr is S T T ct. True? : S T c 2 : S T ct cct cc c 2 Would S S cs lso work? 8
Trnsforming n NFA to Regulr Grmmr 1. Stte nmes ecome the nonterminls. 2. Edge symols of the NFA ecome the ternimls 3. The strt stte ecomes the strt symol of the grmmr. 4. For ech stte trnsition from I to J leled with x construct production I xj. 5. For ech finl stte F construct production F Λ. 9 if x then simply I J
Exmple. Trnsform the following NFA into regulr grmmr. Non-terminls: S, F, I Strt symol: S Terminls:, Productions : S I F I I F F Ʌ 10
11
Solution (conti): 0 1 2 4 3 5 Hence, (0) {0,1, 2, 3, 5} (1) {1, 2, 3, 5} (2) {2, 3, 5} (3) {3, 5} (4) {4} (5) {5} 12
Solution: Drw n NFA nd then use the lgorithm. 0 1 2 5 3 4 Then, 1. uild the tree on the right 2. identify ll distinct nodes 13
Solution: Drw n NFA nd then use the lgorithm. Then, 1. uild the trnsition tle 2. nd write it in simplified form (0) {0,1,2,3,5} {1,2,3,5} {4} {1,2,3,5} {4} {3,5} {4} 14
Solution: Drw n NFA nd then use the lgorithm. Then, 1. uild the trnsition tle 2. nd write it in simplified form T S F {0,1,2,3,5} {1,2,3,5} {4} F {1,2,3,5} {1,2,3,5} {4} T S F 0 1 2 F 1 1 2 {4} {3,5} F {3,5} {4} 2 3 4 F 3 4 2 4 4 4 15
Solution: Drw n NFA nd then use the lgorithm. Then, drw n DFA 0 1 T 3 2 4, S F 0 1 2 F 1 1 2 2 3 4 F 3 4 2 4 4 4 16 Don t need
Solution: Drw n NFA nd then use the lgorithm. Then, drw n DFA 1 I 0 S 2 J 3 F 4, 17 Don t need
Solution: Drw n NFA nd then use the lgorithm. Then, construct regulr grmmr Resulting grmmr : S I J S I I J J F F J F S I F simplify I I F F F cn this e further simplified? 18 I J F S S F F
Trnsforming Regulr Grmmr to n NFA 1. Replce ny production with multiple terminls y productions with single terminls. 2. The strt stte is the grmmr strt symol. 3. Trnsform I J into trnsition from I to J leled with. 4. Trnsform I J into trnsition from I to J leled with Λ. 5. Trnsform ech I into trnsition from I to new single finl stte F leled with. 6. The finl sttes re F together with ech stte I with production I Λ. S J S I I J 19
5. Trnsform ech I into trnsition from I to new single finl stte F leled with. I mens the production stops once is produced, so must e the lel of n edge to finl stte. 6. ech stte I with production I Λ is finl stte. I Ʌ mens the production stops t I, so I must e finl stte. 20
Exmple. Trnsform the following regulr grmmr into n NFA. S S T Λ T ct d Solution. Trnsform S S S I nd I S, so the grmmr ecomes S I T Λ I S T ct d They cn e written s : into 21
S I S T S Ʌ I S T ct T d strt S Ʌ T I c d F Now the NFA cn e drwn: 22
Exmple. Wht is the regulr expression for the lnguge of the grmmr? Answer: ( ) ( ) c d ( ) ( ) c d ( ) ( c d) 23
Properties of Regulr Lnguges By using certin properties of regulr lnguges we cn prove, BWOC, tht certin lnguges re not regulr. Why? A string of length m is uilt y m edges nd so y m+1 sttes. 24
The following grph depicts the sitution. x, z could e empty Dotted rrows represent the pth of cceptnce for s Letters x, y, nd z represent the conctention of the letters long the edges of the pth. So s = xyz nd y Λ. 25
n n Exmple. The lnguge L = { n N} is not regulr. Proof: Assume, BWOC, tht L is regulr. Since L is infinite, the Pumping Lemm pplies. Choose s = m m. Then s = xyz, where y Ʌ, xy 2m, nd xy k z L for ll k ϵ N. We clim tht y consists completely of s or s only. For if y is of the form y = i j, i > 0, j > 0, then we hve x = m i nd z = m j. So, xy 2 z = m i ( i j )( i j ) m j = m j i m L. But this is contrdction. So we hve either 26 y = i, i > 0, or y = j, j > 0.
n n Exmple. The lnguge L = { n N} is not regulr. Proof (conti.): If y = i for some i > 0 then xy is string of s. Since s = xyz = m m nd xy 2 is lso string of s, we hve xy 2 z = m+i m. By Pumping Lemm, this is supposed to e n element of L, ut this is impossile ecuse nd hve different exponents. Similrly we cn lso prove tht if y = j for some j > 0, we would lso get into contrdiction. Hence, L cn not e regulr. QED 27
Exmple. In the previous proof we exhiited contrdiction when k = 2. Find similr contrdictions for k = 0 nd k = 3. 28
End of Regulr Lnguge nd Finite Automt IV 29