النظرية الاحتسابية
النظرية الاحتسابية
النظرية الاحتسابية
CPU memory
input memory
CPU
output memory
Program memory
temporary memory
input memory
CPU
output memory
Program memory
compute xx
compute x x
2
4
Copyright 2011@Xu Dezhi
f ( x) x 3
temporary memory
input memory
x2
CPU
output memory
Program memory
compute xx
compute x x
2
Copyright 2011@Xu Dezhi 5
temporary memory f ( x) x 3
z 2*2 4
f ( x) z * 2 8
input memory
x2
CPU
output memory
Program memory
compute xx
compute x x
2
Automaton
input memory
CPU
output memory
Program memory
temporary memory
input memory
Finite
Automaton
output memory
input memory
Pushdown
Automaton
output memory
input memory
Turing
Machine
output memory
1
Mathematical Preliminaries
• Sets
• Functions
• Relations
• Graphs
• Proof Techniques
2
SETS
A set is a collection of elements
A {1, 2, 3}
B {train, bus, bicycle, airplane}
We write
1 A
ship B
3
Set Representations
C = { a, b, c, d, e, f, g, h, i, j, k }
C = { a, b, …, k } finite set
S = { 2, 4, 6, … } infinite set
4
A = { 1, 2, 3, 4, 5 }
U
6 A
2 3 8
1
7 4 5
9
10
U = { 1 , … , 10 }
5
Set Operations
A = { 1, 2, 3 } B = { 2, 3, 4, 5}
A B
• Union
2 4
1
A U B = { 1, 2, 3, 4, 5 } 3 5
• Intersection
U
A B = { 2, 3 } 2
3
• Difference
A-B={1}
1
B - A = { 4, 5 }
Venn diagrams
6
• Complement
Universal set = {1, …, 7}
A = { 1, 2, 3 } A = { 4, 5, 6, 7}
4
A
A 3 6
1
2
5 7
A=A
7
{ even integers } = { odd integers }
Integers
1 odd
even
6 5
2
0
4
3 7
8
DeMorgan‟s Laws
AUB=A B
U
A B=AUB
U
9
Empty, Null Set:
={}
SU =S
S =
U
= Universal Set
S- =S
-S=
10
Subset
A = { 1, 2, 3} B = { 1, 2, 3, 4, 5 }
A B
U
Proper Subset: A B
U
B
A
11
Disjoint Sets
A = { 1, 2, 3 } B = { 5, 6}
A B=
U
A B
12
Set Cardinality
• For finite sets
A = { 2, 5, 7 }
|A| = 3
(set size)
13
Powersets
A powerset is a set of sets
S = { a, b, c }
2S = { , {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c} }
Observation: | 2S | = 2|S| ( 8 = 23 )
14
Cartesian Product
A = { 2, 4 } B = { 2, 3, 5 }
A X B = { (2, 2), (2, 3), (2, 5), ( 4, 2), (4, 3), (4, 5) }
|A X B| = |A| |B|
AXBX…XZ
15
Functions
domain range
4 A B
f(1) = a a
1
2 b
3 c
5
f : A -> B
If A = domain
then f is a total function
otherwise f is a partial function
16
Relations
R = {(x1, y1), (x2, y2), (x3, y3), …}
xi R yi
17
Equivalence Relations
• Reflexive: xRx
• Symmetric: xRy yRx
• Transitive: x R y and y R z xRz
Example: R = „=„
•x=x
•x=y y=x
• x = y and y = z x=z
18
Equivalence Classes
For equivalence relation R
equivalence class of x = {y : x R y}
Example:
R = { (1, 1), (2, 2), (1, 2), (2, 1),
(3, 3), (4, 4), (3, 4), (4, 3) }
c
• Nodes (Vertices)
V = { a, b, c, d, e }
• Edges
E = { (a,b), (b,c), (b,e),(c,a), (c,e), (d,c), (e,b), (e,d) }
20
Labeled Graph
2
6 e
b 2
1 3
a 6 d
5
c
21
Walk
e
b
a d
22
Path
e
b
a d
base e
b
3
a 1 d
2
c
24
Euler Tour
8 base
7 e
b 1
4 6
a 5 2 d
3
c
25
Hamiltonian Cycle
5 base
e
b 1
4
a 2 d
3
c
26
Finding All Simple Paths
e
b
a d
c
origin
27
Step 1
e
b
a d
c
origin
(c, a)
(c, e)
28
Step 2
e
b
a d
(c, a) c
origin
(c, a), (a, b)
(c, e)
(c, e), (e, b)
(c, e), (e, d)
29
Step 3
e
b
a d
(c, a) c
origin
(c, a), (a, b)
(c, a), (a, b), (b, e)
(c, e)
(c, e), (e, b)
(c, e), (e, d)
30
Step 4
e
b
a d
(c, a)
(c, a), (a, b) c
origin
(c, a), (a, b), (b, e)
(c, a), (a, b), (b, e), (e,d)
(c, e)
(c, e), (e, b)
(c, e), (e, d)
31
Trees
root
parent
leaf
child
32
root
Level 0
Level 1
leaf Height 3
Level 2
Level 3
33
Binary Trees
34
Proof Techniques
• Proof by induction
• Proof by contradiction
•Proof by construction
35
Induction
If we know
• for some b that P1, P2, …, Pb are true
• for any k >= b that
P1, P2, …, Pk imply Pk+1
Then
Every Pi is true
36
Proof by Induction
• Inductive basis
Find P1, P2, …, Pb which are true
• Inductive hypothesis
Let‟s assume P1, P2, …, Pk are true,
for any k >= b
• Inductive step
Show that Pk+1 is true
37
Example
Theorem: A binary tree of height n
has at most 2n leaves.
Proof by induction:
let L(i) be the maximum number of
leaves of any subtree at height i
38
We want to show: L(i) <= 2i
• Inductive basis
L(0) = 1 (the root node)
• Inductive hypothesis
Let‟s assume L(i) <= 2i for all i = 0, 1, …, k
• Induction step
we need to show that L(k + 1) <= 2k+1
39
Induction Step
height
k
k+1
40
Induction Step
height
k L(k) <= 2k
k+1
41
Remark
Recursion is another thing
f(0) = 1, f(1) = 1
42
Proof by Contradiction
43
Example
Theorem: 2 is not rational
Proof:
Assume by contradiction that it is rational
2 = n/m
n and m have no common factors
44
2 = n/m 2 m2 = n2
n is even
Therefore, n2 is even
n=2k
m is even
2 m2 = 4k2 m2 = 2k2
m=2p
Contradiction!
45
Proof by Construction
We want to prove that a statement
about something with a property is
true
– constructing a concrete example with a
property to show that something having
that property exists.
– constructive proof is in contrast to a
non-constructive proof which does not
provide a means of constructing an
example.
46
Example 1
16 can be exactly divided.
Proof
End
47
Example 2
There exist two irrational numbers which make ab
rational.
Proof
Let a=b= 2
End
48
Question
49
Example 3
Proof
Let n be any integer.
Let m = n+1
m is an integer
m>n
Therefore m is an integer that is larger than n
Therefore, for any integer there exists an
integer m = n + 1 that is larger than it.
End
50
Question
51
Example 4
Show that there is no "largest" prime number.
Proof
Let n be any prime number
Let m = n! + 1, then m>n
Case 1:
m = n! + 1 is a prime number, then we have
constructed a prime number that is larger than
the previous prime number.
Case 2:
m = n! + 1 is not a prime number, then it has at
least one prime factor
52
Example 4 (Cont.)
Explanation:
End
53
Question
54
Languages
55
A language is a set of strings
56
Alphabets and Strings
We will use small alphabets: a, b
Strings
a
ab u ab
abba v bbbaaa
baba w abba
aaabbbaabab
57
String Operations
w a1a2 an abba
bbbaaa
v b1b2 bm
Concatenation
58
w a1a2 an ababaaabbb
Reverse
w an a2a1
R
bbbaaababa
59
String Length
w a1a2 an
Length: w n
Examples: abba 4
aa 2
a 1
60
Length of Concatenation
uv u v
Example: u aab, u 3
v abaab, v 5
uv aababaab 8
uv u v 3 5 8
61
Empty String
A string with no letters:
Observations: 0
w w w
String Substring
abbab ab
abbab abba
abbab b
abbab bbab
63
Prefix and Suffix
abbab
Prefixes Suffixes
abbab w uv
a bbab
prefix
ab bab
suffix
abb ab
abba b
abbab
64
Another Operation
w ww
n
w
n
Definition: w
0
abba
0
65
The * Operation
* : the set of all possible strings from
alphabet
a, b
* , a, b, aa, ab, ba, bb, aaa, aab,
66
The + Operation
: the set of all possible strings from
alphabet except
a, b
* , a, b, aa, ab, ba, bb, aaa, aab,
*
a, b, aa, ab, ba, bb, aaa, aab,
67
Languages
A language is any subset of *
Example: a, b
* , a, b, aa, ab, ba, bb, aaa,
Languages:
a, aa, aab
{ , abba, baba, aa, ab, aaaaaa}
68
Note that:
Sets { } {}
Set size {} 0
An infinite language L {a b : n 0}
n n
ab
L abb L
aabb
aaaaabbbbb
70
Operations on Languages
The usual set operations
Definition: L {w : w L}
R R
L {a b : n 0}
n n
L {b a : n 0}
R n n
72
Concatenation
73
Another Operation
Definition: L
n
LL L
n
L {a b : n 0}
n n
L {a b a b : n, m 0}
2 n n m m
aabbaaabbb L 2
75
Star-Closure (Kleene *)
Definition: L* L L L
0 1 2
Example:
,
a, bb,
a, bb*
aa , abb , bba , bbbb,
aaa, aabb, abba, abbbb,
76
Positive Closure
Definition:
L L L
1 2
L *
a, bb,
a, bb aa, abb, bba, bbbb,
aaa, aabb, abba, abbbb,
77
The End
78
Finite Automata
1
Finite Automaton
Input
String
Output
Finite String
Automaton
2
Finite Accepter
Input
String
Output
“Accept”
Finite
or
Automaton
“Reject”
3
Transition Graph
Abba -Finite Accepter a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
initial final
state state
transition
state “accept”
4
Initial Configuration
Input String
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
5
Reading the Input
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
6
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
7
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
8
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
9
Input finished
a b b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Output: “accept”
10
Rejection
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
11
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
12
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
13
a b a
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
14
Input finished
a b a
a, b
Output:
q5 “reject”
a a, b
b a b
q0 a q1 b q2 b q3 a q4
15
Another Rejection
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
16
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
Output:
“reject” 17
Another Example
a a b
a a, b
q0 b q1 a, b q2
18
a a b
a a, b
q0 b q1 a, b q2
19
a a b
a a, b
q0 b q1 a, b q2
20
a a b
a a, b
q0 b q1 a, b q2
21
Input finished
a a b
a a, b
Output: “accept”
q0 b q1 a, b q2
22
Rejection
b a b
a a, b
q0 b q1 a, b q2
23
b a b
a a, b
q0 b q1 a, b q2
24
b a b
a a, b
q0 b q1 a, b q2
25
b a b
a a, b
q0 b q1 a, b q2
26
Input finished
b a b
a a, b
q0 b q1 a, b q2
Output: “reject”
27
Formalities
Deterministic Finite Accepter (DFA)
M Q, , , q0 , F
Q : set of states
: input alphabet
: transition function
q0 : initial state
a, b
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
29
Set of States Q
Q q0 , q1, q2 , q3 , q4 , q5
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
30
Initial State q0
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
31
Set of Final States F
F q4
a, b
q5
a a, b
b a b
q0 a q1 b q2 b q3 a q4
32
Transition Function
:Q Q
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
33
q0 , a q1
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
34
q0 , b q5
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
35
q2 , b q3
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
36
Transition Function
a b
q0 q1 q5
q1 q5 q2
q2 q5 q3
q3 q4 q5 a, b
q4 q5 q5
q5 q5 q5 q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
37
Extended Transition Function *
* : Q * Q
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
38
* q0 , ab q2
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
39
* q0 , abba q4
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
40
* q0 , abbbaa q5
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
41
Observation: There is a walk from q to q
with label w
* q, w q
q w q
w 1 2 k
1 2 k
q q
42
Example: There is a walk from q0 to q5
with label abbbaa
* q0 , abbbaa q5
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
43
Recursive Definition
* q , q
* q, w ( * (q, w), )
q w q1 q
* q , w q
* q, w (q1, )
(q1, ) q
* q, w ( * (q, w), )
* q, w q1
44
* q0 , ab
* (q0 , a ), b
* q0 , , a , b
q0 , a , b
q1 , b
q2 a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
45
Languages Accepted by DFAs
Take DFA M
Definition:
The language LM contains
all input strings accepted by M
46
Example
LM abba M
a, b
q5
b a a, b
a b
q0 a q1 b q2 b q3 a q4
accept
47
Another Example
LM , ab, abba M
a, b
q5
b a a a, b
b
q0 a q1 b q2 b q3 a q4
accept accept accept
48
Formally
For a DFA M Q, , , q0 , F
Language accepted by M :
LM w * : * q0 , w F
q0 w q q F
49
Observation
Language rejected by M :
LM w * : * q0 , w F
q0 w q q F
50
More Examples
LM {a b : n 0}
n
a a, b
q0 b q1 a, b q2
51
LM = { all strings with prefix ab }
a, b
q0 a q1 b q2
b a accept
q3 a, b
52
LM = { all strings without
substring 001 }
1 0 0,1
1
0 1
0 00 001
0
53
Regular Languages
54
Examples of regular languages:
b a
q4
a, b
56
There exist languages which are not Regular:
Example: L {a b : n 0}
n n
57
The End
58
Non Deterministic Automata
1
Nondeterministic Finite Accepter (NFA)
Alphabet = {a}
q1 a q2
a
q0
a
q3
2
Nondeterministic Finite Accepter (NFA)
Alphabet = {a}
Two choices q1 a q2
a
q0
a
q3
3
Nondeterministic Finite Accepter (NFA)
Alphabet = {a}
4
First Choice
a a
q1 a q2
a
q0
a
q3
5
First Choice
a a
q1 a q2
a
q0
a
q3
6
First Choice
a a
q1 a q2
a
q0
a
q3
7
First Choice
a a
All input is consumed
q1 a q2 “accept”
a
q0
a
q3
8
Second Choice
a a
q1 a q2
a
q0
a
q3
9
Second Choice
a a
q1 a q2
a
q0
a
q3
10
Second Choice
a a
q1 a q2
a
q0
a
No transition:
q3
the automaton hangs
11
Second Choice
a a
Input cannot be consumed
q1 a q2
a
q0
a
q3 “reject”
12
An NFA accepts a string:
when there is a computation of the NFA
that accepts the string
AND
all the input is consumed and the automaton
is in a final state
13
Example
“accept”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3 “reject”
because this
computation
accepts aa
14
Rejection example
q1 a q2
a
q0
a
q3
15
First Choice
q1 a q2
a
q0
a
q3
16
First Choice
a
“reject”
q1 a q2
a
q0
a
q3
17
Second Choice
q1 a q2
a
q0
a
q3
18
Second Choice
q1 a q2
a
q0
a
q3
19
Second Choice
q1 a q2
a
q0
a
q3 “reject”
20
An NFA rejects a string:
when there is no computation of the NFA
that accepts the string:
OR
“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3
“reject” q3
a a a
q1 a q2
a
q0
a
q3
23
First Choice
a a a
q1 a q2
a
q0
a
q3
24
First Choice
a a a
q1 a q2
a
q0
a No transition:
the automaton hangs
q3
25
First Choice
a a a
Input cannot be consumed
q1 a q2 “reject”
a
q0
a
q3
26
Second Choice
a a a
q1 a q2
a
q0
a
q3
27
Second Choice
a a a
q1 a q2
a
q0
a
q3
28
Second Choice
a a a
q1 a q2
a
q0
a
No transition:
q3
the automaton hangs
29
Second Choice
a a a
Input cannot be consumed
q1 a q2
a
q0
a
q3 “reject”
30
aaa is rejected by the NFA:
“reject”
q1 a q2 q1 a q2
a a
q0 q0
a a
q3 q3 “reject”
q1 a q2
a
q0
a
q3
32
Lambda Transitions
q0 a q1 q2 a q3
33
a a
q0 a q1 q2 a q3
34
a a
q0 a q1 q2 a q3
35
(read head does not move)
a a
q0 a q1 q2 a q3
36
a a
q0 a q1 q2 a q3
37
all input is consumed
a a
“accept”
q0 a q1 q2 a q3
String aa is accepted
38
Rejection Example
a a a
q0 a q1 q2 a q3
39
a a a
q0 a q1 q2 a q3
40
(read head doesn’t move)
a a a
q0 a q1 q2 a q3
41
a a a
q0 a q1 q2 a q3
No transition:
the automaton hangs
42
Input cannot be consumed
a a a
“reject”
q0 a q1 q2 a q3
q0 a q1 q2 a q3
44
Another NFA Example
q0 a q1 b q2 q3
45
a b
q0 a q1 b q2 q3
46
a b
q0 a q1 b q2 q3
47
a b
q0 a q1 b q2 q3
48
a b
“accept”
q0 a q1 b q2 q3
49
Another String
a b a b
q0 a q1 b q2 q3
50
a b a b
q0 a q1 b q2 q3
51
a b a b
q0 a q1 b q2 q3
52
a b a b
q0 a q1 b q2 q3
53
a b a b
q0 a q1 b q2 q3
54
a b a b
q0 a q1 b q2 q3
55
a b a b
q0 a q1 b q2 q3
56
a b a b
“accept”
q0 a q1 b q2 q3
57
Language accepted
q0 a q1 b q2 q3
58
Another NFA Example
0
q0 q1 0, 1 q2
1
59
Language accepted
•Simple automata:
M1 M2
q0 q0
NFA M1 DFA M2 a
q2
q0 a q1
a
q0 a q1
L( M1 ) = {a} L( M 2 ) = {a}
62
Example
NFA M1
LM1 {10} * 0
q0 q1
1
DFA M2 0,1
LM 2 {10} * 0
q0 q1 1 q2
1
0
63
Formal Definition of NFAs
M Q, , , q0 , F
q0 : Initial state
F: Final states
64
Transition Function
q0 , 1 q1
0
q0 q1 0, 1 q
2
1
65
(q1,0) {q0 , q2}
0
q0 q1 0, 1 q
2
1
66
(q0 , ) {q0 , q2}
0
q0 q1 0, 1 q
2
1
67
(q2 ,1)
0
q0 q1 0, 1 q
2
1
68
Extended Transition Function *
* q0 , a q1
q4 q5
a a
q0 a q1 b q2 q3
69
* q0 , aa q4 , q5
q4 q5
a a
q0 a q1 b q2 q3
70
* q0 , ab q2 , q3 , q0
q4 q5
a a
q0 a q1 b q2 q3
71
Formally
q j * qi , w : there is a walk from qi to q j
with label w
qi w qj
w 1 2 k
1 2 k
qi qj
72
The Language of an NFA M
F q0 ,q5
q4 q5
a a
q0 a q1 b q2 q3
77
Formally
The language accepted by NFA M is:
LM w1, w2 , w3 ,...
78
w LM * (q0 , w)
qi
w
q0
qk qk F
w
w qj
79
NFAs accept the Regular
Languages
80
Equivalence of Machines
if L M1 L M 2
81
Example of equivalent machines
NFA M1
LM1 {10} * 0
q0 q1
1
DFA M2 0,1
LM 2 {10} * 0
q0 q1 1 q2
1
0
82
We will prove:
Languages
Regular
accepted
Languages
by NFAs
Languages
accepted
by DFAs
NFAs and DFAs have the
same computation power
83
Step 1
Languages
accepted Regular
Languages
by NFAs
Languages
accepted Regular
Languages
by NFAs
DFA M
q0
86
Convert NFA to DFA
NFA M
a
q a
0 q q1 2
b
DFA M
q0 a
q1, q2
87
Convert NFA to DFA
NFA M
a
q a
0 q q 1 2
b
DFA M
q0 a
q1, q2
b
88
Convert NFA to DFA
NFA M
a
q a
0 q q 1 2
b
a
DFA M
q0 a
q1, q2
b
89
Convert NFA to DFA
NFA M
a
q a
0 q q 1 2
b
a
DFA M b
q0 a
q1, q2
b
90
Convert NFA to DFA
NFA M
a
q a
0 q q 1 2
b
a
DFA M b
q0 a
q1, q2
b
a, b
91
Convert NFA to DFA
NFA M
a
q a
0 q q 1 2
LM L(M )
b
a
DFA M b
q0 a
q1, q2
b
a, b
92
More example: Converting NFA to DFA
b a
a
2 3
a, b
The NFA N 4
93
Step1: Determine DFA’s states
94
Step2:Determine the start and accept states
95
Step3: Determine transition function
a,a,bb a b
{1} {2} {1,2}
a, b
b b a
b a
a a
{3} {1,3} {2,3} {1,2,3}
a
b
b
96
After removing unnecessary states
a,a,bb
{2}
b b b a
a
a a
{3} a {1,3} {2,3} {1,2,3}
b
b
97
Rearranging states
a a, b
b
{1,3} {3}
a
b b
b a
a
{2} {2,3} {1,2,3}
a
b
98
Renaming states
a a, b
b
q0 a q2 q5
b b
b a
a
q1 q3 q4
a
b
99
More simplified
q0 a q2
b b
b a
a
q1 q3 q4
a
b
100
NFA to DFA: Remarks
We want to convert it
to an equivalent DFA M
q0 , q1, q2 ,...
102
Procedure NFA to DFA
103
Example
NFA M a
q0 a q1 q2
b
DFA M
q0
104
Procedure NFA to DFA
2. For every DFA’s state {qi , q j ,..., qm }
DFA M
q0 a
q1, q2
q0 , a q1, q2
106
δ *(2, b) = {3}, δ *(3, b) = φ
{3} {2,3}
107
Procedure NFA to DFA
108
Example
NFA M a
q0 a q1 q2
b
a
DFA M b
q0 a
q1, q2
b
a, b
109
Procedure NFA to DFA
3. For any DFA state {qi , q j ,..., qm }
110
Example
NFA M a
q0 a q1 q2 q1 F
b
a
DFA M b
q0 a
q1, q2
b q1, q2 F
a, b
111
Theorem
Take NFA M
L M L M
112
Proof
L M L M
L M L M AND L M L M
113
First we show: L M L M
114
w L(M )
M: q0 w qf
w 1 2 k
1 2 k
M: q0 qf
115
We will show that if w L(M )
w 1 2 k
1 2 k
M: q0 qf
1 2 k
M:
{q0 } {q f ,}
w L(M )
116
More generally, we will show that if in M:
(arbitrary string) v a1a2 an
a1 a2 an
M: q0 qi qj ql qm
an
M:
a1 a2
{q0 } {qi ,} {q j ,} {ql ,} {qm ,}
117
Proof by induction on |v|
Induction Basis: v a1
a1
M: q0 qi
M:
a1
{q0 } {qi ,}
118
Induction hypothesis: 1 | v | k
v a1a2 ak
a1 a2 ak
M: q0 qi qj qc qd
M:
a1 a2 ak
{q0 } {qi ,} {q j ,} {qc ,} {qd ,}
119
Induction Step: | v | k 1
v a1a2 ak ak 1 vak 1
v
a1 a2 ak
M: q0 qi qj qc qd
v
M:
a1 a2 ak
{q0 } {qi ,} {q j ,} {qc ,} {qd ,}
v 120
Induction Step: | v | k 1
v a1a2 ak ak 1 vak 1
v
a1 a2 ak ak 1
M: q0 qi qj qc qd qe
v
M:
a1 a2 ak ak 1
{q0 } {qi ,} {q j ,} {qc ,} {qd ,} {qe ,}
v 121
Therefore if w L(M )
w 1 2 k
1 2 k
M: q0 qf
1 2 k
M:
{q0 } {q f ,}
w L(M )
122
We have shown: L M L M
(proof is similar)
123
The End
124
Single Final State for NFAs
1
Any NFA can be converted
to an equivalent NFA
2
Example
a NFA
a b
a Equivalent NFA
a b
b
3
In General
NFA
Equivalent NFA
Single
final state
4
Extreme Case
5
Properties of
Regular Languages
6
For regular languages L1 and L2
we will prove that:
Union: L1 L2
Concatenation: L1L2
Star: Are regular
L1 *
Languages
Reversal: L1R
Complement: L1
Intersection: L1 L2
7
We say: Regular languages are closed under
Union: L1 L2
Concatenation: L1L2
Star: L1 *
Reversal: L1R
Complement: L1
Intersection: L1 L2
8
Regular language L1 Regular language L2
LM1 L1 LM 2 L2
NFA M1 NFA M2
M2
L2 ba b a
10
Union
NFA for L1 L2
M1
M2
11
Example
NFA for L1 L2 {a b} {ba}
n
L1 {a b}n
a
b
L2 {ba}
b a
12
Concatenation
M1 M2
13
Example
L1 {a b}n
a L2 {ba}
b b a
14
Star Operation
NFA for L1 *
L1 *
M1
15
Example
w w1w2 wk
NFA for L1* {a b} *
n
wi L1
L1 {a b} n
a
b
16
Reverse
R
NFA for L1
L1 M1 M1
M1
a
R
L1 {ba }
n b
18
Complement
L1 M1 L1 M1
L1 {a b}
n b a, b
M1
a a, b
L1 {a, b} * {a b}
n
b a, b
20
Intersection
DeMorgan’s Law: L1 L2 L1 L2
L1 , L2 regular
L1 , L2 regular
L1 L2 regular
L1 L2 regular
L1 L2 regular
21
Example
L1 {a b} regular
n
L1 L2 {ab}
L2 {ab, ba} regular regular
22
Regular Expressions
23
Regular Expressions
Regular expressions
describe regular languages
Example: (a b c) *
24
Recursive Definition
Primitive regular expressions: , ,
r1 r2
r1 r2
Are regular expressions
r1 *
r1
25
Examples
26
Languages of Regular Expressions
Example
27
Definition
L
L
La a
28
Definition (continued)
Lr1 * Lr1 *
Lr1 Lr1
29
Example
Regular expression: a b a *
La b a * La b La *
La b La *
La Lb La *
a b a *
a, b , a, aa, aaa,...
a, aa, aaa,..., b, ba, baa,...
30
Example
Regular expression r a b * a bb
31
Example
Lr {a b
2n 2m
b : n, m 0}
32
Example
Regular expression r (0 1) * 00 (0 1) *
33
Example
34
Equivalent Regular Expressions
Definition:
35
Example
L = { all strings without
two consecutive 0 }
r1 (1 01) * (0 )
r2 (1* 011*) * (0 ) 1* (0 )
r1 and r2
L(r1) L(r2 ) L
are equivalent
regular expr.
36
Regular Expressions
and
Regular Languages
37
Theorem
Languages
Generated by
Regular Expressions
Regular
Languages
38
Theorem - Part 1
Languages
Generated by Regular
Languages
Regular Expressions
39
Theorem - Part 2
Languages
Generated by Regular
Languages
Regular Expressions
40
Proof - Part 1
41
Induction Basis
Primitive Regular Expressions: , ,
NFAs
L( M1) L()
regular
L( M 2 ) {} L( )
languages
a
L( M 3 ) {a} L(a)
42
Inductive Hypothesis
Assume
for regular expressions r1 and r2
that
L(r1) and L(r2 ) are regular languages
43
Inductive Step
We will prove:
Lr1 r2
Lr1 r2
Are regular
Languages
Lr1 *
Lr1
44
By definition of regular expressions:
Lr1 * Lr1 *
Lr1 Lr1
45
By inductive hypothesis we know:
L(r1) and L(r2 ) are regular languages
We also know:
Regular languages are closed under:
Union Lr1 Lr2
Concatenation Lr1 Lr2
Star Lr1 *
46
Therefore:
Are regular
Lr1 r2 Lr1 Lr2
languages
Lr1 * Lr1 *
47
And trivially:
48
Proof – Part 2
49
Since L is regular take the
NFA M that accepts it
L( M ) L
Example:
M
a c a c
a, b ab
51
b b
Another Example:
a
q0 q1 a, b q2
b
b b
a
q0 q1 a b q2
b
52
b b
Reducing the states:
a
q0 q1 a b q2
b
bb * a b
q0 bb * (a b) q2
53
Resulting Regular Expression:
bb * a b
q0 bb * (a b) q2
r (bb * a) * bb * (a b)b *
L( r ) L( M ) L
54
In General
Removing states: e
d c
qi q qj
a b
ae * d ce* b
ce* d
qi qj
ae * b
55
The final transition graph:
r1 r4
r3
q0 qf
r2
The resulting regular expression:
r r1 * r2 (r4 r3r1 * r2 ) *
L( r ) L( M ) L
56
Example
a
1
2
a
b
b a
57
Example (Cont.)
b
a
1 2
a
ε ε
b a
b a
s
3 ε
58
Example (Cont.)
aa∪b
2
a
ε
s ab ba∪a a
b ε
bb
59
Example (Cont.)
a(aa∪b)*
s
a
a(aa∪b)*ab∪b (ba∪a)(aa∪b)*∪ε
(ba∪a)(aa∪b)*ab∪bb
60
Example (Cont.)
s a
(a(aa∪b)*ab ∪b)((ba ∪a)(aa ∪b)*ab ∪bb) *((ba ∪ a)(aa ∪b)* ∪ ε) ∪ a(aa ∪b)*
61
The End
62
Grammars
1
Grammars
Grammars express languages
predicate verb
2
article a
article the
noun cat
noun dog
verb runs
verb walks
3
A derivation of “the dog walks”:
4
A derivation of “a cat runs”:
5
Language of the grammar:
L = { “a cat runs”,
“a cat walks”,
“the cat runs”,
“the cat walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks” }
6
Notation
Production Rules
noun cat
noun dog
Variable Terminal
7
Another Example
Grammar: S aSb
S
Derivation of sentence ab :
S aSb ab
S aSb S
8
Grammar: S aSb
S
S aSb S
9
Other derivations:
10
Language of the grammar
S aSb
S
L {a b : n 0}
n n
11
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 }
13
More Notation
Sentential Form:
A sentence that contains
variables and terminals
Example:
Instead of:
15
*
In general we write: w1 wn
If: w1 w2 w3 wn
16
*
By default: w w
17
Example
Grammar Derivations
S aSb *
S
S
*
S ab
*
S aabb
*
S aaabbb
18
Example
Grammar Derivations
S aSb
S aaSbb
S
aaSbb aaaaaSbbbbb
19
Another Grammar Example
Grammar G : S Ab
A aAb
A
Derivations:
S ⇒Ab ⇒b
S ⇒Ab ⇒aAbb ⇒abb
S ⇒Ab ⇒aAbb ⇒aaAbbb ⇒aabbb
20
More Derivations
S Ab aAbb aaAbbb aaaAbbbb
aaaaAbbbbb aaaabbbbb
S aaaabbbbb
S aaaaaabbbbbbb
S a b b
n n
21
Language of a Grammar
For a grammar G
with start variable S :
L(G ) {w : S w}
String of terminals
22
Example
For grammar G: S Ab
A aAb
A
L(G ) {a b b : n 0}
n n
Since: S a b b n n
23
A Convenient Notation
A aAb
A aAb |
A
article a
article a | the
article the
24
Linear Grammars
25
Linear Grammars
Grammars with
at most one variable at the right side
of a production
Examples: S aSb S Ab
S A aAb
A
26
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
27
Another Linear Grammar
Grammar G : SA
A aB |
B Ab
L(G ) {a b : n 0}
n n
28
Right-Linear Grammars
All productions have form: A xB
or
A x
29
Left-Linear Grammars
All productions have form: A Bx
or
A x
Ba
30
Regular Grammars
31
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
32
Observation
Regular grammars generate regular languages
Examples: G2
G1 S Aab
S abS A Aab | B
S a Ba
34
Theorem
Languages
Generated by
Regular Grammars
Regular
Languages
35
Theorem - Part 1
Languages
Generated by Regular
Languages
Regular Grammars
36
Theorem - Part 2
Languages
Generated by Regular
Languages
Regular Grammars
37
Proof – Part 1
Languages
Generated by Regular
Languages
Regular Grammars
38
The case of Right-Linear Grammars
39
Grammar G is right-linear
Example: S aA | B
A aa B
Bb B|a
40
Construct NFA M such that
every state is a grammar variable:
A
special
S VF
final state
B
S aA | B
A aa B
Bb B|a 41
Add edges for each production:
a A
S VF
B
S aA
42
a A
S VF
B
S aA | B
43
A
a a
S a VF
B
S aA | B
A aa B
44
A
a a
S a VF
B
S aA | B
b
A aa B
B bB 45
A
a a
S a VF
a
B
S aA | B
b
A aa B
B bB | a 46
A
a a
S a VF
a
B
Vi a1a2 am
49
We construct the NFA M such that:
V1 V3
V0
VF
V2 V4 special
final state
50
For each production: Vi a1a2 amV j
Vi a1 a2 ………
am V
j
51
For each production: Vi a1a2 am
Vi a1 a2 ………
am
VF
52
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
grammar G with L(G ) L(G) R
54
Since G is left-linear grammar
the productions look like:
A Ba1a2 ak
A a1a2 ak
55
Construct right-linear grammar G
Left A Ba1a2 ak
G
linear
A → Bv
Right
G A ak a2a1B
linear
Av B R
56
Construct right-linear grammar G
Left
G A a1a2 ak
linear
Av
Right
G A ak a2a1
linear
Av R
57
It is easy to see that: L(G ) L(G) R
L(G) L(G) R
L(G )
Regular Regular Regular
Language Language Language
58
Proof - Part 2
Languages
Generated by Regular
Languages
Regular Grammars
59
Any regular language L is generated
by some regular grammar G
Proof idea:
Let M be the NFA with L L(M ).
M a
a
q0 q1 q2
q0 aq1 b
q3
62
b
M a
a
q0 q1 q2
q0 aq1
b
q1 bq1 q3
q1 aq2
63
b
M a
a
q0 q1 q2
q0 aq1
q1 bq1 b
q1 aq2 q3
q2 bq3
64
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
65
In General
a
For any transition: q p
Add production: q ap
Add production: qf
67
Since G is right-linear grammar
with L(G ) L( M ) L
68
The End
69
Standard Representations
of Regular Languages
Regular Languages
DFAs
Regular
Grammars
NFAs
Regular
Expressions
1
When we say: We are given
a Regular Language L
2
What are the differences among NFA/DFA,
regular expression and regular grammar?
3
Elementary Questions
about
Regular Languages
4
Membership Question
Question: Given regular language L
and string w
how can we check if w L?
5
DFA
w
w L
DFA
w
w L
6
Question: Given regular language L
how can we check
if L is empty: ( L ) ?
L
DFA
L
8
Question: Given regular language L
how can we check
if L is finite?
L is infinite
DFA
L is finite
10
Question: Given regular languages L1 and L2
how can we check if L1 L2 ?
Answer: Find if ( L1 L2 ) ( L1 L2 )
11
( L1 L2 ) ( L1 L2 )
L1 L2 and L1 L2
L1 L2 L L2 L1 L1
2
L1 L2 L2 L1
L1 L2
12
( L1 L2 ) ( L1 L2 )
L1 L2 or L1 L2
L1 L2 L2 L1
L1 L2 L2 L1
L1 L2
13
Non-regular languages
14
{a b : n 0}
n n
Non-regular languages
{vv : v {a, b}*}
R
Regular languages
a *b b*c a
b c ( a b) *
etc...
Finite languages are regular 15
How can we prove that a language L
is not regular?
Ha Ha Ha……^-^
16
The Pigeonhole Principle
17
4 pigeons
3 pigeonholes
18
A pigeonhole must
contain at least two pigeons
19
n pigeons
...........
m pigeonholes nm
...........
20
The Pigeonhole Principle
n pigeons
m pigeonholes
There is a pigeonhole
nm with at least 2 pigeons
...........
21
Ex 1: Show that if any five numbers from 1
to 8 are chosen, then two of them will add
up to 9.
Solution: {1,8},{2,7},{3,6},{4,5}
22
Ex 2: show that if any 11 numbers are chosen
from the set {1,2,…,20}, then one of them
will be a multiple of another.
23
Ex 3: Consider the region shown in the
figure. It is bounded by a regular hexagon
whose sides are of length 1 unit. Show that
if any seven points are chosen in this
region, then two of them must be no
farther apart than 1 unit.
24
Ex 4: shirts numbered consecutively from 1
to 20 are worn by the 20 members of a
bowling league. When any 3 of these
members are chosen to be a team, the sum
of their shirt numbers is used as a code
number for the team. Show that if any 8 of
the 20 members are selected, then from
theses 8 we may form at least two
different teams having the same code
number. (one member can be in several
different teams simultaneously)
and
DFAs
26
DFA with 4 states
b
b b
q1 a q2 b q3 b q4
a a
27
In walks of strings: a no state
aa is repeated
aab
b
b b
q1 a q2 a q3 b q4
a a
28
In walks of strings: aabb a state
is repeated
bbaa
abbabb
abbbabbabb...
b
b b
q1 a q2 a q3 b q4
a a
29
If string w has length | w | 4 :
Then the transitions of string w
are more than the states of the DFA
q1 a q2 a q3 b q4
a a
30
In general, for any DFA:
walk of w
...... q ......
Repeated state 31
In other words for a string w:
walk of w
...... q ......
Repeated state
32
The Pumping Lemma
33
The Pumping Lemma:
• Given a infinite regular language L
• there exists an integer m
• for any string w L with length | w| m
• we can write w x y z
• with | x y | m and | y | 1
• such that: xy z L
i i 0, 1, 2, ...
34
Applications
of
35
Theorem: The language L {a nb n : n 0}
is not regular
36
L {a b : n 0}
n n
Since L is infinite
we can apply the Pumping Lemma
37
L {a b : n 0}
n n
We pick wa b
m m
38
Write: a b xyz
m m
Thus: y a , k 1
k
39
x y za bm m
y a , k 1
k
i 0, 1, 2, ...
Thus: xy z L
2
40
x y za b m m
y a , k 1
k
mk m
xy z a...aa...aa...aa...ab...b L
2
x y y z
m k m
Thus: a b L
41
m k m k ≥1
a b L
BUT: L {a b : n 0}
n n
m k m
a b L
CONTRADICTION!!!
42
Therefore: Our assumption that L
is a regular language is not true
43
Non-regular languages {a b : n 0}
n n
Regular languages
44
Understanding pumping lemma more
45
Take an infinite regular language L
m
states
46
Take string w with w L
.........
walk w
47
If string w has length | w | m (number
of states
of DFA)
...... q ......
walk w
48
Let q be the first state repeated in the
walk of w
...... q ......
walk w
49
Write w x y z
...... q ......
x z 50
Observations: length | x y | m number
of states
length | y | 1 of DFA
...... q ......
x z 51
Observation: The string xz
is accepted
...... q ......
x z 52
Observation: The string xyyz
is accepted
...... q ......
x z 53
Observation: The string xyyyz
is accepted
...... q ......
x z 54
i
In General: The string xy z
is accepted i 0, 1, 2, ...
...... q ......
x z 55
In General: i
x y z ∈L i 0, 1, 2, ...
...... q ......
x z 56
In other words, we described:
57
More Applications
of
1
The Pumping Lemma:
• Given a infinite regular language L
• there exists an integer m
• for any string w L with length | w| m
• we can write w x y z
• with | x y | m and | y | 1
• such that: xy z L
i i 0, 1, 2, ...
2
Non-regular languages L {vv : v *}
R
Regular languages
3
Theorem: The language
L {vv : v *}
R
{a, b}
is not regular
4
L {vv : v *}
R
Since L is infinite
we can apply the Pumping Lemma
5
L {vv : v *}
R
length | w| m
We pick wa b b a
m m m m
6
Write a b b a xyz
m m m m
m m m m
xyz a...aa...a...ab...bb...ba...a
x y z
Thus: y a , k 1
k
7
x y za b b a
m m m m
y a , k 1
k
i 0, 1, 2, ...
Thus: xy z L
2
8
x y za b b a
m m m m
y a , k 1
k
m+k m m m
2
xy z = a...aa...aa...a...ab...bb...ba...a ∈L
x y y z
m k m m m
Thus: a b b a L
9
m k m m m
a b b a L k 1
m k m m m
a b b a L
CONTRADICTION!!!
10
Therefore: Our assumption that L
is a regular language is not true
11
Non-regular languages
n l n l
L {a b c : n, l 0}
Regular languages
12
Theorem: The language
n l n l
L {a b c : n, l 0}
is not regular
13
n l n l
L {a b c : n, l 0}
Since L is infinite
we can apply the Pumping Lemma
14
n l n l
L {a b c : n, l 0}
Let m be the integer in the Pumping Lemma
length | w| m
We pick wa b c m m 2m
15
Write
m m 2m
a b c xyz
m m 2m
xyz a...aa...aa...ab...bc...cc...c
x y z
Thus: y a , k 1
k
16
x y za b c
m m 2m
y a , k 1
k
i 0, 1, 2, ...
0
Thus: x y z = xz ∈ L
17
x y za b c
m m 2m
y a , k 1
k
mk m 2 m
Thus: a b c L
18
mk m 2 m
a b c L k 1
BUT: n l n l
L {a b c : n, l 0}
mk m 2 m
a b c L
CONTRADICTION!!!
19
Therefore: Our assumption that L
is a regular language is not true
20
Non-regular languages L {a : n 0}
n!
Regular languages
21
Theorem: The language L {a : n 0}
n!
is not regular
n! 1 2 (n 1) n
22
L {a : n 0}
n!
Since L is infinite
we can apply the Pumping Lemma
23
L {a : n 0}
n!
We pick wa m!
24
Write a m!
xyz
Thus: y a , 1 k m
k
25
x y za m!
y a , 1 k m
k
i 0, 1, 2, ...
Thus: xy z L
2
26
x y za m!
y a , 1 k m
k
mk m!m
xy z a...aa...aa...aa...aa...aa...a L
2
x y y z
Thus: m! k
a L
27
m! k
a L 1 k m
Since: L {a : n 0}
n!
m! k p!
28
However: m! k m! m for m 1
m! m!
m!m m!
m!(m 1)
(m 1)!
m! k (m 1)!
BUT: L {a : n 0}
n!
m! k
a L
CONTRADICTION!!!
30
Therefore: Our assumption that L
is a regular language is not true
31
Context-Free Languages
1
n n R
{a b : n 0} {ww }
Regular Languages
a *b * ( a b) *
2
Context-Free Languages
n n R
{a b } {ww }
Regular Languages
3
Context-Free Languages
Context-Free Pushdown
Grammars Automata
stack
automaton
4
Context-Free Grammars
5
Example
A derivation:
Another derivation:
L(G ) {a b : n 0}
n n
8
Example
A derivation:
Another derivation:
11
Example
A derivation:
S SS aSbS abS ab
12
A context-free grammar G: S aSb
S SS
S
A derivation:
L(G ) {w : na ( w) nb ( w),
and na (v) nb (v)
in any prefix v}
14
Definition: Context-Free Grammars
Grammar G (V ,T , S , P)
*
L(G ) {w : S w, w T *}
16
Definition: Context-Free Languages
A language L is context-free
if and only if
17
Derivation Order
1. S AB 2. A aaA 4. B Bb
3. A 5. B
Leftmost derivation:
1 2 3 4 5
S AB aaAB aaB aaBb aab
Rightmost derivation:
1 4 5 2 3
S AB ABb Ab aaAb aab
18
S aAB
A bBb
B A|
Leftmost derivation:
S aAB abBbB abAbB abbBbbB
abbbbB abbbb
Rightmost derivation:
S aAB aA abBb abAb
abbBbb abbbb
19
Derivation Trees
20
S AB A aaA | B Bb |
S AB
S
A B
21
S AB A aaA | B Bb |
S AB aaAB
S
A B
a a A
22
S AB A aaA | B Bb |
S AB aaAB aaABb
S
A B
a a A B b
23
S AB A aaA | B Bb |
A B
a a A B b
24
S AB A aaA | B Bb |
A B
a a A B b
25
S AB A aaA | B Bb |
A B
yield
a a A B b aab
aab
26
Partial Derivation Trees
S AB A aaA | B Bb |
S AB
Partial derivation tree S
A B
27
S AB aaAB
A B
a a A
28
sentential
S AB aaAB
form
A B
yield
a a A
aaAB
29
Sometimes, derivation order doesn’t matter
Leftmost:
S AB aaAB aaB aaBb aab
Rightmost:
S AB ABb Ab aaAb aab
S
Same derivation tree
A B
a a A B b
30
Ambiguity
31
E E E | E E | (E) | a
a aa
E E E E a E a EE
a a E a a*a
E E
leftmost derivation
a E E
a a
32
E E E | E E | (E) | a
a aa
E E a
a a
33
E E E | E E | (E) | a
a aa
Two derivation trees
E E
E E E E
a E E E E a
a a a a
34
The grammarE E E | E E | (E) | a
is ambiguous:
E E
E E E E
a E E E E a
a a a a
35
The grammarE E E | E E | (E) | a
is ambiguous:
E E E a E a EE
a a E a a*a
37
In other words:
38
Why do we care about ambiguity?
a aa
take a2
E E
E E E E
a E E E E a
a a a a
39
2 22
E E
E E E E
2 E E E E 2
2 2 2 2
40
2 22 6 2 22 8
6 8
E E
2 4 4 2
E E E E
2 2 2 2
2 E E E E 2
2 2 2 2
41
Correct result: 2 22 6
6
E
2 4
E E
2 2
2 E E
2 2
42
• Ambiguity is bad for programming languages
43
We fix the ambiguous grammar:
E E E | E E | (E) | a
E a aa
E T
T T F
F F a
a a
46
The grammar G: E E T
E T
T T F
T F
F (E)
F a
is non-ambiguous:
Every string w L(G ) has
a unique derivation tree
47
Another Ambiguous Grammar
48
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
IF_STMT
Example: L {a b c } {a b c }
n n m n m m
S S1 | S2 S1 S1c | A S2 aS2 | B
A aAb | B bBc |
50
n n n
The string a b c
has two derivation trees
S S
S1 S2
S1 c a S2
51
Simplifications
of
Context-Free Grammars
1
A Substitution Rule
Equivalent
grammar
S aB
S aB | ab
A aaA
Substitute A aaA
A abBc B b A abBc | abbc
B aA
B aA
Bb
2
A Substitution Rule
S aB | ab
A aaA
A abBc | abbc
B aA
Substitute
B aA
S aB | ab | aaA
Equivalent
A aaA
A abBc | abbc | abaAc
grammar
3
In general:
A xBz
B y1
Substitute
B y1
equivalent
A xBz | xy1z grammar
4
Nullable Variables
production : A
5
Removing Nullable Variables
Example Grammar:
S aMb
M aMb
M
Nullable variable
6
Final Grammar
S aMb
S aMb
Substitute S ab
M aMb M
M aMb
M
M ab
7
Unit-Productions
Unit Production: A B
8
Removing Unit Productions
Observation:
A A
Is removed immediately
9
Example Grammar:
S aA
Aa
A B
BA
B bb
10
S aA
S aA | aB
Aa
Substitute Aa
A B A B B A| B
BA
B bb
B bb
11
S aA | aB S aA | aB
Aa Remove Aa
B A| B BB BA
B bb B bb
12
S aA | aB
S aA | aB | aA
Aa Substitute
BA Aa
BA
B bb
B bb
13
Remove repeated productions
Final grammar
S aA | aB | aA S aA | aB
Aa Aa
B bb B bb
14
Useless Productions
S aSb
S
SA
A aA Useless Production
S A aA aaA aaaA
15
Another grammar:
SA
A aA
A
B bA Useless Production
Not reachable from S
16
In general: contains only
terminals
if S xAy w
w L(G )
17
A production A x is useless
if any of its variables is useless
S aSb
S Productions
Variables S A useless
useless A aA useless
useless B C useless
Example Grammar:
S aS | A | C
Aa
B aa
C aCb
19
First: find all variables that can produce
strings with only terminals
S aS | A | C Round 1: { A, B}
Aa SA
B aa
C aCb Round 2: { A, B, S}
20
Keep only the variables
that produce terminal symbols: { A, B, S}
(the rest variables are useless)
S aS | A | C
Aa S aS | A
B aa Aa
C aCb B aa
Remove useless productions
21
Second: Find all variables
reachable from S
S aS | A
Aa S A B
B aa not
reachable
22
Keep only the variables
reachable from S
(the rest variables are useless)
Final Grammar
S aS | A
S aS | A
Aa
Aa
B aa
23
Removing All
24
Normal Forms
for
Context-free Grammars
25
Chomsky Normal Form
A BC or Aa
26
Examples:
S AS S AS
S a S AAS
A SA A SA
Ab A aa
Chomsky Not Chomsky
Normal Form Normal Form
27
Convertion to Chomsky Normal Form
Example: S ABa
A aab
B Ac
Not Chomsky
Normal Form
28
Introduce variables for terminals: Ta , Tb , Tc
S ABTa
S ABa A TaTaTb
A aab B ATc
B Ac Ta a
Tb b
Tc c
29
Introduce intermediate variable: V1
S AV1
S ABTa
V1 BTa
A TaTaTb
A TaTaTb
B ATc
B ATc
Ta a
Ta a
Tb b
Tb b
Tc c
Tc c
30
Introduce intermediate variable: V2
S AV1
S AV1
V1 BTa
V1 BTa
A TaV2
A TaTaTb
V2 TaTb
B ATc
B ATc
Ta a
Ta a
Tb b
Tb b
Tc c
Tc c 31
Final grammar in Chomsky Normal Form:
S AV1
V1 BTa
A TaV2
Initial grammar
V2 TaTb
S ABa B ATc
A aab Ta a
B Ac Tb b
Tc c 32
In general:
we can obtain:
An equivalent grammar
in Chomsky Normal Form
33
The Procedure
First remove:
Nullable variables
Unit productions
34
Then, for every symbol a:
Add production Ta a
New variable: Ta
35
Replace any production A C1C2 Cn
with A C1V1
V1 C2V2
Vn2 Cn1Cn
37
Observations
38
Pushdown Automata
PDAs
1
Pushdown Automaton -- PDA
Input String
Stack
States
2
Initial Stack Symbol
Stack
stack
$
head
bottom
special symbol
3
The States
q1 a, b c q2
4
a, b c
q1 q2
input
a a
stack
b top c
h Replace h
e e
$ $
5
a, c
q1 q2
input
a a
stack c
b top b
h Push h
e e
$ $
6
a, b
q1 q2
input
a a
stack
b top
h Pop h
e e
$ $
7
a,
q1 q2
input
a a
stack
b top b
h No Change h
e e
$ $
8
A Possible Transition
a, $
q1 q2
input
a a
stack empty
$ top Pop
9
A Bad Transition
a, b c
q1 q2
input
a
Empty stack
HALT
a, c
q1 q2
input
a
Empty stack
HALT
x, y z
q1 q2
Empty stack
12
A Good Transition
a, $ b
q1 q2
input
a a
stack
$ top Pop b
13
Non-Determinism
a, b c q2
q1 , b c
q1 q2
a, b c transition
q3
Example:
a, a b, a
q0 , q b, a q , $ $ q
1 2 3
15
Execution Example: Time 0
Input
a a a b b b
$
Stack
current a, a b, a
state
q0 , q b, a q , $ $ q
1 2 3
16
Time 1
Input
a a a b b b
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
17
Time 2
Input
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
18
Time 3
Input a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
19
Time 4
a
Input
a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
20
Time 5
a
Input
a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
21
Time 6
Input a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
22
Time 7
Input
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
23
Time 8
Input
a a a b b b
$
Stack
a, a b, a
accept
q0 , q1 b, a q2 , $ $ q3
24
A string is accepted if there is
a computation such that:
a, a b, a
q0 , q1 b, a q2 , $ $ q3
26
In general,
L {a b : n 0}
n n
a, a b, a
q0 , q1 b, a q2 , $ $ q3
27
Another NPDA example
NPDA M
L( M ) {ww }R
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
28
Execution Example: Time 0
Input
a b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
29
Time 1
Input
a b b a a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
30
Time 2
Input
b
a b b a a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
31
Time 3
Input
b
a b b a
Guess the middle a
of string $
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
32
Time 4
Input
b
a b b a a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
33
Time 5
Input
a b b a a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
34
Time 6
Input
a b b a
$
Stack
a, a a, a
b, b b, b
accept
q0 , q1 , $ $ q2
35
Rejection Example: Time 0
Input
a b b b
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
36
Time 1
Input
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
37
Time 2
Input
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
38
Time 3
Input
b
a b b b
Guess the middle a
of string $
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
39
Time 4
Input
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
40
Time 5
Input There is no possible transition.
a b b b Input is not a
consumed
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
41
Another computation on same string:
Input Time 0
a b b b
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
42
Time 1
Input
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
43
Time 2
Input
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
44
Time 3
Input b
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
45
Time 4 b
Input b
b
a b b b a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
46
Time 5 b
Input b
No final state b
a b b b is reached a
$
Stack
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
47
There is no computation
that accepts string abbb
abbb L(M )
a, a a, a
b, b b, b
q0 , q1 , $ $ q2
48
A string is rejected if there is
no computation such that:
50
Another NPDA example
NPDA M
a, a
b, a
b, $
q0
51
Execution Example: Time 0
Input
a a b
a, a $
b, a Stack
b, $
q0
52
Time 1
Input
a a b a
a, a $
b, a Stack
b, $
q0
53
Time 2
Input
a
a a b a
a, a $
b, a Stack
b, $
q0
54
Time 3
Input
a a b a
a, a $
b, a Stack
b, $
accept
q0
55
Rejection example: Time 0
Input
a b b b
$
Stack
q0
56
Time 1
Input
a b b b a
a, a $
b, a Stack
b, $
q0
57
Time 2
Input
a b b b
a, a $
b, a Stack
b, $
q0
58
Time 3
Input
a b b b
a, a
b, a Stack
b, $
q0
59
Time 4
Input
a b b b
a, a
b, a Stack
b, $
q1 a, b w q2
61
Example:
a, b cdf
q1 q2
input
a a
c pushed
stack d
top string
b f
h Push h
e e
$ $
62
Another NPDA example
NPDA M
L( M ) {w : na nb }
a, $ 0$ b, $ 1$
a, 0 00 b, 1 11
a, 1 b, 0
q1 , $ $ q2
63
Execution Example: Time 0
Input
a b b a a b
$
a, $ 0$ b, $ 1$
Stack
a, 0 00 b, 1 11
a, 1 b, 0
current
state
q1 , $ $ q2
64
Time 1
Input
a b b a a b
0
$
a, $ 0$ b, $ 1$ Stack
a, 0 00 b, 1 11
a, 1 b, 0
q1 , $ $ q2
65
Time 3
Input
a b b b a a
0
$
a, $ 0$ b, $ 1$ Stack
a, 0 00 b, 1 11
a, 1 b, 0
q1 , $ $ q2
66
Time 4
Input
a b b b a a
1
$
a, $ 0$ b, $ 1$ Stack
a, 0 00 b, 1 11
a, 1 b, 0
q1 , $ $ q2
67
Time 5
Input
a b b b a a 1
1
$
a, $ 0$ b, $ 1$ Stack
a, 0 00 b, 1 11
a, 1 b, 0
q1 , $ $ q2
68
Time 6
Input
a b b b a a 1
1
$
a, $ 0$ b, $ 1$ Stack
a, 0 00 b, 1 11
a, 1 b, 0
q1 , $ $ q2
69
Time 7
Input
a b b b a a
1
$
a, $ 0$ b, $ 1$ Stack
a, 0 00 b, 1 11
a, 1 b, 0
q1 , $ $ q2
70
Time 8
Input
a b b b a a
$
a, $ 0$ b, $ 1$
Stack
a, 0 00 b, 1 11
a, 1 b, 0
accept
q1 , $ $ q2
71
Formalities for NPDAs
72
q1 a, b w q2
Transition function:
73
q2
a, b w
q1
a, b w q3
Transition function:
Input Stack
alphabet Transition Initial start
Stack
function state symbol
alphabet
75
Instantaneous Description
( q, u , s )
Current Current
Remaining
state stack
input
contents
76
Example: Instantaneous Description
(q1, bbb, aaa$)
a
Time 4: Input a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
77
Example: Instantaneous Description
(q2 , bb, aa$)
a
Time 5: Input a
a a a b b b a
$
Stack
a, a b, a
q0 , q1 b, a q2 , $ $ q3
78
We write:
79
A computation:
a, a b, a
q0 , q1 b, a q2 , $ $ q3
80
(q0 , aaabbb,$) (q1, aaabbb,$)
(q1, aabbb, a$) (q1, abbb, aa$) (q1, bbb, aaa$)
(q2 , bb, aa$) (q2 , b, a$) (q2 , ,$) (q3 , ,$)
(q0 , aaabbb,$) (q3 , ,$)
81
Formal Definition
L( M ) {w : (q0 , w, s ) (q f , , s' )}
82
Example:
(q0 , aaabbb,$) (q3 , ,$)
aaabbb L(M )
NPDA M :
a, a b, a
q0 , q1 b, a q2 , $ $ q3
83
(q0 , a b ,$) (q3 , ,$)
n n
a b L(M )
n n
NPDA M :
a, a b, a
q0 , q1 b, a q2 , $ $ q3
84
Therefore: L( M ) {a b : n 0}
n n
NPDA M :
a, a b, a
q0 , q1 b, a q2 , $ $ q3
85