Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

11 - Regular - Grammars

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Linear Grammars

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

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

Costas Busch - RPI 1


A Non-Linear Grammar

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 : SA
A  aB | 
B  Ab

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

Costas Busch - RPI 3


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

Example: S  abS string of


S a terminals

Costas Busch - RPI 4


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

Example: S  Aab string of


A  Aab | B terminals

Ba
Costas Busch - RPI 5
Regular Grammars

Costas Busch - RPI 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
Costas Busch - RPI 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) *


Costas Busch - RPI 8
Regular Grammars
Generate
Regular Languages

Costas Busch - RPI 9


Theorem

Languages
Generated by
Regular Grammars
 Regular
Languages

Costas Busch - RPI 10


Theorem - Part 1

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular grammar generates


a regular language

Costas Busch - RPI 11


Theorem - Part 2

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular language is generated


by a regular grammar

Costas Busch - RPI 12


Proof – Part 1

Languages
Generated by  Regular
Languages
Regular Grammars

The language L(G ) generated by


any regular grammar G is regular

Costas Busch - RPI 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 )

Costas Busch - RPI 14


Grammar G is right-linear

Example: S  aA | B
A  aa B
Bb B|a

Costas Busch - RPI 15


Construct NFA M such that
every state is a grammar variable:

A
S special
VF
final state
B
S  aA | B
A  aa B
Bb B|a Costas Busch - RPI 16
Add edges for each production:

a A
S VF
B

S  aA

Costas Busch - RPI 17


a A
S VF

B

S  aA | B

Costas Busch - RPI 18


A
a a

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

S  aA  aaaB  aaabB  aaaba


Costas Busch - RPI 22
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
Costas Busch - RPI 23
In General
A right-linear grammar G

has variables: V0 ,V1,V2 ,

and productions: Vi  a1a2  amV j


or

Vi  a1a2  am
Costas Busch - RPI 24
We construct the NFA M such that:

each variable Vi corresponds to a node:

V1 V3
V0
VF
V2 V4 special
final state
Costas Busch - RPI 25
For each production: Vi  a1a2  amV j

we add transitions and intermediate nodes

Vi a1 a2 ………
am V
j

Costas Busch - RPI 26


For each production: Vi  a1a2  am

we add transitions and intermediate nodes

Vi a1 a2 ………
am
VF

Costas Busch - RPI 27


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 )


Costas Busch - RPI 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
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

Costas Busch - RPI 30


Construct right-linear grammar G
Left A  Ba1a2  ak
G
linear
A → Bv

Right A  ak  a2 a1B
G
linear
R
Av B
Costas Busch - RPI 31
Construct right-linear grammar G
Left A  a1a2  ak
G
linear
Av

Right
G A  ak  a2 a1
linear
R
Av
Costas Busch - RPI 32
R
It is easy to see that: L(G )  L(G)

Since G is right-linear, we have:

R
L(G) L(G) L(G )
Regular Regular Regular
Language Language Language

Costas Busch - RPI 33


Proof - Part 2

Languages
Generated by  Regular
Languages
Regular Grammars

Any regular language L is generated


by some regular grammar G

Costas Busch - RPI 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 )
Costas Busch - RPI 35
Since L is regular
there is an NFA M such that L  L(M )
b
Example:
M a
a
q0 q1 q2
 b
L  ab * ab(b * ab) * q3
L  L(M )
Costas Busch - RPI 36
Convert M to a right-linear grammar
b

M a
a
q0 q1 q2

q0  aq1  b
q3

Costas Busch - RPI 37


b

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

Costas Busch - RPI 38


b

M a
a
q0 q1 q2
q0  aq1
q1  bq1  b

q1  aq2 q3

q2  bq3

Costas Busch - RPI 39


L(G )  L( M )  L
G b

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

variable terminal variable


Costas Busch - RPI 41
For any final state: qf

Add production: qf 

Costas Busch - RPI 42


Since G is right-linear grammar

G is also a regular grammar

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

Costas Busch - RPI 43

You might also like