Regular Grammar and Regular LanguageRegular Grammar and Regular Language[Def 3.1][Def 3.1] Regular GrammarRegular Grammar(use to in lexical analysis)(use to in lexical analysis)
Type 3 grammarType 3 grammar(regular grammar, RG)(regular grammar, RG)
1) A→tB1) A→tBt (right-linear, RLG)t (right-linear, RLG)
2) A→Bt2) A→Btt (left-linear, LLG)t (left-linear, LLG)
A, B V∈A, B V∈ NN, t V∈, t V∈ TT
[Ex] P : S→aA[Ex] P : S→aAbBbBb A→bAb A→bAa B→bSa B→bS
[Ex] P[Ex] P11 : S : S11→0S→0S11 SS11→0→0
PP22 : S : S22→1S→1S2 2 SS22→1→1
new P can be consist of P1 P2 {S→S1∪ ∪new P can be consist of P1 P2 {S→S1∪ ∪ S2}.S2}.
S →SS →S11SS22 (single production rule)(single production rule)
SS11→0S→0S110 0 SS22→1S→1S2211
∴∴ S →0SS →0S11001S1S221 1
SS11→0S→0S1100 SS22→1S→1S2211
Regular Grammar and Regular LanguageRegular Grammar and Regular Language[Def 3.1][Def 3.1] Regular GrammarRegular Grammar(use to in lexical analysis)(use to in lexical analysis)
Type 3 grammarType 3 grammar(regular grammar, RG)(regular grammar, RG)
1) A→tB1) A→tBt (right-linear, RLG)t (right-linear, RLG)
2) A→Bt2) A→Btt (left-linear, LLG)t (left-linear, LLG)
A, B V∈A, B V∈ NN, t V∈, t V∈ TT
[Ex] P : S→aA[Ex] P : S→aAbBbBb A→bAb A→bAa B→bSa B→bS
[Ex] P[Ex] P11 : S : S11→0S→0S11 SS11→0→0
PP22 : S : S22→1S→1S2 2 SS22→1→1
new P can be consist of P1 P2 {S→S1∪ ∪new P can be consist of P1 P2 {S→S1∪ ∪ S2}.S2}.
S →SS →S11SS22 (single production rule)(single production rule)
SS11→0S→0S110 0 SS22→1S→1S2211
∴∴ S →0SS →0S11001S1S221 1
SS11→0S→0S1100 SS22→1S→1S2211
Regular LanguageRegular LanguageRegular LanguageRegular Language
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
▶ ▶ A Regular Grammar is used to scanner.A Regular Grammar is used to scanner.
simple token structuresimple token structure RG is more effective than CFG.RG is more effective than CFG. It can be consist of small module to process front-end part.It can be consist of small module to process front-end part.
Regular ExpressionRegular Expression[Def 3.2][Def 3.2] Regular ExpressionRegular Expression 1. 1. : empty set : empty set 2. 2. : { : {}} 3. 3. aa V∈V∈ TT : { : {aa}}
4. if e4. if e11 is R.E of L is R.E of L11 and e and e22 is R.E of L is R.E of L22,,
(e(e11)) ++ (e(e22)) : represent to L: represent to L22 L∪L∪ 22
(e(e11) ) • • (e(e22)) : represent to L : represent to L11LL22
(e(e11))* * : represent to { : represent to {} L∪} L∪ 1111 L∪L∪ 11
22∪∪...... L∪L∪ 11nn∪∪……
5. other cases are not a R.E.5. other cases are not a R.E.
▶ ▶ A Regular Grammar is used to scanner.A Regular Grammar is used to scanner.
simple token structuresimple token structure RG is more effective than CFG.RG is more effective than CFG. It can be consist of small module to process front-end part.It can be consist of small module to process front-end part.
Regular ExpressionRegular Expression[Def 3.2][Def 3.2] Regular ExpressionRegular Expression 1. 1. : empty set : empty set 2. 2. : { : {}} 3. 3. aa V∈V∈ TT : { : {aa}}
4. if e4. if e11 is R.E of L is R.E of L11 and e and e22 is R.E of L is R.E of L22,,
(e(e11)) ++ (e(e22)) : represent to L: represent to L22 L∪L∪ 22
(e(e11) ) • • (e(e22)) : represent to L : represent to L11LL22
(e(e11))* * : represent to { : represent to {} L∪} L∪ 1111 L∪L∪ 11
22∪∪...... L∪L∪ 11nn∪∪……
5. other cases are not a R.E.5. other cases are not a R.E.
[Ex][Ex] 1) R.E (0+1)1) R.E (0+1)**
2) R.E (a+b)2) R.E (a+b)**abbabb
[Def 3.3][Def 3.3] If two R.Es are represent to same set, there are same R.Es.If two R.Es are represent to same set, there are same R.Es.▶▶ algebraic propertyalgebraic property precedenceprecedence : : ++ < < •• < < ** ⓐ ⓐ ++ = = ++ (ⓑ (ⓑ )) = = (()) ⓒ ⓒ ((++)+)+ = = +( +(++) ) ⓓ ⓓ ((++) = ) = + + ⓔ ⓔ ((++)) = = ++ ⓕ ⓕ ++ = = ⓖ ⓖ ++ = = ⓗ ⓗ = = = = ⓘ ⓘ = = = = ⓙ ⓙ * * = = ++**
ⓚ ⓚ * * = (= (++))** (ⓛ (ⓛ **))* * = = **
ⓜ ⓜ **++ = = ** ⓝ ⓝ **+++ + = = ** ⓞ ⓞ ((++))* * = (= (****))** (ⓟ (ⓟ ))* * = (= (**++**))** ⓠ ⓠ * * = = ⓡ ⓡ * * = =
Regular Grammar Regular Grammar Regular Expression Regular Expression
[Def 3.4][Def 3.4] regular expression equationregular expression equation : The Equation that coefficient is R.E.: The Equation that coefficient is R.E.
[Def 3.3][Def 3.3] If two R.Es are represent to same set, there are same R.Es.If two R.Es are represent to same set, there are same R.Es.▶▶ algebraic propertyalgebraic property precedenceprecedence : : ++ < < •• < < ** ⓐ ⓐ ++ = = ++ (ⓑ (ⓑ )) = = (()) ⓒ ⓒ ((++)+)+ = = +( +(++) ) ⓓ ⓓ ((++) = ) = + + ⓔ ⓔ ((++)) = = ++ ⓕ ⓕ ++ = = ⓖ ⓖ ++ = = ⓗ ⓗ = = = = ⓘ ⓘ = = = = ⓙ ⓙ * * = = ++**
ⓚ ⓚ * * = (= (++))** (ⓛ (ⓛ **))* * = = **
ⓜ ⓜ **++ = = ** ⓝ ⓝ **+++ + = = ** ⓞ ⓞ ((++))* * = (= (****))** (ⓟ (ⓟ ))* * = (= (**++**))** ⓠ ⓠ * * = = ⓡ ⓡ * * = =
Regular Grammar Regular Grammar Regular Expression Regular Expression
[Def 3.4][Def 3.4] regular expression equationregular expression equation : The Equation that coefficient is R.E.: The Equation that coefficient is R.E.
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Theorem 3.1][Theorem 3.1] if if , , is R.E and is R.E and LL, then solution of X= , then solution of X= X+ X+ is X= is X= * * .. X= X= X+X+ = = ((**)+)+ = = ++++ = (= (++++)) = = **
▶▶ Search processing step to R.E of L(G) in LSearch processing step to R.E of L(G) in L
1.1. regular grammar → RE equation regular grammar → RE equation
2.2. X= X= X+X+ → X= → X= **
3.3. RE equation X is replace to RE equation X is replace to ** in RE equation,in RE equation, then construct to X= then construct to X= X+X+ form.form.
4.4. repeat step 3, then solve to X in X= repeat step 3, then solve to X in X= X+X+
[Ex] G=({S, R}, {a, b}, P, S) P: S→aS[Ex] G=({S, R}, {a, b}, P, S) P: S→aSbRbR R→aS ; R→aS ; What is a RE of L(G)?What is a RE of L(G)? ans)ans) 1.1. grammar → R.E equation grammar → R.E equation
S = aS + bR + S = aS + bR + ------ ①------ ① R = aSR = aS ------ ②------ ②
2.2. Non Non3.3. assign to ② ① assign to ② ① S = aS + b(aS) + S = aS + b(aS) + = aS + baS + = aS + baS + = (a + ba)S + = (a + ba)S + 4.4. L(G) is S = (a + ba)S + L(G) is S = (a + ba)S + = (a + ba) = (a + ba)** ∴∴ L(G) = (a + ba)L(G) = (a + ba)**
[Theorem 3.1][Theorem 3.1] if if , , is R.E and is R.E and LL, then solution of X= , then solution of X= X+ X+ is X= is X= * * .. X= X= X+X+ = = ((**)+)+ = = ++++ = (= (++++)) = = **
▶▶ Search processing step to R.E of L(G) in LSearch processing step to R.E of L(G) in L
1.1. regular grammar → RE equation regular grammar → RE equation
2.2. X= X= X+X+ → X= → X= **
3.3. RE equation X is replace to RE equation X is replace to ** in RE equation,in RE equation, then construct to X= then construct to X= X+X+ form.form.
4.4. repeat step 3, then solve to X in X= repeat step 3, then solve to X in X= X+X+
[Ex] G=({S, R}, {a, b}, P, S) P: S→aS[Ex] G=({S, R}, {a, b}, P, S) P: S→aSbRbR R→aS ; R→aS ; What is a RE of L(G)?What is a RE of L(G)? ans)ans) 1.1. grammar → R.E equation grammar → R.E equation
S = aS + bR + S = aS + bR + ------ ①------ ① R = aSR = aS ------ ②------ ②
2.2. Non Non3.3. assign to ② ① assign to ② ① S = aS + b(aS) + S = aS + b(aS) + = aS + baS + = aS + baS + = (a + ba)S + = (a + ba)S + 4.4. L(G) is S = (a + ba)S + L(G) is S = (a + ba)S + = (a + ba) = (a + ba)** ∴∴ L(G) = (a + ba)L(G) = (a + ba)**
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
Finite Automata(FA)Finite Automata(FA)▶▶ ConstructionConstruction
input/outputinput/output finite statefinite state control mechanism(set of transition)control mechanism(set of transition)
[Def 3.5][Def 3.5] FA M = (Q, FA M = (Q, , , , , qq00, F), F)
Q Q : finite set of states: finite set of states : set of input alphabet: set of input alphabet qq00 : initial state: initial state
F F : set of finial state, F Q⊆: set of finial state, F Q⊆ : transition function Q×Σ→2: transition function Q×Σ→2QQ
eg) eg) ((qq, , aa) = {) = {pp11, , pp22, ··· , , ··· , ppnn}}
⇒⇒ after scanning input symbol 'after scanning input symbol 'aa' in state ' in state qq,, next state select to state next state select to state pp11~ ~ ppnn
Finite Automata(FA)Finite Automata(FA)▶▶ ConstructionConstruction
input/outputinput/output finite statefinite state control mechanism(set of transition)control mechanism(set of transition)
[Def 3.5][Def 3.5] FA M = (Q, FA M = (Q, , , , , qq00, F), F)
Q Q : finite set of states: finite set of states : set of input alphabet: set of input alphabet qq00 : initial state: initial state
F F : set of finial state, F Q⊆: set of finial state, F Q⊆ : transition function Q×Σ→2: transition function Q×Σ→2QQ
eg) eg) ((qq, , aa) = {) = {pp11, , pp22, ··· , , ··· , ppnn}}
⇒⇒ after scanning input symbol 'after scanning input symbol 'aa' in state ' in state qq,, next state select to state next state select to state pp11~ ~ ppnn
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
DFA(Deterministic FA)DFA(Deterministic FA) next state is determined to unique statenext state is determined to unique state
((qq, , aa) = ) = pp
[Def 3.6][Def 3.6] DFA M = (Q, DFA M = (Q, , , , , qq00, F), F)
Q Q : finite set of states: finite set of states : set of input alphabet: set of input alphabet qq00 : initial state: initial state
F F : set of finial state, F Q⊆: set of finial state, F Q⊆ : transition function Q× : transition function Q× → Q ≡ → Q ≡ ((qq, , aa) = ) = pp
▶ ▶ DFA propertiesDFA properties 1.1. no no -transition-transition 2.2. determined to unique state determined to unique state
▶ ▶ Extensibility of transition functionExtensibility of transition function Q× Q× * * → Q→ Q ((qq, , ) = ) = qq ((qq, , xaxa) = ) = ((((qq, , xx), ), aa) x∈) x∈**, , aa∈∈ ⇒⇒ extensionextension : : symbolsymbol → → stringstring
DFA(Deterministic FA)DFA(Deterministic FA) next state is determined to unique statenext state is determined to unique state
((qq, , aa) = ) = pp
[Def 3.6][Def 3.6] DFA M = (Q, DFA M = (Q, , , , , qq00, F), F)
Q Q : finite set of states: finite set of states : set of input alphabet: set of input alphabet qq00 : initial state: initial state
F F : set of finial state, F Q⊆: set of finial state, F Q⊆ : transition function Q× : transition function Q× → Q ≡ → Q ≡ ((qq, , aa) = ) = pp
▶ ▶ DFA propertiesDFA properties 1.1. no no -transition-transition 2.2. determined to unique state determined to unique state
▶ ▶ Extensibility of transition functionExtensibility of transition function Q× Q× * * → Q→ Q ((qq, , ) = ) = qq ((qq, , xaxa) = ) = ((((qq, , xx), ), aa) x∈) x∈**, , aa∈∈ ⇒⇒ extensionextension : : symbolsymbol → → stringstring
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Def 3.7][Def 3.7] L(M) that recognized DFA ML(M) that recognized DFA ML(M) = {xL(M) = {x(( q q00,, x x) F}∈) F}∈
[Ex] M=({[Ex] M=({pp, , qq, , rr}, {0, 1}, }, {0, 1}, δδ, , pp, {, {rr})}) : : ((pp, 0) = , 0) = qq ((pp, 1) = , 1) = pp ((qq, 0) = , 0) = rr ((qq, 1) = , 1) = pp
((rr, 0) = , 0) = rr ((rr, 1) = , 1) = rr Can be recognized sentence "1001", "0110"?Can be recognized sentence "1001", "0110"?
ans)ans) 1) 1) ((pp, 1001) = , 1001) = ((pp, 001) = , 001) = ((qq, 01) = , 01) = ((rr, 1) = , 1) = rr F ∈F ∈ ∴∴ acceptaccept 2) 2) ((pp, 0110) = , 0110) = ((qq, 110) = , 110) = ((pp, 10) = , 10) = ((pp, 0) = , 0) = qqF F ∴∴ rejectreject
※※ transition tabletransition table : transition function is represented to matrix : transition function is represented to matrix[Ex] Transition Table[Ex] Transition Table
00 11
pp qq p p
qq rr pp
rr rr rr
[Def 3.7][Def 3.7] L(M) that recognized DFA ML(M) that recognized DFA ML(M) = {xL(M) = {x(( q q00,, x x) F}∈) F}∈
[Ex] M=({[Ex] M=({pp, , qq, , rr}, {0, 1}, }, {0, 1}, δδ, , pp, {, {rr})}) : : ((pp, 0) = , 0) = qq ((pp, 1) = , 1) = pp ((qq, 0) = , 0) = rr ((qq, 1) = , 1) = pp
((rr, 0) = , 0) = rr ((rr, 1) = , 1) = rr Can be recognized sentence "1001", "0110"?Can be recognized sentence "1001", "0110"?
ans)ans) 1) 1) ((pp, 1001) = , 1001) = ((pp, 001) = , 001) = ((qq, 01) = , 01) = ((rr, 1) = , 1) = rr F ∈F ∈ ∴∴ acceptaccept 2) 2) ((pp, 0110) = , 0110) = ((qq, 110) = , 110) = ((pp, 10) = , 10) = ((pp, 0) = , 0) = qqF F ∴∴ rejectreject
※※ transition tabletransition table : transition function is represented to matrix : transition function is represented to matrix[Ex] Transition Table[Ex] Transition Table
00 11
pp qq p p
qq rr pp
rr rr rr
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Def 3.8][Def 3.8] transition diagramtransition diagram statestate : node : node transition functiontransition function : label, directed arc : label, directed arc eg) eg) ((qq, , aa) = ) = pp a a
≡ ≡ ⓠ ⓟ ⓠ ⓟ
final statefinal state : double circle : double circle
[Ex] What is a transition diagram for example before?[Ex] What is a transition diagram for example before?
[Def 3.9][Def 3.9] completely specifiedcompletely specified if ∀if ∀qq Q, ∈Q, ∈ aa∈∈ in M=(Q, in M=(Q, , , , , qq00, F), , F), ((qq, , aa) is existed uniquely next state) is existed uniquely next state
∴ ∴ DFA ≡ completely specifiedDFA ≡ completely specified
[Def 3.8][Def 3.8] transition diagramtransition diagram statestate : node : node transition functiontransition function : label, directed arc : label, directed arc eg) eg) ((qq, , aa) = ) = pp a a
≡ ≡ ⓠ ⓟ ⓠ ⓟ
final statefinal state : double circle : double circle
[Ex] What is a transition diagram for example before?[Ex] What is a transition diagram for example before?
[Def 3.9][Def 3.9] completely specifiedcompletely specified if ∀if ∀qq Q, ∈Q, ∈ aa∈∈ in M=(Q, in M=(Q, , , , , qq00, F), , F), ((qq, , aa) is existed uniquely next state) is existed uniquely next state
∴ ∴ DFA ≡ completely specifiedDFA ≡ completely specified
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
startstartp q r
11
11
00 00
0, 10, 1
▶▶ AAlgorithm that string is recognized by DFA M = (Q, lgorithm that string is recognized by DFA M = (Q, , , , , qq00, F), F)
Algorithm_Recognize()Algorithm_Recognize() { { current_statecurrent_state = = qq00;;
get(get(next_symbolnext_symbol);); while(while(input string not exhaustedinput string not exhausted) {) { current_statecurrent_state = = ((current_statecurrent_state, , next_symbolnext_symbol);); get(get(next_symbolnext_symbol);); }} if(if(current_statecurrent_state F) printf("∈F) printf("∈ AcceptAccept");"); else printf("else printf("RejectReject");"); }}
NFA(Non-deterministic FA)NFA(Non-deterministic FA)
[Def 3.10][Def 3.10] NFA M = (Q, NFA M = (Q, , , , , qq00, F), F)
Q Q : finite set of states: finite set of states : set of input alphabet: set of input alphabet qq00 : initial state: initial state
F F : set of finial state, F Q⊆: set of finial state, F Q⊆ : transition function Q×: transition function Q× → 2 → 2QQ, , ((qq, , aa) = {) = {pp11, , pp22, ···, , ···, ppnn}}
▶▶ AAlgorithm that string is recognized by DFA M = (Q, lgorithm that string is recognized by DFA M = (Q, , , , , qq00, F), F)
Algorithm_Recognize()Algorithm_Recognize() { { current_statecurrent_state = = qq00;;
get(get(next_symbolnext_symbol);); while(while(input string not exhaustedinput string not exhausted) {) { current_statecurrent_state = = ((current_statecurrent_state, , next_symbolnext_symbol);); get(get(next_symbolnext_symbol);); }} if(if(current_statecurrent_state F) printf("∈F) printf("∈ AcceptAccept");"); else printf("else printf("RejectReject");"); }}
NFA(Non-deterministic FA)NFA(Non-deterministic FA)
[Def 3.10][Def 3.10] NFA M = (Q, NFA M = (Q, , , , , qq00, F), F)
Q Q : finite set of states: finite set of states : set of input alphabet: set of input alphabet qq00 : initial state: initial state
F F : set of finial state, F Q⊆: set of finial state, F Q⊆ : transition function Q×: transition function Q× → 2 → 2QQ, , ((qq, , aa) = {) = {pp11, , pp22, ···, , ···, ppnn}}
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Ex] [Ex] What is transition diagram of M=({What is transition diagram of M=({qq00, q, q11, q, q22, q, q33, q, qff}, {0, 1}, }, {0, 1}, , , qq00, {, {qqff})?})?
0 0 1 1qq0 0 {{ q q11, q, q2 2 } {} { q q11, q, q3 3 }}
qq1 1 {{ q q11, q, q2 2 } {} { q q11, q, q3 3 }}
qq2 2 {{ q qf f }} qq3 3 { { qqf f }}
qqff { { q qf f } } { { q qf f }} ※※ ((qq, , aa) = ) = : : not definednot defined
[Ex] [Ex] What is transition diagram of M=({What is transition diagram of M=({qq00, q, q11, q, q22, q, q33, q, qff}, {0, 1}, }, {0, 1}, , , qq00, {, {qqff})?})?
0 0 1 1qq0 0 {{ q q11, q, q2 2 } {} { q q11, q, q3 3 }}
qq1 1 {{ q q11, q, q2 2 } {} { q q11, q, q3 3 }}
qq2 2 {{ q qf f }} qq3 3 { { qqf f }}
qqff { { q qf f } } { { q qf f }} ※※ ((qq, , aa) = ) = : : not definednot defined
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
startstartqq
00
1111
0000
0, 10, 1
qq11
0, 10, 1qq
ff
qq22
qq33
11
0, 10, 1
▶▶ Extension of NFAExtension of NFA
[step 1][step 1] Q×Q× → 2 → 2QQ Q× Q× * * → 2→ 2QQ
((qq, , ) = {) = {qq}} ((qq, , xaxa) = ∪) = ∪ ((pp, , aa)) p∈p∈((qq, , xx))
xx∈∈**, , aa∈∈ ∴∴ symbolsymbol → → extend to stringextend to string
[step 2][step 2] Q×Q× → 2 → 2QQ 2 2QQ××* * → 2→ 2QQ
({({ p p11, , pp22, ···, , ···, ppkk}, }, xx) = ) = ((ppi i , , xx))
∴∴ one stateone state → → extended to more stateextended to more state
[Ex] [Ex] ((qq00, 1001), 1001) = = ((qq11, 001)∪, 001)∪((qq33, 001) , 001)
= = ((qq11, 01) ∪, 01) ∪ ((qq22, 01)∪, 01)∪
= = ((qq11, 1)∪, 1)∪((qq22, 1)∪, 1)∪((qqff, 1), 1)
= {= {qq11, q, q33, q, qff } }
▶▶ Extension of NFAExtension of NFA
[step 1][step 1] Q×Q× → 2 → 2QQ Q× Q× * * → 2→ 2QQ
((qq, , ) = {) = {qq}} ((qq, , xaxa) = ∪) = ∪ ((pp, , aa)) p∈p∈((qq, , xx))
xx∈∈**, , aa∈∈ ∴∴ symbolsymbol → → extend to stringextend to string
[step 2][step 2] Q×Q× → 2 → 2QQ 2 2QQ××* * → 2→ 2QQ
({({ p p11, , pp22, ···, , ···, ppkk}, }, xx) = ) = ((ppi i , , xx))
∴∴ one stateone state → → extended to more stateextended to more state
[Ex] [Ex] ((qq00, 1001), 1001) = = ((qq11, 001)∪, 001)∪((qq33, 001) , 001)
= = ((qq11, 01) ∪, 01) ∪ ((qq22, 01)∪, 01)∪
= = ((qq11, 1)∪, 1)∪((qq22, 1)∪, 1)∪((qqff, 1), 1)
= {= {qq11, q, q33, q, qff } }
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Def 3.11][Def 3.11] L(M) that is recognized by NFA ML(M) that is recognized by NFA M L(M) = {L(M) = {xx((qq00, , xx)∩F≠)∩F≠}}
[Ex] Can be recognized to 1011?[Ex] Can be recognized to 1011? ((qq00, 1011) = , 1011) = ({({qq11, q, q33}, 011) = }, 011) = ({({qq11, q, q22}, 11)}, 11)
= = ({({qq11, q, q33}, 1) = {}, 1) = {qq11, q, q33, q, qff}}
{{qq11, q, q33, q, qff}∩{}∩{qqff}≠}≠
∴∴ 10111011 is accepted by Mis accepted by M
if one more than final state is exist, the string is accepted.if one more than final state is exist, the string is accepted.
state of NFA M : when number of state of NFA M : when number of mm, length(string) = , length(string) = nn number of maximum nodes in tree = number of maximum nodes in tree = mmnn
∴∴ string that is recognized in NFA, excessively time-consumedstring that is recognized in NFA, excessively time-consumed
[Def 3.11][Def 3.11] L(M) that is recognized by NFA ML(M) that is recognized by NFA M L(M) = {L(M) = {xx((qq00, , xx)∩F≠)∩F≠}}
[Ex] Can be recognized to 1011?[Ex] Can be recognized to 1011? ((qq00, 1011) = , 1011) = ({({qq11, q, q33}, 011) = }, 011) = ({({qq11, q, q22}, 11)}, 11)
= = ({({qq11, q, q33}, 1) = {}, 1) = {qq11, q, q33, q, qff}}
{{qq11, q, q33, q, qff}∩{}∩{qqff}≠}≠
∴∴ 10111011 is accepted by Mis accepted by M
if one more than final state is exist, the string is accepted.if one more than final state is exist, the string is accepted.
state of NFA M : when number of state of NFA M : when number of mm, length(string) = , length(string) = nn number of maximum nodes in tree = number of maximum nodes in tree = mmnn
∴∴ string that is recognized in NFA, excessively time-consumedstring that is recognized in NFA, excessively time-consumed
Transformation from NFA to DFATransformation from NFA to DFA
A NFA represent language structure simply(RE→NFA), A NFA represent language structure simply(RE→NFA), but difficult to implementation.but difficult to implementation.
[Theorem 3.2][Theorem 3.2] Let NFA M=(Q, Let NFA M=(Q, , , , , qq00, F)., F).
if L(M) is accepted by NFA M, DFA M'=(Q', if L(M) is accepted by NFA M, DFA M'=(Q', ', ', ', ', qq00', F') is existed.', F') is existed.
≡ ≡ construct DFA M'=(Q', construct DFA M'=(Q', , , ', ', qq00', F')', F')
① ① Q' = 2Q' = 2Q Q (power set of Q), [presented a state Q : [(power set of Q), [presented a state Q : [qq11, q, q22,···, q,···, qii], ], qqii Q]∈Q]∈
② ② qq00'=['=[qq00]]
③ ③ F'={F'={qq Q'∈Q'∈ qq F}∈F}∈ ④ ④ if if ({({ q q11, q, q22,···, q,···, qii}, }, aa)={)={pp11, p, p22,···, p,···, pjj}, then }, then '(['([qq11, q, q22,···, q,···, qii], ], aa)=[)=[ p p11, p, p22,···, p,···, pjj]]
Transformation from NFA to DFATransformation from NFA to DFA
A NFA represent language structure simply(RE→NFA), A NFA represent language structure simply(RE→NFA), but difficult to implementation.but difficult to implementation.
[Theorem 3.2][Theorem 3.2] Let NFA M=(Q, Let NFA M=(Q, , , , , qq00, F)., F).
if L(M) is accepted by NFA M, DFA M'=(Q', if L(M) is accepted by NFA M, DFA M'=(Q', ', ', ', ', qq00', F') is existed.', F') is existed.
≡ ≡ construct DFA M'=(Q', construct DFA M'=(Q', , , ', ', qq00', F')', F')
① ① Q' = 2Q' = 2Q Q (power set of Q), [presented a state Q : [(power set of Q), [presented a state Q : [qq11, q, q22,···, q,···, qii], ], qqii Q]∈Q]∈
② ② qq00'=['=[qq00]]
③ ③ F'={F'={qq Q'∈Q'∈ qq F}∈F}∈ ④ ④ if if ({({ q q11, q, q22,···, q,···, qii}, }, aa)={)={pp11, p, p22,···, p,···, pjj}, then }, then '(['([qq11, q, q22,···, q,···, qii], ], aa)=[)=[ p p11, p, p22,···, p,···, pjj]]
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Ex 21] NFA M=({[Ex 21] NFA M=({qq00, , qq11}, {0, 1}, }, {0, 1}, , , qq00, {, {qq11})})
state name change :state name change : [ [qq00]=A, []=A, [qq11]=B, []=B, [qq00, , qq11]=C]=C
[Ex 22], [Ex 23] [Ex 22], [Ex 23] Reference of Text Reference of Text pp.78 ~ pp.80pp.78 ~ pp.80
[Ex 21] NFA M=({[Ex 21] NFA M=({qq00, , qq11}, {0, 1}, }, {0, 1}, , , qq00, {, {qq11})})
state name change :state name change : [ [qq00]=A, []=A, [qq11]=B, []=B, [qq00, , qq11]=C]=C
[Ex 22], [Ex 23] [Ex 22], [Ex 23] Reference of Text Reference of Text pp.78 ~ pp.80pp.78 ~ pp.80
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
’ ’ 0 10 1
[[qq00] [] [qq00, , qq11] [] [qq00] ]
[[qq00] ] [ [qq00, , qq11] ]
[[qq00, , qq11] [] [qq00, , qq11] ] [ [qq00, , qq11] ]
0 10 1
qq00 { {qq00, , qq11} {} {qq00} }
qq11 { {qq00, , qq11}}
AAAA BBBB
CCCC
startstartstartstart 1111
0000
1111
0,10,1
AAAAstartstartstartstart
1111
CCCC0,10,10000
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Def 3.12][Def 3.12] -NFA M=(Q, -NFA M=(Q, , , , , qq00, F) :, F) :
: Q(: Q( {∪{∪ }) → 2}) → 2QQ
other cases are same to NFA. other cases are same to NFA.
[Def 3.13][Def 3.13] -CLOSURE(-CLOSURE(ss)) case of case of ss that is one state that is one state -CLOSURE(-CLOSURE(ss)) = include itself state = include itself state ss and and all reachable statesall reachable states by label by label from state from state ss (repeat until not changes elements of (repeat until not changes elements of -CLOSURE(-CLOSURE(ss))))
case of case of TT that is one or more states that is one or more states -CLOSURE(-CLOSURE(TT)) = each state in = each state in T, T, executing step executing step and union its all results and union its all results ((-CLOSURE(-CLOSURE(TT)) = ∪)) = ∪ -CLOSURE(-CLOSURE(xx) ) qq∈∈TT
[Def 3.12][Def 3.12] -NFA M=(Q, -NFA M=(Q, , , , , qq00, F) :, F) :
: Q(: Q( {∪{∪ }) → 2}) → 2QQ
other cases are same to NFA. other cases are same to NFA.
[Def 3.13][Def 3.13] -CLOSURE(-CLOSURE(ss)) case of case of ss that is one state that is one state -CLOSURE(-CLOSURE(ss)) = include itself state = include itself state ss and and all reachable statesall reachable states by label by label from state from state ss (repeat until not changes elements of (repeat until not changes elements of -CLOSURE(-CLOSURE(ss))))
case of case of TT that is one or more states that is one or more states -CLOSURE(-CLOSURE(TT)) = each state in = each state in T, T, executing step executing step and union its all results and union its all results ((-CLOSURE(-CLOSURE(TT)) = ∪)) = ∪ -CLOSURE(-CLOSURE(xx) ) qq∈∈TT
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Ex 24] [Ex 24] What is CLOSURE in following What is CLOSURE in following -NFA?-NFA?
CLOSURE(A)={A, B, D}CLOSURE(A)={A, B, D} CLOSURE({A, C})=CLOSURE(A) CLOSURE({A, C})=CLOSURE(A) CLOSURE(C)={A, B, C, D} CLOSURE(C)={A, B, C, D}
[Ex 25] [Ex 25] Transform fromTransform from -NFA to DFA-NFA to DFA
[Ex 24] [Ex 24] What is CLOSURE in following What is CLOSURE in following -NFA?-NFA?
CLOSURE(A)={A, B, D}CLOSURE(A)={A, B, D} CLOSURE({A, C})=CLOSURE(A) CLOSURE({A, C})=CLOSURE(A) CLOSURE(C)={A, B, C, D} CLOSURE(C)={A, B, C, D}
[Ex 25] [Ex 25] Transform fromTransform from -NFA to DFA-NFA to DFA
aaaaaaaa
AAAAstartstartstartstart
BBBB
bbbb
aaaa
CCCC DDDD
aaaa
startstartstartstart
cccc
3333
bbbb
1111 4444
2222
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
state name :state name : A=[1,3,4], B=[2], C=[3,4], D=[4] A=[1,3,4], B=[2], C=[3,4], D=[4]state name :state name : A=[1,3,4], B=[2], C=[3,4], D=[4] A=[1,3,4], B=[2], C=[3,4], D=[4]
a b ca b c
CL(1)={1,3,4}CL(1)={1,3,4} [1,3,4][1,3,4]
[2][2]
[3,4][3,4]
[4] [4]
CL(2)={2} CL(3)={3,4}CL(2)={2} CL(3)={3,4} [2] [2] [3,4] [3,4]
CL(4)={4} CL(4)={4} [4][4]
CL(3)={3,4}CL(3)={3,4} [3,4][3,4]
AA
CC
startstartstartstart aaaaDDBB bbbb
cccc cccc
DFA's state minimizationDFA's state minimization
use of equivalence relation use of equivalence relation →→ state merge state merge
▶▶ ≡≡(equivalence relation)(equivalence relation) p≡q iff p≡q iff δδ((pp, , xx) is an accepting state) is an accepting state iff iff δδ((qq, , xx) is an accepting state) is an accepting state
[Def 3.14][Def 3.14] distinguishdistinguish if an ∃if an ∃ xx such that such that δδ((pp, , xx)=)=rr, , δδ((qq, , xx)=)=tt, a final state is only one of , a final state is only one of rr and and tt..
▶▶ inaccessible stateinaccessible state : only existed out arc, except start state : only existed out arc, except start state
▶▶ minimizing methodminimizing method 1. eliminate inaccessible state1. eliminate inaccessible state 2. grouping final states and non-final states2. grouping final states and non-final states 3. divide state that going to another equivalence class by same input symbol.3. divide state that going to another equivalence class by same input symbol. 4. repeat step 3 when can not divided4. repeat step 3 when can not divided
DFA's state minimizationDFA's state minimization
use of equivalence relation use of equivalence relation →→ state merge state merge
▶▶ ≡≡(equivalence relation)(equivalence relation) p≡q iff p≡q iff δδ((pp, , xx) is an accepting state) is an accepting state iff iff δδ((qq, , xx) is an accepting state) is an accepting state
[Def 3.14][Def 3.14] distinguishdistinguish if an ∃if an ∃ xx such that such that δδ((pp, , xx)=)=rr, , δδ((qq, , xx)=)=tt, a final state is only one of , a final state is only one of rr and and tt..
▶▶ inaccessible stateinaccessible state : only existed out arc, except start state : only existed out arc, except start state
▶▶ minimizing methodminimizing method 1. eliminate inaccessible state1. eliminate inaccessible state 2. grouping final states and non-final states2. grouping final states and non-final states 3. divide state that going to another equivalence class by same input symbol.3. divide state that going to another equivalence class by same input symbol. 4. repeat step 3 when can not divided4. repeat step 3 when can not divided
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
▶▶ new DFA M'= (Q', Σ, new DFA M'= (Q', Σ, δδ', ', qq00', F')', F')
1. Q1. Q'' : set of equivalence class : set of equivalence class eg)eg) [[qq] : equivalence class include ] : equivalence class include qq
2. let [2. let [pp], [], [qq] are equivalence class, if ] are equivalence class, if δδ((pp, , aa) = ) = qq, then , then δδ'(['([qq], ], aa) = [) = [qq].].
3. 3. qq00' is [' is [qq00]]
4. F'= {[4. F'= {[qq]] qq F}∈F}∈
[Ex 26] [Ex 26] DFA's minimizationDFA's minimization
▶▶ new DFA M'= (Q', Σ, new DFA M'= (Q', Σ, δδ', ', qq00', F')', F')
1. Q1. Q'' : set of equivalence class : set of equivalence class eg)eg) [[qq] : equivalence class include ] : equivalence class include qq
2. let [2. let [pp], [], [qq] are equivalence class, if ] are equivalence class, if δδ((pp, , aa) = ) = qq, then , then δδ'(['([qq], ], aa) = [) = [qq].].
3. 3. qq00' is [' is [qq00]]
4. F'= {[4. F'= {[qq]] qq F}∈F}∈
[Ex 26] [Ex 26] DFA's minimizationDFA's minimization
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
aaaa
aaaa
AAstartstartstartstart
DD
BB
bbbb
aaaa
EEbbbb
aaaaCC
aaaa
bbbbbbbb
bbbb
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
1 : {A, B, D}1 : {A, B, D} 2 : {C, E}2 : {C, E}
aa
bb
1 1 11 1 11 2 21 2 2
1 11 12 22 2
1 : {A}1 : {A} 2 : {B, D}2 : {B, D}
aa
bb
2222
2 22 23 33 3
3 : {C, E}3 : {C, E}
2 22 23 33 3
state name :state name : X=[A], Y=[B, D], Z=[C, E] X=[A], Y=[B, D], Z=[C, E]state name :state name : X=[A], Y=[B, D], Z=[C, E] X=[A], Y=[B, D], Z=[C, E]
XXXX YYYY ZZZZstartstartstartstart
aaaa
aaaa
a, ba, ba, ba, b bbbb
bbbb
aa bb
XX
YY
ZZ
YY
YY
YY
δδ''
YY
ZZ
ZZ
A Close Properties of Finite AutomataA Close Properties of Finite Automata
[Theorem 3.3][Theorem 3.3] if Lif L11, L, L22 are R.L. are R.L.
1. L1. L11 L∪ L∪ 22
2. L2. L11·L·L22
3. L3. L11** are R.L are R.L
Proof)Proof) case 1.case 1. LL11, L, L22 are R.L, such that exist G are R.L, such that exist G11=(V=(VN1N1, V, VT1T1, P, P11, S, S11), G), G22=(V=(VN2N2, V, VT2T2, P, P22, S, S22))
∴ ∴ LL11 = L(G = L(G11), L), L22 = L(G = L(G22))
It can be consist of new Grammar G.It can be consist of new Grammar G. G=(G=(VVN1N1 V∪V∪ N2N2 {S}∪{S}∪ , , VVT1T1 V∪V∪ T2T2, , PP11 P∪P∪ 22 {S→S∪{S→S∪ 11 || SS22}}, , SS),),
S S⇒S S⇒ 11⇒⇒11 L(G∈L(G∈ 11) ) oror S S⇒ S S⇒ 22⇒⇒22 L(G∈L(G∈ 22).).
∴ ∴ L(G) = L(GL(G) = L(G11) L(G∪) L(G∪ 22) = L) = L11 L∪L∪ 22
G is R.G L⇒G is R.G L⇒ 11 L∪ L∪ 22 is R.L. is R.L.
case 2, 3.case 2, 3. Same. Same. ∴∴ R.L is closed to union, concatenation, closureR.L is closed to union, concatenation, closure
A Close Properties of Finite AutomataA Close Properties of Finite Automata
[Theorem 3.3][Theorem 3.3] if Lif L11, L, L22 are R.L. are R.L.
1. L1. L11 L∪ L∪ 22
2. L2. L11·L·L22
3. L3. L11** are R.L are R.L
Proof)Proof) case 1.case 1. LL11, L, L22 are R.L, such that exist G are R.L, such that exist G11=(V=(VN1N1, V, VT1T1, P, P11, S, S11), G), G22=(V=(VN2N2, V, VT2T2, P, P22, S, S22))
∴ ∴ LL11 = L(G = L(G11), L), L22 = L(G = L(G22))
It can be consist of new Grammar G.It can be consist of new Grammar G. G=(G=(VVN1N1 V∪V∪ N2N2 {S}∪{S}∪ , , VVT1T1 V∪V∪ T2T2, , PP11 P∪P∪ 22 {S→S∪{S→S∪ 11 || SS22}}, , SS),),
S S⇒S S⇒ 11⇒⇒11 L(G∈L(G∈ 11) ) oror S S⇒ S S⇒ 22⇒⇒22 L(G∈L(G∈ 22).).
∴ ∴ L(G) = L(GL(G) = L(G11) L(G∪) L(G∪ 22) = L) = L11 L∪L∪ 22
G is R.G L⇒G is R.G L⇒ 11 L∪ L∪ 22 is R.L. is R.L.
case 2, 3.case 2, 3. Same. Same. ∴∴ R.L is closed to union, concatenation, closureR.L is closed to union, concatenation, closure
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
Regular GrammarRegular GrammarRegular GrammarRegular Grammar
Regular Regular Regular Regular ExpressionExpressionExpressionExpression
Finite Finite Finite Finite AutomataAutomataAutomataAutomata
Attributes of Regular LanguageAttributes of Regular Language
Relationship of Regular grammar and Finite AutomataRelationship of Regular grammar and Finite Automata
▶▶ Regular grammar Regular grammar FA FA
method of construction to FAmethod of construction to FA
M=(Q, M=(Q, , , , , qq00, F), F)
Q Q : V: VNN {∪{∪ ff}, }, f f : new final state: new final state
: V: VTT
qq00 : S: S
F F : if : if L(G), then {L(G), then {ff} else {S, } else {S, ff}} : if A→: if A→aaB P, then ∈B P, then ∈ (A, (A, aa) B∋) B∋
if A→if A→aa P, then ∈P, then ∈ (A, (A, aa)∋)∋ff
Attributes of Regular LanguageAttributes of Regular Language
Relationship of Regular grammar and Finite AutomataRelationship of Regular grammar and Finite Automata
▶▶ Regular grammar Regular grammar FA FA
method of construction to FAmethod of construction to FA
M=(Q, M=(Q, , , , , qq00, F), F)
Q Q : V: VNN {∪{∪ ff}, }, f f : new final state: new final state
: V: VTT
qq00 : S: S
F F : if : if L(G), then {L(G), then {ff} else {S, } else {S, ff}} : if A→: if A→aaB P, then ∈B P, then ∈ (A, (A, aa) B∋) B∋
if A→if A→aa P, then ∈P, then ∈ (A, (A, aa)∋)∋ff
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
[Ex 28] [Ex 28] Changing regular grammar to FAChanging regular grammar to FA
G=({S, B}, {0, 1}, P, S),G=({S, B}, {0, 1}, P, S), P : P : S→0S S→1B S→0 S→1S→0S S→1B S→0 S→1 B→0S B→0B→0S B→0
M=(Q, M=(Q, , , , , qq00, F), F)
Q Q : V: VNN {∪{∪ ff} = {S, B, } = {S, B, ff}}
: V: VT T ={0, 1}={0, 1}
qq00 : S: S
F F : {: {ff} }
▶▶ FAFA Regular grammar Regular grammar
method of construction to Regular Grammarmethod of construction to Regular Grammar
G=( VG=( VNN, V, VTT, P, S), P, S)
VVNN : Q : Q
VVTT : : S S : : qq00
P P : if : if ((qq, , aa) = ) = rr, then , then qq→→ar;ar; if if qq F, then ∈F, then ∈ qq→→
[Ex 29][Ex 29] FA FA Regular grammar Regular grammar (Reference of Text pp.91)(Reference of Text pp.91)
[Ex 28] [Ex 28] Changing regular grammar to FAChanging regular grammar to FA
G=({S, B}, {0, 1}, P, S),G=({S, B}, {0, 1}, P, S), P : P : S→0S S→1B S→0 S→1S→0S S→1B S→0 S→1 B→0S B→0B→0S B→0
M=(Q, M=(Q, , , , , qq00, F), F)
Q Q : V: VNN {∪{∪ ff} = {S, B, } = {S, B, ff}}
: V: VT T ={0, 1}={0, 1}
qq00 : S: S
F F : {: {ff} }
▶▶ FAFA Regular grammar Regular grammar
method of construction to Regular Grammarmethod of construction to Regular Grammar
G=( VG=( VNN, V, VTT, P, S), P, S)
VVNN : Q : Q
VVTT : : S S : : qq00
P P : if : if ((qq, , aa) = ) = rr, then , then qq→→ar;ar; if if qq F, then ∈F, then ∈ qq→→
[Ex 29][Ex 29] FA FA Regular grammar Regular grammar (Reference of Text pp.91)(Reference of Text pp.91)
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
00 11
SS
BB
ff
{S, {S, ff}}
{S, {S, ff}}
{S, {S, ff}}
Finite Automata and Regular ExpressionFinite Automata and Regular Expression
▶▶ FAFA Regular Expression Regular Expression
method : FA method : FA RG RG RE RE
FA FA transition table transition table
transition table transition table RE RE regular grammar regular grammar regular grammar equationregular grammar equation regular grammar equation regular grammar equation RE RE
▶▶ Regular Expression Regular Expression FA FA
method : RE method : RE NFA( NFA(-NFA) -NFA) DFA DFA
▣▣ RE RE -NFA-NFA
The follow sentences is all same meaning.The follow sentences is all same meaning.1.1. L is generated by some regular grammar. L is generated by some regular grammar.2.2. L is recognized by some FA. L is recognized by some FA.3.3. L is described by some RE. L is described by some RE.
Finite Automata and Regular ExpressionFinite Automata and Regular Expression
▶▶ FAFA Regular Expression Regular Expression
method : FA method : FA RG RG RE RE
FA FA transition table transition table
transition table transition table RE RE regular grammar regular grammar regular grammar equationregular grammar equation regular grammar equation regular grammar equation RE RE
▶▶ Regular Expression Regular Expression FA FA
method : RE method : RE NFA( NFA(-NFA) -NFA) DFA DFA
▣▣ RE RE -NFA-NFA
The follow sentences is all same meaning.The follow sentences is all same meaning.1.1. L is generated by some regular grammar. L is generated by some regular grammar.2.2. L is recognized by some FA. L is recognized by some FA.3.3. L is described by some RE. L is described by some RE.
Regular Regular LanguageLanguage
Regular Regular LanguageLanguage
- The End of Chapter 3 -- The End of Chapter 3 -- The End of Chapter 3 -- The End of Chapter 3 -
CompilerCompilerCompilerCompiler