0% found this document useful (0 votes)
57 views75 pages

SEM05-Reverse of A Regular Language

The reverse of a regular language is also a regular language. To prove this, we can construct a non-deterministic finite automaton (NFA) that accepts the reverse of the language by inverting the transitions of the NFA that accepts the original language. For example, the language L = {ab, ba} is regular, and its reverse LR = {ba, ab} is also regular since we can invert the transitions of the NFA that accepts L to get an NFA that accepts LR. Regular grammars generate regular languages, and any regular language can be generated by a regular grammar.

Uploaded by

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

SEM05-Reverse of A Regular Language

The reverse of a regular language is also a regular language. To prove this, we can construct a non-deterministic finite automaton (NFA) that accepts the reverse of the language by inverting the transitions of the NFA that accepts the original language. For example, the language L = {ab, ba} is regular, and its reverse LR = {ba, ab} is also regular since we can invert the transitions of the NFA that accepts L to get an NFA that accepts LR. Regular grammars generate regular languages, and any regular language can be generated by a regular grammar.

Uploaded by

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

Reverse of a Regular Language

Theorem:
R
The reverse L of a regular language L
is a regular language

Proof idea:
R
Construct NFA that accepts L :

invert the transitions of the NFA


that accepts L
2
Proof
Since L is regular,
there is NFA that accepts L

Example:
b
L  ab * ba
a

b a
3
Invert Transitions

a
b
a
4
Make old initial state a final state

a
b
a
5
Add a new initial state

a 

b 
a
6
Resulting machine accepts L R

L R is regular
L  ab * ba
b
L  b * a  ab
R

a

b
a
7
Grammars
Grammars
Grammars express languages
Example: the English language

sentence  noun _ phrase predicate


noun _ phrase  article noun
predicate  verb

9
article  a
article  the
noun  boy
noun  dog
verb  runs
verb  walks

10
A derivation of “the boy walks”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 the noun verb
 the boy verb
 the boy walks 11
A derivation of “a dog runs”:
sentence  noun _ phrase predicate
 noun _ phrase verb
 article noun verb
 a noun verb
 a dog verb
 a dog runs
12
Language of the grammar:
L = { “a boy runs”,
“a boy walks”,
“the boy runs”,
“the boy walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks” }
13
Notation
noun  boy
noun  dog

Variable Terminal
Production
or
rule
Non-terminal

14
Another Example
Grammar: S  aSb
S 

Derivation of sentence : ab

S  aSb  ab

S  aSb S 
15
Grammar: S  aSb
S 

Derivation of sentence : aabb

S  aSb  aaSbb  aabb

S  aSb S 
16
Other derivations:
S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb

17
Language of the grammar

S  aSb
S 

L  {a b : n  0}
n n

18
More Notation

Grammar G  V ,T , S , P 
V: Set of variables or nonterminals

T: Set of terminal symbols or terminals

S: Start variable or start symbol

P : Set of Production rules


19
Example
S  aSb
Grammar G:
S 

G  V ,T , S , P 

V  {S} T  {a, b} P  {S  aSb, S }

20
More Notation
Sentential Form:
A sentence that contains
variables and terminals

Example:
S  aSb  aaSbb  aaaSbbb  aaabbb

Sentential Forms sentence


21
*
We write: S  aaabbb

Instead of:

S  aSb  aaSbb  aaaSbbb  aaabbb

22
*
In general we write: w1  wn

If: w1  w2  w3    wn

23
*
By default: w  w

24
Example
Grammar Derivations
S  aSb *
S
S  *
S  ab
*
S  aabb
*
S  aaabbb

25
Example

Grammar Derivations
S  aSb 
S  aaSbb
S 


aaSbb  aaaaaSbbbbb

26
Another Grammar Example
Grammar G: S  Ab
A  aAb
A 

Derivations:
S  Ab  b
S  Ab  aAbb  abb
S  aAbb  aaAbbb  aabbb
27
More Derivations
S  Ab  aAbb  aaAbbb  aaaAbbbb
 aaaaAbbbbb  aaaabbbbb


S  aaaabbbbb

S  aaaaaabbbbbbb

S a b b
n n
28
Language of a Grammar

For a grammar G
with start variable : S

L(G )  {w : S  w}

String of terminals
29
Example
For grammar : G S  Ab
A  aAb
A 

L(G )  {a b b : n  0}
n n


Since: S a b b
n n

30
A Convenient Notation

A  aAb A  aAb | 
A 

article  a
article  a | the
article  the

31
Linear Grammars
Linear Grammars
Grammars with at most one variable at the right
side of a production

Examples:
S  aSb S  Ab
S  A  aAb
A 

33
A Non-Linear Grammar
Grammar G: S  SS
S 
S  aSb
S  bSa

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

34
Another Linear Grammar

Grammar G: SA
A  aB | 
B  Ab

L(G)  {a b : n  0}
n n

35
Right-Linear Grammars
A  xB
All productions have the form: or
A x

S  abS
Example: S  a

36
Left-Linear Grammars
A  Bx
All productions have the form: or
A x

S  Aab
Example: A  Aab | B

Ba
37
Regular Grammars
Regular Grammars
A regular grammar is any
right-linear or left-linear grammar

Examples: G1 G2
S  abS S  Aab
S a A  Aab | B
Ba
39
Observation
Regular grammars generate regular languages
Examples:
G1 G2
S  abS S  Aab
S a A  Aab | B
Ba
L(G1)  (ab) * a L(G2 )  aab(ab) *
40
Regular Grammars
Generate
Regular Languages
Theorem

Languages
Generated by
Regular Grammars
 Regular
Languages

42
Theorem - Part 1

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular grammar generates


a regular language

43
Theorem - Part 2

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular language is generated


by a regular grammar

44
Proof – Part 1
Languages
Generated by  Regular
Languages
Regular Grammars

The language L(G ) generated by


any regular grammar G is regular

45
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


with L( M )  L(G )

46
Grammar G is right-linear
Example: S  aA | B
A  aa B
Bb B|a

47
Construct NFA M such that
every state is a grammar variable: A
special
S B VF
final state

S  aA | B
A  aa B
Bb B|a 48
Add edges for each production:
a A
S VF
B

S  aA
49
a A
S VF

B

S  aA | B

50
A
a a

S a VF

B
S  aA | B
A  aa B
51
A
a a

S a VF

B
S  aA | B
b
A  aa B
B  bB 52
A
a a

S a VF
 a
S  aA | B B
A  aa B b
B  bB | a
53
A
a a

S a VF
 a
B
b

S  aA  aaaB  aaabB  aaaba


54
NFA M Grammar
A G
a S  aA | B
a
A  aa B
S a B  bB | a
 VF
a
B
L( M )  L(G ) 
b
aaab * a  b * a
55
In General
A right-linear grammar G

has variables: V0 ,V1,V2 ,

Vi  a1a2  amV j
and productions:
or

Vi  a1a2  am
56
We construct the NFA M such that:

each variable Vi corresponds to a node:


V1 V3
V0
VF
V2 special
V4
final state
57
Vi  a1a2  amV j
For each production:

we add transitions and intermediate nodes

Vi a1 a2 ………
am V
j

58
Vi  a1a2  am
For each production:

we add transitions and intermediate nodes

Vi a1 a2 ………
am
VF

59
Resulting NFA M looks like this:
a9

a2 a4
a1 V1 V3
a3 a5
V0
a3 a4
VF
a8 a9
V2 a5
V4

It holds that: L(G )  L( M ) 60


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 with L(G )  L(G)
R

61
Since G is left-linear grammar
the productions look like:

A  Ba1a2 ak

A  a1a2  ak

62
Construct right-linear grammar G
In G : A  Ba1a2 ak
A  vB

In G : A  ak  a2 a1B
Av B R
63
Construct right-linear grammar G
In G : A  a1a2  ak
Av

In G : A  ak  a2 a1
Av R
64
L(G )  L(G) R

It is easy to see that:

Since G is right-linear, we have:

L(G) L(G) R L(G )


Regular Regular Regular
Language Language Language

65
Proof - Part 2

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular language L is generated


by some regular grammar G

66
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 )
67
Since L is regular L  L(M )
there is an NFA M such that b
Example:
M a
a
q0 q1 q2

 b
L  ab * ab(b * ab) * q3
L  L(M ) 68
Convert M to a right-linear
grammar b

M a
a
q0 q1 q2

 b
q0  aq1
q3

69
b

M a
a
q0 q1 q2
q0  aq1  b
q1  bq1 q3
q1  aq2

70
b

M a
a
q0 q1 q2
q0  aq1

q1  bq1 b

q1  aq2 q3

q2  bq3

71
L(G )  L( M )  L
G b
q0  aq 1 M
a a
q1  bq 1 q0 q1 q2
q1  aq 2
 b
q 2  bq 3
q 3  q1 q3
q3  

72
In General
a
For any transition: q p

Add production: q  ap

variable terminal variable


73
For any final state: qf

Add production: q f 

74
Since G is right-linear grammar

G is also a regular grammar

L(G )  L( M )  L
with

75

You might also like