Languages and Finite Automata

Linear Grammars
Grammars with
at most one variable at the right side
of a production
Examples:
S  aSb
S  Ab
S 
A  aAb
A
2nd Sem 2017
Lecture 7
1
A Non-Linear Grammar
Grammar
G:
S  SS
S 
S  aSb
S  bSa
L(G )  {w : na ( w)  nb ( w)}
Number of
2nd Sem 2017
a in string w
Lecture 7
2
Another Linear Grammar
Grammar
G :
SA
A  aB | 
B  Ab
L(G)  {a b : n  0}
n n
2nd Sem 2017
Lecture 7
3
Right-Linear Grammars
All productions have form:
A  xB
or
A x
Example:
S  abS
S a
2nd Sem 2017
Lecture 7
string of
terminals
4
Left-Linear Grammars
All productions have form:
A  Bx
or
A x
Example:
S  Aab
A  Aab | B
string of
terminals
Ba
2nd Sem 2017
Lecture 7
5
Regular Grammars
2nd Sem 2017
Lecture 7
6
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
2nd Sem 2017
Lecture 7
7
Observation
Regular grammars generate regular languages
Examples:
G2
G1
S  Aab
S  abS
A  Aab | B
S a
Ba
L(G1)  (ab) * a
L(G2 )  aab(ab) *
2nd Sem 2017
Lecture 7
8
Regular Grammars
Generate
Regular Languages
2nd Sem 2017
Lecture 7
9
Theorem

Languages
Generated by
Regular Grammars
2nd Sem 2017
Lecture 7
Regular
Languages
10
Theorem - Part 1

Languages
Generated by
Regular Grammars
Regular
Languages
Any regular grammar generates
a regular language
2nd Sem 2017
Lecture 7
11
Theorem - Part 2

Languages
Generated by
Regular Grammars
Regular
Languages
Any regular language is generated
by a regular grammar
2nd Sem 2017
Lecture 7
12
Proof – Part 1

Languages
Generated by
Regular Grammars
Regular
Languages
The language L(G ) generated by
any regular grammar G is regular
2nd Sem 2017
Lecture 7
13
The case of Right-Linear Grammars
Let
G be a right-linear grammar
We will prove:
Proof idea:
2nd Sem 2017
L(G ) is regular
We will construct NFA
with L( M )  L(G )
Lecture 7
M
14
Grammar
G is right-linear
Example:
S  aA | B
A  aa B
Bb B|a
2nd Sem 2017
Lecture 7
15
Construct NFA M such that
every state is a grammar variable:
A
S
S  aA | B
VF
special
final state
B
A  aa B
Bb B|a
2nd Sem 2017
Lecture 7
16
Add edges for each production:
a
A
S
VF
B
S  aA
2nd Sem 2017
Lecture 7
17
a
S
A
VF

B
S  aA | B
2nd Sem 2017
Lecture 7
18
A
a
a
S
a

VF
B
S  aA | B
A  aa B
2nd Sem 2017
Lecture 7
19
A
a
a
S
S  aA | B

B
b
A  aa B
B  bB
2nd Sem 2017
VF
a
Lecture 7
20
A
a
a
S
S  aA | B
a

2nd Sem 2017
a
B
b
A  aa B
B  bB | a
VF
Lecture 7
21
A
a
a
S
a

VF
a
B
b
S  aA  aaaB  aaabB  aaaba
2nd Sem 2017
Lecture 7
22
NFA
Grammar
M
a
G
S  aA | B
a
B  bB | a
A
a
S

A  aa B
VF
a
B
b
2nd Sem 2017
Lecture 7
L( M )  L(G ) 
aaab * a  b * a
23
In General
A right-linear grammar
has variables:
G
V0 ,V1,V2 ,
and productions:
Vi  a1a2 amV j
or
Vi  a1a2  am
2nd Sem 2017
Lecture 7
24
We construct the NFA
each variable
M such that:
Vi corresponds to a node:
V1
V3
V0
VF
V2
2nd Sem 2017
V4
Lecture 7
special
final state
25
For each production:
Vi  a1a2 amV j
we add transitions and intermediate nodes
Vi
2nd Sem 2017
a1
a2
………
Lecture 7
am V
j
26
For each production:
Vi  a1a2  am
we add transitions and intermediate nodes
Vi
2nd Sem 2017
a1
a2
………
Lecture 7
am
VF
27
Resulting NFA
M looks like this:
a9
a1
V0
a2
V1
a3
V2
2nd Sem 2017
V3
a5
a4
a3
It holds that:
a4
a5
a8
V4
a9
VF
L(G )  L( M )
Lecture 7
28
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
R

grammar G with L(G )  L(G)
2nd Sem 2017
Lecture 7
29
Since G is left-linear grammar
the productions look like:
A  Ba1a2  ak
A  a1a2 ak
2nd Sem 2017
Lecture 7
30
Construct right-linear grammar
Left
linear
G
Right
linear
G
2nd Sem 2017
G
A  Ba1a2  ak
A → Bv
A  ak a2a1B
Av B
R
Lecture 7
31
Construct right-linear grammar
Left
linear
G
Right
linear
G
A  a1a2 ak
Av
A  ak  a2 a1
Av
2nd Sem 2017
G
Lecture 7
R
32
L(G)  L(G)
It is easy to see that:
Since
G is right-linear, we have:
L(G)
L(G)
Regular
Language
Regular
Language
2nd Sem 2017
R
R
Lecture 7
L(G )
Regular
Language
33
Proof - Part 2

Languages
Generated by
Regular Grammars
Regular
Languages
Any regular language L is generated
by some regular grammar G
2nd Sem 2017
Lecture 7
34
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
such that L( M )  L(G )
2nd Sem 2017
Lecture 7
G
35
Since L is regular
there is an NFA M such that
L  L(M )
b
Example:
M
a
q0
q1
a

L  ab * ab(b * ab) *
L  L(M )
2nd Sem 2017
Lecture 7
q2
b
q3
36
Convert
M to a right-linear grammar
b
M
q0
a

q0  aq1
2nd Sem 2017
q1
a
q2
b
q3
Lecture 7
37
b
M
q0
q0  aq1
q1
a

q1  bq1
q2
b
q3
q1  aq2
2nd Sem 2017
a
Lecture 7
38
b
M
q0  aq1
q0
a
q1
a

q1  bq1
q1  aq2
q2
b
q3
q2  bq3
2nd Sem 2017
Lecture 7
39
L(G )  L( M )  L
b
G
q0  aq1
q1  bq1
M
q0
a
q1
a

q1  aq2
q2  bq3
q2
b
q3
q3  q1
q3  
2nd Sem 2017
Lecture 7
40
In General
a
For any transition:
q
Add production:
q  ap
variable
2nd Sem 2017
Lecture 7
terminal
p
variable
41
For any final state:
qf
qf 
Add production:
2nd Sem 2017
Lecture 7
42
Since
G is right-linear grammar
G is also a regular grammar
with
2nd Sem 2017
L(G )  L( M )  L
Lecture 7
43