11 - Regular - Grammars
11 - Regular - Grammars
11 - Regular - Grammars
Grammars with
at most one variable at the right side
of a production
Examples: S aSb S Ab
S A aAb
A
Grammar G: S SS
S
S aSb
S bSa
L(G ) {w : na ( w) nb ( w)}
Number of a in string w
Costas Busch - RPI 2
Another Linear Grammar
Grammar G : SA
A aB |
B Ab
n n
L(G ) {a b : n 0}
Ba
Costas Busch - RPI 5
Regular Grammars
Examples:
G1 G2
S abS S Aab
S a A Aab | B
Ba
Costas Busch - RPI 7
Observation
Regular grammars generate regular languages
Examples: G2
G1 S Aab
S abS A Aab | B
S a Ba
Languages
Generated by
Regular Grammars
Regular
Languages
Languages
Generated by Regular
Languages
Regular Grammars
Languages
Generated by Regular
Languages
Regular Grammars
Languages
Generated by Regular
Languages
Regular Grammars
Example: S aA | B
A aa B
Bb B|a
A
S special
VF
final state
B
S aA | B
A aa B
Bb B|a Costas Busch - RPI 16
Add edges for each production:
a A
S VF
B
S aA
S aA | B
S a VF
B
S aA | B
A aa B
Costas Busch - RPI 19
A
a a
S a VF
B
S aA | B
b
A aa B
B bB Costas Busch - RPI 20
A
a a
S a VF
a
B
S aA | B
b
A aa B
B bB | a Costas Busch - RPI 21
A
a a
S a VF
a
B
Vi a1a2 am
Costas Busch - RPI 24
We construct the NFA M such that:
V1 V3
V0
VF
V2 V4 special
final state
Costas Busch - RPI 25
For each production: Vi a1a2 amV j
Vi a1 a2 ………
am V
j
Vi a1 a2 ………
am
VF
a2 a4
a1 V1 V3
a3 a5
V0
a3 a4
VF
a8 a9
V2 a5
V4
Proof idea:
We will construct a right-linear
grammar G with R
L(G ) L(G)
Costas Busch - RPI 29
Since G is left-linear grammar
the productions look like:
A Ba1a2 ak
A a1a2 ak
Right A ak a2 a1B
G
linear
R
Av B
Costas Busch - RPI 31
Construct right-linear grammar G
Left A a1a2 ak
G
linear
Av
Right
G A ak a2 a1
linear
R
Av
Costas Busch - RPI 32
R
It is easy to see that: L(G ) L(G)
R
L(G) L(G) L(G )
Regular Regular Regular
Language Language Language
Languages
Generated by Regular
Languages
Regular Grammars
Proof idea:
Let M be the NFA with L L(M ).
M a
a
q0 q1 q2
q0 aq1 b
q3
M a
a
q0 q1 q2
q0 aq1
b
q1 bq1 q3
q1 aq2
M a
a
q0 q1 q2
q0 aq1
q1 bq1 b
q1 aq2 q3
q2 bq3
q0 aq1 M a
a
q0 q1 q2
q1 bq1
q1 aq2 b
q2 bq3 q3
q3 q1
q3
Costas Busch - RPI 40
In General
a
For any transition: q p
Add production: q ap
Add production: qf
with L(G ) L( M ) L