CS311 Computtionl Strutures Regulr Lnguges nd Regulr Grmmrs Leture 6 1
Wht we know so fr: RLs re losed under produt, union nd * Every RL n e written s RE, nd every RE represents RL Every RL n e reognized y NFA nd we know how to uild it NFAs nd DFA hve the sme power Every NFA n e turned in to DFA the suset onstrution 2
Whtʼs Next? How to turn FSA into regulr grmmr nd vie-vers Miniml-stte DFAs Myhill-Nerode Theorem Lnguge indistinguishility 3
Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4
Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4
Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T Vriles We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4
Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4
Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4
Phrse-Struture Grmmrs A grmmr is set of rules for trnsforming strings Strings n involve vriles nd terminl symols S T Terminl symols We derive string of terminls y repetedly pplying rules eginning from designted strt vrile (often S) The lnguge defined y grmmr is the set of strings tht n e derived 4
Regulr Grmmrs Hein Setion 11.4.1 Whtʼs Regulr Grmmr? A prtiulr kind of grmmr in whih ll the produtions hve one of these forms: S ε S w S T S wt w is sequene of terminl symols t most one vrile n pper on the rhs, nd it must e on the right. Exmples: S Y Y Z S AB 5
Exmples 6
Exmples 6
Exmples * 6
Exmples * S ε S 6
Exmples * S ε S + S 6
Exmples * S ε S + S 6
Exmples * S ε S + (+)* S S ε S S 6
Exmples * S ε S + (+)* S S ε S S 6
Exmples * S ε S + (+)* S S ε S S * + * S A B A ε A B ε B 6
Exmples * S ε S + (+)* S S ε S S * + * S A B A ε A B ε B 6
Lnguges nd Grmmrs Any regulr lnguge hs regulr grmmr Any regulr grmmr genertes regulr lnguge 7
From NFA to Regulr Grmmr Hein Algorithm 11.11 1. Renme the sttes Q to set of upper-se letters 2. The strt symol of the grmmr is the nme of the strt stte q0. 3. For eh trnsition I J, rete the prodution I J. 4. For eh trnsition I ε J, rete the prodution I J. 5. For eh finl stte K, rete the prodution K ε. 8
Exmple A C W X 9
Exmple A C W X 10
Exmple A A A C W X 10
Exmple A A A C A C W X 10
Exmple A A A C A C A W W X 10
Exmple A A A C A C A W C C W X 10
Exmple A A A C A C A W C C C ε W X 10
Exmple A A A C A C A W C C C ε W X W X 10
Exmple A A A C A C A W C C C ε W X W X X ε 10
Exmple A C W X 11
Exmple A C A A C W W X 11
Exmple A C A A C W C C ε W X 11
Exmple A C A A C W C C ε W W X 11
From Regulr Grmmr to FSM Hein Algorithm 11.12 1. Trnsform the grmmr so tht ll produtions re of the form A x or A xb, where x is either single letter or ε. 2. The strt stte of the NFA is the grmmrʼs strt symol. 3. Crete stte QF nd dd it to the set F of finl sttes. 4. For eh prodution I J, rete the trnsition xxxxxxxxxxx 5. For eh prodution I J, rete the trnsition xxxxxxxxxxxx I J 6. For eh prodution K ε, dd K to the set of finl sttes F 7. For eh prodution I, rete the trnsition I I ε J Q F 12
Exmple S S B B ε B B 13
Exmple S S B S B ε B B 13
Exmple S S B S QF B ε B B 13
Exmple B S S B S QF B ε B B 13
Exmple B S S B S QF B ε B B 13
Exmple ε B S S B S QF B ε B B 13
Exmple ε B S S B S QF B ε B B 13
Exmple ε B S S B S QF B ε B B 13
Exmple ε B S S B S QF B ε B B Whtʼs the lnguge? 13
Exmple ε B S S B S QF B ε B B Whtʼs the lnguge? + * 13
Lnguge Indistinguishility Consider lnguge L over n lphet A. Two strings x, y A* re L- indistinguishle if for ll z A*, xz L whenever yz L. We write x L y L is n equivlene reltion The index of L is the numer of equivlene lsses indued y L 14
Exmple: L = + * + * = { ε,,,,,, } L? ε L? L? L? 15
Exmple: L = + * + * = { ε,,,,,, } L? ε L? L? L? Wht re the equivlene lsses of L? 1. {} 2. {,,,, } 3. {ε} 4. everything else 15
Myhill-Nerode Theorem The equivlene reltion L hrterizes extly wht the stte of n utomton tht epts L needs to rememer out the red portion of the input: if the red portion of the input is x, then the stte needs to rememer the equivlene lss [x]. This is suffiient, euse if x L y, then it does not mtter if the red portion of the input ws x or y; ll tht mtters (for deiding whether to ept or rejet) is the future portion of the input, sy z, euse xz L iff yz L. It is lso neessry, euse if x L y, then there is some possile future portion z of the input suh tht xz needs to e epted nd yz rejeted (or vie vers). 16
Theorem Sttement (Prt A) If the index of lnguge A is k, then there is k-stte DFA MA suh tht L(MA) = A 17
Mimimum-stte DFA For ny lnguge L, there is unique mimimum-stte DFA tht reognizes L unique mens unique up to n isomorphism, tht is, renming of the sttes. Any DFA n e trnsformed into minimum-stte DFA 18
Equivlent Sttes Two sttes p nd q in DFA m={q, Σ, q0, δ, F} re equivlent if, for ll z Σ*, ˆδ(p, z) δ*(p, w) is finl stte extly when ˆδ(q, z) δ*(q, w) is finl stte, i.e., p q iff z Σ.(ˆδ(p, z) F ) (ˆδ(q, z) F ) Is this n equivlene reltion? 19
How to Clulte Stte Equivlene, Exmple: 3 nd 4 re not equivlent (why)? First guess: 1 2 3 5 6 4 7 1, 2, 3, 5, 6 4, 7, This works for strings w of length 0 20
E0: 1, 2, 3, 5, 6 4, 7 2 3, 4 Tke sttes 1 nd 2 for ll single-hrter inputs, do we end up in 1 5 6 7 equivlent sttes? δ(1, ) = 2; δ(2, ) = 2, δ(1, ) = 5; δ(2, ) = 3. 5 3 in E0 So the pir 1, 2 stys in the sme equivlene lss in the next guess, E1 21
E0: 1, 2, 3, 5, 6 4, 7 2 3, 4 Wht out sttes 3 nd 5? 1 for ll single-hrter 5 6 7 inputs, do we end up in equivlent sttes?, δ(3, ) = 3; δ(5, ) = 6 δ(3, ) = 4; δ(5, ) = 5. 5 4 in E0 So the sttes 3 nd 5 re not in the sme equivlene lss in the next guess, E1 22 5
E1: 1, 2, 5 4, 7, 2 3 4 Wht out sttes 1 nd 5? 1 for ll single-hrter 5 6 7 inputs, do we end up in equivlent sttes?, δ(1, ) = 2; δ(5, ) = 6 δ(1, ) = 5; δ(5, ) = 5. 2 6 in E1 So the sttes 1 nd 5 re not in the sme equivlene lss in the next guess, E2 23 65
Then we repet to get the next guess E2 The equivlene reltion En represents sttes tht t in the sme wy fter reding input strings of length n Rememer, reltion is nothing more thn set of pirs. So we uild E0 E1 E2 When do we stop? 24
Minimizing DFA How n we esily ompute whether or not two sttes p nd q in DFA re equivlent? Suppose tht they re not equivlent: Then some (finite) string z will e epted when the mhine strts in p, nd rejeted when the mhine strts in q. p q iff z Σ.(ˆδ(p, z) F ) (ˆδ(q, z) F ) 25
Minimizing DFA How n we esily ompute whether or not two sttes p nd q in DFA re equivlent? Suppose tht they re not equivlent: Then some (finite) string z will e epted when the mhine strts in p, nd rejeted when the mhine strts in q. p q iff z Σ.(ˆδ(p, z) F ) (ˆδ(q, z) F ) p q iff z Σ.(ˆδ(p, z) F ) (ˆδ(q, z) F ) 25
Computing sets of equivlent sttes E 0 p, q where p F q F E 1 = E 0 \{ p, q Σ. δ(p, ), δ(q, ) E 0 }... E n+1 = E n \{ p, q Σ. δ(p, ), δ(q, ) E n } 26
Construting Miniml DFA Hein Constrution 11.10 Algorithm to Construt Minimum-Stte DFA (11.10) Given: Output: A DFA with set of sttes S nd trnsition tle T. Assume tht ll sttes tht nnot e rehed from the strt stte hve lredy een thrown wy. A minimum-stte DFA reognizing the sme regulr lnguge s the input DFA. 1. Construt the equivlent pirs of sttes y lulting the desending sequene of sets of pirs E 0! E 1!... defined s follows: E 0 = {{s, t} s nd t re distint nd either oth sttes re finl or oth sttes re nonfinl}. E i+1 = {{s, t} {s, t} " E i nd for every " A either T(s, ) = T(t, ) or or {T(s, ), T(t, )} " E i }. The omputtion stops when E k = E k+1 for some index k. E k is the desired set of equivlent pirs.
2. Use the equivlene reltion generted y the pirs in E k to prtition S into set of equivlene lsses. These equivlene lsses re the sttes of the new DFA. 3. The strt stte is the equivlene lss ontining the strt stte of the input DFA. 4. A finl stte is ny equivlene lss ontining finl stte of the input DFA. 5. The trnsition tle T min for the minimum-stte DFA is defined s follows, where [s] denotes the equivlene lss ontining s nd is ny letter: T min ([s], ) = [T(s, )].