Grammars for regular languages

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