Class5 Regular Grammars
Class5 Regular Grammars
R
The reverse L of a regular language
is a regular language
LR
Proof
Since L is regular,
there is NFA that accepts
L
Invert Transitions
Example:
L = ab * + ba
a
5
1
PDF created with pdfFactory Pro trial version www.pdffactory.com
LR
LR
is regular
Grammars
L = ab * + ba
LR = b * a + ab
Grammars
article a
article the
noun boy
predicate
noun dog
noun _ phrase article
noun
verb runs
predicate verb
verb walks
10
predicate
predicate
noun _ phrase
verb
noun _ phrase
verb
article
verb
article
noun
verb
a noun
verb
noun
the noun
verb
a dog verb
a dog runs
11
12
2
PDF created with pdfFactory Pro trial version www.pdffactory.com
Notation
Language of the grammar:
noun boy
L = { a boy runs,
a boy walks,
the boy runs,
the boy walks,
a dog runs,
a dog walks,
the dog runs,
the dog walks }
noun dog
Variable
or
Non-terminal
Production
rule
Terminal
13
14
Another Example
Grammar:
Grammar:
S aSb
S
Derivation of sentence
ab :
Derivation of sentence
aabb :
S aSb ab
S aSb
S aSb
S
S aSb
15
Other derivations:
16
S aSb
S
L = {a nb n : n 0}
aaaaSbbbb aaaabbbb
17
18
3
PDF created with pdfFactory Pro trial version www.pdffactory.com
More Notation
Example
G = (V , T , S , P )
Grammar
Grammar
V:
Set of variables
T:
S:
Start variable
P:
G :
S aSb
S
G = (V , T , S , P )
V = {S }
T = {a, b}
P = {S aSb, S }
19
20
More Notation
Sentential Form:
A sentence that contains
variables and terminals
We write:
S aaabbb
Example:
Instead of:
sentence
21
In general we write:
If:
22
w1 wn
By default:
w w
w1 w2 w3 wn
23
24
4
PDF created with pdfFactory Pro trial version www.pdffactory.com
Example
Grammar
Example
Grammar
Derivations
*
S aSb
S
Derivations
S aSb
S
S
*
S aaSbb
S ab
aaSbb aaaaaSbbbbb
S aabb
*
S aaabbb
25
26
More Derivations
S aaaabbbbb
Derivations:
S Ab
b
S Ab aAbb abb
S Ab aAbb aaAbbb aabbb
S aaaaaabbbbbbb
S a nb nb
27
28
Language of a Grammar
Example
For grammar
For a grammar G
with start variable
S Ab
G:
A aAb
A
L (G ) = {a nb nb : n 0}
L (G ) = {w : S w}
Since:
String of terminals
29
S a nb nb
30
5
PDF created with pdfFactory Pro trial version www.pdffactory.com
A Convenient Notation
A aAb
A
A aAb |
article a
Linear Grammars
article a | the
article the
31
32
Linear Grammars
A Non-Linear Grammar
Grammars with
at most one variable at the right side
of a production
S aSb
S
Examples:
Grammar
G:
S SS
S
S aSb
S bSa
S Ab
A aAb
A
L (G ) = {w : na ( w) = nb ( w)}
33
Number of
a in string w
Right-Linear Grammars
All productions have form:
Grammar
34
SA
A aB |
B Ab
A xB
or
A x
Example:
L (G ) = {a nb n : n 0}
35
S abS
S a
string of
terminals
36
6
PDF created with pdfFactory Pro trial version www.pdffactory.com
Left-Linear Grammars
All productions have form:
A Bx
or
A x
Example:
S Aab
Regular Grammars
string of
terminals
A Aab | B
Ba
37
38
Regular Grammars
Observation
Examples:
G1
S abS
S a
G2
G1
G2
S Aab
A Aab | B
Ba
39
S abS
S a
S Aab
A Aab | B
Ba
L (G1 ) = (ab) * a
40
Theorem
Languages
Generated by
Regular Grammars
Regular Grammars
Generate
Regular Languages
41
Regular
Languages
42
7
PDF created with pdfFactory Pro trial version www.pdffactory.com
Theorem - Part 1
Languages
Generated by
Regular Grammars
Theorem - Part 2
Languages
Generated by
Regular Grammars
Regular
Languages
Regular
Languages
43
Proof Part 1
Languages
Generated by
Regular Grammars
44
Regular
Languages
be a right-linear grammar
We will prove:
Proof idea:
L(G )
is regular
45
Grammar
G is right-linear
Example:
M
46
S aA | B
A aa B
B b B|a
47
S aA | B
A aa B
B b B|a
VF
special
final state
48
8
PDF created with pdfFactory Pro trial version www.pdffactory.com
VF
VF
S aA | B
S aA
49
50
a
S
VF
S aA | B
A aa B
51
S aA | B
A aa B
B bB
b
52
S aA | B
A aa B
B bB | a
a
a
VF
a
S
VF
VF
B
b
b
53
54
9
PDF created with pdfFactory Pro trial version www.pdffactory.com
NFA
has variables:
A aa B
A right-linear grammar
G
S aA | B
A
a
In General
Grammar
B
b
V0 ,V1,V2 ,
B bB | a
VF
Vi a1a2 amV j
and productions:
L ( M ) = L (G ) =
or
aaab * a + b * a
Vi a1a2 am
55
Vi
such that:
corresponds to a node:
V1
56
Vi a1a2 amV j
V3
Vi
V0
a1
a2
am V
j
VF
V2
V4
special
final state 57
Vi a1a2 am
58
Resulting NFA
Vi
a1
a2
am
V0
VF
V1
It holds that:
a4
V3
a5
a4
a3
V2
59
a2
a3
a5
a8
V4
L (G ) = L ( M )
a9
VF
60
10
PDF created with pdfFactory Pro trial version www.pdffactory.com
G be a left-linear grammar
We will prove:
L (G )
is regular
A Ba1a2 ak
A a1a2 ak
Proof idea:
We will construct a right-linear
grammar G with L (G ) = L(G) R
61
Right
linear
62
A Ba1a2 ak
A Bv
A ak a2 a1B
A vRB
Left
linear
Right
linear
A a1a2 ak
Av
A ak a2 a1
A vR
63
64
Proof - Part 2
It is easy to see that:
Since
L (G ) = L(G)
R
Languages
Generated by
Regular Grammars
G is right-linear, we have:
L (G)
L(G) R
L (G )
Regular
Language
Regular
Language
Regular
Language
Regular
Languages
65
66
11
PDF created with pdfFactory Pro trial version www.pdffactory.com
Since L is regular
there is an NFA M such that
L = L(M )
b
Example:
Proof idea:
Let
L = L (M ).
G
67
q0
Convert
q1
q2
L = ab * ab(b * ab) *
L = L(M )
q3
68
to a right-linear grammar
M
M
q0
q1
q0 aq1
q0
q2
q0 aq1
q1 bq1
q1
q2
b
q3
q1 aq2
q3
69
70
L (G ) = L ( M ) = L
b
q0 aq1
q1 bq1
q1 aq2
q2 bq3
q0
q1
G
a
q0 aq1
q2
q1 bq1
q1 aq2
q2 bq3
q3
M
q0
q1
q2
b
q3
q3 q1
71
q3
72
12
PDF created with pdfFactory Pro trial version www.pdffactory.com
In General
a
Add production:
q ap
variable
terminal
Add production:
qf
variable
73
Since
qf
74
G is right-linear grammar
G is also a regular grammar
with
L (G ) = L ( M ) = L
75
13
PDF created with pdfFactory Pro trial version www.pdffactory.com