## 5

## Regular Expression

#### Introduction

Regular expression, in short RE, is the language part of the type 3 grammar, which is called regular grammar. RE is accepted by the machine called finite automata (FA). RE was first proposed by an American mathematician *Stephen Kleene* as a notion of describing the algebra of a regular set. Later on, *Ken Thompson* used the RE in early computer text editor ‘QED’ and Unix editor ‘ed’. QED uses RE for searching text. RE became useful in the Unix text processing program ‘grep’ and modern programming languages such as ‘PERL’. A Canadian computer scientist *Henry Spencer* wrote ‘regex’, which is widely used as a software library for RE.

They are called ‘regular’ because they describe a language with very ‘regular’ properties. In the previous chapter, we have already discussed about FA. Now, in this chapter, we shall learn about RE and some important properties related to RE.

#### 5.1 Basics of Regular Expression

An RE can be defined as a language or string accepted by an FA.

We know that an FA consists of 5 touples {Q, Σ, δ, q_{0}, F}. Among them, an RE is a string on Σ, i.e., it consists of only input alphabets.

In short, a regular expression is written as RE or regex or regexp.

#### 5.1.1 Some Formal Recursive Definitions of RE

- Any terminals, i.e., the symbols that belong to
**Σ**are RE. The null string(**Λ**) and the null set(Ø) are also RE. - If P and Q are two REs, then the union of the two REs denoted by P+Q is also an RE.
- If P and Q are two REs, then their concatenation denoted by PQ is also an RE.
- If P is an RE, then the iteration (repetition or closure) denoted by P* is also an RE.
- If P is an RE, then (P) is an RE.
- The expressions got by the repeated application of the rules from (1) to (5) over Σ are also REs.

#### 5.2 Basic Operations on Regular Expression

Three basic operations are union, concatenation, and closure.

**The operation of union on RE:**If R_{1}and R_{2}are two REs over Σ, then L(R_{1}∪ R_{2}) is denoted byL(R

_{1}+ R_{2}).L(R

_{1}∪ R_{2}) is a string from R_{1}or a string from R_{2}.L(R

_{1}+ R_{2}) = L(R_{1}) ∪ L(R_{2}).**The operation of concatenation on RE:**If R_{1}and R_{2}are two REs over Σ, then L(R_{1}∩ R_{2}) is denoted by L(R_{1}R_{2}).L(R

_{1}∩ R_{2}) is a string from R_{1}followed by a string from R_{2}.L(R

_{1}R_{2}) = L(R_{1})L(R_{2})**The operation of closure on RE:**If R_{1}and R_{2}are two REs over Σ, then L(R*) is a string obtained by concatenating n elements for n ≥ 0.

#### 5.2.1 Kleene’s Closure

It is defined as a set of all strings including null (Λ) obtained from the alphabets of the set Σ. It is denoted as Σ*.

If Σ = {0}, then Σ* = {Λ, 0, 00, 000, ……..}

If Σ = {0, 1}, then Σ* = {Λ, 0, 1, 01, 00, 11, 010, 011, 100, …….}

**5.2.1.1 Difference Between Closure and Kleene’s Closure**

- Closure is nothing but the iteration of 0 to ∞ times, but Kleene’s closure is the set including Λ.
- Closure is applied on RE, but Kleene’s closure is applied on Σ.
- If R = 01, then the closure on R denoted by R* are Λ, 01, 0101, 010101……etc., i.e., the iteration of the same string 01.
- If Σ = {0, 1}, then Kleene’s closure is denoted by Σ* = {Λ, 0, 1, 01, 00, 11, 010, 011, 100,…….}, i.e., the set of any combinations of 0 and 1 including Λ.

** Precedence of Operator:** Among the previously discussed operators, Kleene star has the highest priority. Then, concatenation comes into play and finally union has the lowest priority.

REs can be described in English language. Consider the following examples.

#### Example 5.1

Describe the following REs in English language.

- 10*1
- a*b*
- (ab)*
- (a* + b*) c*
- (00)*(11)*1

- The strings that we get from the RE 10*1 are {11, 101, 1001, 10001…..}. From the set, we can conclude that the strings that are generated from the REs are beginning with 1 and ending with 1. In between 1 and 1, there are any numbers of 0.
So, we can describe in English like this:

*Set of all strings beginning and ending with 1 with any number of 0 in between the two 1s*. - The strings that we get from the RE a*b* are a* . b*, i.e.,
{Λ, a, aa, aaa, ……}.{Λ, b, bb, bbb, …….} = {Λ, a, b, aa, bb, ab, abb, aab, ……..}.

From the set, we can conclude that the strings that are generated from the RE contain any number of ‘a’ followed by any number of ‘b’.

So, we can describe in English like this:

*Set of all strings of ‘a’ and ‘b’ containing any number of ‘a’ followed by any number of ‘b’*. - The strings that we get from the RE (ab)* are {Λ, ab, abab, ababab, ………}.
In English, it is described as:

*Set of all strings of a and b with equal number of a and b containing ‘ab’ as repetition*. - (a* + b*) means any combination of a or any combination of b. c* means any combination of c, which is concatenated with (a* + b*).
In English, it can be described as:

*Any combination of ‘a’ or any combination of ‘b’ followed by any combination of ‘c’*. - The strings that we get from the RE (00)*(11)*1 are {1, 001, 111, 00111, ……}. In the string, there are always odd number of 1 and even number of 0.
In English, it can be described as:

*Set of all strings of ‘0’ and ‘1’ with even number of 0 followed by odd number of 1*.

Alternatively, the RE described in English language can be denoted using alphabet and symbols. The following examples describe this.

#### Example 5.2

Built regular expression of the following

- An RE consists of any combination of a and b, beginning with a and ending with b.
- A language of any combination of a and b containing abb as a substring.
- RE of a and b containing at least 2 ‘a’s.
- Write an RE for the language L = {a
^{n}b^{m}| where m + n is even}. - An RE of a and b, having exactly one ‘a’.

*Solution:*

- The RE consists of any combination of a and b, i.e., (a + b)*. But the RE starts with a and ends with b. In between a and b, any combination of a and b occurs. So, the RE is
L = a(a + b)*b.
- In the RE, abb occurs as a substring. The RE consists of any combination of a and b. Before the substring, abb may occur at the beginning, at the middle, or at the last. If abb occurs at the beginning, then after abb there is any combination of a and b. If abb occurs at the middle, then before and after abb there are any combination of a and b. If abb occurs at the last, then before abb there is any combination of a and b.
The RE is L = (a + b)*abb(a + b)*.

- In the RE, there are at least two ‘a’s. The expression consists of a and b. Therefore, any number of ‘b’ can occur before the first ‘a’ and before the second ‘a’, i.e., after the first ‘a’ and after the second ‘a’.
So, the RE will be L = b*ab*ab*.

- The RE consists of n number of ‘a’ followed by m number of ‘b’. But m + n is always even. This is possible if
(a) m and n both are even or (b) m and n both are odd.

If m and n are both even, then the expression is (aa)*(bb)*.

If m and n are both odd, then the expression is (aa)*a(bb)*b.

By combining these two, the final RE becomes

L = (aa)*(bb)* + (aa)*a(bb)*b. - In the RE, there is exactly one ‘a’. Before and after a, there is any combination of b.
Therefore, the RE is L = b*ab*.

#### 5.3 Identities of Regular Expression

An *identity* is a relation which is tautologically true. In mathematics, an equation which is true for every value of the variable is called an identity equation. As an example, (a + b)^{2} = a^{2} + 2ab + b^{2} is an identity equation. Based on these identities, some other problems can be proved. In the RE also, there are some identities which are true for every RE. In this section, we shall discuss those identities related to RE.

- Ø + R = R + Ø = R
- Ø R = R Ø = Ø
Note: In both the previous cases, Ø denotes a null set, which contains nothing.

- ΛR = RΛ = R
- Λ* = Λ & Ø* = Λ
Same for Ø

.*** - R + R = R
- R*R* = R*
- R*R = RR*
- (R*)* = R*
- Λ + RR* = Λ + R*R = R*
- (PQ)*P = P(QP)*
- (P + Q)* = (P*Q*)* = (P* + Q*)* (D’Morgan’s theorem)
- (P + Q)R = PR + QR

#### Example 5.3

From the identities of RE, prove that

(1 + 100*) + (1 + 100*)(0 + 10*)(0 + 10*)* = 10*(0 + 10*)*.

** Solution:** LHS.

#### Example 5.4

From the identities of RE, prove that the following three are equivalent

- (011((11)* + (01)*)*)*011
- 011(((1 + 0)1)*011)*
- 011(((11)*(01)*)*011)*

** Solution:** Let P = (11) and Q = (01).

We know (P + Q)* = (P*Q*)* = (P* + Q*)*.

Thus, ((11) + (01))* = ((11)*(01)*)* [in c] = ((11)* + (01)*)*[in a] = ((1 + 0)1)* [in b].

Consider 011 as R and (P + Q)* as S.

We know that (RS)*****R = R(SR)*

Thus,

Similarly,

As (i) ≡ (ii) and (i) ≡ (iii), (i) ≡ (ii) ≡ (iii).

#### Example 5.5

From the identities of RE, prove that

**Solution**:

#### 5.4 The Arden’s Theorem

The Arden’s theorem is used to construct the RE from a transitional system of FA.

** Theorem 5.1:** Statement: Let P and Q be two REs over Σ. If P does not contain Λ, then the equation R = Q + RP has a unique (one and only one) solution, R = QP*.

** Proof:** Now, point out the statements in the Arden’s theorem in general form.

**P**and**Q**are two REs.**P**does not contain the**Λ**symbol.**R**=**Q**+**RP**has a solution, i.e.,**R**=**QP***.- This solution is the one and only solution of the equation.

If R = QP* is a solution of the equation R = Q + RP, then by putting the value of R in the equation, we shall get the value ‘0’.

R − Q − RP = 0

LHS R – Q – RP

(Putting the value of R in the LHS, we get)

So, from here it is proved that R = QP* is a solution of the equation R = Q + RP.

Now, we have to prove that R = QP* is the one and only solution of the equation R = Q + RP.

As R = Q + RP, put the value of R again and again in the RHS of the equation.

After several steps, we shall get

^{2}+ P

^{3}+ .... P

^{n}) + RP

^{n + 1}.

Now let a string w belong to R. If w is a Λ string, then in which part will the Λ belong?

This string will belong to either the Q(Λ + P + P^{2} + P^{3} + .... P^{n}) part or the RP^{n + 1} part. But, according to point number (ii), P does not contain Λ, and so the string w does not belong to RP^{n + 1}. So, it will obviously belong to Q(Λ + P + P^{2} + P^{3} + .... P^{n}) which is nothing but QP^{*}. [(Λ + P + P^{2} + P^{3} + .... P^{n}) is any combination of P including Λ.]

As this string belongs only in one part, R and QP^{*} represent the same set. That means R = QP^{*} is the unique solution of the equation R = Q + RP.

#### 5.4.1 Process of Constructing Regular Expression from Finite Automata

There are some assumptions:

- In the transitional graph, there must be no Λ-move.
- In the FA, there is only one initial state.

Now, we have to construct equations for all the states. There are n number of equations if there are n-states.

For any FA, these equations are constructed in the following way.

For the beginning state, there is an arrow at the beginning coming from no state. So, a Λ is added with the equation of the beginning state.

Then, these equations have to be solved by the identities of RE. The expression obtained for the final state and consists of only the input symbol (Σ) is the Regular Expression for the Finite Automata.

The following examples describe the process of the construction of an RE from FA.

#### Example 5.6

Construct an RE from the given FA in Fig. 5.1 by the algebraic method using Arden’s theorem

**Fig. 5.1**

*Solution:*

For the previously given FA, the equations are

Put the value of q_{2} in equation no. (2).

This equation is in the form R = Q + RP, where R = q_{1}, Q = q_{0}.0, and P = (1 + 0.0).

So, the solution of the equation is R = QP*, i.e., q_{1} = q_{0}.0. (1 + 0.0)*.

Putting the value q_{2} = q_{1}.0 in equation no. (1), we get

_{0}= q

_{0}.1 + q

_{1}.0.1 + Λ.

Again putting the value of q_{1} in the previous equation, we get

This equation is in the form R = Q + RP, where R = q_{0}, Q = Λ, and P = (1 + 0.(1 + 0.0)*.0.1). So, the solution of the equation is

Putting the value of q_{0} in the solution of q_{1}, we get

_{1}= (1 + 0.(1 + 0.0)*.0.1)*.0. (1 + 0.0)*.

Replacing the value of q_{1} in equation (3), we get

_{2}= (1 + 0.(1 + 0.0)*.0.1)*.0. (1 + 0.0)*.0.

As q_{2} is the final state of the FA, all the strings will halt on this state. Therefore, the RE is (1 + 0. (1 + 0.0)*.0.1)*.0. (1 + 0.0)*.0, thus accepting the FA.

#### Example 5.7

Construct an RE from the given FA in Fig. 5.2 by the algebraic method using Arden’s theorem.

**Fig. 5.2**

** Solution:** For the previously given FA, the equations are

Putting the value of q_{0} in equation q_{1}, it becomes q_{1} = 0 + q_{1}0. The equation is in the form R = Q + RP, where R = q_{1}, Q = 0, and P = 0. By the Arden’s theorem, the solution of the equation is R = QP*, i.e.,

_{1}= 00*.

Putting the value of q_{1} in the equation (3), we get q_{2} = 00*1 + q_{2}1.

The equation is in the form R = Q + RP, where R = q_{2}, Q = 00*1, and P = 1. By the Arden’s theorem, the solution of the equation is R = QP*, i.e.,

_{2}= 00*11*.

Putting the value of q_{2} and q_{0} in equation (4), we get

_{3}= (00*11*0 + 1) + q

_{3}(0 + 1).

The equation is in the form R = Q + RP, where R = q_{3}, Q = (00*11*0 + 1), and P = (0 + 1). By the Arden’s theorem, the solution of the equation is R = QP*, i.e.,

_{3}= (00*11*0 + 1) (0 + 1)*.

As q_{3} is the final state, the RE generated from the given FA is (00*11*0 + 1) (0 + 1)*.

#### Example 5.8

Construct an RE from the given FA in Fig. 5.3 by the algebraic method using Arden’s theorem

**Fig. 5.3**

** Solution:** For the given FA, the equations are

Substituting the value of q_{2} and q_{3} in q_{1}, we get

If q_{1} is treated as R, Λ as Q, and (01 + 10) as P, then the equation becomes R = Q + RP.

The solution for the equation is R = QP*.

As q_{1} is the only final state, the string accepted by the FA is (01 + 10)*.

(Note: In the automata the transition from q_{2} and q_{3} for input 1 is missing. A dead state may be added to complete this FA, but the equation for dead state will be in no need in solving the automata. From here, it is also proved that the dead state is only necessary to prove that a string is not accepted by an FA by totally traversing the string.)

#### Example 5.9

Construct an RE from the given FA in Fig. 5.4 by the algebraic method using Arden’s theorem.

**Fig. 5.4**

** Solution:** For the given FA, the equations will be

q_{3} = q_{2.}a, replace this in equation (2). The equation becomes

The equation is in the format of R = Q + RP, q_{2} [R] = q_{2}[R](b + a.b)[P] + q_{1}.b [Q].

The solution is R = QP*, i.e., q_{2} = q_{1}b.(b + ab)*.

Replace the value of q_{2} in equation (3).

_{3}= q

_{1}b.(b + ab)*.a.

Replace the value of q_{3} in equation (1).

The equation is in the format of R = Q + RP, q_{1}[R] = q_{1}[R].(a + b.(b + ab)*.a.a)[P] + **Λ**[Q]. The solution of the equation is R = QP*, i.e., q_{1} = **Λ**.(a + b.(b + ab)*.a.a)*,

q_{1} is the final state, and so the RE accepting the FA is

#### 5.5 Construction of Finite Automata from a Regular Expression

RE is the language format of the FA. Deterministic finite automata (DFA) can be constructed from a given RE in two ways:

- Conversion of an RE to non-deterministic finite automata (NFA) with
*ε*transition, and then converting it to DFA by*ε*closure. - Direct conversion of an RE to DFA.

#### 5.5.1 Conversion of an RE to NFA with ε transition

For making an FA from an RE, there are two steps as given in the following.

** Step I:** From the given RE, an equivalent transitional system (FA) with Λ move has to be constructed.

** Step II:** From the transitional system with Λ moves, a DFA needs to be constructed by the ε closure method.

For constructing step I, again there are two methods (i) top-down approach and (ii) bottom-up approach. In the top-down approach, the FA construction starts from the given RE and ends by reaching a single element for each transition. In the bottom-up approach, the construction starts from the basic element and ends on reaching the RE.

**5.5.1.1 Top-Down Approach**

This is divided into several steps as given in the following.

- Take two states, one is the beginning state and another is the final state. Make a transition from the beginning state to the final state and place the RE in between the beginning and final states (Fig. 5.5).
**Fig. 5.5** - If in the RE there is a + (union) sign, then there are parallel paths between the two states (Fig. 5.6).
**Fig. 5.6** - If in the RE there is a .(dot) sign, then one extra state is added between the two states (Fig. 5.7).
**Fig. 5.7** - If in the RE there is a ‘*’ (closure) sign, then a new state is
*added in between. A loop is added on the new state and the label Λ is put*between the first to new and new to last (Fig. 5.8)**Fig. 5.8**

The following examples make the described process clear for understanding.

#### Example 5.10

Construct an FA equivalent to the RE:

*Solution:*

**Two states are taken:**one is the beginning state and another is the final state. The RE is placed between the two states with a transition from the beginning state to the final state as given in Fig. 5.9(a)**Fig. 5.9(a)**- In the RE, there are two concatenations, between (a + b)* and (aa + bb) and between (aa + bb) and (a + b)*. Two extra states are added between q
_{0}and q_{f}as given in Fig. 5.9(b).**Fig. 5.9(b)** - In aa and bb, there is a + (union) sign. So, between q
_{1}and q_{2}, parallel paths are constructed as given in Fig. 5.9(c).**Fig. 5.9(c)** - There are two * (closure) signs between q
_{0}and q_{1}, and q_{2}and q_{f}. Two new states q_{3}and q_{4}are added, respectively, between q_{0}, q_{1}and q_{2}, q_{f}. The loops with label a + b are added to q_{3}and q_{4}, making transitions between (q_{0}, q_{3}), (q_{3}, q_{1}), (q_{2}, q_{4}), and (q_{4}, q_{f}) with label Λ as in Fig. 5.9(d).**Fig. 5.9(d)** - Removing + (union) and • (concatenation) of the final FA with Λ transitions is given in Fig. 5.9(e).
**Fig. 5.9(e)** - Between q
_{0}, q_{3}and q_{4}, q_{f}, there is a label Λ. It means that from q_{0}by getting no input it can reach to q_{3}, and the same thing is for q_{4}and q_{f}. Therefore, q_{0}and q_{3}may be treated as the same state, and the same for q_{4}and q_{f}. The transitions from q_{3}and q_{f}are added with q_{0}and q_{4}, respectively, with same label.

By removing the Λ transactions, the FA is given in Fig. 5.9(f)

**Fig. 5.9(f)**

#### Example 5.11

Construct Finite Automata equivalent to the Regular Expression

*Solution:*

** Step I:** Take a beginning state q

_{0}and a final state q

_{f}. Between the beginning and final state place the regular expression (Fig. 5.10a).

** Step II:** There are three dots (.) between ab, (aa + bb), (a + b)*, and b. Three extra states are added between q

_{0}and q

_{f}(Fig. 5.10b).

** Step III:** Between ‘a’ and ‘b’ there is a dot (.), so extra state is added (Fig. 5.10c).

** Step IV:** In aa + bb there is a +, therefore there is parallel edges between q

_{1}and q

_{2}. Between q

_{2}and q

_{3}there is (a + b)*. So, extra state q

_{5}is added between q

_{2}and q

_{3}. Loop with label a, b is placed on q

_{5}and

**Λ**transition is made between q

_{2}, q

_{5}and q

_{5}, q

_{3}(Fig. 5.10d).

** Step V:** In aa and bb there are dots (.). Thus two extra states are added between q

_{1}and q

_{2}(one for aa and another bb). The final finite automata for the given regular expression is given in Fig. 5.10e.

**Figs. 5.10(a) to (e)** Construction of FA for L = ab(aa + bb)(a + b)*b.

#### Example 5.12

Construct an FA equivalent to the RE:

*Solution***:**

** Step I:** Take a beginning state q

_{0}and final state q

_{f}. Between them, place the RE.

** Step II:** For ‘ + ’, there will be two parallel edges between q

_{0}and q

_{f}.

** Step III:** For ‘.’ dots between a and b, (aa + bb), and (a + b)* and (a + b)* and b, add three new states.

** Step IV:** For (aa + bb), two parallel edges with two extra states are added.

** Step V:** ‘*’ is removed by putting the loop on another extra state with null transitions between q

_{2}to the new state and the new state to q

_{3}.

** Step VI:** Removing the Λ transitions, the final FA is given in Fig. 5.11.

**Fig. 5.11** Construction of FA for L = ab + (aa + bb)(a + b)*b.

The bottom-up approach to convert an RE to an FA is proposed by Thomson. This method is known as the Thomson construction. The process is described in the following text.

**5.5.1.2 Bottom-up Approach (Thomson Construction)**

- For input ε, the transition diagram is constructed as in Fig. 5.12(a).
- For input a ∈ ∑, the transition diagram is as shown in Fig. 5.12(b).
- If NFA(r
_{1}) is the NFA for RE r_{1}and NFA(r_{2}) is the NFA for RE r_{2}, then for the RE r_{1 }+ r_{2}the transition diagram is as shown in Fig. 5.12(c). - If NFA(r
_{1}) is the NFA for RE r_{1}and NFA(r_{2}) is the NFA for RE r_{2}, then for the RE r_{1}.r_{2}the transition diagram is as shown in Fig. 5.12(d). - If NFA(r) is the NFA for RE r, then for the RE r
^{*}, the transition diagram is as shown in Fig. 5.12(e).

**Figs. 5.12(a)–(e)** The Thomson Construction

#### Example 5.13

Construct Finite Automata equivalent to the Regular Expression

*Solution:*

** Step I:** The terminal symbols in L are ‘a’ and ‘b’. The transition diagrams for ‘a’ and ‘b’ are given in Fig. 5.13(a).

** Step II:** The transition diagrams for ‘aa’, ‘ab’, ‘bb’ are given in Fig. 5.13(b).

** Step III:** The transition diagram for (a+b) is given in Fig. 5.13(c)

** Step IV:** The transition diagram for (a+b)* is given in Fig. 5.13(d).

** Step V:** For (aa+bb) the transitional diagram is given in Fig. 5.13(e).

** Step VI:** The constructed transitional diagram for ab(aa+bb) is given in Fig. 5.13(f).

** Step VII:** The constructed transitional diagram for ab(aa + bb)(a + b)*a is given in Fig. 5.13(g).

This can be simplified to Fig. 5.13(h) by removing ε transitions.

**Figs. 5.13 (a – h) **Construction of FA from RE by Thomson Construction

** Kleene’ s Theorem:** If R is an RE, then there exists an NDFA, M

_{NFA}with ε transition, which accepts the language generated by R.

#### 5.5.2 Direct Conversion of RE to DFA

Using this method, a given RE is converted to a DFA without the construction of an NFA. Before describing the process, we need to know a few definitions such as ‘firstpos’, ‘followpos’, ‘nullable’, and the construction processes of those.

** firstpos:** It is the abbreviation of first position. It is defined as the set of the positions of the first symbols of strings generated by the sub-expression rooted by n.

** followpos:** It is the abbreviation of follow position. It is defined as the set of the positions of the last symbols of strings generated by the sub-expression rooted by n.

** Nullable:** It gives two results ‘true’ or ‘false’. It is true if the empty string is a member of strings generated by the sub-expression rooted by n and false otherwise.

The construction of firstpos is made according to the following table.

followpos is constructed only for the leaf nodes. It is constructed in the following way.

- If n is a dot (.) node containing the left child c
_{1}and the right child c_{2}, and i is a position in lastpos(c_{1}), then all positions in firstpos(c_{2}) belong to followpos(i). - If n is a star node, and i is a position in lastpos(n), then all positions in firstpos(n) belong to followpos(i).

The DFA is constructed by the following steps:

** Step I:** Make the RE R as augmented by placing an end marker #, and making it M#. Generate a parse tree from M#.

** Step II:** Calculate the firstpos and lastpos for all the internal and leaf nodes. Calculate the followpos for the leaf nodes.

** Step III:** Take the firstpos(root) as an unmarked state S of the constructing DFA.

** Step IV:** while (there exists an unmarked state S in the states of DFA)

do

Mark S and construct a transition from S using the following process for each input symbol ‘a’ as an alphabet of R

do

let S contain ‘a’ in position i_{1},i_{2} ...,i_{n}, then

if (S′ is not empty and have not appeared in the states of the DFA)

put S′ as an unmarked state into the states of the DFA.

#### Example 5.14

Convert the RE (a + b)*abb directly into an DFA.

** Solution:** The augmented RE is (a + b)*abb#

The parse tree constructed from the augmented RE in given in Fig. 5.14.

**Fig. 5.14**

The LHS of each node (including the leaf) is the firstpost of that node and the RHS of each node is the lastpos of that node. The firstpos and lastpos are constructed from the table given previously.

[Consider the dot (•) node connecting * node and ‘a’. The dot node is considered as c_{1} and the * node as c_{2}. The nullable(c_{1}) is true as c_{1} is a star node. So, the firstpos of the dot node is firstpos(c_{1}) ∪ firstpos(c_{2}).]

Consider the * node. The lastpos of the * node is {1,2}, and thus the followpos(1) and followpos(2) contain {1, 2}, as {1, 2} is the firstpos of the * node.

followpos(2) = {1, 2}

Consider the dot node connecting the * node (as c_{1}) and ‘a’ with label ‘3’(as c_{2}). The lastpos of c_{1} contains {1, 2} and the firstpos of c_{2} contains {3}. So, the followpos(1) and followpos(2) contain {3}

followpos(2) = {1, 2, 3}

Consider the dot node connecting the dot node (as c_{1}) and ‘b’ with label ‘4’(as c_{2}). The lastpos of c_{1} contains {3} and the firstpos of c_{2} contains {4}. So, the followpos(3) contains {4}

Consider the dot node connecting the dot node (as c_{1}) and ‘b’ with label ‘5’(as c_{2}). The lastpos of c_{1} contains {4} and the firstpos of c_{2} contains {5}. Thus, the followpos(4) contains {5}.

Consider the dot node connecting the dot node (as c_{1}) and ‘#’ with label ‘6’(as c_{2}). The lastpos of c_{1} contains {5} and the firstpos of c_{2} contains {6}. Thus, followpos(5) contains {6}.

followpos(6) = { }

a {1,2,3}. Mark it as S_{1}. The RE contains two symbols ‘a’ and ‘b’. ‘a’ exists in positions ‘1’ and ‘3’, and ‘b’ appears in positions ‘2’, ‘4’, and ‘5’.

It is other than S_{1}; therefore, mark it as S_{2}.

_{1}, b) = followpos(2) = {1, 2, 3} same as S

_{1}.

All the states are traversed, and no new state appears. Therefore, it is the final DFA. The transitional diagram of the DFA is given is Fig. 5.15.

**Fig. 5.15**

#### 5.6 NFA with ε Move and Conversion to DFA by ε-Closure Method

If any FA contains any *ε* (null) move or transaction, then that FA is called an NFA with ε moves. An example is shown in Fig. 5.16.

**Fig. 5.16**

The previous FA in Fig. 5.16 contains two null moves. So, the previous FA is an NFA with null move.

From the definition of an NFA, we know that for an NFA from a single state for a single input the machine can go to more than one state, i.e., Q × Σ → 2^{Q}, where 2^{Q} is the power set of Q.

From a state by getting ε input, the machine is confined into that state. An FA with null move must contain at least one ε move. For this type of FA for input *ε*, the machine can go to more than one state. (One is that same state and the another is the ε-transaction next state). So, an FA with ε move can be called as an NFA.

For the previous FA,

An NFA with ε move can be defined as

_{NFA }

_{null}= {Q, Σ, δ, q

_{0}, F},

where Σ: Set of input alphabets including *ε* and δ is a transitional function mapping Q × Σ → 2^{Q}, where 2^{Q} is the power set of Q.

** ∈-closure:** ε-Closure of a state is defined as the set of all states S, such that it can reach from that state to all the states in S with input ε (i.e., with no input).

#### Example 5.15

Consider the following NFA in Fig. 5.17 with ε moves.

**Fig. 5.17**

From state q_{0} by input ε (i.e., with no input), we can reach states q_{1} and q_{2}. With no input, we can reach the same state, i.e., q_{0}. So, we can write

*ε*-closure(q

_{0}) = {q

_{0}, q

_{1}, q

_{2}}

Similarly, ε-closure(q_{1}) = {q_{0}, q_{1}, q_{2}}.

ε-closure(q_{2}) = {q_{0}, q_{1}, q_{2}} (from q_{1} with no input we can reach q_{0}, from q_{2} with no input we can reach q_{1}, and so from q_{2} with no input we can reach q_{0}).

#### 5.6.1 Conversion of an NFA with ε move to a DFA

In the FA, it is discussed on how to remove ε moves from an FA. The method was the transitional method. In this section, we shall learn the removal of ε moves from an FA using the ε-closure method.

** Step I:** Find

*ε*-closure of all the states.

** Step II:** Construct a transitional function δ′ for the beginning state for all inputs. The function δ′ will be calculated similar to the following.

Let *ε*–closure(A) = {q_{1}, q_{2}, q_{3}} = Q, So, the previous equation will become

** Step III:** First, find the ε

**-**closure of the initial state. Rename the set of states as a new state. Then, find the function δ′ of that state for all inputs. If δ′ of that state for all inputs is constructed, then the state is known to be marked (fully traversed for all the inputs). If any new combinations of state appear in the process of marking other than the marked state, then rename that as a new state.

** Step IV:** Repeat step III for all new unmarked states. If no new state appears, then stop the construction.

** Step V:** In this process, the states which contain the final state as entry are the final states of the new NFA without ε move.

The following examples describe the process in detail.

#### Example 5.16

Convert the following NFA with ε-move in Fig. 5.18 to an equivalent DFA.

**Fig. 5.18**

*Solution:*

ε-closure(q_{0}) = {q_{0}, q_{1}, q_{2}}. Let us rename this state as A. Then, construct the δ′ function for the new unmarked state A for inputs 0, 1, and 2.

As it is a new combination of state other than A, rename it as B.

As it is a new combination of state other than A and B, rename it as C.

As δ′ is constructed for all inputs on A, A is marked.

Then, construct the δ′ function for the new unmarked state B for inputs 0, 1, and 2.

As δ′ is constructed for all inputs on B, B is marked.

C is unmarked still.

Then, construct the δ′ function for the new unmarked state C for inputs 0, 1, and 2.

A, B, and C all contain q_{2}, which is the final state; therefore, A, B, and C are final states.

The equivalent DFA is given in Fig. 5.19.

**Fig. 5.19**

#### Example 5.17

Convert the following NFA with ε-move in Fig. 5.20 to an equivalent DFA.

**Fig. 5.20**

*Solution:*

ε-closure(q_{0}) = {q_{0}, q_{1}, q_{3}}. Let us rename this state as A. Then, construct the δ′ function for the new unmarked state A for inputs ‘a’ and ‘b’.

As it is a new combination of state other than A, rename it as B.

As it is a new combination of state other than A and B, rename it as C.

As δ^{/} is constructed for all inputs on A, A is marked.

B is unmarked still.

Then, construct the δ′ function for the new unmarked state B for inputs ‘a’ and ‘b’.

As it is a new combination of state other than A, B, and C, rename it as D.

As δ′ is constructed for all inputs on B, B is marked.

C is unmarked still.

Then, construct the δ′ function for the new unmarked state C for inputs ‘a’ and ‘b’.

As δ′ is constructed for all inputs on C, C is marked.

D is unmarked still.

Then, construct the δ′ function for the new unmarked state D for inputs ‘a’ and ‘b’.

States A and C are final states as the states contain q_{3}.

The equivalent DFA is given in Fig. 5.21

**Fig. 5.21**

#### 5.7 Equivalence of Two Finite Automata

Two FA are said to be equivalent if they generate the same (equivalent) RE. If both the FA are minimized, they reveal the equivalence by being minimized to the same number of states and the same transitional functions. In this section, we shall discuss the process of proving the equivalence between two FA.

Let there be two FA, M and M′, where the number of input symbols are the same.

- Make a comparison table with n + 1 columns, where n is the number of input symbols.
- In the first column, there will be a pair of vertices (q, q′) where q ∈ M and q′ ∈ M′. The first pair of vertices will be the initial states of the two machines M and M′. The second column consists of (q
_{a}, q′_{a}), where q_{a}is reachable from the initial state of the machine M for the first input, and q′_{a}is reachable from the initial state of the machine M′ for the first input. The other n − 2 columns consist of a pair of vertices from M and M′ for n − 1 inputs, where n = 2, 3,………… n − 1. - If any new pair of states appear in any of the n − 1 next state columns, which were not taken in the first column, take that pair in the present state column and construct subsequent column elements like the first row.
- If a pair of states (q,q′) appear in any of the n columns for a pair of states in the present state column, where q is the final state of M and q′ is the non-final state of M′ or vice versa, terminate the construction and conclude that M and M′ are not equivalent.
- If no new pair of states appear, which were not taken in the first column, stop the construction and declare that M and M′ are equivalent.

The following examples describe this in detail.

#### Example 5.18

Find whether the two DFAs given in Fig. 5.22 are equivalent or not.

**Fig. 5.22**

** Solution:** In the previous two DFAs, the number of input is two 0 and 1. Therefore, the comparison table is of three columns. The beginning state pair is (A, G). From there, for input 0, we are getting (B, H) and for input 1 we are getting (C, H). Both of them are new combination of states. Among them, first take (B, H) in the present state column. The next state pairs for input 0 and 1 are constructed. By this process, the comparison continues.

After the fifth step, further construction is stopped as no new state combinations have appeared. In the whole table, no such type of combination of states appear where one state is the final state of one machine and the other is the non-final state of another machine.

Therefore, the two DFAs are equivalent.

Present State (q, q′) | Next State | |
---|---|---|

For I/P 0 (q _{0}, q′_{0}) | For I/P 1 (q _{1}, q′_{1}) | |

(A, G) | (B, H) | (C, H) |

(B, H) | (C, H) | (D, I) |

(C, H) | (C, H) | (E, I) |

(E, I) | (E, I) | (E, I) |

(D, I) | (D, I) | (D, I) |

#### Example 5.19

Find whether the two DFAs given in Fig. 5.23 are equivalent or not.

**Fig. 5.23**

** Solution:** In the previous two DFAs, the number of input is two 0 and 1. Therefore, the comparison table is of three columns. The beginning state pair is (A, E). From there, for input 0, we get (B, F) and for input 1 we get (D, H). Both of them are new combination of states. Among them, take

**(**B, F) in the present state column. The next state pairs for input 0 is (C, G) and for input 1 is (C, H). (C, H) is a combination of states where C is the final state of M and H is the non-final state of M′. Further construction stops here declaring that the two DFAs are not equivalent.

Present State (q, q′) | Next State | |
---|---|---|

For I/P 0 (q _{0}, q′_{0}) | For I/P 1 (q _{1}, q′_{1}) | |

(A, E) | (B, F) | (D, H) |

(B, F) | (C, G) | (C, H) |

#### 5.8 Equivalence of Two Regular Expressions

For every RE, there is an accepting FA. If the FA constructed from both of the REs are the same, then we can say that two REs are equivalent.

#### Example 5.20

Prove that the following REs are equivalent.

_{1}= (a + b)* L

_{2}= a*(b*a)*

** Solution:** The FA for L

_{1}is constructed in Fig. 5.24.

**Fig. 5.24**

The FA for L_{2} is constructed in Fig. 5.25.

**Fig. 5.25**

We are seeing that the FA generated by the two REs L_{1} and L_{2} are the same. So, we can decide that the two FA are equivalent.

#### Example 5.21

Prove that the following REs are equivalent.

_{1}= 1*(011)*(1*(011)*)* L

_{2}= (1 + 011)*

** Solution:** The FA for L

_{1}is constructed in Fig. 5.26.

**Fig. 5.26**

The FA for L2 is constructed in Fig. 5.27.

**Fig. 5.27**

The FA generated by the two REs L_{1} and L_{2} are the same and, therefore, they are equivalent.

#### 5.9 Construction of Regular Grammar from an RE

We have learnt how to construct a grammar from a language. But in the case of an RE, it is a little difficult to find the exact grammar of it using the conventional methods. The following section describes the process of constructing regular grammar from an RE.

** Step I:** Construct the equivalent FA for the given RE (eliminate all null moves).

** Step II:** The number of non-terminals of the grammar will be equal to the number of states of the FA.

** Step III:** For all transitional functions in the form δ(Q

_{1}, a) → Q

_{2}, [Q

_{1}, Q

_{2}∈ Q and a ∈ Σ], the production rule is in the form A → aA

_{1}. If Q

_{2}is a final state, then for the transitional function δ(Q

_{1}, a) → Q

_{2}the production rules are A → aA

_{1}and A → a. The start symbol is the symbol representing the initial state of the FA.

The following examples describe the discussed method in detail.

#### Example 5.22

Construct a regular grammar for the following REs:

- a*(a + b)b*
- ab(a + b)*
- 10 + (0 + 11)0*1

*Solution:*

- The FA for the string a*(a + b)b* is constructed in Fig. 5.28.
**Fig. 5.28**The number of states of the FA is 2, which means that there are two non-terminals in the grammar for the RE. Let us take them as A (for q

_{0}) and B (for q_{1}).For the transitional function δ(q

_{0}, a) → q_{0}, the production rule will be A → aA. For the transitional function δ(q_{0}, a) → q_{1}, the production rule will be A → aB. As q_{1}is a final state, there will be another production rule A → a.The start symbol will be A as q

_{0}is the beginning state.The grammar G for the RE a*(a + b)b* is {V

_{N}, Σ, P, S} whereVN = {A, B}

Σ = {a, b}P : {A → aA/ bB/a/b, B → bB/b}

S :{A}.

**Fig. 5.29** - The DFA for the string ab(a + b)* is given in Fig. 5.29.
There are three states in the DFA. The number of non-terminals for the grammar of the RE will be 3. Let us take them as A (for q

_{0}), B (for q_{1}), and C (for q_{2}).For the transitional function δ(q

_{0}, a) → q_{1}, the production rule will be A → aBThe start symbol will be A as q

_{0}is the beginning state.The grammar G for the RE ab(a + b)* is {V

_{N}, Σ, P, S} whereVN = {A, B, C}

Σ = {a, b}P : {A → aB, B → bC/b, C → aC/bC/a/b}

S: {A}.

- The NFA without null move for the RE 10 + (0 + 11)0*1 is shown in Fig. 5.30.
The equivalent DFA for the previous NFA is given in Fig. 5.31.

There are four states of the DFA for the RE 10 + (0 + 11)0*1. So, for the grammar of the RE, there will be four non-terminals. Let us take them as A (for Q

_{0}), B (for Q_{1}), C (for Q_{2}), and D (for Q_{3}).**Fig. 5.30****Fig. 5.31**Now, we have to construct the production rules of the grammar.

For the transitional function δ(Q

_{0}, 0) → Q_{1}, the production rule will be A → 0B.Similar to δ(Q

_{0}, 1) → Q_{2}, the production rule will be A → 1C.δ(Q

_{1}, 0) → Q_{3}, the production rule will be B → 0D and B → 0 (as Q_{3}is the final state)δ(Q

_{1}, 1) → Q_{2}, the production rule will be B → 1Cδ(Q

_{2}, 0) → Q_{2}, the production rule will be C → 0Cδ(Q

_{2}, 1) → Q_{3}, the production rule will be C → 1D and C → 1 (As Q_{3}is the final state).The start symbol will be A as Q

_{0}is the beginning state.The grammar G for the RE 10 + (0 + 11)0*1 is {V

_{N}, Σ, P, S} whereVN = {A, B, C, D}

Σ = {0, 1}P : {A → 0B/1C, B → 0D/1C/0, C → 0C/1D/1}

S : {A}.

(N.B: From NFA without ∈-move, the regular grammar can be constructed. We can do this by the construction of the equivalent DFA.)

#### 5.10 Constructing FA from Regular Grammar

FA can directly be constructed from regular grammar. This can be considered as a reverse process of constructing the FA from an RE. The following section describes the process of constructing the FA from an RE.

*Step I:*

- If the grammar does not produce any null string, then the number of states of the FA is equal to the number of non-terminals of the regular grammar +1. Each state of the FA represents each non-terminal and the extra state is the final state of the FA. If it produces a null string, then the number of states is the same as the number of non-terminals.
- The initial state of the FA is the start symbol of the regular grammar.
- If the language generated by the regular grammar contains a null string, then the initial state is also the final state of the constructing FA.

*Step II:*

- For a production in the form A → aB, make a δ function δ(A, a) → B.
There is an arc from state A to state B with label a.

- For a production in the form A → a, make a δ function δ(A, a) → final state.
- For a production A → ∈, make a δ function δ(A, ∈) → A, and A is the final state.

Consider the following examples.

#### Example 5.23

Convert the following regular grammar into FA:

** Solution:** In the grammar, there are three non-terminals, namely S, A, and B. Therefore, the number of states of the FA is four. Let us name the final state as C.

- For the production S → aA/bB, the transitional diagram is given in Fig. 5.32(a)
- For the production S → a/b, the transitional diagram including the previous one becomes as Fig. 5.32(b).
**Fig. 5.32(a)****Fig. 5.32(b)** - For the production A → aS/bB/b, the transitional diagram including the previous one looks like Fig. 5.32(c).
**Fig. 5.32(c)**For the production B → aA/bS, the transitional diagram including the previous one looks like Fig. 5.32(d)

**Fig. 5.32(d)**

This is the FA for the given regular grammar.

#### Example 5.24

Convert the following regular grammar into FA:

** Solution:** In the grammar, there are three non-terminals, namely S, A, and B. So, the number of states of the FA will be four. Let us name the final state as C.

- For the production S → aA/bS, the transitional diagram becomes
- For the production A → bB/a, the transitional diagram including the previous one is
- For the production B → aS/b, the transitional diagram including the previous one looks like

This is the FA for the given regular grammar. The construction process is described in Figs. 5.33(a) to 5.33(c).

**Fig. 5.33(a)**

**Fig. 5.33(b)**

**Fig. 5.33(c)**

#### 5.11 Pumping Lemma for Regular Expression

There is a necessary condition for an input string to belong to a regular set. This necessary condition is the pumping lemma. Pumping means generating. This lemma is called a pumping lemma because it gives a method of generating many input strings from a given string.

As the pumping lemma is a necessary condition for a string to belong to a regular set, it is used to prove that certain sets are not regular. If any set fulfills all the conditions of Pumping Lemma it can not be said confirm that the set is regular.

But the reverse is true, i.e., if any set breaks the conditions of the pumping lemma, it can be said that the set is not regular. So, the pumping lemma is used to prove that certain sets are not regular.

*Theorem 5.2:***Statement of the pumping lemma:** Let M = {Q, Σ, δ, q_{0}, F} be an FA with n number of states. Let L be a regular set accepted by M. Let w be a string that belongs to the set L and | w | ≥ m. If m ≥ n, i.e., the length of the string is greater than or equal to the number of states, then there exists x, y, z such that w = xyz, where | xy | ≤ n, | y | >0 and xy^{i}z ∈ L for each i ≥ 0.

(It needs some clarification, after that we shall go to the proof of it.

Let us take the following FA in Fig. 5.34. Here, from q_{0}, by getting ‘a’ it goes to q_{1} and from q_{1} by getting ‘b’ it goes to q_{2}, which is the final state. The FA consists of three states, q_{0}, q_{1}, and q_{2}. The string that is accepted by the FA is ba. The length of the string is 2 which is less than the number of states of the FA.

**Fig. 5.34**

Here, from a state by getting a single input, it goes to a single distinct state. But, if the length of the string is equal or greater than the number of states of the FA, then from a state by getting a single input it is not possible to get all single distinct states. There must be a repetition of at least a single state. This can be described by the following diagram in Fig. 5.35.

**Fig. 5.35**

The RE accepted by the automata is ba*b. The expression can be divided into three parts: x, y, z where y is the looping portion, x is the portion before looping, and z is the portion after the looping portion.)

** Proof:** Let w be a string of length m where m is greater than n, the number of states of the FA accepting the string w as given in Fig. 5.36.

**Fig. 5.36**

_{1}a

_{2}a

_{3}……..am | w | = m ≥ n

Starting from the beginning state q_{0}, by getting the string w as input, the machine reaches the final state.

In general,

_{0}, a

_{1}a2

_{a3}……..a

_{i}) = q

_{i}for i = 1,2,3, ……m.

The set of the states followed by the string w in the transitional path from the beginning state to the final state are Q_{n} = {q_{0}, q_{1}, q_{2}, . . . . . q_{m}}. The number of states in the set is m.

But the number of states of the FA accepting the string w is n. As m ≥ n, at least two states in the set Q_{n} must coincide.

Take two integers j and k where 0 ≤ j < k ≤ n. Among the various pairs of repeated states, take a pair q_{j}, q_{k}. The string w can be decomposed into three substrings: a_{1}a_{2 }. . . . . a_{j}, a_{j + 1 }. . . . . a_{k}, and a_{k + 1 }. . . . . a_{m.} Let us denote them as x, y, and z, respectively. As k ≤ n, the length of the string a_{1}…a_{k} is ≤ n, i.e., | xy | ≤ n and w = xyz.

The automaton starts from the initial state q_{0}. On applying the string x, it reaches the state q_{j}. On applying the string y, it comes back to q_{j} as q_{j} = q_{k} as denoted in Fig. 5.37. So, on applying the string y, i number of times (where i ≥ 0) on q_{j}, it will be in the same state q_{j}. On applying z on q_{j}, the automaton will reach the final state. Hence, the string xy^{i}z with i ≥ 0 belongs to the language set L., i.e., xy^{i}z ∈ L.

**Fig. 5.37**

** Application of the pumping lemma:** The pumping lemma is used to prove that certain sets are not regular. If an expression satisfies the conditions for the pumping lemma to be good, then it cannot be said that the expression is regular. But the opposite is true. If any expression breaks any condition of the pumping lemma, it can be declared that the expression is not good.

This needs certain steps.

** Step I:** Assume that the set L is regular. Let n be the number of states of the FA accepting L.

** Step II:** Choose a string w (w ∈ L) such that | w | ≥ n. By using the pumping lemma, we can write w = xyz, with | xy | ≤ n and | y | > 0.

** Step III:** Find a suitable integer i such that xy

^{i}z ∉ L. This will contradict our assumption. From here, L will be declared as not regular.

The following examples help to clear this.

#### Example 5.25

Show that L = {a^{i2} | i ≥ 1} is not regular.

*Solution:*

** Step I:** Assume the set L is regular. Let n be the number of states of the FA accepting the set L.

** Step II:** Let w = a

^{n2}. | w | = n

^{2}which is greater than n, the number of states of the FA accepting L. By using the pumping lemma, we can write w = xyz with | xy | ≤ n and | y | > 0

** Step III:** Take i = 2. So, the string will become xy

^{2}z.

From step II, we know | w | = | xyz | = | x | + | y | + | z | = n^{2}.

So, | xy^{2}z | > n^{2}.

Again, | xy^{2}z | = | x | + 2 | y | + | z | = | x | + | y | + | z | + | y | = n^{2} + | y | .

As | xy | ≤ n, | y | ≤ n.

Therefore, | xy^{2}z | ≤ n^{2} + n.

From the previous derivations, we can write

^{2}< | xy

^{2}z | ≤ n

^{2}+ n < n

^{2}+ n + n + 1

n

^{2}< | xy

^{2}z | < (n + 1)

^{2}.

Hence, | xy^{2}z | lies between n^{2} and (n + 1)^{2}. They are the square of two consecutive positive integers. In between the square of two consecutive positive integers, no square of positive integer belongs. But ai^{2}, where i ≥ 1 is a perfect square of an integer. So, the string derived from it, i.e., | xy^{2}z | is also a square of an integer, which lies between the square of two consecutive positive integers. This is not possible.

So, xy^{2}z ∉ L. This is a contradiction.

So, L = L = {*a*^{i2} | i ≥ 1} is not regular.

#### Example 5.26

Show that L = {a^{p} | p is prime} is not regular.

*Solution:*

** Step I:** Assume that the set L is regular. Let n be the number of states in the FA accepting L.

** Step II:** Let p be a prime number which is greater than n. Let the string w = a

^{p}, w ∈ L. By using the pumping lemma, we can write w = xyz with | xy | ≤ n and | y | > 0. As the string w consists of only ‘a’, x, y, and z are also a string of ‘a’s. Let us assume that y = a

^{m}for some m with 1≤ m ≤ n.

** Step III:** Let us take i = p + 1. | xy

^{i}z | will be | xyz | + | y

^{i-1}|

p(1 + m) is not a prime number as it has factors p and (1 + m) including 1 and p(1 + m). So, xy^{i}z ∉ L. This is a contradiction.

Therefore, L = {a^{p} | p is prime} is not regular.

#### Example 5.27

Show that L = {a^{i3} | i ≥ 1} is not regular.

*Solution:*

** Step I:** Assume that the set L is regular. Let n be the number of states of the FA accepting the set L.

** Step II:** Let L = a

^{n3}. | w | + n

^{3}which is greater than n, the number of states of the FA accepting L. By using the pumping lemma, we can write w = xyz with | xy | ≤ n and | y | > 0.

** Step III:** Take i = 3. So, the string will become xy

^{3}z.

As | xy | ≤ n so, | y | ≤ n.

Therefore, | xy^{3}z | = n^{3} + 2 | y | ≤ n^{3} + n < n^{3} + 3n^{2} + 3n + 1 = (n + 1)^{3}.

As | y | > 0, | xy^{3}z | = | xyz | + 2 | y | > n^{3}.

We can write

^{3}< | xy

^{3}z | < (n + 1)

^{3}.

xy^{3}z is a perfect cube which lies between n^{3} and (n + 1)^{3}, i.e., two consecutive perfect cube numbers. In between the cube of two consecutive positive integers, no cube of positive integer belongs. But a^{i3}, where i ≥ 1 is a perfect cube of an integer. So, the string derived from it, i.e., | xy^{3}z | , is also a cube of an integer, which lies between a cube of two consecutive positive integers. This is not possible. So, xy^{3}z ∉ L. This is a contradiction. So, L = {a^{i3} | i ≥ 1} is not regular.

#### Example 5.28

Show that L = {a^{n}b^{n} where n ≥ 1} is not regular.

*Solution:*

** Step I:** Let us suppose that the set L is regular. Let n be the number of states of the FA accepting L.

** Step II:** Let w = a

^{n}b

^{n}, | w | + 2n >n. By the pumping lemma, we can write w = xyz with | xy | ≤ n and | y | > 0.

** Step III:** We want to find a suitable i so that xy

^{i}z ∉ L.

The string y can be of any of the following:

- y is a string of only ‘a’, i.e., y = a
^{k}for some k ≥ 1 - y is a string of only ‘b’, i.e., y = b
^{k}for some k ≥ 1 - y is a string of both ‘a’ and ‘b’, i.e., y = a
^{k}b^{l}for some k, 1 ≥ 1

For case (i), take i = 0. As xyz = a^{n}b^{n}, xy^{0}z = xz will be a^{n-k}b^{n}.

^{0}z ∉ L.

For case (ii), take i = 0. As xyz = a^{n}b^{n}, xy^{0}z = xz will be a^{n}b^{n-k}.

^{0}z ∉ L.

For Case (iii), take i = 2. As xyz = a^{n}b^{n}, xy^{2}z = xyyz.

^{n}b

^{n}= a

^{n-k}a

^{k}b

^{l}b

^{n-l}. So, xyyz will be

a^{n-k}a^{k} b^{l} a^{k} b^{l} b^{n-l } = a^{n}b^{l}a^{k}b^{n}, which is not in the form a^{n}b^{n}. So, xy^{2}z ∉ L.

For all the three cases, we are getting contradiction. So, L is not regular.

#### Example 5.29

Show that L = palindrome over {a, b} is not regular.

*Solution:*

** Step I:** Suppose that the set L is regular. Let n be the number of states of the FA accepting L.

** Step II:** Let w = a

^{n}ba

^{n}, | w | = (2n + 1) > n. By the pumping lemma, we can write w = xyz with | xy | ≤ n and | y | > 0.

** Step III:** We want to find a suitable i so that xy

^{i}z ∉ L. The string y may consist of only ‘a’, for example, a

^{j}where j > 0.

Let us take i = 0. xyz = a^{n}ba^{n} = a^{n-j}a^{j} ba^{n}. So, xy^{0}z = a^{n-j}ba^{n}.

As j > 0, n – j ≠ n. So, a^{n-j}ba^{n} is not a palindrome.

So, xy^{0}z ∉ L. Hence, it is proved that L is not regular.

#### Example 5.30

Show that L = {ww, where w ∈ (a, b)*} is not regular.

*Solution:*

** Step I:** Let us consider that the set L is regular. Let n be the number of states of the FA accepting L.

** Step II:** Let ww = a

^{n}ba

^{n}b ∈ L, and so | ww | = 2(n + 1) > n. By applying the pumping lemma, we can write w = xyz with | xy | ≤ n and | y | > 0.

** Step III:** We want to find a suitable i so that xy

^{i}z ∉ L. The string consists of ‘a’ and ‘b’. So, y can be any of these.

- y has no ‘b’, i.e., y = a
^{k} - y has only one ‘b’ (because the string is a
^{n}ba^{n}b)

Let us take i = 0 in case I. If i = 0, then xy^{0}z = xz and it is in the form a^{n-k}ba^{n}b or a^{n}ba^{n-k}b.

This is not in the form ww, i.e., xz ∉ L. Hence, L is not regular.

In case II, if we take i = 0, then xy^{0}z = xz which contains only one ‘b’, which is not in the form ww, i.e., xz ∉ L. Hence, L is not regular.

#### Example 5.31

Show that L = {a^{n}ba^{n}, where n ≥ 1} is not regular.

*Solution:*

** Step I:** Assume that L is regular. Let n be the number of states of the FA accepting L.

** Step II:** Let w = a

^{n}ba

^{n}. So, | w | = 2n + 1 > n. According to the pumping lemma, we can write w = xyz, with w = xyz with | xy | ≤ n and | y | > 0.

** Step III:** We want to find a suitable i so that xy

^{i}z ∉ L. The string consists of ‘a’ and ‘b’. So, y can be any of these.

- y has no ‘b’ i.e. y = a
^{k} - y has only one ‘b’ (because the string is a
^{n}ba^{n})

For case I, take i = 2.

The string xyz = a^{n–k }a^{k}ba^{n} or a^{n}ba^{k}a^{n–k}.

So, xy^{2}z = a^{n–k}a^{2k}ba^{n} = a^{n + k}ba^{n} or a^{n}ba^{2k}a^{n–k} = a^{n}ba^{n+k}.

As k ≠ 0, n+k ≠ n. Therefore, xy^{2}z ∉ L.

This is a contradiction, and so L is not regular.

For Case II, let us take i = k. So, xy^{k}z = a^{n}b^{k}a^{n}. This is not in the form a^{n}ba^{n}. Therefore, xy^{k}z ∉ L.

This is a contradiction, and so L is not regular.

#### Example 5.32

Show that L = {a^{n}b^{n}ab^{n + 1}} is not regular.

*Solution:*

** Step I:** Assume that L is regular. Let n be the number of states of the FA accepting L.

** Step II:** Let w = a

^{n}b

^{n}ab

^{n + 1}, and so | w | = (3n + 2) > n. By using the pumping lemma, we can write w = xyz, with w = xyz with | xy | ≤ n and | y | > 0.

** Step III:** We want to find a suitable i so that xy

^{i}z ∉ L. The string consists of ‘a’ and ‘b’. So, y can be of any of these.

- y contains only ‘a’, and let y = a
^{k} - y contains only ‘b’, and let y = b
^{l} - y contains both ‘a’ and ‘b’, and let y = b
^{k}a

For case I, take i = 0.

For case II, take i = 0

For case III, take i = 2

In all the three cases, there are contradictions and, hence, the language is not regular.

#### Example 5.33

Show that the language containing the set of all balanced parenthesis is not regular.

*Solution:*

** Step I:** Assume that L is regular. Let n be the number of states of the FA accepting L.

** Step II:** Let w = (((….()….))) = (

^{n})

^{n}. | w | = 2n > n. By using the pumping lemma, we can write w = xyz, with w = xyz with | xy | ≤ n and | y | > 0.

** Step III:** We want to find a suitable i so that xy

^{i}z ∉ L. The string consists of ‘(‘ and ‘)’. So, y can be of any of these

- y consists of only ‘(‘, and let y = (
^{k} - y consists of only ‘)’, and let y = )
^{k} - y consists of both ‘(‘ and ‘)’, and let y = (
^{k})^{k}

For case I, take i = 0.

For case II, take i = 0.

For case III, take i = 2.

In all the three cases, there are contradictions and, hence, the language is not regular.

#### Example 5.34

Show that L = {0^{n}1^{2n}, where n ≥ 1} is not regular.

*Solution:*

** Step I:** Assume that L is regular. Let n be the number of states of the FA accepting L.

** Step II:** Let w = 0

^{n}1

^{2n}, where n ≥ 1. | w | = 3n> n. By using the pumping lemma, we can write

** Step III:** We want to find a suitable i so that xy

^{i}z ∉ L. The string consists of ‘0’ and ‘1’. y can be any of the following forms

- y consists of only ‘0’, and let y = 0
^{k} - y consists of only ‘1’, and let y = 1
^{k} - y consists of both ‘0’ and ‘1’, and let y = 0
^{k}1^{k}

For case I, take i = 0.

^{n}1

^{2n}= 0

^{n−k}0

^{k}1

^{2n}

xy

^{0}z = 0

^{n−k}1

^{2}

^{n}, as k ≠ 0, (n−k) ≠ n.

For case II, take i = 0.

^{n}1

^{2n}= 0

^{n}1

^{k}1

^{2n−k}

xy

^{0}z = 0

^{n}1

^{2n−k}, as k ≠ 0, (2n−k) ≠ n.

For case III, take i = 2.

^{n}1

^{2n}= 0

^{n}–

^{k}0

^{k}1

^{k}1

^{2n−k}

xy

^{2}z = 0

^{n}1

^{k}0

^{k}1

^{2n}, which is not in the form 0

^{n}1

^{2n}

For all the three cases, we are getting contradictions and, therefore, L is not regular.

#### 5.12 Closure Properties of Regular Set

A set is closed (under an operation) if and only if the operation on two elements of the set produces another element of the set. If an element outside the set is produced, then the operation is not closed.

Closure is a property which describes when we combine any two elements of the set; the result is also included in the set.

If we multiply two *integer numbers*, we will get another integer number. Since this process is always true, it is said that the integer numbers are ‘closed under the operation of multiplication’. There is simply no way to escape the set of integer numbers when multiplying.

Let S = {1,2,3,4,5,6,7,8,9,10….} is a set of integer numbers.

1 × 2 = 2

2 × 3 = 6

5 × 2 = 10

All are included in the set of integer numbers.

We can conclude that integer numbers are closed under the operation of multiplication.

** Theorem 5.3:** Two REs L

_{1}and L

_{2}over Σ are closed under union operation.

** Proof:** We have to prove that if L

_{1}and L

_{2}are regular over Σ, then their union, i.e., L

_{1}∪ L

_{2}will be also regular.

As L_{1} and L_{2} are regular over Σ, there must exist FA M_{1} = (Q_{1}, Σ, δ_{1}, q_{01}, F_{1}) and M_{2} = (Q_{2}, Σ, δ_{2}, q_{02}, F_{2}) such that L_{1} ε M_{1} and L_{2} ε M_{2}

Assume that there is no common state between Q_{1} and Q_{2}, i.e., Q_{1} ∩ Q_{2} = Ø.

Define another FA, M_{3} = (Q, Σ, δ, q_{0}, F) where

- Q = Q
_{1}∪ Q_{2}∪{q_{0}}, where q_{0}is a new state ∉ Q_{1}∪Q_{2} - F = F
_{1}∪ F_{2} - Transitional function δ is defi ned as δ (q
_{0}, ε) → {q_{01}, q_{02}}and

It is clear from the previous discussion that from q_{0} we can reach either the initial state q_{1} of M_{1} or the initial state q_{2} of M_{2}.

Transitions for the new FA, M, are similar to the transitions of M_{1} and M_{2}.

As F = F_{1} ∪ F_{2}, any string accepted by M_{1} or M_{2} will also be accepted by M.

Therefore, L_{1} ∪ L_{2} is also regular.

*Example:*

Let

_{1}= a*(a + b)b*

L

_{2}= ab(a + b)*

The FA M_{1} accepting L_{1} is as shown in Fig. 5.38(a).

The FA M_{2} accepting L_{2} is as shown in Fig. 5.38(b).

The machine M produced by combining M_{1} and M_{2} is as shown in Fig. 5.38(c).

**Figs. 5.38 (a)–(c)**

It accepts L_{1} ∪ L_{2}.

** Theorem 5.4:** The complement of an RE is also regular.

If L is regular, we have to prove that L^{T} is also regular.

As L is regular, there must be an FA, M = (Q, Σ, δ, q_{0}, F), accepting L. M is an FA, and so M has a transitional system.

Let us construct another transitional system M′ with the state diagram of M but reversing the direction of the directed edges. M′ can be designed as follows:

- The set of states of M′ is the same as M
- The set of input symbols of M′ is the same as M
- The initial state of M′ is the same as the final state of M (M′ is the reverse direction of M)
- The final state of M′ is the same as the initial state of M (M′ is the reverse direction of M)

Let a string w belong to L, i.e., w ∈ M. So, there is a path from q_{0} to F with path value w. By reversing the edges, we get a path from F to q_{0} (beginning and final state of M′) in M′. The path value is the reverse of w, i.e., w^{T}. So, w^{T} ∈ M′.

So, the reverse of the string w is regular.

*Example:*

Let L = ab(a + b)*.

The FA M accepting L is as shown in Fig. 5.39(a).

The reverse of the FA M′ accepting L^{c} is shown in Fig. 5.39(b).

**Fig. 5.39 (a)–(b)**

M′ accepts (a + b)*ba which is reverse of L.

** Theorem 5.5:** If L is regular and L is a subset of Σ*, prove that Σ* – L is also a regular set.

As L is regular, there must be an FA, M = (Q, Σ, δ, q_{0}, F), accepting L. Let us construct another DFA M′ = (Q, Σ, δ, q_{0}, F′) where F′ = Q–F. So, the two DFA differ only in their final states. A final state of M is a non-final state of M′ and vice versa.

Let us take a string w which is accepted by M′. So, δ(q_{0}, w) ∈ F′, i.e., δ(q_{0}, w) ∈ (Q – F).

The string w cannot be accepted by M, because δ(q_{0}, w) ∉ F [as F does not belong to (Q – F)].

So, w∉L, i.e., Σ* ** –** L ≠ L. But Σ*** –** L is accepted by M′ which is an FA.

Therefore, Σ*** –** L is a regular set.

** Theorem 5.6:** REs are closed under intersection operation.

** Proof:** From D’Morgan’s theorem, we know

We know that if L_{1} and L_{2} are regular, then are also regular.

As are regular, is also regular (RE is closed under union operation).

As is regular, so complement ()C is also regular.

So, L_{1}∩L_{2} is also regular, i.e., the regular sets are closed under intersection.

** Theorem 5.7:** Two DFA are closed under cross product.

** Proof:** Let D

_{1}= {Q

_{1}, Σ, δ

_{1}, q

_{01}, F

_{1}} and D

_{2}= {Q

_{2}, Σ, δ

_{2}, q

_{02}, F

_{2}} are two DFA accepting two RE L

_{1}and L

_{2}respectively. Let us construct a new FA, D as follows.

_{0}, F}

where

Clearly D is a DFA. Thus DFA are closed under cross product.

#### 5.13 Decision Problems of Regular Expression

Decision problems are the problems which can be answered in ‘yes’ or ‘no’. FA are one type of finite state machines which contain a finite number of memory elements. Thus, it can memorize only finite amount of information. FA can answer to those decision problems which require only a finite amount of memory.

The decision problems related to RE are

- Whether a string x belongs to a regular expression R? (x ∈ R?)
- Whether the language set of an FA M is empty? [L(M) = Ø ?]
- Whether the language set of an FA is finite? [L(M) is finite?]
- Whether there exists any string accepted by two FA, M
_{1}and M_{2}? - Whether the language set of an FA M
_{1}is a subset of the language set of another FA M_{2}? [L(M_{1}) ⊆ L(M_{2})?] - Whether two FA M
_{1}and M_{2}accept the same language? [L(M_{1}) = L(M_{2})] - Whether two REs R
_{1}and R_{2}are from the same language set? - Whether an FA is the minimum state FA for a language L?

*Proof*

- This problem is known as the membership problem. R can be converted to the equivalent FA M (see Section 5.5). x is applied on M. If M reaches its final state upon finishing x; x ∈R else x ∉ R.
- An FA does not accept any string if it does not have any final state or if the final state is an inaccessible state. Let us calculate the set of state S
_{k}reached from the beginning state q_{0}upon applying a string of length k.If k = 0, it reaches the beginning state q

_{0}. It reaches S_{K}if it was in state S_{k−1}and ‘a’ as input is applied on S_{K−1}.Compare the set S

_{K.}- for string length k ≥ 0 whether a final state appears or
- S
_{K}= S_{K−1}(loop) for k > 0.

For the case (a), L(M) ≠ Ø, and for case (b) L(M) = Ø .

- In the pumping lemma (Section 4.11), it is discussed that for accepting a string of length ≥ n (the number of states of an FA), it has to traverse at least one loop. The language accepted by an FA is finite if it has length < n. Formulate an algorithm for testing as follows.
Give an input to M, the strings of length n or > n in increasing order. If for a string ‘s’ of length in between n and ≤ 2n, it reaches M then L(M) is infinite; else L(M) is finite.

Infinite means there exists x, y, z such that s = xyz, where | xy | ≤ n, | y | >0 and xy

^{i}z ∈ L for each i ≥ 0 (pumping lemma). Here, y^{i}is the looping portion, which can generate an infinite number of strings. - For this decidable problem, construct an FA M accepting L(M
_{1}) ∩ L(M_{2}). Then apply the decision problem 2 on M. - For this decidable problem, construct an FA M accepting L(M
_{1}) − L(M_{2}). Then, apply decision problem 2 on M. It is true if L(M_{1}) − L(M_{2}) = Ø. (If L(M_{1}) is a subset of L(M_{2}), L(M_{1}) − L(M_{2}) will produce null.) - Two sets A and B are the same if A ⊆ B and B ⊆ A. Construct an FA, M, accepting L(M
_{1}) − L(M_{2}) and M′ accepting L(M_{2}) − L(M_{1}) (reducing it to problem v). Problem vi is decidable if L(M_{1}) − L(M_{2}) = Ø and L(M_{2}) − L(M_{1}) = Ø. - There exists an algorithm to convert an RE to FA (see Section 5.5). Reduce this problem to problem (vi).
- Minimize an FA, M, using 3.13 and generate M′. If the number of states of M and M′ are the same, then it is minimized; else it is not. Hence, decidable.

#### 5.14 ‘Grep’ and Regular Expression

REs have been an integral part of Unix since the beginning. Ken Thompson used the RE in the early computer text editor ‘QED’ and the Unix editor ‘ed’ for searching text. ‘grep’ is a find string command in Unix. It is an acronym for ‘globally search a regular expression and print it’. The command searches through files and folders (directories in Unix) and checks which lines in those files match a given RE. ‘grep’ gives output the filenames and the line numbers or the actual lines that matched the RE.

Let us start with some simple grep commands.

This command searches the string ‘RE’ in the file ‘chapter5’.

This command searches the strings with case insensitive.

This command displays all strings of length 4 starting with ‘h’ and ending with ‘a’.

If we enter into the internal operation of ‘grep’, we will see that we are searching for a string that belongs to an RE. Let us take the last grep which searches for four length strings starting with ‘h’ and ending with ‘a’. For this case, the RE is h(ch)^{+}(ch)^{+}a, where ‘ch’ is any symbol that belongs to a file (Generally symbols available in the keyboard).

#### 5.15 Applications of Regular Expression

RE is mainly used in lexical analysis of compiler design. In the programming language, we need to declare a variable or identifier. That identifier must start with a letter followed by any number of letters or digits. The structure of the identifier is represented by RE. The definition of an identifier in a programming language is

The definition of an unsigned number in a programming language is

The other application of RE is pattern searching from a given text.

#### What We Have Learned So Far

- An RE can be defined as a language or string accepted by an FA.
- Any terminal symbols, null string (∧), or null set (ɸ) are RE.
- Union, concatenation, iteration of two REs, or any combination of them are also RE.
- The Arden’s theorem states that if P and Q are two REs over Σ, and if P does not contain Λ, then the equation R = Q + RP has a unique (one and only one) solution R = QP*.
- The Arden’s theorem is used to construct an RE from a given FA by the algebraic method.
- If any FA contains any ∈ (null) move or transaction, then that FA is called NFA with ∈-moves.
- The ∈-closure of a state is defined as the set of all states S, such that it can reach from that state to all the states in S with input ∈ (i.e., with no input).
- The pumping lemma for RE is used to prove that certain sets are not regular.
- A set is closed (under an operation) if and only if the operation on two elements of the set produces another element of the set.
- Closure is a property which describes when we combine any two elements of the set, the result is also included in the set.
- REs are closed under union, complementation, and intersection operation.

- Describe the following REs in English language.
- a(a+b)*abb
- (a+b)*aba(a+b)*
- (0+1)*1(0+1)*0(0+1)*

*Solution:*- The language starts with ‘a’ and ends with ‘abb’. In the middle of ‘a’ and ‘b’, there is any combination of ‘a’ and ‘b’.
Hence, the RE described in English language is

{Set of any combination of ‘a’ and ‘b’ beginning with ‘a’ and ending with ‘abb’}.

- The expression is divided into three parts: (a+b)*, aba, and (a+b)*. In each element of the language set, there is aba as a substring. In English language, the RE is described as {Set of any combination of ‘a’ and ‘b’ containing ‘aba’ as substring}.
- The expression is divided into five parts: (0+1)*, 1, (0+1)*, 0 and (0+1)*. In each element of the language set, there is 1 and 0, where 1 appears first. In English language, the RE is described as
{Set of any combination of ‘0’ and ‘1’ containing at least one 1 and one 0 where 1 appears first}

- Find the RE for the following:
- The set of language of all strings of 0 and 1 containing exactly two 0’s
[UPTU 2004]

- The set of languages of any combination of ‘a’ and ‘b’ beginning with ‘a’.
- The set of all strings of 0 and 1 that do not end with 11.
- The set of languages of any combination of ‘0’ and ‘1’ containing at least one double symbol.
- The set of all strings over {a, b} in which the number of occurrences of ‘a’ is divisible by 3.
[UPTU 2004]

- The set of all strings where the 10th symbol from the right end is a 1.
[JNTU 2007]

- The set of languages of any combination of ‘0’ and ‘1’ containing no double symbol.

*Solution:*- The language contains exactly two 0’s. But the language consists of 0 and 1. 1 may appear at first or in the middle or at the last. Thus, the language is L = 1*01*01*.
- The set of any combination of ‘a’ and ‘b’ is denoted by (a+b)
^{*}. The RE isL = a(a+b)*. - The strings may end with 00, 01, or 10. Thus, the language is L = (0+1)*(00 + 01+10).
- The language consists of two symbols ‘0’ and ‘1’. A double (same) symbol means either 00 or 11. The part of the language containing at least one double symbol is denoted by (00 + 11). So, the language of any combination of ‘0’ and ‘1’ containing at least one double symbol is expressed as L = (0+1)* (00+11) (0+1)*.
- The number of ‘a’ is divisible by 3 means that the number of ‘a’ may be 0, 3, 6, 9, 12, …… The number of ‘b’ may be 0, 1, 2, 3,………. The RE is (b*ab*ab*ab*)*.
- The 10
^{th}symbol from the right hand side should be ‘a’, whereas the other symbols may be ‘a’ or ‘b’. A string of length n has (n−10) symbols from start and the last 9 symbols are of ‘a’ or ‘b’. The RE is (a + b)*a(a + b)10. - The language consists of two symbols ‘0’ and ‘1’. A double (same) symbol means either 00 or 11. According to the condition, in the language, 00 or 11 will not appear. The language may start with ‘0’ or start with ‘1’. The language may start with ‘0’ with no double symbol is (01)*. The language may start with ‘1’ with no double symbol is (10)*.

The expression is L = (01)* + (10)*.

- The set of language of all strings of 0 and 1 containing exactly two 0’s
- Construct an RE from the given FA by the algebraic method using Arden’s theorem.
For the previously given FA, the equations are*Solution:*The equation 1 is in the form R = Q + RP, where R = A, Q = Λ, and P = b. According to the Arden’s theorem, the solution for the equation is R = QP*.

So,

A = Λb* = b* (as ΛR = R Λ = R).Putting the value of A in equation 2, we get

B = b*a + Bb.The equation is in the format R = Q + RP, where R = B, Q = b*a, and P = b. According to the Arden’s theorem, the solution for the equation is R = QP

^{*}.So,

B = b*ab*.Putting the value of B in equation 3, we get

C = b*ab*a + C(a+b).The equation is in the format R = Q + RP, where R = C, Q = b

^{*}ab^{*}a, and P = (a + b). According to the Arden’s theorem, the solution for the equation is R = QP*.So, C = b*ab*a(a+b)*

As C is the final state, the RE accepted by the FA is b*ab*a(a+b)*.

- Construct an RE from the given FA by the algebraic method using Arden’s theorem.
For the previously given FA, the equations are*Solution:*The equation 1 is in the form R = Q + RP, where R = A, Q = Λ, and P = a. According to the Arden’s theorem, the solution for the equation is R = QP*.

So, A = Λa

*****= a*****(as ΛR = R Λ = R).Putting the value of A in equation (2), we get

B = a*b.Putting the value of B in equation (3), we get

C = a*bb+Cb.This is in the format R = Q + RP, where R = C, Q = a*bb, and P = b. According to the Arden’s theorem, the solution for the equation is R = QP*.

C = a*bbb*.Putting the value of B and C in equation (4), we get

D = a*ba + a*bbb*a + D(a+b).This is in the format R = Q + RP, where R = D, Q = a*ba + a*bbb*a, and P = a+b. According to the Arden’s theorem, the solution for the equation is R = QP*.

D = (a*ba + a*bbb*a)(a+b)*****.In the FA, there are two final states B and D. So, the RE accepted by the FA is

L = a*b + (a*ba + a*bbb*a)(a+b)*. - Find the RE corresponding to the following FA.
For the Previously given FA, the equations are*Solution:*Take equation number (2)

B = bA + bB.The equation is in the form R = Q + RP, where R = B, Q = bA, and P = b.

The solution of the equation is B = bAb*.

Replacing the value of B in equation (1), we get

A = abAb* + aA +

**Λ**+**Λ**+ A (abb* + a). The equation is in the form R = Q + RP where R = A, Q =**Λ**, and P = (abb* + a). The solution of the equation is A =**Λ**(abb* + a)* = (abb* + a)*.Replacing this value in the solution of B, we get b(abb

^{*}+ a)^{*}b^{*}. As B is the final state, the string accepted by the FA is b(abb* + a)*b*. - Find out the RE of the given diagram.
[UPTU 2003]

For the previously given FA, the equations are

Replacing the value of q

_{3}in (2), we getThis is in the form R = Q + RP. Hence, the solution is

q_{2}= q_{1}b(b + aa)*.Replacing the value of q

_{2}in (i), we getThis is in the form R = Q + RP. Hence, the solution is

q

_{3}is the final state and, therefore, the RE accepted by the FA is (a + b(b + aa)*b)* b(b + aa)* b. - Find the RE corresponding to the following figure:
[WBUT 2007]

There is only one initial state. In the FA, there are no null moves.*Solution:*The equations for q

_{1}, q_{2}, q_{3}, and q_{4}areNow, we need to solve the equations by using the Arden’s theorem to get the RE.

is in the form R = Q + RP, where R = q

_{2}, Q = q_{1}.1, P = (1 + 011).The solution of the equation is

q_{2}= q_{1}.1 (1 + 011)*.From here,

The equation is in the form R = Q + RP, where R = q

_{1}, Q =**Λ**, and P = 1 (1 + 011)* (00 + 010).By applying Arden’s theorem, the solution of the equation is R = QP

^{*}.q_{1}= (0 + 1 (1 + 011)* (00 + 010))*The final state of the automaton is q

_{4}= (0 + 1 (1 + 011)* (00 + 010))*(1 (1 + 011)*01).The RE corresponding to the given automaton is

(0 + 1 (1 + 011)* (00 + 010))*(1 (1 + 011)*01). - Construct an FA equivalent to the RE, L = (a + b) + a(a + b)* (ab + ba).
*Solution:*Take a beginning state q*Step I:*_{0}and a final state q_{f}. Between the beginning and final states, place the RE.Between (a+b) and a(a+b)* (ab+ba), there is a + sign, and so there will be parallel edges between q*Step II:*_{0}and q_{f}.Between (a + b)*Step III:*^{*}and (ab + ba), there is a .(dot) sign, and so an extra state is added between q_{0}and q_{f}.Between ab and ba, there is + sign, and so there will be parallel edges between q*Step IV:*_{1}and q_{f}.Between ‘a’ and b, there is a + sign. So, between q0 and qf there is a parallel edge.*Step V:*Between ‘a’ and ‘b’ and between ‘b’ and ‘a’, there are .(dots). So, two extra states are added between q*Step VI:*_{1}and q_{f}. An extra state is added between q_{0}and q_{1}for a(a + b)*****.The * between q*Step VII:*_{4}and q_{1}is removed by adding an extra state with label a, b, and the ∈ transition from q_{4}to that state and from that state to q_{1.}Removing ∈, the automata become*Step VIII:* - Construct an FA equivalent to the RE, L = (00 + 11)* 11 (0 + 1)*0.
*Solution:**Step I***:**Take a beginning state q_{0}and a final state q_{f}. Between the beginning and final state, place the RE.There are four .(dots) in between (00 + 11)*Step II:*^{*}and 1, 1 and 1, 1 and (0 + 1)^{*}, and (0 + 1)^{*}and 0. So, the four extra states are added in between q_{0}and q_{f}.The * between q*Step III:*_{0}and q_{1}is removed by adding an extra state with label 00 and 11 as loop and ∈ transition from q_{0}to that state and from that state to q_{1}. The same is applied for the removal of * between q_{3}and q_{4}.Removing the + sign between 00 and 11, parallel edges are added and for two .(dots) signs (between 0, 0 and 1,1), two extra states are added.*Step IV:*Use the ∈ removal technique to find the corresponding DFA.*Step V:* - Construct an FA for the RE 10 + (0 + 11)0*1.
[UPTU 2004]

*Solution:* - Convert an RE (0 + 1)*(10) + (00)*(11)* to an NFA with ∈ move.
[Gujrat Technological University 2010]

- Construct an FA equivalent to the regular expression
(01(1(10)* + 111)* + 0)*1.
[Cochin University 2009]

- Convert the following NFA with ∈-move to an equivalent DFA.
*Solution:*As 0 is the beginning state, start with ∈-closure(0) = {0, 1, 2, 4, 7}. Let us rename it as A.

A is still unmarked.

Then, construct a δ′ function for the new unmarked state A for inputs a and b.

This is a new state, so rename it as B.

This is a new state, so rename it as C.

Then, construct a δ′ function for the new unmarked state B for inputs a and b.

This is a new state, so rename it as D.

C is still unmarked.

Then, construct a δ′ function for the new unmarked state C for inputs a and b.

D is still unmarked.

Then, construct a δ′ function for the new unmarked state D for inputs a and b.

This is a new state, so rename it as E.

Here, the final state is E.

- Construct a regular grammar for the RE, L = (a + b)*(aa + bb)(a + b)*.
The NFA for the RE is*Solution:*There are four states in the FA. So, in the regular grammar, there are four non-terminals.

Let us take them as A (for q

_{0}), B (for q_{1}), C (for q_{2}), and D (for q_{3}).Now, we have to construct the production rules of the grammar.

For the state q

_{0}, the production rules areA → aA, A → bA, A → aB, A → bC.For the state q

_{1}, the production rules areB → aD, B → a (as D is the final state).For the state q

_{3}, the production rules areC → b D, C → b (as D is the final state).For the state q

_{4}, the production rules areD → aD, D → bD, D → a/ b.The grammar = {V

_{N}, Σ, P, S} - Consider the given FA and construct the smallest DFA which accepts the same language. Draw an RE and the grammar that generates it.
[UPTU 2002]

This problem can be solved by the ∈-closure method.*Solution:*As 1 is the beginning state, start with ∈-closure(1) = {1,4}. Let us rename it as A.

A is still unmarked.

Then, construct a δ′ function for the new unmarked state A for inputs 0 and 1.

It is a new state. Mark it as B.

It is a new state. Mark it as D.

It is a new state. Mark it as E.

It is a new state. Mark it as C.

The beginning state is A, and the final state is E.

The transitional diagram of the DFA is

The RE is constructed using the Arden’s theorem.

Replacing A in B and C, we get

Replacing C in B, we get B = 0 + 01.

Replacing the new value of B, C, and E in D, we get

It is in the format of R = Q + RP, where Q = (0 + (0 + 01) + 11), P = (1 + 10).

The solution is R = QP*.

D = (0 + (0 + 01) + 11) (1 + 10)*.Replacing the value of D in E, we get E = 0(0 + (0 + 01) + 11) (1 + 10)*.

As E is the final state, the RE accepted by the FA is

0(0 + (0 + 01) + 11) (1 + 10)*.The regular grammar is constructed as follows taking each state into account.

There are five states in the FA. So, in the regular grammar, there are five non-terminals.

Let us take them as A, B, C, D, and E according to the name of the states.

Now we have to construct the production rules of the grammar.

For the state A, the production rules are

A → 0B, A → 1C.For the state B, the production rules are

B → 0D.For the state C, the production rules are

C → 0B C → 1D.For the state D, the production rules are

D → 1D D → 0E D → 0.For the state E, the production rules are

E → 1D. - Find the RE recognized by the finite state automaton of the following figure.
[GATE 1994]

The equation for the FA is*Solution:*Solving the equation (1) using the Arden’s theorem, we get A = ∧0* = 0*.

Putting the value of A in equation (2), we get

B = 10* + 1B.Using the Arden’s theorem, we get

B = 10*1*.Both A and B are final states, and thus the string accepted by the FA is

#### Multiple Choice Questions

- The machine format of regular expression is
- Finite automata
- Push down automata
- Turing machine
- All of the above

- The regular expression is accepted by
- Finite automata
- Push down automata
- Turing machine
- All of the above

- The language of all words with at least 2 a’s can be described by the regular expression
- (ab)*a
- (a + b)*ab*a(a + b)*
- b*ab*a(a + b)*
- All of the above

- The set of all strings of {0, 1} having exactly two 0’s is
- 1*01*01*
- {0 + 1)*1
- {11 + 0}*
- {00 + 11}*

- The set of all strings of {0, 1} where 0 is followed by 1 and 1 is followed by 0 is
- (01)*
- 0*1*
- 0*1 + 1*0
- 0*1* + 1*0*

- Which of the strings do not belong to the regular expression (ba + baa)*aaba
- baaaba
- babaabaaaba
- babababa
- baaaaba

- Which of the following type of language is regular expression?
- Type 0
- Type 1
- Type 2
- Type 3

- Which of the following RE over {0,1} denotes the set of all strings not containing 100 as substring?
- (1 + 0)*0*
- 0*1010*
- 0*1*01
- All of the above

- ∧ + RR* = ?
- R
- R*
- R
^{+} - ∧

- {a/b} denotes the set
- {ab}
- {a, b}
- {a, b}*
- {ab}*

- What is the solution for the equation R = Q + RP (If P and Q are RE and P does not contain ∧)?
- R = QP*
- R = QP
- R = PQ*
- R = P*Q*

- Which of the languages is accepted by the following FA?
- b(a + bba*)*a* b)
- a(a + bba*)*b*
- a(a + bb*a*)*b*d)
- None of these

- Which is true for ∈-closure of a state?
- All the states reachable from the state with input null excluding the state
- The state only
- All the other states
- All the states reachable from the state with input null

- The pumping lemma for regular expression is used to prove that
- Certain sets are regular
- Certain sets are not regular
- Certain regular grammar produce RE
- Certain regular grammar does not produce RE

- Regular sets are closed under
- Union
- Concatenation
- Kleene closure
- All of the above

*Answers:*

- a
- d
- b
- a
- d
- c
- c
- c
- b
- b
- a
- a
- d
- a
- d

#### GATE Questions

- Which two of the following four regular expressions are equivalent?
- (00)*(∈ + 0)
- (00)*
- 0*0*
- 0(00)*

- (i) and (ii)
- (i) and (iv)
- (ii) and (iii)
- (iii) and (iv)

- Which of the following alternatives is true for the following three regular expressions?
- (011((11)* + (01)*)*)*011
- 011(((1 + 0)1)*011)*
- 011(((1 + 0)*1*)*011)*

- Consider the following statements:
- Ø* = ∈
- s(rs + s)*r = (sr + s)*sr
- (r + s)* = (r* + s*) +

Find out the correct alternatives.

- All are true
- Only (ii) is false
- only (iii) is false
- Both (ii) and (iii) are false

- Which regular expression best describes the language accepted by the following non-deterministic automation?
- (a + b)* a(a + b)b
- (abb)*
- (a + b)*a(a + b)*b(a + b)*
- (a + b)*

- Which of the following regular expressions describe the language over {0, 1} consisting of strings that contain exactly two 1’s?
- (0 + 1)*11(0 + 1)*
- 0*110*
- 0*10*10*
- (0 + 1)*1(0 + 1)*1(0 + 1)*

- Find the false statement if S = (a, b)
- L = {a
^{n}b^{m}, m, n ≥ 1} is regular - L = {X, where no of a > no of b} is not regular
- L = {a
^{n}b^{n}, n ≥ 1} is regular - L = {X, where no of a and no of b are equal} is not regular

- L = {a
- Which of the following regular expression identified is true?
- r
^{(}*^{)}= r* - (r*s*) = (r + s)*
- (r + s)* = r* + s*
- r*s* = r* + s*

- r
- In some programming languages, an identifier is permitted to be a letter followed by any number of letters or digits. If L and D denote the sets of letters and digits, respectively, which of the following expressions define an identifier?
- (L ∪ D)*
- L(L ∪ D)*
- (L.D)*
- L(L.D)*

- Which two of the following four regular expressions are equivalent?
- (00)*(∈ + 0)
- (00)*
- 0*
- 0(00)*

- (i) and (ii)
- (ii) and (iii)
- (i) and (iii)
- (iii) and (iv)

- Let L ⊆ Σ*, where Σ = {a, b}. Which of the following is true?
- L = {x | x has equal number of a’s and b’s} is regular
- L = {a
^{n}b^{n}| n ≥ 1} is regular - L = {x | x has more a’s than b’s} is regular
- L = {a
^{m}b^{n}| m ≥ 1, n ≥ 1} is regular

- The string 1101 does not belong to the set represented by
- 110*(0 + 1)
- 1(0 + 1)*101
- (10)*(01)*(00 + 11)*
- (00 + (11)*01)*

- Give a regular expression for the set of binary strings where every 0 is immediately followed by exactly k number of 1’s and preceded by at least k number of 1’s (k is a fixed integer).
- 1*1
^{k}(01^{k})*1* - 1*1
^{k}01^{k} - 1*(1
^{k}01^{k})* - 1*(1
^{k}0)* 1*

- 1*1
- Let S and T be the languages over Σ = {a, b} represented by the regular expressions (a + b*)* and (a + b)*, respectively. Which of the following is true?
- S ⊂ T
- T ⊂ S
- S = T
- S ∩ T = Ø

- The regular expression 0*(10*)* denotes the same set as
- (1*0)*1*
- 0 + (0 + 10)*
- (0 + 1)*10(0 + 1)*
- None of the above

- Consider the following NFA M
Let the language accepted by M be L. Let L

_{1}be the language accepted by the NFA M_{1}obtained by changing the accepting states of M to a non-accepting state and by changing the non-accepting states of M to accepting states. Which of the following statements is true?- L
_{1}= {0, 1}* − L - L
_{1}= {0, 1}* - L
_{1}⊆ L - L
_{1}= L

- L
- Consider the regular language L = (111 + 11111)*. The minimum number of states in any DFA accepting this language is
- 3
- 5
- 8
- 9

- Which of the following is true?
- Every subset of a regular set is regular.
- Every finite subset of a non-regular set is regular.
- The union of two non-regular sets is not regular.
- Infinite union of finite sets is regular.

- The language accepted by this automaton is given by the regular expression
- b*ab*ab*ab*
- (a + b)*
- b*a(a + b)*
- b*ab*ab*

- The minimum state automation equivalent to the previous FSA has the following number of states.
- 1
- 2
- 3
- 4

- Which of the following is true for the language {a
^{p}| p is prime}?- It is not accepted by a Turing machine
- It is regular but not context-free
- It is context-free but not regular
- It is neither regular nor context-free but accepted by a Turing machine.

- Match the following NFAs with the regular expressions they correspond to.
- ∈ + 0(01*1 + 00)*01*
- ∈ + 0(10*1 + 00)*0
- ∈ + 0(10*1 + 10)*1
- ∈ + 0(10*1 + 10)*10*

- P −2, Q −1, R −3, S −4
- P −1, Q −3, R −2, S −4
- P −1, Q −2, R −3, S −4
- P −3, Q −2, R −1, S −4

- Which of the following are regular sets?
- {a
^{n}b^{2m}| n ≥ 0, m ≥ 0} - {a
^{n}b^{m}| n = 2m} - {a
^{n}b^{m}| n ≠ m} - {xcy | x, y ∈ {a, b}*}

- I and IV only
- I and III only
- I only
- IV only

- {a
- Which one of the following languages over the alphabet {0, 1} is described by the regular expression:
(0 + 1)*0(0 + l)*0(0 + 1)* ?

- The set of all strings containing the substring 00.
- The set of all strings containing at most two 0’s.
- The set of all strings containing at least two 0’s.
- The set of all strings that begin and end with either 0 or 1

- Let L = {w∈(0 + 1)* | w has an even number of 1s}, i.e., L is the set of all bit strings with even number of 1s. Which one of the given regular expressions represent L?
- (0*10*1)*
- 0*(10*10*)*
- 0*(10*1*)*0*
- 0*1(10*1)*10*

- What is the complement of the language accepted by the given NFA? Assume that Σ = {a} and ∈ is the empty string.
- ∈
- {∈}
- a*
- {a, ∈}

- Given the language L = {ab, aa, baa}, which of the following strings are in L*?
- abaabaaabaa
- aaaabaaaa
- baaaaabaaaab
- baaaaabaa

- 1, 2, and 3
- 2, 3, and 4
- 1, 2, and 4
- 1, 3, and 4

- Which one of the following regular expressions is not equivalent to the regular expression (a + b + c)*?
- (a* + b* + c*)*
- (a*b*c*)*
- ((ab)* + c*)*
- (a*b* + c*)

- Let M = (K, Σ, δ,s, F) be a finite state automaton, where K = {A, B}, = {a, b}, s = A, F = {B}, δ(A, a) = A, δ(A, b) = B, δ(B, a) = B, and δ(B, b) = A.
A grammar to generate the language accepted by M can be specified as G = (V, Σ, R, S), where V = K ∪ Σ and S = A.

Which one of the following set of rules will make L (G) = L(M)?

- {A → aB, A → bA, B → bA, B → aA, B → ∈}
- {A → aA, A → bB, B → bb, B → bA, B → ∈}
- {A → bB, A → aB, B → aA, B → bA, B → ∈}
- {A → aA, A → bA, B → aB, B → bA, A → ∈}

*Answers:*

- b
- c
- d
- a
- c
- c
- a
- b
- c
- d
- c
- a
- c
- a
- b
- b
- c
- c
- b
- d
- c
- c
- c
- a,b,d
- b
- c
- c
- b

**Hints:**

2. See example 5.4.

11. Both generates odd and even length strings

14. (a + b*)* = (a* (b*)*)* = (a*b*)*

15. Both of these generate any combination of 0 and 1

16. Construct the transitional diagram.

17.

18. a) (0 + 1)* is regular but 0^{n}1^{n} is not regular.

20. The simplified automata is

21. It is not regular (proved by the pumping lemma). Not CFG. But accepted by TM as TM accepts unrestricted language.

25. Only c may generate odd number of 1.

26. The language accepted by the FA is a^{+}. The complement of it is Σ* − a^{+} = ∈.

28. C will not generate ac or bc but all the others will generate.

#### Fill in the Blanks

- Regular expression is a string on ______________ among {Q, Σ, δ, q
_{0}, F} - If R is a regular expression, then Λ + RR* = ______________.
- According to the Arden’s theorem, the solution of the equation R = Q + RP is ______________.
- The set of all states that can be reached from that state to all the states with input ∈ is called ______________.
- For the following NFA with ∈-move
∈-closure of q

_{1}is ____________. - A grammar where all productions are in the form A → Bα or A → α is called ______________.
- A grammar where all productions are in the form A → αB or A → α is called ______________.
- The pumping lemma for regular expression is used to prove that certain sets are not ______________.
- The property which describes when we combine any two elements of the set and the result is also included in the set is called ______________.
- If any finite automata contains any ∈ (null) move or transaction, then that finite automata is called ______________.
- The machine format of regular expression is ______________.

*Answers:*

- Σ
- R*
- R = QP*
- ∈-closure
- q
_{0}, q_{1}, q_{2} - left linear grammar
- right linear grammar
- regular
- closure property
- NFA with Σ moves
- Finite automata

- Prove that (0 + 011*) = (0 + 011*)(01 + 0100*)(01 + 0100*)* = 01*(010*)*
- Find the regular expression for the following.
- Set of all strings over (a, b) containing exactly one a
- Set of all strings over (a, b) containing at least two a’s
- Set of all strings over (0, 1) containing exactly two a’s
- Set of all strings over (a, b) beginning and ending with b
- Set of all strings over (0, 1) containing 010 as substring
- Set of all strings over (0, 1) not containing substring 00

- Describe the following regular expressions in English language
- b*ab*
- (a + b)* aba(a + b)*
- a(a + b)*ab

- Find the regular expressions corresponding to the automaton generated by the following finite automata.
- Convert the following NFA with null move to an equivalent DFA by the ∈-closure method.
- Develop the finite automata for the following regular expressions
- 10 + (0 + 11)*01
- (a*ab + ba)*a
- a(ab + a)*(aa + b)

- Check whether the following two finite automata are equivalent or not.
- Check whether the following two RE are equivalent or not
L

_{1}= 10 + (1010)*(1010)* L_{2}= 10 + (1010)* - Construct the regular grammar for the following finite automata.
- ab + (aa + bb)(a + b)*
- 01(00 + 11)*11*
- a*b(a + b)*
- a*(aa + bb)* + b*(ab + ba)*

- Using the pumping lemma show that the following sets are not regular.
- L = {a
^{n}b^{m}, where 0 < n < m} - L = {a
^{n}b^{n + 1}, where n > 0} - L = {a
^{i}b^{j}c^{k}, where i, j, k > 0}

- L = {a