0% found this document useful (0 votes)
32 views169 pages

Regular Expressions

Regular expressions can be used to describe regular languages. A regular expression is made up of symbols from an alphabet, operators like concatenation, union and Kleene star. Regular expressions can be recursively defined, with primitive expressions like symbols and empty string as the base cases. The language represented by a regular expression consists of all strings that can be generated by applications of the operators. Equivalent regular expressions represent the same regular language.

Uploaded by

shruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views169 pages

Regular Expressions

Regular expressions can be used to describe regular languages. A regular expression is made up of symbols from an alphabet, operators like concatenation, union and Kleene star. Regular expressions can be recursively defined, with primitive expressions like symbols and empty string as the base cases. The language represented by a regular expression consists of all strings that can be generated by applications of the operators. Equivalent regular expressions represent the same regular language.

Uploaded by

shruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 169

Regular Expression & Regular Languages

Regular Language
• A language L is known as regular if and only if it is recognized by a finite
accepter (FA).
• Language L is regular if and only if it is recognized by a DFA. (??)
• Language L is regular if and only if it is recognized by an NFA. (??)

• A language L is known as regular if and only if it is described by a regular


expression (RE).
• A language L is recognized by a FA if and only if L is described by a regular
expression.

• NFA recognize exactly the regular languages.


• Regular expressions describe exactly the regular languages.
How to show that a given language is regular ?
Regular Expression
• A regular expression consists of strings of symbols from some alphabet S,
parentheses (), and the operators +, . and *.

• Let S be a given alphabet. Then,


• f, λ and a Î S are all regular expressions. These are known as primitive regular
expressions.
• Recursive Definition:
• If r1 and r2 are regular expressions (REs), then the following expressions are also regular:
• r1 + r2 OR r1 | r2 è (r1 or r2)
• r1.r2 OR r1r2 è (r1 followed by r2)
• r1* è (r1 repeated zero or more times)
• (r1)

• A strings of symbols is a regular expression if and only if it can be derived


from primitive regular expressions by finite applications of recursive
definition.
Precedence of Operators
Rules for Specifying Regular Expressions:
PRECEDENCE
() HIGHEST
.
* .
.
. .
.
| LOWEST
Rules for REs
For r, s and t be RE over Σ
ü r+Æ=Æ+r=r
ü rׯ = Æ×r = Æ
ü Æ* = L

ü r+L=L+r =r
ü r × L = L ×r = r
ü L* = L
ü (r + L)+ = r*

ü r+s=s+r
ü r×(s + t) = r×s + r×t
ü r×(s.t) = (r×s) ×t

ü r+ = r r*
ü r* = r*(r + L) = r* r* = (r*)*
ü (r*s*)* = (r + s)*
Valid Regular Expressions: Example
• Let Σ ={a, b, c}

• f, λ, a, b, c
• a*, b*
• a.b, b.a, a+b, (a.b)*, (b.a)*, (a+b)*
• a + b is equivalent to b+a
• a.b is not equivalent to b.a
• (a + b.c)*
• (c+f)

Why ?
Invalid Regular Expressions: Example
• Let Σ ={a, b, c}

• *a, *b*, +a*, .b*


• +a.b, *b.a, .*a+b, (++a.b)*, (..*b.a***)*, (++a++b**)*
• (+a + b.c)*
• (c+f*+*)

Why ?
Some Notations
• Parentheses in regular expressions can be omitted when the order
of evaluation is clear.
• ((0+1)*) = (0+1)* ¹ 0+1*
• ((0*)+(1*)) = 0* + 1*
• For concatenation, × can be omitted.
• r× r× r… r is denoted by rn.

n times
Simple Examples over S= {0,1}
• {aÎS*|a does not contain 1’s}
• 0*

• {aÎS*|a contains 1’s only}


• 1×(1*) (which can be denoted by (1+))

• {aÎS*|a contains only 0’s or only 1’s}


• (00*)+(11*)

• S*
• (0+1)*
• Note: 0* + 1* ¹ (0+1)*
Examples over S = {0,1}
• Strings of even length, L={00,01,10,11} *
• (00+01+10+11) * or
• ((0+1)(0+1))*

• Strings of length 6, L={aÎS*| the length of a is 6}


• 000000+….+111111
• (0+1)(0+1) (0+1)(0+1) (0+1)(0+1) =(0+1)6

• Strings of length 6 or less, L={aÎS*| the length of a is less than or


equal to 6}
• λ +0+1+00+01+10+11….+111111
• (0+1+ λ)6
Examples over S = {0,1}
• {aÎS*| a is a binary number divisible by 4}
• (0+1)*00

• {aÎS*| a does not contain 11}


• (0+10)* (1+ λ)

• {aÎS*| a contains odd number of 1’s}


• 0*(10*10*)*10*

• {aÎS*| any two 0’s in a are separated by three 1’s}


• 1*(0111)*01* + 1*
Regular Expressions: Example
• All strings of 1s and 0s
(0 | 1)*

• All strings of 1s and 0s beginning with a 1


1 (0 | 1)*

• All strings containing two or more 0s


(1|0)*0(1|0)*0(1|0)*

• All strings containing an even number of 0s


(1*01*01*)* | 1*
Regular Expressions : Example
• All strings containing an even number of 0s and even number of 1s
Assume that ( 0 0 | 1 1 ) is X
X* | (X* ( 0 1 | 1 0 ) X* ( 0 1 | 1 0 ) X*)*
OR
( 0 0 | 1 1 )*(( 0 1 | 1 0 )( 0 0 | 1 1 )*( 0 1 | 1 0 )( 0 0 | 1 1 )*)*

• All strings of alternating 0s and 1s


( λ | 1 ) ( 0 1 )* ( λ | 0 )

• Strings over the alphabet {a, b} in which substrings ab and ba occur an


unequal number of times
• (a+b+)+ | (b+a+)+
Regular Expressions : Example
• Strings over the alphabet {0, 1} with no consecutive 0's
• (1 | 01 )* (0 | e)
• 1*(01+)* (0 | e)
• 1*(011*)* (0 | e)

• Strings over the alphabet {a, b} with exactly three b's


• a*ba*ba*ba*

• Strings over the alphabet {a, b, c} containing (at least once) bc


• (a|b|c)*bc(a|b|c)*
Regular Expressions : Example
• (1 | 10)*
• all strings starting with “1” and containing no “00”

• (0 | 1)*011
• all strings ending with “011”

• 0*1*
• all strings with no “0” after “1”

• 00*11*
• all strings with at least one “0” and one “1”, and no “0” after “1”
Regular Expressions : Example

• What languages do the following RE represent?

• ((0 | 1)(0 | 1))* | ((0 | 1)(0 | 1)(0 | 1))*


Regular Languages
• Each RE has an equivalent regular language (RL).

• A language L is regular if there is a regular expression r such that L


= L(r).

• The language L(r) denoted by any regular expression r is defined by


the following rules.
• Φ is a regular expression. L(Φ) = {} =Φ
• λ is a regular expression. L(λ) = {λ}
• a Î S are all regular expressions. L(a) = {a}
Regular Languages: Cont..
• If r1 and r2 are regular expressions (REs).

( ) ( )
• r1 + r2 is R.E., then L r1 + r2 = L r1 È L r2 ( )
L(r1)ÈL(r2) = {w | w Î L(r1) or w Î L(r2)}

• r1.r2 is R.E., then L(r1 × r2 ) = L(r1 ) L(r2 )


L(r1).L(r2) = {w1.w2 : w1Î L(r1) and w2 Î L(r2)}

• r1* is R.E., then L(r1 *) = ( L(r1 )) *


(L(r1))* = L(r1)0 È L(r1)1 È L(r1)2 È L(r1)3 È …

• (r1) is R.E., then L((r1 )) = L(r1 )


Regular Expression to Regular Language

(a + b ) × a *
Regular Expression:

L((a + b ) × a *) = L((a + b )) L(a *)


= L(a + b ) L(a *)
= ( L(a ) È L(b )) ( L(a )) *
= ({a} È {b}) ({a}) *
= {a, b}{l , a, aa, aaa,...}
= {a, aa, aaa,..., b, ba, baa,...}
RE to RL

r = (a + b ) * (a + bb )
L(r ) = {a, bb, aa, abb, ba, bbb,...}
r = (aa ) * (bb ) * b
L(r ) = {a b b : n, m ³ 0}
2n 2m

r = (0 + 1) * 00 (0 + 1) *
L(r ) = { all strings containing substring 00 }
RE & RL: Example
• λ* is RE, then the language
L(λ *) = {λ}* = {λ}

• f* is RE, then the language


L(f*) = {f}* = { }

• 0* is RE, then the language


L(0*) = {0}* = {λ, 0, 00, 000, 0000, …}

• (0+1).(00+11) is RE, then the language


L( (0+1).(00+11) ) = {0, 1}{00, 11} = {000, 011, 100, 111}

• (10+01) * is RE, then the language


L((10+01)*) = {10, 01}* = {λ, 10, 1010, 101010, …, 01, 0101, 010101, …,
1001, 100101, 10010101, …, 0110, 011010, 01101010, …}
RE & RL: Example
• Let L be a language over {a, b}, each string in L contains the substring bb
• L = {a, b}*{bb}{a, b}*

• L is regular language (RL). Why?


• {a} and {b} are RLs
• {a, b} is RL
• {a, b}* is RL
• {b}{b} = {bb} is also RL
• Then L = {a, b}*{bb}{a, b}* is RL
RE & RL: Example
• Let L be a language over {a, b}, each string in L
• begins and ends with an a AND contains at least one b
• L = {a}{a, b}*{b}{a, b}*{a}

• L is regular language (RL). Why?


• {a} and {b} are RLs
• {a, b} is RL
• {a, b}* is RL
• Then L = {a}{a, b}*{b}{a, b}*{a} is RL
RL - Example
• The RE (b + ab*a)*ab* represents the strings over {a, b} with an
odd number of a’s
• Note: this is a set equality; to prove it you have to show the
following:
• strings with an odd number of a’s are in this language; and
• any string in this language has an odd number of a’s.
RE & RL: Example
• Let å= {a, b}
• RE a|b è L = {a, b}
• RE (a|b)(a|b) è L = {aa, ab, ba, bb}
• RE aa|ab|ba|bb same as above
• RE a* è L = {l, a , aa, aaa, …}
• RE (a|b)* è L = set of all strings of a’s and b’s including l
• RE (a*b*)* è same as above
• RE a|a*b è L = {a,b,ab,aab,aaab, …}
RE & RL
• 01*
• {0, 01, 011, 0111, …..}

• (01*)(01)
• {001, 0101, 01101, 011101, …..}

• (0 | 1)*
• {0, 1, 00, 01, 10, 11, …..}
• i.e., all strings of 0 and 1

• (0 | 1)* 00 (0 | 1)*
• {00, 1001, …..}
• i.e., all 0 and 1 strings containing a “00”
EQUIVALENT REs
• Two regular expressions r and s are equivalent (r=s), if and only if r and s
represent/generate the same language.

• Example-:
• r = a|b, s = b|a è r = s Why?
• Since L(r) = L(s) = {a, b}

• RE = (a)|((b)*(c)) is equivalent to a + b*c


EQUIVALENT REs
• Examples,
• (a*b*)* = (a+b)*
• (a+b)*ab(a+b)*+b*a* = (a+b)*
• First equality rather clear.
• For the second equality, note that (a+b)* denotes strings over a and b, that a string
either contains ab or it doesn’t; the first half of the left-hand expression describes the
strings that contain the substring ab and the second half describes those that don’t; the +
says “take the union”.
Regular Expressions: Exercise
• Construct a RE over S={0,1} such that
• It does not contain any string with two consecutive “0”s
• It has no prefix with two or more “0”s than “1” nor two or more “1”s than “0”
• The set of all strings ending with “00”
• The set of all strings with 3 consecutive 0’s
• The set of all strings beginning with “1”, which when interpreted as a binary no., is divisible by 5
• The set of all strings with a “1” at the 5th position from the right
• The set of all strings not containing 101 as a sub-string

• Construct a RE for the set {anbm: n >=3, m is even}.


• Construct a RE for the set {anbm: n >=4, m <= 3}.
• Construct a RE for the set {w: |w| mod 3 =0}.
• Construct a RE for the set {w: |w| mod 3 = 1}
Regular Expression to NFA
Regular Language
• A language L is known as regular if and only if it is recognized by a finite
accepter (FA).
• Language L is regular if and only if it is recognized by a DFA. (??)
• Language L is regular if and only if it is recognized by an NFA. (??)

• A language L is known as regular if and only if it is described by a regular


expression (RE).
• A language L is recognized by a FA if and only if L is described by a regular
expression.

How to show that “A given language is regular ?”


Connection Between RE & RL
• A language L is called regular if and only if there exists some DFA M such that
L = L(M).

• Since a DFA has an equivalent NFA, then


• A language L is called regular if and only if there exists some NFA N such that L = L(N).

• If we have a RE r, we can construct an NFA that accept L(r).


Connection Between RE & RL
NFAs for Primitive Regular Expression
1. For regular expression Æ, construct NFA

L(Æ) = { } = Æ
start q0 qf

2. For regular expression !, construct NFA


start
q0 ! qf L(!) = {!}

3. For regular expression a Î S, construct NFA

start a
q0 qf L (a)= {a}
Connection Between RE & RL
If r1 and r2 are regular expressions, Mr1 and Mr2 are their NFAs.
Then, r1 + r2 has NFA. Convert Mr1 into NFA with single final state.
Convert Mr2 into NFA with single final state.

! Mr1 !
start
qi qf
! !
Mr2
L( (r1 + r2 ) ) = L(Mr1) È L(Mr2)

where qi and qf are new initial / final states, and !-moves are introduced from qi to the old
start states of Mr1 and Mr2 as well as from all of their final states to qf.
Connection Between RE & RL
If r1 and r2 are regular expressions, Mr1 and Mr2 are their NFAs.
Then, r1.r2 has NFA.
Convert Mr1 into NFA with single final state.
Convert Mr2 into NFA with single final state.

L( (r1.r2 ) ) = L(Mr1).L(Mr2)

start
qi ! ! ! qf
Mr1 Mr2

where qi is the new initial state of Mr1 and qf is the new final state of Mr2.
!-move is introduced from final state of Mr1 to initial state of Mr2 .
Connection Between RE & RL
If r1 is a regular expressions and Mr1 its NFA,
(r1)* (Kleene star) has NFA:
L( (r1)* ) = ( L(r1) )*
!

start
qi ! ! qf
Ms

where : qi is new start state and qf is new final state


!-move qi to qf (to accept null string)
!-moves qi to old start, old final(s) to qf
!-moves old final(s) to qf
!-move old final to old start (WHY? Repetition)
Example-1
• Build an NFA-e that accepts r1 = (a|b)*ba
• The RE r1 consists of a, b, ba and a|b

start a

start b

start b ! a
q1

a
!
!
start

! b
!
Example-1
• Build an NFA-e that accepts (a|b)*ba

(a|b)*
!

a
!
! !
!

! b !

!
Example-1
• Build an NFA-e that accepts (a|b)*ba

a
!
! !
!

! b !

!
b ! a
Example-2
R.E. a ( b | c )*
1. a, b, & c S0
a
S1 S0
b
S1 S0
c
S1

b
e S1 S2 e

2. b | c S0 S5
c
e S3 S4 e

e
b
e S2 S3 e
S0
e S1 S6
e S7
e c
3. ( b | c )* S4 S5 e
e
Example-2
4. a ( b | c )*

e e e
b
e S4 S5 e
S0
a
S1 e
S2 S3 S8 S9
e c
S6 S7 e
e

b|c

a
S0 S1
Example-3
NFA for : a | abb | a*b+
NFA’s :
a start a
1 2

abb start a b b
3 4 5 6

a b
a*b+ start
7 8
b
Example-3
NFA for : a | abb | a*b+

! a
1 2

start ! a b b
0 3 4 5 6

a b
!
7 8
b
Example-4

Regular Expression: (ab*c) | (a(b|c*))


Example-4

e
a e b e c
2 3 4 5 6 7
e e
e
17
1 b
10 11
e e e
e e
a e c e
8 9 12 13 14 15 16

e
Construct NFAs for RE over S = {0,1}
• Strings of even length, L={00,01,10,11} *
• (00+01+10+11) * or
• ((0+1)(0+1))*

• Strings of length 6, L={aÎS*| the length of a is 6}


• 000000+….+111111
• (0+1)(0+1) (0+1)(0+1) (0+1)(0+1) =(0+1)6

• Strings of length 6 or less, L={aÎS*| the length of a is less than or


equal to 6}
• λ +0+1+00+01+10+11….+111111
• (0+1+ λ)6
Construct NFAs for RE over S = {0,1}
• {aÎS*| a is a binary number divisible by 4}
• (0+1)*00

• {aÎS*| a does not contain 11}


• (0+10)* (1+ λ)

• {aÎS*| a contains odd number of 1’s}


• 0*(10*10*)*10*

• {aÎS*| any two 0’s in a are separated by three 1’s}


• 1*(0111)*01* + 1*

• Strings over the alphabet {a, b} with exactly three b's


• a*ba*ba*ba*

• Strings over the alphabet {a, b, c} containing (at least once) bc


• (a|b|c)*bc(a|b|c)*
Construct FAs for RE over S = {a, b}

• Construct NFA for the language L(ab*aa + bba*ab)


• Construct NFA for the language L( (a + b)*b(a + bb)* )
• Construct NFA for the set {anbm: n >=3, m is even}.
• Construct NFA for the set {anbm: n >=4, m <= 3}.
• Construct NFA for the set {w: |w| mod 3 =0}.
• Construct NFA for the set {w: |w| mod 3 = 1}

• Construct DFA for the language L( ab*a* ) ∪ L( (ab)*ba )


• Construct DFA for the language L( ab*a* ) ∩ L( (ab)*ba )
• Find the minimal DFA for the language L( a*bb ) ∩ L( ab*ba )
NFA to Regular Expression
NFA to RE
• If L is accepted by some NFA-e, then L is represented by some regular
expression.
• A regular expression for an NFA-e consists of labels of all the walks from initial state
(q0) to final state (s) qf.
• The computation of labels of all the walks does not look too difficult but it is
complicated by the existence of cycles (The cycles can be traversed arbitrarily, in any
order).
Generalized Transition Graph
• A generalized transition graph (GTG or Expression graph) is like a transition
diagram but it can have regular expressions as labels on arcs
• An NFA-e is a GTG.
• An NFA is a GTG.
• A DFA is a GTG.
Complete GTG
• A complete GTG is a graph in which all edges are present.
• A complete GTG with |V| vertices has exactly |V|2 edges.

• If a GTG has some edges missing


• Add edges with label f.

Incomplete GTG
Complete GTG

Incomplete GTG

Complete GTG
Complete GTG

Incomplete GTG

Complete GTG
GTG Reduction to Regular Expression
• A GTG G can be reduced to one GTG G’ with just two states (Initial and final
states)
• If we reduce an NFA-e in this way, the arc label then corresponds to the
regular expression representing it.
RE for GTG
• For two-state complete GTG, the Regular Expression is given as.
r = (r1)*r2 (r4 + r3(r1)*r2)*

• The regular expression r covers all possible paths from initial state to final
state.
• First path q0 to qf (Self Loop + Direct path from q0 to qf )
OR
• Second path q0 to qf (Self Loop + Direct path from q0 to qf + Indirect path (qf to q0 to qf))
Example

Convert the labels


Convert to into RE
Complete GTG

r = (a)*(a+b) ( c + f(a)*(a+b) )*
r = (a)*(a+b) ( c + f)*
r = a*(a|b) c*

r = (r1)*r2 (r4 + r3(r1)*r2)*


RE for GTG
• When a GTG has more than two states (initial and final states are distinct).
• Add missing edges in order to make it complete GTG
• Find an equivalent GTG by removing one state at a time.
• Remove non-final and non-initial states only.
• Next, find its regular expression.
RE for GTG
• Remove state q2
• Consider all paths from q1 to q3
• q1 to q1
• q1 to q3
• q3 to q1
• q3 to q3

• Regular Expressions for


• Path q1 to q1 is e + af*b
• Path q1 to q3 is h + af*c
• Path q3 to q1 is i + df*b
• Path q3 to q3 is g + df*c
• The above regular expressions becomes labels
for transitions.
RE for GTG
• After removal of state q2
• Find RE
RE for GTG
• Find RE

• r = (r1)*r2 (r4 + r3(r1)*r2)*

• r = (e + af*b )*(h + af*c) ( (g + df*c) + (i + df*b) (e + af*b )*(h + af*c) )*


RE for GTG: Example
b b
a
q0 q1 a, b q2
b
Covert to
Transitions into RE

b b
Transition labels a
are regular q0 q1 a + b q2
expressions
b
RE for GTG: Example
Covert to
Complete GTG

Remove q1

Simplify It
RE for GTG: Example

Find regular expression

r = (r1)*r2 (r4 + r3(r1)*r2)*

r = (bb*a)*(bb*(a+b)) ( b + f(bb*a)*(bb*(a+b)) )*

r = (bb*a)*(bb*(a+b)) (b+ f)*

r = (bb*a)*(bb*(a|b)).b*
NFA to RE :Procedure
• Step-1:
• Convert NFA N into NFA N’ with single final State distinct from its initial state.
• Step-2:
• Convert NFA N’ into complete GTG G.
• Let rij stand for the label of the edge from qi to qj.
• Step-3:
• If the GTG has only two states, with qi as its initial state and qj its final state.
• The associated regular expression is r.

r = (rii)* rij (rjj + rji . (rii)* . rij )*


NFA to RE :Procedure
• Step-4:
• If the GTG has three states, with initial state qi, final state qj and third state qk,
• Introduced new edges, labeled rpq + rpk (rkk)*rkq for p = i, j; q = i, j.
• Remove vertex qk and its associated edges.
NFA to RE :Procedure
• Step-5:
• If the GTG has four or more states, pick a state qk to be removed.
• Apply rule 4 for all pairs of states (qi, qj), i ≠ k, j ≠k.
At each step apply the rules
r+f =r
r. f = f.r = f
f *= l
• Step-6:
• Repeat step 3 to 5 until correct regular expression is obtained.
NFA to RE :Procedure

Initial Transition graph Resulting GTG


q0

qf

Repeat the process until two states are left.


Find RE for NFA: Example-1
Example-1

Convert to
Complete GTG
Example-1

Remove State OE

Considerable Paths
EE to EE
EE to OO
OO to EE
OO to OO
EO to EO
Example-1

Remove State OO

Considerable Paths
EE to EE
EE to EO
EO to EE
EO to EO
Example-1

r = (aa+ab(bb)*ba)*(b+ ab(bb)*a) .
Find RE (a(bb)*a + (b+a(bb)*ba)(aa+ab(bb)*ba)*(b+ ab(bb)*a) )*
Find RE for NFA: Example-2
Example-2
b b
a
q0 q1 a, b q2
b
Convert transitions into RE

b b
a
q0 q1 a + b q2
b
Example-2

Covert to
Complete GTG

Remove q1

Simplify It
Example-2

Find regular expression

r = (r1)*r2 (r4 + r3(r1)*r2)*

r = (bb*a)*(bb*(a+b)) ( b + f(bb*a)*(bb*(a+b)) )*

r = (bb*a)*(bb*(a+b)) (b+ f)*

r = (bb*a)*(bb*(a|b)).b*
Find RE for NFAs

0 0,1

1 1
Start 3 1 2

0
Theorem

Languages
Generated by
Regular Expressions
= Regular
Languages
Theorem - Part 1

Languages
Generated by Í Regular
Languages
Regular Expressions

1. For any regular expression r


the language L(r ) is regular
Theorem - Part 2

Languages
Generated by Ê Regular
Languages
Regular Expressions

2. For any regular language L there is


a regular expression r with L(r ) = L
Proof - Part 1

1. For any regular expression r


the language L(r ) is regular

Proof by induction on the size of r


Induction Basis
Æ, l , a
• Primitive Regular Expressions:
NFAs

L( M1) = Æ = L(Æ)

regular
L( M 2 ) = {l} = L(l )
languages

a
L( M 3 ) = {a} = L(a )
Inductive Hypothesis

• Assume r1 and r2 are regular expressions.


• L(r1 ) and L ( r2 )are regular languages
Inductive Step
L(r1 + r2 )
• We will prove:

L(r1 × r2 )
Are regular
Languages
L(r1 *)

L((r1 ))
• By definition of regular expressions:

L(r1 + r2 ) = L(r1 ) È L(r2 )

L(r1 × r2 ) = L(r1 ) L(r2 )

L(r1 *) = ( L(r1 )) *

L((r1 )) = L(r1 )
By inductive hypothesis we know:
L(r1) and L(r2 ) are regular languages

We also know:
There exists NFAs for the following languages
Union L(r1 ) È L(r2 )
Concatenation L(r1 ) L(r2 )
Star ( L(r1 )) *
• Therefore:

L(r1 + r2 ) = L(r1 ) È L(r2 )

Are regular
L(r1 × r2 ) = L(r1 ) L(r2 )
languages

L(r1 *) = ( L(r1 )) *
• And trivially:

L((r1)) is a regular language


Proof – Part 2
2. For any regular language L there is a regular
expression r with L(r ) = L

Proof by construction of regular expression.


• For any regular language, there exists NFA.
• We can construct RE for given NFA.

Therefore, for every regular language L


There exists RE r such that L = L(r).
Linear Grammars
Linear Grammars
A grammar G = (V, T, S, P) is said to be linear if all productions have at
most one variable at the right side and have exactly one variable on
left side.

Example:

S ® aSb S ® Ab
S ®l A ® aAb
A®l
A Non-Linear Grammar
Grammar G: S ® SS
S ®l
S ® aSb
S ® bSa

L(G ) = {w : na ( w) = nb ( w)}

Number of a in string w
Another Linear Grammar

G S®A
Grammar : A ® aB | l
B ® Ab

n n
L(G ) = {a b : n ³ 0}
Right-Linear Grammars
A grammar G = (V, T, S, P) is said to be right-linear if all productions are
of the form
A ® xB
or
A® x
Where
A, B ε V and x ε T*
Example
S ® abS
S ®a
Left-Linear Grammars
A grammar G = (V, T, S, P) is said to be left-linear if all productions are of
the form
A ® Bx
or
A® x
Where
A, B ε V and x ε T*
Left-Linear Grammars
Example

S ® Aab S ® Aab
A ® Aab | B A ® Aab | B
B®a B®a
Regular Grammars
Regular Grammars
A regular grammar is one that is either right-linear or left-linear grammar.

Examples:

G1 G2
S ® abS S ® Aab
S ®a A ® Aab | B
B®a
Observation
Regular grammars generate regular languages L(G2 ) = aab(ab) *
Examples:
G2
G1 S ® Aab
S ® abS A ® Aab | B
S ®a B®a
L(G1) = L( (ab)*a ) L(G1) = L(aab(ab)*)
Example
G1 G2 G3
S S1ab
S aaA | Abb S Aabc
S1 S1ab | S2
A aB A abBc S2 c
B b B c Linear, Left Linear
Linear Only Linear Only Regular

G5 G6
G4 S S1
S abS1
S A S1 abS1 | S2 S1 S2
A aB | l S2 c S2 a| b | c
B Ab Linear, Right Linear, Linear, Right Linear, Left Linear
Regular Regular
Linear only
Regular Grammars
Generate
Regular Languages
Theorem

Languages
Generated by
Regular Grammars
= Regular
Languages
Theorem - Part 1

Languages
Generated by
Regular Grammars
Í Regular
Languages

Any regular grammar generates


a regular language
Theorem - Part 2

Languages
Generated by
Regular Grammars
Ê Regular
Languages

Any regular language is generated by a regular grammar.


Proof – Part 1
Languages
Generated by
Regular Grammars
Í Regular
Languages

The language L (G ) generated by any regular grammar G is


regular.
• The Regular grammar is either Right-linear or Left-
linear.
The case of Right-Linear Grammars

Let G be a right-linear grammar

We will prove: L(G) is regular

Proof idea: We will construct NFA M such that

L( M ) = L(G )
Right-Linear Grammar To NFA
Grammar G is right-linear.
G = (V, T, S, P) = ({S, A, B}, {a, b}, S, P)

S ® aA | B
Example:
A ® aa B
B®b B|a
Right-Linear Grammar To NFA
Construct NFA M such that every state is a grammar variable:
M = (Q, {a, b}, δ, S, {VF})

A
Q = {S, A, B, VF} special
S VF final state
B
S ® aA | B
A ® aa B
B®b B|a
Right-Linear Grammar To NFA
Add edges for each production: Q = {S, A, B, VF}

S ® aA
a A
S VF
B
Right-Linear Grammar To NFA
Q = {S, A, B, VF}
S ® aA | B
a A
S VF
l
B
Right-Linear Grammar To NFA
A Q = {S, A, B, C, VF}
S ® aA | B a
a
A ® aa B
C
S a VF
l
B
Right-Linear Grammar To NFA
A Q = {S, A, B, C, VF}
S ® aA | B
a a
A ® aa B
C
B ® bB
S a VF
l
B
b
Right-Linear Grammar To NFA
A
S ® aA | B
a a
A ® aa B
C
B ® bB | a
S a VF
l a
M =(Q, Σ, δ, S, F)
B Q = {S, A, B, C, VF}
Σ = {a, b}
b F = {VF}
Right-Linear Grammar To NFA
d*(q0, aaaba)
= d( d*(q0, aaab), a)
= d( d ( d*(q0, aaa), b), a)
= d( d ( d ( d*(q0, aa), a), b), a)
= d( d ( d ( d (d*(q0, a), a), a), b), a)
= d( d ( d ( d (d (d*(q0, l), a), a), a), b), a)
= d( d ( d ( d (d (q0, a), a), a), b), a)
= d( d ( d ( d (A, a), a), b), a)
= d( d ( d ( C, a), b), a)
= d( d ( B, b), a)
= d( B, a) = VF

S Þ aA Þ aaaB Þ aaabB Þ aaaba


NFA M Grammar G
S ® aA | B
A ® aa B
B ® bB | a

L(M) = L(G) = L( aaab*a + b*a )


Right-Linear Grammar To NFA: In General
A right-linear grammar G = (V, T, S, P)

has variables: V0 ,V1,V2 ,!

and productions: Vi ® a1a2 !amV j


or
Vi ® a1a2 !am
Right-Linear Grammar To NFA
We construct the NFA M such that: Σ = T

each variable Vi corresponds to a node:

V1
V0 V3

V2 V4
VF
Add one special state VF.
special
V0 is initial state and VF is final.
final state
Right-Linear Grammar To NFA

For each production: Vi ® a1a2 !amV j

we add transitions and intermediate states

Vi a1 a2 qj ……… am V
qi qk j

Add intermediate states qi, qj, …. qk to Q


Right-Linear Grammar To NFA
For each production: V
i ® a1a2 !am
we add transitions and intermediate nodes

Vi a1 a2 ……… am
q’i q’j q’k
VF

Add intermediate states q’i, q’j, …. q'k to Q


Resulting NFA
Resulting NFAM looks like this:

It holds that: L(G ) = L( M )


Find NFA
L = {w Î {a, b}* : w ends with the pattern aaaa}.

• Regular grammar: G = (V, T, S, P), where


Exercise-1
• V = {S, B, C, D}, T = {a, b},
• P = {S ® aS, S ® bS, S ® aB, B ® aC, C ® aD, D ® a}

L = {w Î {a, b}* : |w| is even}

Regular grammar: G = (V, Σ, S, P), where


V = {S, T }, Σ = {a, b}, Exercise-2
P = { S ® e , S ® aT , S ® bT, T ® aS, T ® bS}
Find NFA

Exercise-3
S®e A ® bA C ® aC
S ® aB A ® cA C ® bC
S ® aC A®e C®e
S ® bA B ® aB
S ® bC B ® cB
S ® cA B®e
S ® cB
The case of Left-Linear Grammars

Let G be a left-linear grammar

We will prove: L(G ) is regular

Proof idea:
We will construct a right-linear grammar G¢such that
R
L(G ) = L(G¢)
The case of Left-Linear Grammars
Since G is left-linear grammar.
The productions look like:

A ® Ba1a2 !ak

A ® a1a2 !ak
The case of Left-Linear Grammars
Let G be left-linear grammar.
• Construct right-linear grammar G’ by reversing the production rules
• Construct NFA N for right-linear grammar G’
• Convert NFA N into NFA N’ with single final state
• Reverse direction of all edges (Now NFA N’’)
• Mark initial state as final state and final state as initial state.
The case of Left-Linear Grammars
Construct right-linear grammar G’ by reversing the production rules
Left A ® Ba1a2 !ak
linear
G
A → Bv

Right
G¢ A ® ak !a2a1B
linear
R
A®v B
The case of Left-Linear Grammars
Construct right-linear grammar G’ by reversing the production rules
Left
G A ® a1a2 !ak
linear
A®v

Right
linear G¢ A ® ak !a2a1
R
A®v
The case of Left-Linear Grammars
G Left-Linear G’ Right-Linear
S Aabc S cbaA
A Ab | B A bA | B
B Cac B caC
C Cb | l C bC | l
The case of Left-Linear Grammars
G’ Right-Linear
S cbaA Construct NFA N for right-linear grammar G’
A bA | B
B caC
C bC | l
S c D
b
E a A l B c
F
a
b
M =(Q, Σ, δ, S, F) l C
Q = {S, A, B, C,D, E, F, VF} VF
Σ = {a, b, c} b
F = {VF}
The case of Left-Linear Grammars

Convert NFA N into NFA N’ with single final state


Reverse direction of all edges (Now NFA N’’)
Mark initial state as final state and final state as initial
state.

c b a A l B c a l VF
S D E F C

b
b
The case of Left-Linear Grammars
G Left-Linear
S Aabc
A Ab | B L(G) = L(b*acb*abc)
B Cac
C Cb | l

c b a A l B c a l VF
S D E F C

M b
L(M) = L( b*acb*abc) b
The case of Left-Linear Grammars

R
It is easy to see that: L(G ) = L(G¢)
Since G¢ is right-linear, we have:
R
L(G¢) L(G¢) L(G )
Regular Regular Regular
Language Language Language
Find NFA
• Regular grammar: G = (V, T, S, P), where
• V = {S, B, C, D}, T = {a, b},
Exercise-1 • P = {S ® Sa, S ® Sb, S ® Ba, B ® Ca, C ® Da, D ® a}

Regular grammar: G = (V, Σ, S, P), where


V = {S, T }, Σ = {a, b},
P = { S ® e , S ® Ta , S ® Tb, T ® Sa, T ® Sb} Exercise-2
Find NFA

Exercise-3
S®e A ® Ab C ® Ca
S ® Ba A ® Ac C ® Cb
S ® Ca A®e C®e
S ® Ab B ® Ba
S ® Cb B ® Bc
S ® Ac B®e
S ® Bc
Proof - Part 2
Languages
Generated by
Regular Grammars
Ê Regular
Languages

Any regular language L is generated by some regular


grammar G
Any regular language L is generated
by some regular grammar G

Proof idea:
Let M be the NFA with L = L(M ).
Construct from M a regular grammar G
such that

L( M ) = L(G )
NFA-to-RG
Since L is regular
there is an NFA M such that L = L(M )

b
Example:
M a
a
q0 q1 q2
l b
L = L( ab*ab(b*ab)* ) q3
L = L(M )
NFA-to-RG
Convert M to a right-linear grammar G

q0 ® aq1
NFA-to-RG
Convert M to a right-linear grammar G

q0 ® aq1
q1 ® bq1
q1 ® aq2
NFA-to-RG
Convert M to a right-linear grammar G

q0 ® aq1
q1 ® bq1
q1 ® aq2
q2 ® bq3
NFA-to-RG
Convert M to a right-linear grammar G

q0 ® aq1
q1 ® bq1
q1 ® aq2
q2 ® bq3
q3 ® q1
q3 ® l
NFA-to-RG
q0 ® aq1
G q1 ® bq1
q1 ® aq2
q2 ® bq3
q3 ® q1
L(M) = L( ab*ab(b*ab)* )
q3 ® l

L(G) = L( ab*ab(b*ab)* ) L(G ) = L( M ) = L


NFA-to-RG: In General

q a p
For any transition:

Add production: q ® ap

variable terminal variable


NFA-to-RG: In General

For any final state: qf

Add production: qf ®l
NFA-to-RG: In General

Since G is right-linear grammar.

Then, G is also a regular grammar

with L(G ) = L( M ) = L
Find Regular Grammar(Right-Linear)
Find Regular Grammar(Right-Linear)
Any regular language L is generated
by some regular grammar G

Proof idea:
Let M be the NFA with L = L(M ).
Construct from M a regular grammar G
such that

L( M ) = L(G )
NFA-to-RG (Left Linear Grammar)
Since L is regular
there is an NFA M such that L = L(M )

b
Example:
M a
a
q0 q1 q2
l b
L = L( ab*ab(b*ab)* ) q3
L = L(M )
NFA-to-RG (Left Linear Grammar)

b
M’
Construct NFA M’ with single a a
final state q0 q1 q2
Reverse direction of all
edges (Now NFA N’’) l b
Mark initial state as final
state and final state as q3
initial state.
NFA-to-RG (Left Linear Grammar)
M’
Find Right-Linear Grammar G’

q3 bq2
q2 aq1
q1 bq1
q1 aq0
q1 q3
q0 l
NFA-to-RG (Left Linear Grammar)
M’
Find reverse Linear Grammar G

Reverse Linear Grammar G


q3 q2 b
q2 q1 a
q1 q1 b
q1 q0 a
q1 q3
q0 l
q3 is start variale
NFA-to-RG (Left Linear Grammar)

Left-Linear Grammar G
q3 q2 b
q2 q1 a
q1 q1 b
q1 q0 a
q1 q3
q0 l L = L( ab*ab(b*ab)* )
q3 is start variale
NFA-to-RG (Left Linear Grammar):In-general
Let L be regular language.
L is recognized by NFA M.
Construct NFA with single final state
Reverse direction of all edges
Mark initial state as final state and final state as initial state (Now NFA M’)
Find Right-linear grammar G’
Find reverse grammar G
G is Left-linear grammar.
Find Regular Grammar(Left-Linear)
Find Regular Grammar(Left-Linear)
Right-Linear Grammar to Left Linear Grammar
Let G be right-linear.
Construct NFA N for grammar G
Convert NFA N into NFA with single final state
Reverse direction of all edges
Mark initial state as final state and final state as initial state (Now NFA N’)
Find Right-linear grammar G’ from NFA
Find reverse grammar G using G’
G is Left-linear grammar.
Right-Linear Grammar to Left Linear Grammar
S ® aA | B A Construct NFA N for grammar G
Convert NFA N into NFA with single final state

A ® aa B a a
B ® bB | a C
S a VF
l a
B
b
Right-Linear Grammar to Left Linear Grammar
Construct NFA N for grammar G
S ® aA | B A Convert NFA N into NFA N’ with single final state
Reverse direction of all edges
Mark initial state as final state and final state as initial state
A ® aa B a a Find Right-linear grammar G’ from NFA

B ® bB | a C
VF aB
S a VF B bB
l a B S
B B aC
C aA
b A aS
S !
Right-Linear Grammar to Left Linear Grammar
S ® aA | B A
Find reverse grammar G using G’

G’
A ® aa B a a
B ® bB | a C
S a VF
VF Ba l a
B Bb
B S
B
B Ca
C Aa
b
A Sa
S !
Left-Linear Grammar G: V is start variable
Left-Linear Grammar to Right-Linear Grammar

Let G be left-linear.
Find reverse grammar G’
Construct NFA N for grammar G’
Convert NFA N into NFA with single final state
Reverse direction of all edges
Mark initial state as final state and final state as initial state (Now NFA
N’)
Find Right-linear grammar from NFA
Left-Linear Grammar to Right-Linear Grammar
G Left-Linear G’ Right-Linear
S Aabc S cbaA Find reverse grammar G’

A Ab | B A bA | B
B Cac B caC
C Cb | l C bC | l
Left-Linear Grammar to Right-Linear Grammar
G’ Right-Linear
S cbaA Construct NFA N for right-linear grammar G’
A bA | B
B caC
C bC | l
S c D
b
E a A l B c
F
a
b
l C
VF
b
Left-Linear Grammar to Right-Linear Grammar

Convert NFA N into NFA with single final state


Reverse direction of all edges
Mark initial state as final state and final state as initial
state.

c b a A l B c a l VF
S D E F C

b
b
Left-Linear Grammar to Right-Linear Grammar
Find Right-linear grammar.

VF C C bC
C aF F cB
B A A bA
A aE E bD
D cS S l VF –Start Variable

c b a A l B c a l VF
S D E F C

b
b
M
L(M) = L( b*acb*abc)
Left-Linear Grammar to Right-Linear Grammar
Standard Representation for Regular Languages
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy