0% found this document useful (0 votes)
18 views

Unit 2 TOC 3

Regular expressions are used to represent regular languages. A regular expression is defined recursively and can include symbols, concatenation, union and Kleene star. Some key properties of regular expressions include: the union, intersection and difference of regular languages are also regular. Arden's theorem can be used to solve equations of the form R = Q + RP to find the regular expression corresponding to a finite state machine.

Uploaded by

rapil43576
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unit 2 TOC 3

Regular expressions are used to represent regular languages. A regular expression is defined recursively and can include symbols, concatenation, union and Kleene star. Some key properties of regular expressions include: the union, intersection and difference of regular languages are also regular. Arden's theorem can be used to solve equations of the form R = Q + RP to find the regular expression corresponding to a finite state machine.

Uploaded by

rapil43576
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

Regular Expressions

Regular Expressions are used to denote regular languages. An expression is


regular if:

• ɸ is a regular expression for regular language ɸ.

• ɛ is a regular expression for regular language {ɛ}.

• If a ∈ Σ (Σ represents the input alphabet), a is regular expression with


language {a}.

• If a and b are regular expression, a + b is also a regular expression with


language {a,b}.

• If a and b are regular expression, ab (concatenation of a and b) is also regular.

• If a is regular expression, a* (0 or more times a) is also regular.


Regular Expressions
• Regular Grammar : A grammar is regular if it has rules of form
A -> a or A -> aB or A -> ɛ where ɛ is a special symbol called
NULL.

• Regular Languages : A language is regular if it can be


expressed in terms of regular expression.
A Regular Expression can be recursively defined as follows −

• If X is a Regular Expression denoting the language L(X) and Y is


a Regular Expression denoting the language L(Y), then

– X + Y is a Regular Expression corresponding to the


language L(X) ∪ L(Y) where L(X+Y) = L(X) ∪ L(Y).

– X . Y is a Regular Expression corresponding to the language


L(X) . L(Y) where L(X.Y) = L(X) . L(Y)

– R* is a Regular Expression corresponding to the language


L(R*)where L(R*) = (L(R))*
Some RE Examples
Properties of Regular Sets

• Property 1. The union of two regular set is regular.


• Proof −
• Let us take two regular expressions
• RE1 = a(aa)* and RE2 = (aa)*
• So, L1 = {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)
• and L2 ={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length including
Null)
• L1 ∪ L2 = { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,.......}
• (Strings of all possible lengths including Null)
• RE (L1 ∪ L2) = a* (which is a regular expression itself)
Properties of Regular Sets

• Property 2. The intersection of two regular set is regular.


• Proof −
• Let us take two regular expressions
• RE1 = a(a*) and RE2 = (aa)*
• So, L1 = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
• L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
• L1 ∩ L2 = { aa, aaaa, aaaaaa,.......} (Strings of even length excluding Null)
• RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.
• Property 3. The complement of a regular set is regular.
• Proof −
• Let us take a regular expression −
• RE = (aa)*
• So, L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even length including Null)
• Complement of L is all the strings that is not in L.
• So, L’ = {a, aaa, aaaaa, .....} (Strings of odd length excluding Null)
• RE (L’) = a(aa)* which is a regular expression itself.
• Property 4. The difference of two regular set is regular.
• Proof −
• Let us take two regular expressions −
• RE1 = a (a*) and RE2 = (aa)*
• So, L1 = {a, aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)
• L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)
• L1 – L2 = {a, aaa, aaaaa, aaaaaaa, ....}
• (Strings of all odd lengths excluding Null)
• RE (L1 – L2) = a (aa)* which is a regular expression.
• Property 5. The reversal of a regular set is regular.
• Proof −
• We have to prove LR is also regular if L is a regular set.
• Let, L = {01, 10, 11, 10}
• RE (L) = 01 + 10 + 11 + 10
• LR = {10, 01, 11, 01}
• RE (LR) = 01 + 10 + 11 + 10 which is regular
• Property 6. The closure of a regular set is regular.
• Proof −
• If L = {a, aaa, aaaaa, .......} (Strings of odd length excluding Null)
• i.e., RE (L) = a (aa)*
• L* = {ε , a, aa, aaa, aaaa , aaaaa,……………} (Strings of all lengths
inclusing Null)
• RE (L*) = (a)*
Hence Regular
• Property 7. The concatenation of two regular sets is regular.
• Proof −
• Let RE1 = (0+1)*0 and RE2 = 01(0+1)*
• Here, L1 = {0, 00, 10, 000, 010, ......} (Set of strings ending in 0)
• and L2 = {01, 010,011,.....} (Set of strings beginning with 01)
• Then, L1 L2 =
{001,0010,0011,0001,00010,00011,1001,10010,.............}
• Set of strings containing 001 as a substring which can be
represented by an RE − (0 + 1)*001(0 + 1)*
Identities Related to Regular Expressions

• Given R, P, L, Q as regular expressions, the following


identities hold −
• ∅* = ε
• ε* = ε
• RR* = R*R
• R*R* = R*
• (R*)* = R*
• RR* = R*R
• (PQ)*P =P(QP)*
• (a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
Identities Related to Regular Expressions

• R + ∅ = ∅ + R = R (The identity for union)

• R ε = ε R = R (The identity for concatenation)

• ∅ L = L ∅ = ∅ (The annihilator for concatenation)

• R + R = R (Idempotent law)

• L (M + N) = LM + LN (Left distributive law)

• (M + N) L = ML + NL (Right distributive law)

• ε + RR* = ε + R*R = R*
Arden’s Theorem

• Arden’s theorem state that:


“If P and Q are two regular expressions over ∑, and if P does
not contain ε , then the following equation in R given by R = Q
+ RP has an unique solution i.e., R = QP*.”

That means, whenever we get any equation in the form of


• R = Q + RP, then we can directly replaced by R = QP*.

• So, here first we will prove that R = QP* is the solution of


this equation and then we will also prove that it is the unique
solution of this equation.
Arden’s Theorem
• Let’s start by taking this equation as
• R = Q + RP ......(i)
• Now, replacing R by R = QP*,
• we get,
• R = Q + QP*P Taking Q as common,
• R = Q(ε + P*P)
• R = QP* (As we know that ε + R*R = R*).
Hence proved.
• Thus, R = QP* is the solution of the equation R = Q + RP.
Arden’s Theorem
• Now, we have to prove that this is the only solution of this equation. Let me take this equation
again:
• R = Q + RP
• Now, replace R by R = Q + RP,
• R = Q + (Q + RP)P
• R= Q + QP + RP2
• Again, replace R by R = Q + RP:-
• R= Q + QP + (Q + RP) P2
• R= Q + QP + QP2 + R P3
• R= Q + QP + Q P2 + Q P3 .. + Q P(n-1) + Q Pn + Q P*
• R = Q(ε + P + P2 + P3.. + P*) [represent the closure of P]
• R = Q P*
• Hence proved.
• Thus, R = QP* is the unique solution of the equation R = Q + RP.
Example
Solution −
• Here the initial state and final state is q1.
• The equations for the three states q1, q2, and
q3 are as follows −
• q1 = q1a + q3a + ε (ε move is because q1 is the
initial state0
• q2 = q1b + q2b + q3b
• q3 = q2a
• Now, we will solve these three equations −
• q2 = q1b + q2b + q3b
• = q1b + q2b + (q2a)b (Substituting value of q3)
• = q1b + q2(b + ab)
• = q1b (b + ab)* (Applying Arden’s Theorem)
• q1 = q1a + q3a + ε
• = q1a + q2aa + ε (Substituting value of q3)
• = q1a + q1b(b + ab*)aa + ε (Substituting value of q2)
• = q1(a + b(b + ab)*aa) + ε
• = ε (a+ b(b + ab)*aa)*
• = (a + b(b + ab)*aa)*
• Hence, the regular expression is (a + b(b + ab)*aa)*.
• Here the initial state is q1 and the final state is q2
• Now we write down the equations −
• q 1 = q 10 + ε
• q 2 = q 11 + q 20
• q 3 = q 21 + q 30 + q 31
• Now, we will solve these three equations −
• q1 = ε0* [As, εR = R]
• So, q1 = 0*
• q2 = 0*1 + q20
• So, q2 = 0*1(0)* [By Arden’s theorem]
• Hence, the regular expression is 0*10*.
• Solution:
• Let us write down the equations
• q1 = q1 0 + ε
• Since q1 is the start state, so ε will be added, and the input 0 is coming to q1
from q1 hence we write
State = source state of input × input coming to it
• Similarly,
• q2 = q1 1 + q2 1 q3 = q2 0 + q3 (0+1)
• Since the final states are q1 and q2, we are interested in solving q1 and q2
only. Let us see q1 first
• q1 = q1 0 + ε
• We can re-write it as
• q1 = ε + q1 0
• Which is similar to R = Q + RP, and gets reduced to R = OP*.
• Assuming R = q1, Q = ε, P = 0
• We get
• q1 = ε.(0)* q1 = 0* (ε.R*= R*)
• Substituting the value into q2, we will get
• q2 = 0* 1 + q2 1 q2 = 0* 1 (1)* (R = Q + RP →
Q P*)
• The regular expression is given by
• r = q1 + q2 = 0* + 0* 1.1* r = 0* + 0* 1+ (1.1* = 1+)
• We see, that on state C, there is a state transition coming from B when a is
the input

• C = Ba On state B, There is a self loop on input b, a transition from A when


input is a, a transition from state C when input is b

• B = Bb + Cb + Aa On state A, There is a transition ( being the start state,


transition must be included), a self loop on input a, a transition from B
when input is b.

• A = ε + Aa + Bb Putting (2) in (1), we get

• C = Ba C = (Aa + Bb + Cb)a C = Aaa + Bba + Cba


• Putting (1) in (2), we get
• B = Bb + Cb + Aa
• B = Aa + Bb + (Ba)b
• B = Aa + B(b + ab)
• B = Aa(b + ab)* (Using R = QP*) Putting (2) in (3), we get
• A = ε + Aa + Bb
• A = ε + Aa + Aa(b + ab)*b
• A = ε + A(a + a(b + ab)*b)
• A = ε (a + a(b + ab)*b)*
• A = (a + a(b + ab)*b)* As a final step, Let’s combine all the simplified equations onto
the final state C


C = Ba C = Aa(b + ab)*a C = (a + a(b + ab)*b)* a (b + ab)* a
• Problem : Derive a regular expression to represent a language having even
no. of a’s
• For this case, it’s difficult to arrive at a regular expression with just trial
and error methodology.
We might come across sample solutions like :-
• ( aa + b )*
• ( b*ab* )*
• (b*ab*ab*)*
• which might satisfy some cases, but also leads to unwanted cases and
missing cases with alternate a’s and b’s.
The best way to solve this problem is to first draw a finite state machine
for the same, and then derive the regular expression from the same.
The DFA for the provided problem
• Now that we have the DFA, let’s solve it using Arden’s Theorem of
Individual State Equations.
• We see that on state A, there is a self loop with input b and transition
from B with input a


A = ε + Ab + Ba We see that on state B, there is a self loop on input b and
transition from A when input is a.
• B = Aa + Bb
• Taking equation for B, we can apply Arden’s
theorem
• B = Aa + Bb B = Aab* Substituting the value of B in
A we get
• A = ε + Ab + Ba
• A = ε + Ab + (Aab*)a
• A = ε ( b + ab*a )*
• A = ( b + ab*a )* Hence, the regular expression for
the provided problem is RE : ( b + ab*a )*
Try this
Two-Way Finite Automata

• Two-way finite automata are similar to the machines we have been


studying, except that they can read the input string in either direction.
• We think of them as having a read head, which can move left or right over
the input string.
• Like ordinary finite automata, they have a finite set Q of states and can be
either deterministic (2DFA) or nondeterministic (2NFA).
Two-Way Finite Automata
Two-Way Finite Automata
Two-Way Finite Automata
Two-Way Finite Automata
Two-Way Finite Automata
Example
Two-Way Finite Automata
• 1. Let S and T be language over ={a,b}
represented by the regular expressions
(a+b*)* and (a+b)*, respectively. Which of
the following is true? (GATE CS 2000)

(a) ScT (S is a subset of T)


(b) TcS (T is a subset of S)
(c) S=T
(d) SnT=Ø
• 2. Let L denotes the language generated by
the grammar S – OSO/00. Which of the
following is true? (GATE CS 2000)
(a) L = O
(b) L is regular but not O
(c) L is context free but not regular
(d) L is not context free
• 3. Consider the following two statements:
S1: { 0^2n |n >= l} is a regu1ar language
S2: { 0^m 0^n 0^(m+n) l m >= 1 and n >= 2} is a
regu1ar language
Which of the following statements is correct?
(GATE CS 2001)
a) Only S1 is correct
b) Only S2 is correct
c) Both S1 and S2 are correct
d) None of S1 and S2 is correct

You might also like