Beruflich Dokumente
Kultur Dokumente
1
Theorem:
R
The reverse L of a regular language L
is a regular language
Proof idea:
R
Construct NFA that accepts L :
Example:
b
L ab * ba
a
b
a
3
Invert Transitions
a
b
a
4
Make old initial state a final state
a
b
a
5
Add a new initial state
a
b
a
6
Resulting machine accepts R
L
R is regular
L
L ab * ba
b R
L b * a ab
a
b
a
7
Grammars
8
Grammars
Grammars express languages
predicate verb 9
article a
article the
noun boy
noun dog
verb runs
verb walks 10
A derivation of “the boy walks”:
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” }
13
Notation
noun boy
noun dog
Variable Terminal
Production
or
rule
Non-terminal
14
Another Example
Grammar: S aSb
S
Derivation of sentence ab :
S aSb ab
S aSb S
15
Grammar: S aSb
S
S aSb S
16
Other derivations:
17
Language of the grammar
S aSb
S
n n
L {a b : n 0}
18
More Notation
Grammar G V ,T , S , P
V : Set of variables
Grammar G: S aSb
S
G V ,T , S , P
V {S } T {a, b}
P {S aSb, S }
20
More Notation
Sentential Form:
A sentence that contains
variables and terminals
Example:
Instead of:
22
*
In general we write: w1 wn
If: w1 w2 w3 wn
23
*
By default: w w
24
Example
Grammar Derivations
S aSb *
S
S
*
S ab
*
S aabb
*
S aaabbb
25
Example
Grammar Derivations
S aSb
S aaSbb
S
aaSbb aaaaaSbbbbb
26
Another Grammar Example
Grammar G : S Ab
A aAb
A
Derivations:
S Ab b
S Ab aAbb abb
S aAbb aaAbbb aabbb
27
More Derivations
S Ab aAbb aaAbbb aaaAbbbb
aaaaAbbbbb aaaabbbbb
S aaaabbbbb
S aaaaaabbbbbbb
n n
S a b b
28
Language of a Grammar
For a grammar G
with start variable S:
L(G ) {w : S w}
String of terminals
29
Example
For grammar G: S Ab
A aAb
A
n n
L(G ) {a b b : n 0}
n n
Since: S a b b
30
A Convenient Notation
A aAb
A aAb |
A
article a
article a | the
article the
31
Linear Grammars
32
Linear Grammars
Grammars with
at most one variable at the right side
of a production
Examples: S aSb S Ab
S A aAb
A
33
A Non-Linear Grammar
Grammar G: S SS
S
S aSb
S bSa
L(G ) {w : na ( w) nb ( w)}
34
Another Linear Grammar
Grammar G : SA
A aB |
B Ab
n n
L(G ) {a b : n 0}
35
Right-Linear Grammars
All productions have form: A xB
or
A x
Example: S abS
S a
36
Left-Linear Grammars
All productions have form: A Bx
or
A x
Example: S Aab
A Aab | B
Ba
37
Regular Grammars
38
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
Ba
39
Observation
Regular grammars generate regular languages
Examples: G2
G1 S Aab
S abS A Aab | B
S a Ba
41
Theorem
Languages
Generated by
Regular Grammars
Regular
Languages
42
Theorem - Part 1
Languages
Generated by Regular
Languages
Regular Grammars
43
Theorem - Part 2
Languages
Generated by Regular
Languages
Regular Grammars
44
Proof – Part 1
Languages
Generated by Regular
Languages
Regular Grammars
45
The case of Right-Linear Grammars
Example: S aA | B
A aa B
Bb B|a
47
Construct NFA M such that
every state is a grammar variable:
A
S special
VF
final state
B
S aA | B
A aa B
Bb B|a 48
Add edges for each production:
a A
S VF
B
S aA
49
a A
S VF
B
S aA | B
50
A
a a
S a VF
B
S aA | B
A aa B
51
A
a a
S a VF
B
S aA | B
b
A aa B
B bB 52
A
a a
S a VF
a
B
S aA | B
b
A aa B
B bB | a 53
A
a a
S a VF
a
B
Vi a1a2 am
56
We construct the NFA M such that:
V1 V3
V0
VF
V2 special
V4
final state 57
For each production: Vi a1a2 amV j
Vi a1 a2 ………
am V
j
58
For each production: Vi a1a2 am
Vi a1 a2 ………
am
VF
59
Resulting NFA M looks like this:
a9
a2 a4
a1 V1 V3
a3 a5
V0
a3 a4
VF
a8 a9
V2 a5
V4
Proof idea:
We will construct a right-linear
R
grammar G with L(G ) L(G)
61
Since G is left-linear grammar
the productions look like:
A Ba1a2 ak
A a1a2 ak
62
Construct right-linear grammar G
In G: A Ba1a2 ak
A vB
In G : A ak a2a1B
R
Av B
63
Construct right-linear grammar G
In G: A a1a2 ak
Av
In G : A ak a2a1
R
Av
64
R
It is easy to see that: L(G ) L(G)
R
L(G) L(G) L(G )
Regular Regular Regular
Language Language Language
65
Proof - Part 2
Languages
Generated by Regular
Languages
Regular Grammars
66
Any regular language L is generated
by some regular grammar G
Proof idea:
Let M be the NFA with L L(M ).
b
L ab * ab(b * ab) * q3
L L(M ) 68
Convert M to a right-linear grammar
b
M a
a
q0 q1 q2
q0 aq1 b
q3
69
b
M a
a
q0 q1 q2
q0 aq1
b
q1 bq1 q3
q1 aq2
70
b
M a
a
q0 q1 q2
q0 aq1
q1 bq1 b
q1 aq2 q3
q2 bq3
71
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
72
In General
a
For any transition: q p
Add production: q ap
Add production: qf
74
Since G is right-linear grammar
with L(G ) L( M ) L
75