250 likes | 507 Views
Regular Language. Regular Grammar and Regular Language [Def 3.1] Regular Grammar (use to in lexical analysis) Type 3 grammar (regular grammar, RG) 1) A→tB t (right-linear, RLG) 2) A→Bt t (left-linear, LLG) A, B∈V N , t∈V T
E N D
Regular Language • Regular Grammar and Regular Language [Def 3.1]Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) 1) A→tBt (right-linear, RLG) 2) A→Btt (left-linear, LLG) A, B∈VN, t∈VT [Ex] P : S→aAbBb A→bAa B→bS [Ex] P1 : S1→0S1 S1→0 • P2 : S2→1S2 S2→1 • new P can be consist of P1∪P2∪{S→S1S2}. • S →S1S2(single production rule) • S1→0S10 S2→1S21 • ∴S →0S101S21 • S1→0S10 S2→1S21
Regular Language ▶A Regular Grammar is used to scanner. simple token structure RG is more effective than CFG. It can be consist of small module to process front-end part. • Regular Expression [Def 3.2]Regular Expression 1. : empty set 2. : {} 3. a∈VT : {a} 4. if e1 is R.E of L1 and e2 is R.E of L2, (e1)+(e2): represent to L2∪L2 (e1) • (e2) : represent to L1L2 (e1)* : represent to {}∪L11∪L12∪...∪L1n∪… 5. other cases are not a R.E. [Ex] 1) R.E (0+1)* 2) R.E (a+b)*abb
Regular Language [Def 3.3]If two R.Es are represent to same set, there are same R.Es. • ▶algebraic property • precedence : + < •< * • ⓐ + = + ⓑ () = () • ⓒ (+)+ = +(+) ⓓ (+) = + • ⓔ (+) = + ⓕ + = • ⓖ + = ⓗ = = • ⓘ = = ⓙ * = +* • ⓚ * = (+)* ⓛ (*)* = * • ⓜ *+ = * ⓝ *++ = * • ⓞ (+)* = (**)* ⓟ ()* = (*+*)* • ⓠ * = ⓡ * = • Regular Grammar Regular Expression • [Def 3.4]regular expression equation • : The Equation that coefficient is R.E.
Regular Language [Theorem 3.1] if , is R.E and L, then solution of X= X+ is X= * . • X= X+ = (*)+ = ++= (++)= * • ▶ Search processing step to R.E of L(G) in L • 1. regular grammar → RE equation • 2. X= X+ → X= * • 3. RE equation X is replace to *in RE equation, • then construct to X= X+form. • 4. repeat step 3, then solve to X in X= X+ • [Ex] G=({S, R}, {a, b}, P, S) P: S→aSbR R→aS ; What is a RE of L(G)? • ans)1. grammar → R.E equation • S = aS + bR + ------ ① • R = aS ------ ② • 2. Non • 3. ② assign to ① • 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 • Finite Automata(FA) ▶ Construction input/output • finite state • control mechanism(set of transition) • [Def 3.5]FA M = (Q, , , q0, F) • Q : finite set of states • : set of input alphabet • q0 : initial state • F : set of finial state, F⊆Q • : transition function Q×Σ→2Q • eg) (q, a) = {p1, p2, ··· , pn} • ⇒ after scanning input symbol 'a' in state q, • next state select to state p1~ pn
Regular Language DFA(Deterministic FA) • next state is determined to unique state • (q, a) = p • [Def 3.6]DFA M = (Q, , , q0, F) • Q : finite set of states • : set of input alphabet • q0 : initial state • F : set of finial state, F⊆Q • : transition function Q× → Q ≡ (q, a) = p • ▶ DFA properties • 1. no -transition • 2. determined to unique state • ▶ Extensibility of transition function • Q× * → Q • (q, ) = q(q, xa) = ((q, x), a) x∈*, a∈ • ⇒extension : symbol → string
Regular Language [Def 3.7]L(M) that recognized DFA M • L(M) = {x( q0, x)∈F} • [Ex] M=({p, q, r}, {0, 1}, δ, p, {r}) • : (p, 0) = q(p, 1) = p • (q, 0) = r(q, 1) = p • (r, 0) = r(r, 1) = r • Can be recognized sentence "1001", "0110"? • ans) 1) (p, 1001) = (p, 001) = (q, 01) = (r, 1) = r∈F ∴accept • 2) (p, 0110) = (q, 110) = (p, 10) = (p, 0) = qF ∴reject • ※transition table : transition function is represented to matrix • [Ex] Transition Table • 0 1 • p q p • q r p • r r r
1 0, 1 start 0 0 p q r 1 Regular Language [Def 3.8]transition diagram state : node transition function : label, directed arc eg) (q, a) = p a ≡ ⓠ ⓟ final state : double circle [Ex] What is a transition diagram for example before? [Def 3.9]completely specified if ∀q∈Q, a∈ in M=(Q, , , q0, F), (q, a) is existed uniquely next state ∴ DFA ≡ completely specified
Regular Language ▶ Algorithm that string is recognized by DFA M = (Q, , , q0, F) • Algorithm_Recognize() • { current_state = q0; • get(next_symbol); • while(input string not exhausted) { • current_state = (current_state, next_symbol); • get(next_symbol); • } • if(current_state∈F) printf("Accept"); • else printf("Reject"); • } NFA(Non-deterministic FA) • [Def 3.10]NFA M = (Q, , , q0, F) • Q : finite set of states • : set of input alphabet • q0 : initial state • F : set of finial state, F⊆Q • : transition function Q× → 2Q, (q, a) = {p1, p2, ···, pn}
q 2 0 0 0, 1 0, 1 start 0, 1 q q q 1 f 0 1 1 1 q 3 Regular Language [Ex] What is transition diagram of M=({q0, q1, q2, q3, qf}, {0, 1}, , q0, {qf})? 0 1 q0 { q1, q2 } { q1, q3 } q1 { q1, q2 } { q1, q3 } q2 { qf } q3 { qf } qf { qf } { qf } ※(q, a) = : not defined
Regular Language ▶Extension of NFA • [step 1] • Q× → 2Q Q× * → 2Q • (q, ) = {q} • (q, xa) = ∪ (p, a) • p∈(q, x) • x∈*, a∈ • ∴symbol → extend to string • [step 2] • Q× → 2Q 2Q×* → 2Q • ({ p1, p2, ···, pk}, x) = (pi , x) • ∴one state → extended to more state • [Ex] (q0, 1001) = (q1, 001)∪(q3, 001) • = (q1, 01)∪ (q2, 01)∪ • = (q1, 1)∪(q2, 1)∪(qf, 1) • = {q1, q3, qf }
Regular Language [Def 3.11]L(M) that is recognized by NFA M L(M) = {x(q0, x)∩F≠} [Ex] Can be recognized to 1011? (q0, 1011) = ({q1, q3}, 011) = ({q1, q2}, 11) = ({q1, q3}, 1) = {q1, q3, qf} {q1, q3, qf}∩{qf}≠ ∴1011is accepted by M if one more than final state is exist, the string is accepted. state of NFA M : when number of m, length(string) = n number of maximum nodes in tree = mn ∴string that is recognized in NFA, excessively time-consumed
Regular Language Transformation from NFA to DFA • A NFA represent language structure simply(RE→NFA), • but difficult to implementation. • [Theorem 3.2] Let NFA M=(Q, , , q0, F). • if L(M) is accepted by NFA M, DFA M'=(Q', ', ', q0', F') is existed. • ≡ construct DFA M'=(Q', , ', q0', F') • ① Q' = 2Q (power set of Q), [presented a state Q : [q1, q2,···, qi], qi∈Q] • ② q0'=[q0] • ③ F'={q∈Q'q∈F} • ④ if ({ q1, q2,···, qi}, a)={p1, p2,···, pj}, then '([q1, q2,···, qi], a)=[ p1, p2,···, pj]
’ 0 1 [q0] [q0, q1] [q0] [q0] [q0, q1] [q0, q1] [q0, q1] [q0, q1] 1 start B A 1 0,1 1 0 start A C 0 0,1 C Regular Language • [Ex 21] NFA M=({q0, q1}, {0, 1}, , q0, {q1}) • state name change : [q0]=A, [q1]=B, [q0, q1]=C • [Ex 22], [Ex 23] Reference of Text pp.78 ~ pp.80 0 1 q0 {q0, q1} {q0} q1 {q0, q1}
Regular Language [Def 3.12]-NFA M=(Q, , , q0, F) : : Q(∪{}) → 2Q other cases are same to NFA. [Def 3.13]-CLOSURE(s) case of s that is one state -CLOSURE(s) = include itself state sand all reachable states by label from state s (repeat until not changes elements of -CLOSURE(s)) case of T that is one or more states -CLOSURE(T) = each state in T, executing step and union its all results (-CLOSURE(T)) = ∪ -CLOSURE(x) q∈T
Regular Language [Ex 24] What is CLOSURE in following -NFA? CLOSURE(A)={A, B, D} CLOSURE({A, C})=CLOSURE(A) CLOSURE(C)={A, B, C, D} [Ex 25] Transform from-NFA to DFA b a a e e a start C D B A e 2 b a start 1 4 c e e 3
Regular Language a b c CL(2)={2} CL(3)={3,4} [2] [3,4] CL(1)={1,3,4} [1,3,4] [2] [3,4] [4] CL(4)={4} [4] CL(3)={3,4} [3,4] state name : A=[1,3,4], B=[2], C=[3,4], D=[4] start a b B A D c c C
Regular Language DFA's state minimization • use of equivalence relation → state merge • ▶≡(equivalence relation) • p≡q iff δ(p, 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. • ▶inaccessible state : only existed out arc, except start state • ▶minimizing method • 1. eliminate inaccessible state • 2. grouping final states and non-final states • 3. divide state that going to another equivalence class by same input symbol. • 4. repeat step 3 when can not divided
Regular Language ▶new DFA M'= (Q', Σ, δ', q0', F') 1. Q' : set of equivalence class eg)[q] : equivalence class include q 2. let [p], [q] are equivalence class, if δ(p, a) = q, then δ'([q], a) = [q]. 3. q0' is [q0] 4. F'= {[q]q∈F} [Ex 26] DFA's minimization a b B C a a start A b b a b b D E a
Regular Language 3 : {C, E} 1 : {A} 2 : {B, D} 1 : {A, B, D} 2 : {C, E} a b a b 2 2 3 3 2 2 3 3 2 2 1 1 2 2 1 1 1 1 2 2 state name : X=[A], Y=[B, D], Z=[C, E] a b δ' X Y Z Y Y Y Y Z Z b a b start a, b X Z Y a
Regular Language A Close Properties of Finite Automata • [Theorem 3.3]if L1, L2 are R.L. • 1. L1∪ L2 • 2. L1·L2 • 3. L1* are R.L • Proof) • case 1. • L1, L2 are R.L, such that exist G1=(VN1, VT1, P1, S1), G2=(VN2, VT2, P2, S2) • ∴ L1 = L(G1), L2 = L(G2) • It can be consist of new Grammar G. • G=(VN1∪VN2∪{S}, VT1∪VT2, P1∪P2∪{S→S1|S2}, S), • S⇒S1⇒1∈L(G1) or S⇒S2⇒2∈L(G2). • ∴ L(G) = L(G1)∪L(G2) = L1∪L2 • G is R.G ⇒ L1∪ L2 is R.L. • case 2, 3. Same. • ∴R.L is closed to union, concatenation, closure
Finite Regular Automata Expression Regular Language • Attributes of Regular Language Relationship of Regular grammar and Finite Automata • ▶Regular grammar FA • method of construction to FA • M=(Q, , , q0, F) • Q : VN∪{f}, f : new final state • : VT • q0 : 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 Regular Grammar
0 1 S B f {S, f} {S, f} {S, f} Regular Language [Ex 28] Changing regular grammar to FA G=({S, B}, {0, 1}, P, S), • P : S→0S S→1B S→0 S→1 • B→0S B→0 • M=(Q, , , q0, F) • Q : VN∪{f} = {S, B, f} • : VT ={0, 1} • q0 : S • F : {f} • ▶FA Regular grammar • method of construction to Regular Grammar • G=( VN, VT, P, S) • VN : Q • VT : • S : q0 • P : if (q, a) = r, then q→ar; • if q∈F, then q→ • [Ex 29]FA Regular grammar (Reference of Text pp.91)
Regular Language Finite Automata and Regular Expression • ▶FA Regular Expression • method : FA RG RE • FA transition table • transition table RE • regular grammar • regular grammar equation • regular grammar equation RE • ▶Regular Expression FA • method : RE NFA(-NFA) DFA • ▣ RE -NFA • 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.
Compiler - The End of Chapter 3 -