Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Regular Grammar and Regular Language [Def 3.1] Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) Type 3 grammar(regular.

Similar presentations


Presentation on theme: " Regular Grammar and Regular Language [Def 3.1] Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) Type 3 grammar(regular."— Presentation transcript:

1

2  Regular Grammar and Regular Language [Def 3.1] Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) Type 3 grammar(regular grammar, RG) 1) A→tB  t (right-linear, RLG) 1) A→tB  t (right-linear, RLG) 2) A→Bt  t (left-linear, LLG) 2) A→Bt  t (left-linear, LLG) A, B ∈ V N, t ∈ V T A, B ∈ V N, t ∈ V T [Ex] P : S→aA  bB  b A→bA  a B→bS [Ex] P : S→aA  bB  b A→bA  a B→bS [Ex] P 1 : S 1 →0S 1 S 1 →0 [Ex] P 1 : S 1 →0S 1 S 1 →0 P 2 : S 2 →1S 2 S 2 →1 P 2 : S 2 →1S 2 S 2 →1 new P can be consist of P1 ∪ P2 ∪ {S→S1  S2}. new P can be consist of P1 ∪ P2 ∪ {S→S1  S2}. S →S 1  S 2 (single production rule) S →S 1  S 2 (single production rule) S 1 →0S 1  0 S 2 →1S 2  1 S 1 →0S 1  0 S 2 →1S 2  1 ∴ S →0S 1  0  1S 2  1 ∴ S →0S 1  0  1S 2  1 S 1 →0S 1  0S 2 →1S 2  1 S 1 →0S 1  0S 2 →1S 2  1  Regular Grammar and Regular Language [Def 3.1] Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) Type 3 grammar(regular grammar, RG) 1) A→tB  t (right-linear, RLG) 1) A→tB  t (right-linear, RLG) 2) A→Bt  t (left-linear, LLG) 2) A→Bt  t (left-linear, LLG) A, B ∈ V N, t ∈ V T A, B ∈ V N, t ∈ V T [Ex] P : S→aA  bB  b A→bA  a B→bS [Ex] P : S→aA  bB  b A→bA  a B→bS [Ex] P 1 : S 1 →0S 1 S 1 →0 [Ex] P 1 : S 1 →0S 1 S 1 →0 P 2 : S 2 →1S 2 S 2 →1 P 2 : S 2 →1S 2 S 2 →1 new P can be consist of P1 ∪ P2 ∪ {S→S1  S2}. new P can be consist of P1 ∪ P2 ∪ {S→S1  S2}. S →S 1  S 2 (single production rule) S →S 1  S 2 (single production rule) S 1 →0S 1  0 S 2 →1S 2  1 S 1 →0S 1  0 S 2 →1S 2  1 ∴ S →0S 1  0  1S 2  1 ∴ S →0S 1  0  1S 2  1 S 1 →0S 1  0S 2 →1S 2  1 S 1 →0S 1  0S 2 →1S 2  1 Regular Language

3 ▶ A Regular Grammar is used to scanner. ▶ A Regular Grammar is used to scanner.  simple token structure  simple 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 Expression [Def 3.2] Regular Expression 1.  : empty set 1.  : empty set 2.  : {  } 2.  : {  } 3. a ∈ V T : {a} 3. a ∈ V T : {a} 4. if e 1 is R.E of L 1 and e 2 is R.E of L 2, 4. if e 1 is R.E of L 1 and e 2 is R.E of L 2,  (e 1 ) + (e 2 ) : represent to L 2 ∪ L 2  (e 1 ) + (e 2 ) : represent to L 2 ∪ L 2  (e 1 ) (e 2 ) : represent to L 1 L 2  (e 1 ) (e 2 ) : represent to L 1 L 2  (e 1 ) * : represent to {  } ∪ L 1 1 ∪ L 1 2 ∪... ∪ L 1 n ∪ …  (e 1 ) * : represent to {  } ∪ L 1 1 ∪ L 1 2 ∪... ∪ L 1 n ∪ … 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 structure  simple 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 Expression [Def 3.2] Regular Expression 1.  : empty set 1.  : empty set 2.  : {  } 2.  : {  } 3. a ∈ V T : {a} 3. a ∈ V T : {a} 4. if e 1 is R.E of L 1 and e 2 is R.E of L 2, 4. if e 1 is R.E of L 1 and e 2 is R.E of L 2,  (e 1 ) + (e 2 ) : represent to L 2 ∪ L 2  (e 1 ) + (e 2 ) : represent to L 2 ∪ L 2  (e 1 ) (e 2 ) : represent to L 1 L 2  (e 1 ) (e 2 ) : represent to L 1 L 2  (e 1 ) * : represent to {  } ∪ L 1 1 ∪ L 1 2 ∪... ∪ L 1 n ∪ …  (e 1 ) * : represent to {  } ∪ L 1 1 ∪ L 1 2 ∪... ∪ L 1 n ∪ … 5. other cases are not a R.E. 5. other cases are not a R.E. [Ex] 1) R.E (0+1) * 1) R.E (0+1) * 2) R.E (a+b) * abb 2) R.E (a+b) * abb

4 [Def 3.3] If two R.Es are represent to same set, there are same R.Es. ▶ algebraic property precedence : + < < * precedence : + < < * ⓐ  +  =  +  ⓑ (  )  =  (  ) ⓐ  +  =  +  ⓑ (  )  =  (  ) ⓒ (  +  )+  =  +(  +  ) ⓓ  (  +  ) =  +  ⓒ (  +  )+  =  +(  +  ) ⓓ  (  +  ) =  +  ⓔ (  +  )  =  +  ⓕ  +  =  ⓔ (  +  )  =  +  ⓕ  +  =  ⓖ  +  =  ⓗ  =  =  ⓖ  +  =  ⓗ  =  =  ⓘ  =  =  ⓙ  * =  +  * ⓘ  =  =  ⓙ  * =  +  * ⓚ  * = (  +  ) * ⓛ (  * ) * =  * ⓚ  * = (  +  ) * ⓛ (  * ) * =  * ⓜ  * +  =  * ⓝ  * +  + =  * ⓜ  * +  =  * ⓝ  * +  + =  * ⓞ (  +  ) * = (  *  * ) * ⓟ (  ) * = (  * +  * ) * ⓞ (  +  ) * = (  *  * ) * ⓟ (  ) * = (  * +  * ) * ⓠ  * =  ⓡ  * =  ⓠ  * =  ⓡ  * =   Regular Grammar  Regular Expression [Def 3.4] regular expression equation : The Equation that coefficient is R.E. : The Equation that coefficient is R.E. [Def 3.3] If two R.Es are represent to same set, there are same R.Es. ▶ algebraic property precedence : + < < * precedence : + < < * ⓐ  +  =  +  ⓑ (  )  =  (  ) ⓐ  +  =  +  ⓑ (  )  =  (  ) ⓒ (  +  )+  =  +(  +  ) ⓓ  (  +  ) =  +  ⓒ (  +  )+  =  +(  +  ) ⓓ  (  +  ) =  +  ⓔ (  +  )  =  +  ⓕ  +  =  ⓔ (  +  )  =  +  ⓕ  +  =  ⓖ  +  =  ⓗ  =  =  ⓖ  +  =  ⓗ  =  =  ⓘ  =  =  ⓙ  * =  +  * ⓘ  =  =  ⓙ  * =  +  * ⓚ  * = (  +  ) * ⓛ (  * ) * =  * ⓚ  * = (  +  ) * ⓛ (  * ) * =  * ⓜ  * +  =  * ⓝ  * +  + =  * ⓜ  * +  =  * ⓝ  * +  + =  * ⓞ (  +  ) * = (  *  * ) * ⓟ (  ) * = (  * +  * ) * ⓞ (  +  ) * = (  *  * ) * ⓟ (  ) * = (  * +  * ) * ⓠ  * =  ⓡ  * =  ⓠ  * =  ⓡ  * =   Regular Grammar  Regular Expression [Def 3.4] regular expression equation : The Equation that coefficient is R.E. : The Equation that coefficient is R.E. Regular Language

5 [Theorem 3.1] if ,  is R.E and  L , then solution of X=  X+  is X=  * . X=  X+  =  (  *  )+  =  +  +  = (  + +  )  =  *  X=  X+  =  (  *  )+  =  +  +  = (  + +  )  =  *  ▶ Search processing step to R.E of L(G) in L 1. regular grammar → RE equation 1. regular grammar → RE equation 2. X=  X+  → X=  *  2. X=  X+  → X=  *  3. RE equation X is replace to  *  in RE equation, 3. RE equation X is replace to  *  in RE equation, then construct to X=  X+  form. then construct to X=  X+  form. 4. repeat step 3, then solve to X in X=  X+  4. repeat step 3, then solve to X in X=  X+  [Ex] G=({S, R}, {a, b}, P, S) P: S→aS  bR  R→aS ; What is a RE of L(G)? [Ex] G=({S, R}, {a, b}, P, S) P: S→aS  bR  R→aS ; What is a RE of L(G)? ans) 1. grammar → R.E equation ans) 1. grammar → R.E equation S = aS + bR +  ------ ① S = aS + bR +  ------ ① R = aS ------ ② R = aS ------ ② 2. Non 2. Non 3. ② assign to ① S = aS + b(aS) +  = aS + baS +  = (a + ba)S +  S = aS + b(aS) +  = aS + baS +  = (a + ba)S +  4. L(G) is S = (a + ba)S +  = (a + ba) * ∴ L(G) = (a + ba) * [Theorem 3.1] if ,  is R.E and  L , then solution of X=  X+  is X=  * . X=  X+  =  (  *  )+  =  +  +  = (  + +  )  =  *  X=  X+  =  (  *  )+  =  +  +  = (  + +  )  =  *  ▶ Search processing step to R.E of L(G) in L 1. regular grammar → RE equation 1. regular grammar → RE equation 2. X=  X+  → X=  *  2. X=  X+  → X=  *  3. RE equation X is replace to  *  in RE equation, 3. RE equation X is replace to  *  in RE equation, then construct to X=  X+  form. then construct to X=  X+  form. 4. repeat step 3, then solve to X in X=  X+  4. repeat step 3, then solve to X in X=  X+  [Ex] G=({S, R}, {a, b}, P, S) P: S→aS  bR  R→aS ; What is a RE of L(G)? [Ex] G=({S, R}, {a, b}, P, S) P: S→aS  bR  R→aS ; What is a RE of L(G)? ans) 1. grammar → R.E equation ans) 1. grammar → R.E equation S = aS + bR +  ------ ① S = aS + bR +  ------ ① R = aS ------ ② R = aS ------ ② 2. Non 2. Non 3. ② assign to ① S = aS + b(aS) +  = aS + baS +  = (a + ba)S +  S = aS + b(aS) +  = aS + baS +  = (a + ba)S +  4. L(G) is S = (a + ba)S +  = (a + ba) * ∴ L(G) = (a + ba) * Regular Language

6  Finite Automata(FA) ▶ Construction input/output input/output finite state finite state control mechanism(set of transition) control mechanism(set of transition) [Def 3.5] FA M = (Q, , , q 0, F) [Def 3.5] FA M = (Q, , , q 0, F) Q : finite set of states Q : finite set of states  : set of input alphabet  : set of input alphabet q 0 : initial state q 0 : initial state F : set of finial state, F ⊆ Q F : set of finial state, F ⊆ Q  : transition function Q×Σ→2 Q  : transition function Q×Σ→2 Q eg)  (q, a) = {p 1, p 2, ···, p n } ⇒ after scanning input symbol 'a' in state q, ⇒ after scanning input symbol 'a' in state q, next state select to state p 1 ~ p n next state select to state p 1 ~ p n  Finite Automata(FA) ▶ Construction input/output input/output finite state finite state control mechanism(set of transition) control mechanism(set of transition) [Def 3.5] FA M = (Q, , , q 0, F) [Def 3.5] FA M = (Q, , , q 0, F) Q : finite set of states Q : finite set of states  : set of input alphabet  : set of input alphabet q 0 : initial state q 0 : initial state F : set of finial state, F ⊆ Q F : set of finial state, F ⊆ Q  : transition function Q×Σ→2 Q  : transition function Q×Σ→2 Q eg)  (q, a) = {p 1, p 2, ···, p n } ⇒ after scanning input symbol 'a' in state q, ⇒ after scanning input symbol 'a' in state q, next state select to state p 1 ~ p n next state select to state p 1 ~ p n Regular Language

7  DFA(Deterministic FA) next state is determined to unique state next state is determined to unique state  (q, a) = p  (q, a) = p [Def 3.6] DFA M = (Q, , , q 0, F) [Def 3.6] DFA M = (Q, , , q 0, F) Q : finite set of states Q : finite set of states  : set of input alphabet  : set of input alphabet q 0 : initial state q 0 : initial state F : set of finial state, F ⊆ Q F : set of finial state, F ⊆ Q  : transition function Q×  → Q ≡  (q, a) = p  : transition function Q×  → Q ≡  (q, a) = p ▶ DFA properties ▶ DFA properties 1. no  -transition 1. no  -transition 2. determined to unique state 2. determined to unique state ▶ Extensibility of transition function ▶ Extensibility of transition function Q×  * → Q Q×  * → Q  (q,  ) = q  (q, xa) =  (  (q, x), a) x ∈  *, a ∈   (q,  ) = q  (q, xa) =  (  (q, x), a) x ∈  *, a ∈  ⇒ extension : symbol → string ⇒ extension : symbol → string  DFA(Deterministic FA) next state is determined to unique state next state is determined to unique state  (q, a) = p  (q, a) = p [Def 3.6] DFA M = (Q, , , q 0, F) [Def 3.6] DFA M = (Q, , , q 0, F) Q : finite set of states Q : finite set of states  : set of input alphabet  : set of input alphabet q 0 : initial state q 0 : initial state F : set of finial state, F ⊆ Q F : set of finial state, F ⊆ Q  : transition function Q×  → Q ≡  (q, a) = p  : transition function Q×  → Q ≡  (q, a) = p ▶ DFA properties ▶ DFA properties 1. no  -transition 1. no  -transition 2. determined to unique state 2. determined to unique state ▶ Extensibility of transition function ▶ Extensibility of transition function Q×  * → Q Q×  * → Q  (q,  ) = q  (q, xa) =  (  (q, x), a) x ∈  *, a ∈   (q,  ) = q  (q, xa) =  (  (q, x), a) x ∈  *, a ∈  ⇒ extension : symbol → string ⇒ extension : symbol → string Regular Language

8 [Def 3.7] L(M) that recognized DFA M L(M) = {x  ( q 0, x) ∈ F} [Ex] M=({p, q, r}, {0, 1}, δ, p, {r})  :  (p, 0) = q  (p, 1) = p  :  (p, 0) = q  (p, 1) = p  (q, 0) = r  (q, 1) = p  (q, 0) = r  (q, 1) = p  (r, 0) = r  (r, 1) = r  (r, 0) = r  (r, 1) = r Can be recognized sentence "1001", "0110"? Can be recognized sentence "1001", "0110"? ans) 1)  (p, 1001) =  (p, 001) =  (q, 01) =  (r, 1) = r ∈ F ∴ accept ans) 1)  (p, 1001) =  (p, 001) =  (q, 01) =  (r, 1) = r ∈ F ∴ accept 2)  (p, 0110) =  (q, 110) =  (p, 10) =  (p, 0) = q  F ∴ reject 2)  (p, 0110) =  (q, 110) =  (p, 10) =  (p, 0) = q  F ∴ reject ※ transition table : transition function is represented to matrix ※ transition table : transition function is represented to matrix [Ex] Transition Table  01  01 pqp pqp qrp qrp rrr rrr [Def 3.7] L(M) that recognized DFA M L(M) = {x  ( q 0, x) ∈ F} [Ex] M=({p, q, r}, {0, 1}, δ, p, {r})  :  (p, 0) = q  (p, 1) = p  :  (p, 0) = q  (p, 1) = p  (q, 0) = r  (q, 1) = p  (q, 0) = r  (q, 1) = p  (r, 0) = r  (r, 1) = r  (r, 0) = r  (r, 1) = r Can be recognized sentence "1001", "0110"? Can be recognized sentence "1001", "0110"? ans) 1)  (p, 1001) =  (p, 001) =  (q, 01) =  (r, 1) = r ∈ F ∴ accept ans) 1)  (p, 1001) =  (p, 001) =  (q, 01) =  (r, 1) = r ∈ F ∴ accept 2)  (p, 0110) =  (q, 110) =  (p, 10) =  (p, 0) = q  F ∴ reject 2)  (p, 0110) =  (q, 110) =  (p, 10) =  (p, 0) = q  F ∴ reject ※ transition table : transition function is represented to matrix ※ transition table : transition function is represented to matrix [Ex] Transition Table  01  01 pqp pqp qrp qrp rrr rrr Regular Language

9 [Def 3.8] transition diagram state : node state : node transition function : label, directed arc transition function : label, directed arc eg)  (q, a) = p a eg)  (q, a) = p a ≡ ⓠ ⓟ ≡ ⓠ ⓟ final state : double circle final state : double circle [Ex] What is a transition diagram for example before? [Ex] What is a transition diagram for example before? [Def 3.9] completely specified if ∀ q ∈ Q, a ∈  in M=(Q, , , q 0, F),  (q, a) is existed uniquely next state if ∀ q ∈ Q, a ∈  in M=(Q, , , q 0, F),  (q, a) is existed uniquely next state ∴ DFA ≡ completely specified ∴ DFA ≡ completely specified [Def 3.8] transition diagram state : node state : node transition function : label, directed arc transition function : label, directed arc eg)  (q, a) = p a eg)  (q, a) = p a ≡ ⓠ ⓟ ≡ ⓠ ⓟ final state : double circle final state : double circle [Ex] What is a transition diagram for example before? [Ex] What is a transition diagram for example before? [Def 3.9] completely specified if ∀ q ∈ Q, a ∈  in M=(Q, , , q 0, F),  (q, a) is existed uniquely next state if ∀ q ∈ Q, a ∈  in M=(Q, , , q 0, F),  (q, a) is existed uniquely next state ∴ DFA ≡ completely specified ∴ DFA ≡ completely specified Regular Language start pq r11 0 0 0, 1

10 ▶ Algorithm that string is recognized by DFA M = (Q, , , q 0, F) ▶ Algorithm that string is recognized by DFA M = (Q, , , q 0, F) Algorithm_Recognize() Algorithm_Recognize() { current_state = q 0 ; { current_state = q 0 ; get(next_symbol); get(next_symbol); while( input string not exhausted ) { while( input string not exhausted ) { current_state =  (current_state, next_symbol); current_state =  (current_state, next_symbol); get(next_symbol); get(next_symbol); } if(current_state ∈ F) printf("Accept"); if(current_state ∈ F) printf("Accept"); else printf("Reject"); else printf("Reject"); }  NFA(Non-deterministic FA) [Def 3.10] NFA M = (Q, , , q 0, F) [Def 3.10] NFA M = (Q, , , q 0, F) Q : finite set of states Q : finite set of states  : set of input alphabet  : set of input alphabet q 0 : initial state q 0 : initial state F : set of finial state, F ⊆ Q F : set of finial state, F ⊆ Q  : transition function Q×  → 2 Q,  (q, a) = {p 1, p 2, ···, p n }  : transition function Q×  → 2 Q,  (q, a) = {p 1, p 2, ···, p n } ▶ Algorithm that string is recognized by DFA M = (Q, , , q 0, F) ▶ Algorithm that string is recognized by DFA M = (Q, , , q 0, F) Algorithm_Recognize() Algorithm_Recognize() { current_state = q 0 ; { current_state = q 0 ; get(next_symbol); get(next_symbol); while( input string not exhausted ) { while( input string not exhausted ) { current_state =  (current_state, next_symbol); current_state =  (current_state, next_symbol); get(next_symbol); get(next_symbol); } if(current_state ∈ F) printf("Accept"); if(current_state ∈ F) printf("Accept"); else printf("Reject"); else printf("Reject"); }  NFA(Non-deterministic FA) [Def 3.10] NFA M = (Q, , , q 0, F) [Def 3.10] NFA M = (Q, , , q 0, F) Q : finite set of states Q : finite set of states  : set of input alphabet  : set of input alphabet q 0 : initial state q 0 : initial state F : set of finial state, F ⊆ Q F : set of finial state, F ⊆ Q  : transition function Q×  → 2 Q,  (q, a) = {p 1, p 2, ···, p n }  : transition function Q×  → 2 Q,  (q, a) = {p 1, p 2, ···, p n } Regular Language

11 [Ex] What is transition diagram of M=({q 0, q 1, q 2, q 3, q f }, {0, 1}, , q 0, {q f })?  0 1 q 0 { q 1, q 2 } { q 1, q 3 } q 1 { q 1, q 2 } { q 1, q 3 } q 2 { q f }  q 3  { q f } q f { q f } { q f } ※  (q, a) =  : not defined [Ex] What is transition diagram of M=({q 0, q 1, q 2, q 3, q f }, {0, 1}, , q 0, {q f })?  0 1 q 0 { q 1, q 2 } { q 1, q 3 } q 1 { q 1, q 2 } { q 1, q 3 } q 2 { q f }  q 3  { q f } q f { q f } { q f } ※  (q, a) =  : not defined Regular Language start q 0 1 1 0 0 0, 1 q 1 q f q 2 q 3 1

12 ▶ Extension of NFA ▶ Extension of NFA [step 1] Q×  → 2 Q  Q×  * → 2 Q Q×  → 2 Q  Q×  * → 2 Q  (q,  ) = {q}  (q,  ) = {q}  (q, xa) = ∪  (p, a)  (q, xa) = ∪  (p, a) p ∈  (q, x) p ∈  (q, x) x ∈  *, a ∈  x ∈  *, a ∈  ∴ symbol → extend to string ∴ symbol → extend to string [step 2] [step 2] Q×  → 2 Q  2 Q ×  * → 2 Q Q×  → 2 Q  2 Q ×  * → 2 Q  ({ p 1, p 2, ···, p k }, x) =  (p i, x)  ({ p 1, p 2, ···, p k }, x) =  (p i, x) ∴ one state → extended to more state ∴ one state → extended to more state [Ex]  (q 0, 1001) =  (q 1, 001) ∪  (q 3, 001) =  (q 1, 01) ∪  (q 2, 01) ∪  =  (q 1, 01) ∪  (q 2, 01) ∪  =  (q 1, 1) ∪  (q 2, 1) ∪  (q f, 1) =  (q 1, 1) ∪  (q 2, 1) ∪  (q f, 1) = {q 1, q 3, q f } = {q 1, q 3, q f } ▶ Extension of NFA ▶ Extension of NFA [step 1] Q×  → 2 Q  Q×  * → 2 Q Q×  → 2 Q  Q×  * → 2 Q  (q,  ) = {q}  (q,  ) = {q}  (q, xa) = ∪  (p, a)  (q, xa) = ∪  (p, a) p ∈  (q, x) p ∈  (q, x) x ∈  *, a ∈  x ∈  *, a ∈  ∴ symbol → extend to string ∴ symbol → extend to string [step 2] [step 2] Q×  → 2 Q  2 Q ×  * → 2 Q Q×  → 2 Q  2 Q ×  * → 2 Q  ({ p 1, p 2, ···, p k }, x) =  (p i, x)  ({ p 1, p 2, ···, p k }, x) =  (p i, x) ∴ one state → extended to more state ∴ one state → extended to more state [Ex]  (q 0, 1001) =  (q 1, 001) ∪  (q 3, 001) =  (q 1, 01) ∪  (q 2, 01) ∪  =  (q 1, 01) ∪  (q 2, 01) ∪  =  (q 1, 1) ∪  (q 2, 1) ∪  (q f, 1) =  (q 1, 1) ∪  (q 2, 1) ∪  (q f, 1) = {q 1, q 3, q f } = {q 1, q 3, q f } Regular Language

13 [Def 3.11] L(M) that is recognized by NFA M L(M) = {x  (q 0, x)∩F≠  } L(M) = {x  (q 0, x)∩F≠  } [Ex] Can be recognized to 1011? [Ex] Can be recognized to 1011?  (q 0, 1011) =  ({q 1, q 3 }, 011) =  ({q 1, q 2 }, 11)  (q 0, 1011) =  ({q 1, q 3 }, 011) =  ({q 1, q 2 }, 11) =  ({q 1, q 3 }, 1) = {q 1, q 3, q f } =  ({q 1, q 3 }, 1) = {q 1, q 3, q f } {q 1, q 3, q f }∩{q f }≠  {q 1, q 3, q f }∩{q f }≠  ∴ 1011 is accepted by M ∴ 1011 is 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 m, length(string) = n state of NFA M : when number of m, length(string) = n number of maximum nodes in tree = m n number of maximum nodes in tree = m n ∴ string that is recognized in NFA, excessively time-consumed ∴ string that is recognized in NFA, excessively time-consumed [Def 3.11] L(M) that is recognized by NFA M L(M) = {x  (q 0, x)∩F≠  } L(M) = {x  (q 0, x)∩F≠  } [Ex] Can be recognized to 1011? [Ex] Can be recognized to 1011?  (q 0, 1011) =  ({q 1, q 3 }, 011) =  ({q 1, q 2 }, 11)  (q 0, 1011) =  ({q 1, q 3 }, 011) =  ({q 1, q 2 }, 11) =  ({q 1, q 3 }, 1) = {q 1, q 3, q f } =  ({q 1, q 3 }, 1) = {q 1, q 3, q f } {q 1, q 3, q f }∩{q f }≠  {q 1, q 3, q f }∩{q f }≠  ∴ 1011 is accepted by M ∴ 1011 is 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 m, length(string) = n state of NFA M : when number of m, length(string) = n number of maximum nodes in tree = m n number of maximum nodes in tree = m n ∴ string that is recognized in NFA, excessively time-consumed ∴ string that is recognized in NFA, excessively time-consumed

14  Transformation from NFA to DFA  Transformation 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] Let NFA M=(Q, , , q 0, F). if L(M) is accepted by NFA M, DFA M'=(Q',  ',  ', q 0 ', F') is existed. if L(M) is accepted by NFA M, DFA M'=(Q',  ',  ', q 0 ', F') is existed. ≡ construct DFA M'=(Q', ,  ', q 0 ', F') ≡ construct DFA M'=(Q', ,  ', q 0 ', F') ① Q' = 2 Q (power set of Q), [presented a state Q : [q 1, q 2,···, q i ], q i ∈ Q] ① Q' = 2 Q (power set of Q), [presented a state Q : [q 1, q 2,···, q i ], q i ∈ Q] ② q 0 '=[q 0 ] ② q 0 '=[q 0 ] ③ F'={q ∈ Q'  q ∈ F} ③ F'={q ∈ Q'  q ∈ F} ④ if  ({ q 1, q 2,···, q i }, a)={p 1, p 2,···, p j }, then  '([q 1, q 2,···, q i ], a)=[ p 1, p 2,···, p j ] ④ if  ({ q 1, q 2,···, q i }, a)={p 1, p 2,···, p j }, then  '([q 1, q 2,···, q i ], a)=[ p 1, p 2,···, p j ]  Transformation from NFA to DFA  Transformation 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] Let NFA M=(Q, , , q 0, F). if L(M) is accepted by NFA M, DFA M'=(Q',  ',  ', q 0 ', F') is existed. if L(M) is accepted by NFA M, DFA M'=(Q',  ',  ', q 0 ', F') is existed. ≡ construct DFA M'=(Q', ,  ', q 0 ', F') ≡ construct DFA M'=(Q', ,  ', q 0 ', F') ① Q' = 2 Q (power set of Q), [presented a state Q : [q 1, q 2,···, q i ], q i ∈ Q] ① Q' = 2 Q (power set of Q), [presented a state Q : [q 1, q 2,···, q i ], q i ∈ Q] ② q 0 '=[q 0 ] ② q 0 '=[q 0 ] ③ F'={q ∈ Q'  q ∈ F} ③ F'={q ∈ Q'  q ∈ F} ④ if  ({ q 1, q 2,···, q i }, a)={p 1, p 2,···, p j }, then  '([q 1, q 2,···, q i ], a)=[ p 1, p 2,···, p j ] ④ if  ({ q 1, q 2,···, q i }, a)={p 1, p 2,···, p j }, then  '([q 1, q 2,···, q i ], a)=[ p 1, p 2,···, p j ] Regular Language

15 [Ex 21] NFA M=({q 0, q 1 }, {0, 1}, , q 0, {q 1 }) state name change : [q 0 ]=A, [q 1 ]=B, [q 0, q 1 ]=C state name change : [q 0 ]=A, [q 1 ]=B, [q 0, q 1 ]=C [Ex 22], [Ex 23] Reference of Text pp.78 ~ pp.80 [Ex 21] NFA M=({q 0, q 1 }, {0, 1}, , q 0, {q 1 }) state name change : [q 0 ]=A, [q 1 ]=B, [q 0, q 1 ]=C state name change : [q 0 ]=A, [q 1 ]=B, [q 0, q 1 ]=C [Ex 22], [Ex 23] Reference of Text pp.78 ~ pp.80 Regular Language  ’ 0 1  ’ 0 1 [q 0 ] [q 0, q 1 ] [q 0 ] [q 0 ] [q 0, q 1 ] [q 0 ] [q 0 ]  [q 0, q 1 ] [q 0 ]  [q 0, q 1 ] [q 0, q 1 ] [q 0, q 1 ] [q 0, q 1 ]  0 1  0 1 q 0 {q 0, q 1 } {q 0 } q 0 {q 0, q 1 } {q 0 } q 1  {q 0, q 1 } q 1  {q 0, q 1 } AA BB CC startstart1100 11 0,1 AA startstart 11 CC 0,1 00

16 Regular Language [Def 3.12]  -NFA M=(Q, , , q 0, F) :   : Q(  ∪ {  }) → 2 Q   : Q(  ∪ {  }) → 2 Q  other cases are same to NFA.  other cases are same to NFA. [Def 3.13]  -CLOSURE(s)  case of s that is one state  case of s that is one state  -CLOSURE(s)  -CLOSURE(s) = include itself state s and all reachable states by label  from state s = include itself state s and all reachable states by label  from state s (repeat until not changes elements of  -CLOSURE(s)) (repeat until not changes elements of  -CLOSURE(s))  case of T that is one or more states  case of T that is one or more states  -CLOSURE(T)  -CLOSURE(T) = each state in T, executing step  and union its all results = each state in T, executing step  and union its all results (  -CLOSURE(T)) = ∪  -CLOSURE(x) (  -CLOSURE(T)) = ∪  -CLOSURE(x) q ∈ T q ∈ T [Def 3.12]  -NFA M=(Q, , , q 0, F) :   : Q(  ∪ {  }) → 2 Q   : Q(  ∪ {  }) → 2 Q  other cases are same to NFA.  other cases are same to NFA. [Def 3.13]  -CLOSURE(s)  case of s that is one state  case of s that is one state  -CLOSURE(s)  -CLOSURE(s) = include itself state s and all reachable states by label  from state s = include itself state s and all reachable states by label  from state s (repeat until not changes elements of  -CLOSURE(s)) (repeat until not changes elements of  -CLOSURE(s))  case of T that is one or more states  case of T that is one or more states  -CLOSURE(T)  -CLOSURE(T) = each state in T, executing step  and union its all results = each state in T, executing step  and union its all results (  -CLOSURE(T)) = ∪  -CLOSURE(x) (  -CLOSURE(T)) = ∪  -CLOSURE(x) q ∈ T q ∈ T

17 Regular Language [Ex 24] What is CLOSURE in following  -NFA? CLOSURE(A)={A, B, D} CLOSURE(A)={A, B, D} CLOSURE({A, C})=CLOSURE(A)  CLOSURE(C)={A, B, C, D} CLOSURE({A, C})=CLOSURE(A)  CLOSURE(C)={A, B, C, D} [Ex 25] Transform from  -NFA to DFA [Ex 24] What is CLOSURE in following  -NFA? CLOSURE(A)={A, B, D} CLOSURE(A)={A, B, D} CLOSURE({A, C})=CLOSURE(A)  CLOSURE(C)={A, B, C, D} CLOSURE({A, C})=CLOSURE(A)  CLOSURE(C)={A, B, C, D} [Ex 25] Transform from  -NFA to DFA aa aa AA startstart  BB bb aa   CCDD aa startstart  cc 33  bb 11 44 22

18 Regular Language state name : A=[1,3,4], B=[2], C=[3,4], D=[4]  a b c 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}  [2]  [3,4]  [2]  [3,4] CL(4)={4}  [4]  [4] CL(3)={3,4}  [3,4]  [3,4]           A C startstart aa D B bb cc cc

19  DFA's state minimization  DFA's state minimization use of equivalence relation → state merge use of equivalence relation → state merge ▶ ≡(equivalence relation) p≡q iff δ(p, x) is an accepting state p≡q iff δ(p, x) is an accepting state iff δ(q, x) is an accepting state iff δ(q, x) is an accepting state [Def 3.14] distinguish if ∃ an x such that δ(p, x)=r, δ(q, x)=t, a final state is only one of r and t. if ∃ an x such that δ(p, x)=r, δ(q, x)=t, a final state is only one of r and t. ▶ inaccessible state : only existed out arc, except start state ▶ minimizing method 1. eliminate inaccessible state 1. eliminate inaccessible state 2. grouping final states and non-final states 2. 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 divided 4. repeat step 3 when can not divided  DFA's state minimization  DFA's state minimization use of equivalence relation → state merge use of equivalence relation → state merge ▶ ≡(equivalence relation) p≡q iff δ(p, x) is an accepting state p≡q iff δ(p, x) is an accepting state iff δ(q, x) is an accepting state iff δ(q, x) is an accepting state [Def 3.14] distinguish if ∃ an x such that δ(p, x)=r, δ(q, x)=t, a final state is only one of r and t. if ∃ an x such that δ(p, x)=r, δ(q, x)=t, a final state is only one of r and t. ▶ inaccessible state : only existed out arc, except start state ▶ minimizing method 1. eliminate inaccessible state 1. eliminate inaccessible state 2. grouping final states and non-final states 2. 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 divided 4. repeat step 3 when can not divided Regular Language

20 ▶ new DFA M'= (Q', Σ, δ', q 0 ', F') ▶ new DFA M'= (Q', Σ, δ', q 0 ', F') 1. Q ' : set of equivalence class 1. Q ' : set of equivalence class eg) [q] : equivalence class include q eg) [q] : equivalence class include q 2. let [p], [q] are equivalence class, if δ(p, a) = q, then δ'([q], a) = [q]. 2. let [p], [q] are equivalence class, if δ(p, a) = q, then δ'([q], a) = [q]. 3. q 0 ' is [q 0 ] 3. q 0 ' is [q 0 ] 4. F'= {[q]  q ∈ F} 4. F'= {[q]  q ∈ F} [Ex 26] DFA's minimization ▶ new DFA M'= (Q', Σ, δ', q 0 ', F') ▶ new DFA M'= (Q', Σ, δ', q 0 ', F') 1. Q ' : set of equivalence class 1. Q ' : set of equivalence class eg) [q] : equivalence class include q eg) [q] : equivalence class include q 2. let [p], [q] are equivalence class, if δ(p, a) = q, then δ'([q], a) = [q]. 2. let [p], [q] are equivalence class, if δ(p, a) = q, then δ'([q], a) = [q]. 3. q 0 ' is [q 0 ] 3. q 0 ' is [q 0 ] 4. F'= {[q]  q ∈ F} 4. F'= {[q]  q ∈ F} [Ex 26] DFA's minimization Regular Language aa aa A startstart D B bb aa E bb aa C aa bb bb bb

21 1 : {A, B, D} 2 : {C, E} ab 1 1 1 1 2 2 1 1 2 2 1 : {A} 2 : {B, D} ab 22 2 2 3 3 3 : {C, E} 2 2 3 3 state name : X=[A], Y=[B, D], Z=[C, E] XX YY ZZ startstart aa aa a, b bb bb ab XYZ YYY δ'δ'δ'δ' YZZ

22  A Close Properties of Finite Automata  A Close Properties of Finite Automata [Theorem 3.3] if L 1, L 2 are R.L. 1. L 1 ∪ L 2 1. L 1 ∪ L 2 2. L 1 ·L 2 2. L 1 ·L 2 3. L 1 * are R.L 3. L 1 * are R.LProof) case 1. case 1. L 1, L 2 are R.L, such that exist G 1 =(V N1, V T1, P 1, S 1 ), G 2 =(V N2, V T2, P 2, S 2 ) L 1, L 2 are R.L, such that exist G 1 =(V N1, V T1, P 1, S 1 ), G 2 =(V N2, V T2, P 2, S 2 ) ∴ L 1 = L(G 1 ), L 2 = L(G 2 ) ∴ L 1 = L(G 1 ), L 2 = L(G 2 ) It can be consist of new Grammar G. It can be consist of new Grammar G. G=(V N1 ∪ V N2 ∪ {S}, V T1 ∪ V T2, P 1 ∪ P 2 ∪ {S→S 1 | S 2 }, S), G=(V N1 ∪ V N2 ∪ {S}, V T1 ∪ V T2, P 1 ∪ P 2 ∪ {S→S 1 | S 2 }, S), S ⇒ S 1 ⇒  1 ∈ L(G 1 ) or S ⇒ S 2 ⇒  2 ∈ L(G 2 ). S ⇒ S 1 ⇒  1 ∈ L(G 1 ) or S ⇒ S 2 ⇒  2 ∈ L(G 2 ). ∴ L(G) = L(G 1 ) ∪ L(G 2 ) = L 1 ∪ L 2 ∴ L(G) = L(G 1 ) ∪ L(G 2 ) = L 1 ∪ L 2 G is R.G ⇒ L 1 ∪ L 2 is R.L. G is R.G ⇒ L 1 ∪ L 2 is R.L. case 2, 3. Same. case 2, 3. Same. ∴ R.L is closed to union, concatenation, closure ∴ R.L is closed to union, concatenation, closure  A Close Properties of Finite Automata  A Close Properties of Finite Automata [Theorem 3.3] if L 1, L 2 are R.L. 1. L 1 ∪ L 2 1. L 1 ∪ L 2 2. L 1 ·L 2 2. L 1 ·L 2 3. L 1 * are R.L 3. L 1 * are R.LProof) case 1. case 1. L 1, L 2 are R.L, such that exist G 1 =(V N1, V T1, P 1, S 1 ), G 2 =(V N2, V T2, P 2, S 2 ) L 1, L 2 are R.L, such that exist G 1 =(V N1, V T1, P 1, S 1 ), G 2 =(V N2, V T2, P 2, S 2 ) ∴ L 1 = L(G 1 ), L 2 = L(G 2 ) ∴ L 1 = L(G 1 ), L 2 = L(G 2 ) It can be consist of new Grammar G. It can be consist of new Grammar G. G=(V N1 ∪ V N2 ∪ {S}, V T1 ∪ V T2, P 1 ∪ P 2 ∪ {S→S 1 | S 2 }, S), G=(V N1 ∪ V N2 ∪ {S}, V T1 ∪ V T2, P 1 ∪ P 2 ∪ {S→S 1 | S 2 }, S), S ⇒ S 1 ⇒  1 ∈ L(G 1 ) or S ⇒ S 2 ⇒  2 ∈ L(G 2 ). S ⇒ S 1 ⇒  1 ∈ L(G 1 ) or S ⇒ S 2 ⇒  2 ∈ L(G 2 ). ∴ L(G) = L(G 1 ) ∪ L(G 2 ) = L 1 ∪ L 2 ∴ L(G) = L(G 1 ) ∪ L(G 2 ) = L 1 ∪ L 2 G is R.G ⇒ L 1 ∪ L 2 is R.L. G is R.G ⇒ L 1 ∪ L 2 is R.L. case 2, 3. Same. case 2, 3. Same. ∴ R.L is closed to union, concatenation, closure ∴ R.L is closed to union, concatenation, closure Regular Language

23 Regular Grammar Regular Regular ExpressionExpression Finite Finite AutomataAutomata  Attributes of Regular Language  Relationship of Regular grammar and Finite Automata ▶ Regular grammar  FA method of construction to FA method of construction to FA M=(Q, , , q 0, F) M=(Q, , , q 0, F) Q : V N ∪ {f}, f : new final state  : V T q 0 : S q 0 : S F : if  L(G), then {f} else {S, f}  : if A→aB ∈ P, then  (A, a) ∋ B if A→a ∈ P, then  (A, a) ∋ f if A→a ∈ P, then  (A, a) ∋ f  Attributes of Regular Language  Relationship of Regular grammar and Finite Automata ▶ Regular grammar  FA method of construction to FA method of construction to FA M=(Q, , , q 0, F) M=(Q, , , q 0, F) Q : V N ∪ {f}, f : new final state  : V T q 0 : S q 0 : S F : if  L(G), then {f} else {S, f}  : if A→aB ∈ P, then  (A, a) ∋ B if A→a ∈ P, then  (A, a) ∋ f if A→a ∈ P, then  (A, a) ∋ f Regular Language

24 [Ex 28] Changing regular grammar to FA [Ex 28] Changing regular grammar to FA G=({S, B}, {0, 1}, P, S), P : S→0S S→1B S→0 S→1 P : S→0S S→1B S→0 S→1 B→0S B→0 B→0S B→0  M=(Q, , , q 0, F) Q : V N ∪ {f} = {S, B, f} Q : V N ∪ {f} = {S, B, f}  : V T ={0, 1}  : V T ={0, 1} q 0 : S q 0 : S F : {f} F : {f} ▶ FA  Regular grammar method of construction to Regular Grammar method of construction to Regular Grammar G=( V N, V T, P, S) V N : Q V N : Q V T :  V T :  S : q 0 S : q 0 P : if  (q, a) = r, then q→ar; P : if  (q, a) = r, then q→ar; if q ∈ F, then q→  if q ∈ F, then q→  [Ex 29] FA  Regular grammar (Reference of Text pp.91) [Ex 29] FA  Regular grammar (Reference of Text pp.91) [Ex 28] Changing regular grammar to FA [Ex 28] Changing regular grammar to FA G=({S, B}, {0, 1}, P, S), P : S→0S S→1B S→0 S→1 P : S→0S S→1B S→0 S→1 B→0S B→0 B→0S B→0  M=(Q, , , q 0, F) Q : V N ∪ {f} = {S, B, f} Q : V N ∪ {f} = {S, B, f}  : V T ={0, 1}  : V T ={0, 1} q 0 : S q 0 : S F : {f} F : {f} ▶ FA  Regular grammar method of construction to Regular Grammar method of construction to Regular Grammar G=( V N, V T, P, S) V N : Q V N : Q V T :  V T :  S : q 0 S : q 0 P : if  (q, a) = r, then q→ar; P : if  (q, a) = r, then q→ar; if q ∈ F, then q→  if q ∈ F, then q→  [Ex 29] FA  Regular grammar (Reference of Text pp.91) [Ex 29] FA  Regular grammar (Reference of Text pp.91) Regular Language 01 SBf {S, f}  

25  Finite Automata and Regular Expression ▶ FA  Regular Expression method : FA  RG  RE method : FA  RG  RE  FA  transition table  FA  transition table  transition table  RE  transition table  RE  regular grammar  regular grammar  regular grammar equation  regular grammar equation  regular grammar equation  RE  regular grammar equation  RE ▶ Regular Expression  FA method : RE  NFA(  -NFA)  DFA method : RE  NFA(  -NFA)  DFA ▣ RE   -NFA ▣ RE   -NFA  The follow sentences is all same meaning.  The follow sentences is all same meaning. 1. L is generated by some regular grammar. 2. L is recognized by some FA. 3. L is described by some RE.  Finite Automata and Regular Expression ▶ FA  Regular Expression method : FA  RG  RE method : FA  RG  RE  FA  transition table  FA  transition table  transition table  RE  transition table  RE  regular grammar  regular grammar  regular grammar equation  regular grammar equation  regular grammar equation  RE  regular grammar equation  RE ▶ Regular Expression  FA method : RE  NFA(  -NFA)  DFA method : RE  NFA(  -NFA)  DFA ▣ RE   -NFA ▣ RE   -NFA  The follow sentences is all same meaning.  The follow sentences is all same meaning. 1. L is generated by some regular grammar. 2. L is recognized by some FA. 3. L is described by some RE. Regular Language

26 - The End of Chapter 3 - Compiler


Download ppt " Regular Grammar and Regular Language [Def 3.1] Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) Type 3 grammar(regular."

Similar presentations


Ads by Google