The Equivalent Transforming among RG, RE and FA
A Grammar G is a quadruple (四元组):G = (VN, VT, S, P )
Where,
Regular Grammar (RG) (正规文法):
α∈VN and β ∈VT∪VTVN
Regular Expression:
Regular expressions over ∑ are defined as :
Deterministic Finite Automata:
A Deterministic Finite Automaton (DFA) is a quintuple (五元组)
M = (S, ∑ , f, s0 , Z),
where
Nondeterministic Finite Automata:
An Nondeterministic Finite Automata (NFA) is also a quintuple
M = ( S, ∑ , f, s0 , Z),
where:
The definitions of transition functions of DFA and NFA are different:
DFA f: S×∑ →S
NFA f: S×∑* →ρ(S)
That means: a state of DFA has a unique next state and can not transit without input, but a state of NFA may have more than one next states and can transit without input.
Let r be an RE on∑. Construct G = (VN, VT, S, P):
Initialization: VT=∑; P = {S → r}; VN ={S};
For any production in P, rewrite it as follows:
⑴ A → x*y => A → xA |y.
⑵ A→x | y => A → x | y;
⑶A → xy => A → xB B → y; VN = VN ∪{B};
⑷A→(x | y)B =>A → xB | yB; VN = VN ∪{B};
where x and y be RE’s, B be a new nonterminal.
Repeat step 2 until each rule has one termianal.
Transform a(a | d)* to RG
VT={a, b}; P = {S → a(a | d)* }; VN ={S};
S → a(a | d)* => S → aA A → (a | d)*
A → (a | d)* => A → (a | d)A |ε
A → (a | d)A => A → aA | dA
Finally we get the regular grammar G is
({S, A}, {a, b}, S, {S→ aA A→ aA|dA|ε})
Consider the grammar G:S → aS | aA A → bB B → aB | a
Transform the rules into a group of equations:
S = aS + aA (1)
A = bB (2)
B = aB + a (3)
For equation 3 we have: B = a*a;
For equation 2 we have: A = ba*a;
For equation 1 we have: S = aS + aba*a;
Finally we have: S = a*aba*a;
G = (VN, VT, S, P) is a right linear grammar, there is an FA M such that L(M) = L(G).
Let M = (Q, ∑, f, s0 , {Z}), where
∑= VT; Q = VN∪{Z}; Z ∉ \notin ∈/ VN; s0= S;
For ∀ \forall ∀ A→tB∈P, t∈VT∪{e}, A, B∈VN, then f(A, t) = B;
For ∀ \forall ∀ A→t∈P, A∈VN, t∈VT∪{ε}, then f(A, t) = Z.
Given G=({A,B,C,D},{0,1},f,A,P),
P = {A → 0 | 0B | 1D B → 0D | 1C
C → 0 | 0B| 1D D → 0D | 1D }
M=(Q, ∑, f, s0 , F) is a DFA, there is a right linear grammar G such that L(M) = L(G).
Let G = (VN, VT, S, P), where
VT = ∑; VN = Q; S = s0;
For ∀ \forall ∀ t∈VT, ∀ \forall ∀ A, B∈VN, if f(A, t) = B
then If B ∈F then A →tB | t∈P
else A→tB∈P;
Given M=({0,1},{A,B,C,D},f,A,{B})
Construc G = (VN,VT, A, P),
VN = {A, B, C, D},
VT = {0, 1},
P = { A → 0B | 0 | 1D
B → 0D | 1C
C → 0B | 0 | 1D D → 0D | 1D }
Given an RE r, there is an FA M, such that L(M) = L®.
Assume that for any RE r1 and r2, if |r1| < k and |r2| < k, they have their NFA M1 and M2.
Initialization: set up two state X and Y, such as:
Repeat dividing the RE r on the arcs according to the following rules (1) to (3):
Until each arc labeled by a symbol.
Initialization: Add two state : X , Y. X is the unique initial state and Y is the unique final state
Repeat linking the RE r on the arcs according to the following rules (1) to (3):