Regular grammars, regular expressions, finite automata and conversions between them (notes)

The Equivalent Transforming among RG, RE and FA

regular grammar

A Grammar G is a quadruple: G = (V N , V T , S, P )

Where,

  • VN is a finite set of nonterminals.
  • VT is a finite set of terminals.
  • S is the start symbol, S ∈ \in VN.
  • P is a finite set of productions (production type).

Regular Grammar (RG) (regular grammar):

α∈VN and β ∈VT∪VTVN

regular expression

Regular Expression:

Regular expressions over ∑ are defined as :

  1. ε and ϕ \phiϕ are RE’s, denoting the sets {ε} and Φ , respectively ;
  2. Any a ∈ \in ∑ , a is an RE, denoting the set { a };
  3. If a and b are RE’s, denoting the sets A and B respectively, then a b , a | b, and a* are RE’s, denoting the sets AB, AUB and A* respectively;
  4. Nothing is an RE unless it follows from 1 to 3 finite times.

finite automata

deterministic finite automata

Deterministic Finite Automata:

A Deterministic Finite Automaton (DFA) is a quintuple

M = (S, ∑ , f, s0 , Z),

where

  • S is a finite set of states;
  • ∑ is the set of input symbols;
  • f : S×∑ →S, the transition function;
  • s0 ∈ \in S, the initial state;
  • Z ⊆ \subseteq S, the set of final states.

non-deterministic finite automata

Nondeterministic Finite Automata:

An Nondeterministic Finite Automata (NFA) is also a quintuple

M = ( S, ∑ , f, s0 , Z),

where:

  • S is a finite set of states;
  • ∑ is the set of input symbols;
  • f : S×∑* →ρ(S), the transition function;
  • s0 ∈ \in S, the initial state;
  • Z ⊆ \subseteq S, the set of final states.

Difference between DFA & NFA

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.

Regular Expression to Regular Grammar

Transforming RE to RG

Let r be an RE on∑. Construct G = (VN, VT, S, P):

  1. Initialization: VT=∑; P = {S → r}; VN ={S};

  2. For any production in P, rewrite it as follows:

    14 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.

  3. Repeat step 2 until each rule has one termianal.

sample

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|ε})

Regular Grammar to Regular Expression

Transforming RG to RE

  1. Transform RG to a group of equations by replacing → and | as = and +, respectively.
  2. For equations X = aY + a and Y = b, we replace Y by b, and get X = ab + a.
  3. If there is an equation X=rX + t, we have the solution X = r*t;
  4. Repeat step 2 and 3, until the solution S=r is got. Then r is the corresponding RE.

sample

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;

Converting Regular Grammars to Finite Automata

Transforming RG to FA

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.

sample

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 }

  • Construct M = (Q,∑,f,A,{Z}):
  • Q={A,B,C,D,Z}, ∑ = {0, 1},
  • f(A, 0)=B, f(A, 0)=Z, f(A, 1)=D,
  • f(B, 0)=D, f(B, 1)=C,
  • f(C, 0)=B, f(C, 0)=Z, f(C, 1)=D,
  • f(D, 0)=D, f(D, 1)=D.

insert image description here

Converting Finite Automata to Regular Grammars

Transforming FA to RG

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;

sample

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 }

Regex conversion to finite automata

Transforming RE to FA

Given an RE r, there is an FA M, such that L(M) = L®.

  • Basis: |r| = 1.
  • We prove it by induction on the length of r.

insert image description here

Assume that for any RE r1 and r2, if |r1| < k and |r2| < k, they have their NFA M1 and M2.

  • For any RE r, |r| = k, we have:

insert image description here

Initialization: set up two state X and Y, such as:

insert image description here

Repeat dividing the RE r on the arcs according to the following rules (1) to (3):

insert image description here

Until each arc labeled by a symbol.

sample

insert image description here

Finite automata converted to regular expressions

Transforming FA to RE

Initialization: Add two state : X , Y. X is the unique initial state and Y is the unique final state

insert image description here

Repeat linking the RE r on the arcs according to the following rules (1) to (3):

insert image description here

insert image description here

sample

Guess you like

Origin blog.csdn.net/m0_61465701/article/details/130957192