• No results found

Regular Expression & Regular Languages

N/A
N/A
Protected

Academic year: 2022

Share "Regular Expression & Regular Languages"

Copied!
169
0
0

Loading.... (view fulltext now)

Full text

(1)

Regular Expression & Regular Languages

(2)

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 ?

(3)

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.

(4)

Rules for Specifying Regular Expressions:

()

* .

|

Precedence of Operators

PRECEDENCE HIGHEST .

.. ..

LOWEST

(5)

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)*

(6)

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 ?

(7)

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 ?

(8)

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 r

n

.

n times

(9)

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)*

(10)

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

(11)

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*

(12)

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*

(13)

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+)+

(14)

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)*

(15)

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”

(16)

Regular Expressions : Example

What languages do the following RE represent?

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

(17)

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}

(18)

Regular Languages: Cont..

• If r

1

and r

2

are regular expressions (REs).

r

1

+ r

2

is R.E., then

L(r

1

) È L(r

2

) = {w | w Î L(r

1

) or w Î L(r

2

)}

r

1

.r

2

is R.E., then

L(r

1

).L(r

2

) = {w

1

.w

2

: w

1

Î L(r

1

) and w

2

Î L(r

2

)}

r

1*

is R.E., then

(L(r

1

))

*

= L(r

1

)

0

È L(r

1

)

1

È L(r

1

)

2

È L(r

1

)

3

È …

(r

1

) is R.E., then

( r 1 r 2 ) L ( ) r 1 L ( ) r 2

L + = È

( r 1 r 2 ) L ( ) ( ) r 1 L r 2

L × =

( ) r 1 * ( L ( ) r 1 ) *

L =

( )

( ) r 1 L ( ) r 1

L =

(19)

Regular Expression to Regular Language

Regular Expression: ( a + b ) × a *

( )

( a b a * )

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

L +

=

( ) ( )

( L a È L b ) ( ) ( L a ) *

=

{ } { }

( a È b ) { } ( a ) *

=

{ a , b }{ l , a , aa , aaa ,... }

=

{ a , aa , aaa ,..., b , ba , baa ,... }

=

(20)

RE to RL

* ) 1 0

( 00

* ) 1 0

( + +

= ) r

( r

L

= { all strings containing substring 00 }

( ) ( ) aa bb b

r = * *

( ) r = { a 2 b 2 b : n , m ³ 0 }

L n m

( a b ) ( a bb )

r = + * +

( ) { r a , bb , aa , abb , ba , bbb ,... }

L =

(21)

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, …}

(22)

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

(23)

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

(24)

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.

(25)

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, …}

(26)

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”

(27)

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

(28)

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

(29)

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}

(30)

Regular Expression to NFA

(31)

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 ?”

(32)

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

(33)

Connection Between RE & RL

NFAs for Primitive Regular Expression

3. For regular expression a Î S, construct NFA

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

2. For regular expression

!

, construct NFA

!

start q0 qf L(!) = {!}

1. For regular expression Æ, construct NFA

start q0 qf L(Æ) = { } =

Æ

(34)

Connection Between RE & RL

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.

If r1 and r2 are regular expressions, Mr1 and Mr2 are their NFAs.

Then, r1 + r2 has NFA.

start

!

qi qf

!

Mr1

Mr2 !

!

L( (r1 + r2 ) ) = L(Mr1) È L(Mr2) Convert Mr1 into NFA with single final state.

Convert Mr2 into NFA with single final state.

(35)

Connection Between RE & RL

If r1 and r2 are regular expressions, Mr1 and Mr2 are their NFAs.

Then, r1.r2 has NFA.

Mr1

!

start qi ! Mr2 ! qf

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 .

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

Convert Mr1 into NFA with single final state.

Convert Mr2 into NFA with single final state.

(36)

Connection Between RE & RL

qf Ms

!

start qi !

!

!

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) If r1 is a regular expressions and Mr1 its NFA,

(r1)* (Kleene star) has NFA:

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

(37)

Example-1

• Build an NFA- e that accepts r

1

= (a|b)

*

ba

The RE r

1

consists of a, b, ba and a|b

start a

! a

start b q1

start b

a

b

start !

!

!

!

(38)

Example-1

• Build an NFA- e that accepts (a|b)

*

ba (a|b)

*

a

b

!

!

!

!

! !

!

!

(39)

Example-1

• Build an NFA- e that accepts (a|b)

*

ba

! a b

! a

b

!

!

!

!

! !

!

!

(40)

Example-2

R.E. a ( b | c )* 1. a, b, & c

2. b | c

3. ( b | c )*

S0 a S1 S0 b S1 S0 c S1

S1 b S2 S3 c S4

S0 S5

S2 b S3 S4 c S5

S1 S6

S0 S7

e e

e e

e e

e

e e

e

e e

(41)

Example-2

4. a ( b | c )

*

S0 a S1 e S4 S5

b

S6 c S7

S3 S8

S2 S9

e e

e e

e e

S0 a S1 b | c

e e

(42)

Example-3

NFA for : a | abb | a*b+

a abb

a*b+

NFA’s :

start

start

start

1

b b

b b

a

a a

2

3 4 5

8 7

6

(43)

Example-3

NFA for : a | abb | a*b+

!

!

! 0

b b

b b

a

a a

2

3 4 5

8 7

6 1

start

(44)

Example-4

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

(45)

Example-4

b e

e e e

a c

c e

e e e

e e

b

a

e

e e

e

1

6 5

4 3

8

2

10

9 12 13 14

11

15

7

16 17

(46)

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

(47)

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)*

(48)

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 )

(49)

NFA to Regular Expression

(50)

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

(51)

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.

(52)

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

(53)

Complete GTG

Incomplete GTG

Complete GTG

(54)

Complete GTG

Incomplete GTG

Complete GTG

(55)

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.

(56)

RE for GTG

• For two-state complete GTG, the Regular Expression is given as.

r = (r

1

)*r

2

(r

4

+ r

3

(r

1

)*r

2

)*

• 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))

(57)

Example

r = (r

1

)*r

2

(r

4

+ r

3

(r

1

)*r

2

)*

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

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

r = a*(a|b) c*

Convert to Complete GTG

Convert the labels into RE

(58)

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.

(59)

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.

(60)

RE for GTG

• After removal of state q

2

• Find RE

(61)

RE for GTG

• Find RE

• r = (r

1

)*r

2

(r

4

+ r

3

(r

1

)*r

2

)*

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

(62)

b a + a b

b

q 0 q 1 q 2

b a , a b

b

q 0 q 1 q 2

b

b

Transition labels are regular

expressions

RE for GTG: Example

Covert to

Transitions into RE

(63)

RE for GTG: Example

Covert to

Complete GTG

Remove q1

Simplify It

(64)

RE for GTG: Example

Find regular expression r = (r

1

)*r

2

(r

4

+ r

3

(r

1

)*r

2

)*

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*

(65)

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 )*

(66)

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.

(67)

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.

(68)

Repeat the process until two states are left.

Initial Transition graph Resulting GTG

NFA to RE :Procedure

q0

qf

(69)

Find RE for NFA: Example-1

(70)

Example-1

Convert to Complete GTG

(71)

Example-1

Remove State OE

Considerable Paths EE to EE

EE to OO OO to EE OO to OO EO to EO

(72)

Example-1

Remove State OO

Considerable Paths EE to EE

EE to EO EO to EE EO to EO

(73)

Example-1

Find RE

r = (aa+ab(bb)*ba)*(b+ ab(bb)*a) .

(a(bb)*a + (b+a(bb)*ba)(aa+ab(bb)*ba)*(b+ ab(bb)*a) )*

(74)

Find RE for NFA: Example-2

(75)

b a + a b

b

q 0 q 1 q 2

b a , a b

b

q 0 q 1 q 2

b

b

Example-2

Convert transitions into RE

(76)

Example-2

Covert to

Complete GTG

Remove q1

Simplify It

(77)

Example-2

Find regular expression r = (r

1

)*r

2

(r

4

+ r

3

(r

1

)*r

2

)*

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*

(78)

Find RE for NFAs

3

Start 1 1 1 2

0

0

0,1

(79)

Theorem

Languages

Generated by

Regular Expressions

Regular

Languages

=

(80)

Theorem - Part 1

r )

( r L

1. For any regular expression

the language is regular Languages

Generated by

Regular Expressions

Regular

Languages

Í

(81)

Theorem - Part 2

Languages

Generated by

Regular Expressions

Regular

Languages

Ê

L

r L ( r ) = L 2. For any regular language there is

a regular expression with

(82)

Proof - Part 1

r )

( r L

1. For any regular expression

the language is regular

Proof by induction on the size of r

(83)

Induction Basis

• Primitive Regular Expressions: Æ , l , a

NFAs

) (

)

( M 1 = Æ = L Æ L

) (

} {

)

( M 2 l L l

L = =

) ( }

{ )

( M 3 a L a

L = =

regular

languages

a

(84)

Inductive Hypothesis

• Assume and are regular expressions.

• and are regular languages r 1 r 2 )

( r 1

L L ( r 2 )

(85)

Inductive Step

• We will prove: ( )

( )

( ) ( ) ( ) 1

1

2 1

2 1

* r L

r L

r r

L

r r

L

× +

Are regular

Languages

(86)

• By definition of regular expressions:

( ) ( ) ( )

( ) ( ) ( ) ( ) ( ( ) )

( )

( ) 1 ( ) 1

1 1

2 1

2 1

2 1

2 1

*

*

r L r

L

r L r

L

r L r

L r

r L

r L r

L r

r L

=

=

=

×

È

=

+

(87)

) ( r 1

L L ( r 2 )

By inductive hypothesis we know:

and are regular languages

There exists NFAs for the following languages

( ) ( ) ( ) ( )

( )

( 1 1 ) * 2

2 1

r L

r L r

L

r L r

L È

Union

Concatenation Star

We also know:

(88)

• Therefore:

( ) ( ) ( )

( ) ( ) ( ) ( ) 1 * ( ( ) 1 ) *

2 1

2 1

2 1

2 1

r L r

L

r L r

L r

r L

r L r

L r

r L

=

=

×

È

= +

Are regular

languages

(89)

• And trivially:

)) (( r 1

L is a regular language

(90)

Proof – Part 2

r L ( r ) = L L

2. For any regular language there is a regular expression with

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

(91)

Linear Grammars

(92)

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:

l

®

®

® A

aAb A

Ab S

l

®

® S

aSb

S

(93)

A Non-Linear Grammar

bSa S

aSb S

S

SS S

®

®

®

®

l

Grammar : G

)}

( )

( :

{ )

( G w n w n w

L = a = b

Number of in string a w

(94)

Another Linear Grammar

Grammar :

Ab B

aB A

A S

®

®

®

l

|

} 0 :

{ )

( G = a b n ³

L n n

G

(95)

Right-Linear Grammars

A grammar G = (V, T, S, P) is said to be right-linear if all productions are of the form

xB A ®

x A ®

or

a S

abS S

®

®

Where

A, B ε V and x ε T*

Example

(96)

Left-Linear Grammars

A grammar G = (V, T, S, P) is said to be left-linear if all productions are of the form

x A ®

or Where

A, B ε V and x ε T*

Bx

A ®

(97)

Left-Linear Grammars

Example

a B

B Aab

A

Aab S

®

®

®

| a

B

B Aab

A

Aab S

®

®

®

|

(98)

Regular Grammars

(99)

Regular Grammars

A regular grammar is one that is either right-linear or left-linear grammar.

Examples:

a S

abS S

®

®

a B

B Aab

A

Aab S

®

®

®

|

G 1 G 2

(100)

Observation

Regular grammars generate regular languages Examples:

L(G

1

) = L( (ab)*a ) L(G

1

) = L(aab(ab)*)

a S

abS S

®

®

a B

B Aab

A

Aab S

®

®

®

|

* ) (

)

( G 2 aab ab

L =

G 1

G 2

(101)

Example

G

1

S aaA | Abb A aB

B b

Linear Only

G

2

S Aabc A abBc B c

Linear Only

G3 S S1ab

S1 S1ab | S2 S2 c

Linear, Left Linear Regular

G

4

S A

A aB | l B Ab

Linear only

G5 S abS1

S1 abS1 | S2 S2 c

Linear, Right Linear, Regular

G6 S S1

S1 S2

S2 a| b | c

Linear, Right Linear, Left Linear Regular

(102)

Regular Grammars Generate

Regular Languages

(103)

Theorem

Languages

Generated by

Regular Grammars

Regular

Languages

=

(104)

Theorem - Part 1

Languages

Generated by

Regular Grammars

Regular

Languages

Í

Any regular grammar generates

a regular language

(105)

Theorem - Part 2

Languages

Generated by

Regular Grammars

Regular

Languages

Ê

Any regular language is generated by a regular grammar.

(106)

Proof – Part 1

Languages

Generated by

Regular Grammars

Regular

Languages

Í

The language generated by any regular grammar is regular.

• The Regular grammar is either Right-linear or Left- linear.

) (G

L G

(107)

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

) (

)

( M L G

L =

(108)

Right-Linear Grammar To NFA

Grammar G is right-linear.

G = (V, T, S, P) = ({S, A, B}, {a, b}, S, P)

Example:

a B

b B

B aa

A

B aA

S

|

|

®

®

®

(109)

Right-Linear Grammar To NFA

Construct NFA M such that every state is a grammar variable:

M = (Q, {a, b}, δ, S, {V

F

})

a B

b B

B aa

A

B aA

S

|

|

®

®

®

S V F

A

B

special

final state

Q = {S, A, B, V

F

}

(110)

Right-Linear Grammar To NFA

Add edges for each production:

S V F

A

B

aA a

S ®

Q = {S, A, B, V

F

}

(111)

Right-Linear Grammar To NFA

S V F

A

B a

B aA

S ® |

l

Q = {S, A, B, V

F

}

(112)

Right-Linear Grammar To NFA

S V F

A

B a

l B

aa A

B aA

S

®

® | a

a

Q = {S, A, B, C, V

F

}

C

(113)

Right-Linear Grammar To NFA

S V F

A

B a

l bB

B

B aa

A

B aA

S

®

®

® |

a a

b

Q = {S, A, B, C, V

F

}

C

References

Related documents

Percentage of countries with DRR integrated in climate change adaptation frameworks, mechanisms and processes Disaster risk reduction is an integral objective of

The older folk describe small shifting cultivation periods when seeds were available and bartering with plains people of Anamalai and Kottur villages for rations in exchange

These gains in crop production are unprecedented which is why 5 million small farmers in India in 2008 elected to plant 7.6 million hectares of Bt cotton which

Planned relocation is recognized as a possible response to rising climate risks in the Cancun Adaptation Framework under the United Nations Framework Convention for Climate Change

• Recall a closure property is a statement that a certain operation on languages, when applied to languages in a class, produces a result that is also in that class.. • For

Businesses can play their part by decarbonising their operations and supply chains through continuously improving energy efficiency, reducing the carbon footprint of

Daystar Downloaded from www.worldscientific.com by INDIAN INSTITUTE OF ASTROPHYSICS BANGALORE on 02/02/21.. Re-use and distribution is strictly not permitted, except for Open

The candidates bearing the following Roll Numbers are declared to have passed the 2ND SEMESTER B.A.. College 198. Centre