Languages and Finite Automata

Regular Grammars
Generate
Regular Languages
1
Theorem
Regular grammars generate exactly the
class of regular languages:
If G is a
regular grammar
then L(G) is
a regular language
If L is a regular
language then
there is a
regular grammar G
with L(G)  L
2
Proof
First we prove:
If G is a
regular grammar
then L(G) is
a regular language
G can be:
Right-linear grammar
or
Left-linear grammar
3
The case of Right-Linear Grammars
Let G be a right-linear grammar
We will show: L(G) is regular
The proof:
We will construct an NFA M
with L( M )  L(G)
4
Grammar G is right-linear
Example:
S  aA | B
A  aa B
Bb B|a
5
Construct NFA M such that
every state is a variable:
A
S
S  aA | B
VF
special
final state
B
A  aa B
Bb B|a
6
Add edges for each production:
a
A
S
VF
B
S  aA
7
a
S
A
VF

B
S  aA | B
8
A
a
a
S
a

VF
B
S  aA | B
A  aa B
9
A
a
a
S
VF
a

B
S  aA | B
A  aa B
B  bB
b
10
A
a
a
S
S  aA | B
A  aa B
B  bB | a
a

VF
a
B
b
11
A
a
a
S
a

VF
a
B
b
S  aA  aaaB  aaabB  aaaba
12
NFA M
Grammar
G
S  aA | B
A  aa B
B  bB | a
A
a
a
S
a

VF
a
B
b
L( M )  L(G ) 
aaab * a  b * a
13
In General
A right-linear grammar G
has variables:
V0 ,V1,V2 ,
and productions:
Vi  a1a2 amV j
or
Vi  a1a2 am
14
We construct the NFA M
such that:
each variable Vi corresponds to a node:
V1
V3
V0
VF
V2
V4
15
For each production: Vi  a1a2  amV j
we add transitions and intermediate nodes
Vi
a1
a2
………
am V
j
16
For each production: Vi  a1a2  am
we add transitions and intermediate nodes
Vi
a1
a2
………
am
VF
17
Resulting NFA M looks like this:
a9
a1
V0
a2
V1
a3
a4
V3
a5
a4
a3
V2
a5
a8
V4
a9
VF
18
Now, we need to show:
L(G)  L( M )
L(G)  L( M )
L( M )  L(G)
w  L(G)  w  L( M )
w  L( M )  w  L(G)
19
The Case: L(G)  L( M )
Take w L(G)
We will show:
there is a path with label w in M
from state V0 to state V f
V0
w
…………
w L(M )
VF
20
Grammar G
looks like:
V0  xV1
V1  yV2
strings

x, y, , z
Vn  z
21
w  xy  z
w L(G) :
V0  xV1  xyV2    xy  Vn  xy  z
V0  xV1
V1  yV2

Vn  z
22
V0  xVi
M
V0
x
……
V1
V0  xVi
23
V0  xVi  xyVz
M
V0
x
……
y
V1
……
V2
V1  yV2
24
V0  xV1  xyV2    xy  Vn  xy  z
M
V0
x
……
y
V1
……
V2
Vn
z
……
VF
Vn  z
25
Since:
M
V0
x
……
y
V1
……
V2
Vn
z
……
VF
We have: xy  z  L(M )
26
Since:
We have:
We have:
w  xy  z
and
M
V0
w
……
xy  z  L(M )
VF
w L(M )
27
The Case: L( M )  L(G)
Take
w L(M )
We will show that in G :
*
V0  w
w L(G)
28
Since
w L(M )
there is a path
M
V0
w
……
VF
29
Write:
w  xy  z
There is a path
M
V0
x
……
y
V1
……
V2
Vn
z
……
VF
30
Since:
M
V0
x
……
y
V1
……
V2
Vn
z
……
VF
This derivation is possible
V0  xV1  xyV2    xy  Vn  xy  z
31
Since:
V0  xV1  xyV2    xy  Vn  xy  z
w  xy  z

We have:
V0  w
We have:
w L(G)
32
The Case of Left-Linear Grammars
Let G be a left-linear grammar
We will show: L(G) is regular
The proof:
We will construct a right-linear
R
grammar G with L(G )  ( L(G))
33
Since G is left-linear grammar
the productions look like:
A  a1a2 ak B
A  a1a2 ak
34
Construct right-linear grammar
In G :
G
A  a1a2 ak B
A  vB
In G :
A  ak a2a1B
Av B
R
35
In G :
A  a1a2 ak
Av
In G :
A  ak a2a1
Av
R
36
It is easy to see that:
L(G)  ( L(G))
R
Since G is right-linear, we have:
L(G)
( L(G))
L(G)
is regular language
R
is regular language (homework)
is regular language
37
Proof - Part 2
Now we will prove:
If L is a regular
language then
there is a
regular grammar G
with L(G)  L
Proof outline:
we will take an NFA for L
and convert it to a regular grammar
38
Since L is regular
There is an NFA M such that L  L(M )
b
Example:
M
q0
a
q0
a

L  ab * ab(b * ab) *
L  L(M )
q0
b
q1
39
Convert M to a right-linear grammar
b
M
q0
q0  aq1
a
q1
a

q2
b
q3
40
b
M
q0
q0  aq1
q1  bq1
a
q1
a

q2
b
q3
41
b
G
q0  aq1
q1  bq1
q1  aq2
q2  bq3
M
q0
a
q1
a

q2
b
q3
q2  b
q2  q1
42
We can generalize this process for
any regular language L :
For any regular language L
we obtain an right-linear grammar G
with L  L(G)
43
Since G
is right-linear grammar
G is also a regular grammar
with
L  L(G)
44