7
a
b
b
a
_
_
10
walks verb
runs verb
dog noun
boy noun
the article
a article
11
A derivation of the boy walks:
walks boy the
verb boy the
verb noun the
verb noun article
verb phrase noun
predicate phrase noun sentence
_
_
12
A derivation of a dog runs:
runs dog a
verb dog a
verb noun a
verb noun article
verb phrase noun
predicate phrase noun sentence
_
_
13
Language of the grammar:
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 }
14
Notation
dog noun
boy noun
Variable
or
Non-terminal
Terminal
Production
rule
15
Another Example
Grammar:
Derivation of sentence :
S
aSb S
ab aSb S
ab
aSb S S
16
aabb aaSbb aSb S
aSb S S
aabb
S
aSb S
Grammar:
Derivation of sentence :
17
Other derivations:
aaabbb aaaSbbb aaSbb aSb S
aaaabbbb aaaaSbbbb
aaaSbbb aaSbb aSb S
18
Language of the grammar
S
aSb S
} 0 : { > = n b a L
n n
19
More Notation
Grammar ( ) P S T V G , , , =
: V
: T
: S
: P
Set of variables
Set of terminal symbols
Start variable
Set of Production rules
20
Example
Grammar :
S
aSb S G
( ) P S T V G , , , =
} {S V = } , { b a T =
} , { = S aSb S P
21
More Notation
Sentential Form:
A sentence that contains
variables and terminals
Example:
aaabbb aaaSbbb aaSbb aSb S
Sentential Forms sentence
22
We write:
Instead of:
aaabbb S
*
25
Example
S
aSb S
aaabbb S
aabb S
ab S
S
*
*
*
*
Grammar
Derivations
26
b aaaaaSbbbb aaSbb
aaSbb S
-
-
S
aSb S
Grammar
Example
Derivations
27
Another Grammar Example
Grammar :
A
aAb A
Ab S
Derivations:
aabbb aaAbbb aAbb S
abb aAbb Ab S
b Ab S
G
28
More Derivations
aaaabbbbb aaaaAbbbbb
aaaAbbbb aaAbbb aAbb Ab S
b b a S
bbb aaaaaabbbb S
aaaabbbbb S
n n
-
-
-
29
Language of a Grammar
For a grammar
with start variable :
G
S
} : { ) ( w S w G L
-
=
String of terminals
30
Example
For grammar :
A
aAb A
Ab S
} 0 : { ) ( > = n b b a G L
n n
Since:
b b a S
n n
-
G
31
A Convenient Notation
A
aAb A
| aAb A
the article
a article
the a article |
32
Linear Grammars
33
Linear Grammars
Grammars with
at most one variable at the right side
of a production
Examples:
A
aAb A
Ab S
S
aSb S
34
A Non-Linear Grammar
bSa S
aSb S
S
SS S
Grammar : G
)} ( ) ( : { ) ( w n w n w G L
b a
= =
35
Another Linear Grammar
Grammar :
Ab B
aB A
A S
|
} 0 : { ) ( > = n b a G L
n n
G
36
Right-Linear Grammars
All productions have form:
Example:
xB A
x A
or
a S
abS S
37
Left-Linear Grammars
All productions have form:
Example:
Bx A
a B
B Aab A
Aab S
|
x A
or
38
Regular Grammars
39
Regular Grammars
A regular grammar is any
right-linear or left-linear grammar
Examples:
a S
abS S
a B
B Aab A
Aab S
|
1
G
2
G
40
Observation
Regular grammars generate regular languages
Examples:
a S
abS S
a ab G L * ) ( ) (
1
=
a B
B Aab A
Aab S
|
* ) ( ) (
2
ab aab G L =
1
G
2
G
41
Regular Grammars
Generate
Regular Languages
42
Theorem
Languages
Generated by
Regular Grammars
Regular
Languages
=
43
Theorem - Part 1
Languages
Generated by
Regular Grammars
Regular
Languages
_
Any regular grammar generates
a regular language
44
Theorem - Part 2
Languages
Generated by
Regular Grammars
Regular
Languages
_
Any regular language is generated
by a regular grammar
45
Proof Part 1
Languages
Generated by
Regular Grammars
Regular
Languages
_
The language generated by
any regular grammar is regular
) (G L
G
46
The case of Right-Linear Grammars
Let be a right-linear grammar
We will prove: is regular
Proof idea: We will construct NFA
with
G
) (G L
M
) ( ) ( G L M L =
47
Grammar is right-linear
G
Example:
a B b B
B aa A
B aA S
|
|
48
Construct NFA such that
every state is a grammar variable:
M
a B b B
B aa A
B aA S
|
|
S
F
V
A
B
special
final state
49
Add edges for each production:
S
F
V
A
B
a
aA S
50
S
F
V
A
B
a
B aA S |
51
S
F
V
A
B
a
B aa A
B aA S
|
a
a
52
S
F
V
A
B
a
bB B
B aa A
B aA S
|
a
a
b
53
S
F
V
A
B
a
a bB B
B aa A
B aA S
|
|
a
a
b
a
54
aaaba aaabB aaaB aA S
S
F
V
A
B
a
a
a
b
a
55
S
F
V
A
B
a
a
a
b
a
a bB B
B aa A
B aA S
|
|
G
M
Grammar
NFA
a b a aaab
G L M L
* *
) ( ) (
+
= =
56
In General
A right-linear grammar
has variables:
and productions:
G
, , ,
2 1 0
V V V
j m i
V a a a V
2 1
m i
a a a V
2 1
or
57
We construct the NFA such that:
each variable corresponds to a node:
M
i
V
0
V
F
V
1
V
2
V
3
V
4
V
special
final state
58
For each production:
we add transitions and intermediate nodes
j m i
V a a a V
2 1
i
V
j
V
1
a
2
a
m
a
59
For each production:
we add transitions and intermediate nodes
m i
a a a V
2 1
i
V
F
V
1
a
2
a
m
a
60
Resulting NFA looks like this: M
0
V
F
V
1
V
2
V
3
V
4
V
1
a
3
a
3
a
4
a
8
a
2
a
4
a
5
a
9
a
5
a
9
a
) ( ) ( M L G L = It holds that:
61
The case of Left-Linear Grammars
Let be a left-linear grammar
We will prove: is regular
Proof idea:
We will construct a right-linear
grammar with
G
) (G L
G
'
R
G L G L ) ( ) (
'
=
62
Since is left-linear grammar
the productions look like:
G
k
a a Ba A
2 1
k
a a a A
2 1
63
Construct right-linear grammar
G
'
In :
G
k
a a Ba A
2 1
In :
G
'
B a a a A
k 1 2
vB A
B v A
R
64
Construct right-linear grammar
G
'
In :
G
k
a a a A
2 1
In :
G
'
1 2
a a a A
k
v A
R
v A
65
It is easy to see that:
Since is right-linear, we have:
R
G L G L ) ( ) (
'
=
) (G L
'
R
G L ) (
'
G
'
) (G L
Regular
Language
Regular
Language
Regular
Language
66
Proof - Part 2
Languages
Generated by
Regular Grammars
Regular
Languages
_
Any regular language is generated
by some regular grammar
L
G
67
Proof idea:
Let be the NFA with .
Construct from a regular grammar
such that
Any regular language is generated
by some regular grammar
L
G
M
) (M L L =
M G
) ( ) ( G L M L =
68
Since is regular
there is an NFA such that
L
M
) (M L L =
Example:
a
b
a
b
* ) * ( * ab b ab ab L =
) (M L L =
M
1
q
2
q
3
q
0
q
69
Convert to a right-linear grammar M
a
b
a
b
M
0
q
1
q
2
q
3
q
1 0
aq q
70
a
b
a
b
M
0
q
1
q
2
q
3
q
2 1
1 1
1 0
aq q
bq q
aq q
71
a
b
a
b
M
0
q
1
q
2
q
3
q
3 2
2 1
1 1
1 0
bq q
aq q
bq q
aq q
72
a
b
a
b
M
0
q
1
q
2
q
3
q
3
1 3
3 2
2 1
1 1
1 0
q
q q
bq q
aq q
bq q
aq q
G
L M L G L = = ) ( ) (
73
In General
For any transition:
a
q p
Add production:
ap q
variable terminal variable
74
For any final state:
f
q
Add production:
f
q
75
Since is right-linear grammar
is also a regular grammar
with
G
G
L M L G L = = ) ( ) (