Languages and Finite Automata
Download
Report
Transcript Languages and Finite Automata
PDAs Accept
Context-Free Languages
Prof. Busch - LSU
1
Theorem:
Context-Free
Languages
(Grammars)
Prof. Busch - LSU
Languages
Accepted by
PDAs
2
Proof - Step 1:
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
Convert any context-free grammar G
to a PDA M with: L(G ) L( M )
Prof. Busch - LSU
3
Proof - Step 2:
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
Convert any PDA M to a context-free
grammar G with: L(G ) L( M )
Prof. Busch - LSU
4
Proof - step 1
Convert
Context-Free Grammars
to
PDAs
Prof. Busch - LSU
5
Take an arbitrary context-free grammar
We will convert
G
G
to a PDA M such that:
L(G) L( M )
Prof. Busch - LSU
6
Conversion Procedure:
For each
production in
For each
terminal in
G
Aw
G
a
Add transitions
, A w
q0
, S
a, a
q1
Prof. Busch - LSU
, $ $
q2
7
Example
Grammar
S aSTb
S b
T Ta
T
q0
PDA
, S aSTb
, S b
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
8
PDA simulates leftmost derivations
Grammar
Leftmost Derivation
PDA Computation
(q0 , 1 k k 1 n ,$)
S
(q1 , 1 k k 1 n , S $)
1 k X 1 X m
1 k k 1 n
(q2 , ,$)
Scanned
symbols
(q1 , k 1 n , X 1 X m $)
Stack
contents
Prof. Busch - LSU
9
Grammar
Leftmost Derivation
Terminals
Leftmost
variable
Variables
or terminals
xAy
x i j Bzy
Production applied
Variables
or terminals
A i j Bz
Terminals
Variable
Prof. Busch - LSU
10
Grammar
Leftmost Derivation
PDA Computation
xAy
(q1 , i n , Ay $)
x i j Bzy
Production applied
A i j Bz
(q1 , i n , i j Bzy $)
Transition applied
, A i j Bz
q0 , S
Prof. Busch - LSU
q1
, $ $
q2
11
Grammar
Leftmost Derivation
PDA Computation
xAy
(q1 , i n , Ay $)
x i j Bzy
(q1 , i n , i j Bzy $)
(q1 , i 1 n , i 1 j Bzy $)
Transition applied
Read i from input
and remove it from stack
q0 , S
Prof. Busch - LSU
i ,i
q1
, $ $
q2
12
Grammar
Leftmost Derivation
xAy
(q1 , i n , Ay $)
x i j Bzy
PDA Computation
(q1 , i n , i j Bzy $)
(q1 , i 1 n , i 1 j Bzy $)
(q1 , j 1 n , Bzy $)
All symbols i j
have been removed
from top of stack
Last Transition applied
j , j
q0 , S
Prof. Busch - LSU
q1
, $ $
q2
13
The process repeats with the next
leftmost variable
xAy
x i j Bzy
(q1 , j 1 n , Bzy $)
x i j j 1 k Cpzy
(q1 , j 1 n , j 1 k Cpzy $)
(q1 , k 1 n , Cpzy $)
Production applied
B j 1 kCp
And so on……
Prof. Busch - LSU
14
Example:
Input
Time 0
q0
a b a b
, S aSTb
$
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
15
Derivation: S
Input
Time 1
q0
a b a b
S
$
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
16
Derivation: S aSTb
Input
Time 2
q0
a
S
T
b
$
a b a b
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
17
Derivation: S aSTb
Input
Time 3
q0
a
S
T
b
$
a b a b
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
18
Derivation: S aSTb abTb
Input
Time 4
q0
b
T
b
$
a b a b
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
19
Derivation: S aSTb abTb
Input
Time 5
q0
b
T
b
$
a b a b
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
20
Derivation: S aSTb abTb abTab
Input
Time 6
q0
T
a
b
$
a b a b
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
21
Derivation: S aSTb abTb abTab abab
Input
Time 7
q0
T
a
b
$
a b a b
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
22
Derivation: S aSTb abTb abTab abab
Input
Time 8
q0
a b a b
a
b
$
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
23
Derivation: S aSTb abTb abTab abab
Input
Time 9
q0
a b a b
b
$
, S aSTb
, S b
Stack
a, a
, T Ta
b, b
, T
, S
q1
Prof. Busch - LSU
, $ $
q2
24
Derivation: S aSTb abTb abTab abab
Input
a b a b
Time 10
, S aSTb
$
, S b
Stack
a, a
, T Ta
b, b
, T
accept
q0
, S
q1
Prof. Busch - LSU
, $ $
q2
25
Grammar
Leftmost Derivation
S
aSTb
abTb
abTab
abab
PDA Computation
(q0 , abab,$)
(q1 , abab, S $)
(q1 , bab, STb$)
(q1 , bab, bTb$)
(q1 , ab, Tb$)
(q1 , ab, Tab$)
(q1 , ab, ab$)
(q1 , b, b$)
(q1 , ,$)
(q2 , ,$)
Prof. Busch - LSU
26
In general, it can be shown that:
Grammar
generates
string w
*
G
If and
Only if
Sw
Therefore
PDA M
accepts
w
*
(q0 , w,$)(q2 , ,$)
L(G) L( M )
Prof. Busch - LSU
27
Proof - step 2
Convert
PDAs
to
Context-Free Grammars
Prof. Busch - LSU
28
Take an arbitrary PDA M
We will convert M
to a context-free grammar G such that:
L(M ) L(G )
Prof. Busch - LSU
29
First modify PDA
M so that:
1. The PDA has a single accept state
2. Use new initial stack symbol #
3. On acceptance the stack contains only
stack symbol # (this symbol is not used in any transition)
4. Each transition either pushes a symbol
or pops a symbol but not both together
Prof. Busch - LSU
30
1. The PDA has a single accept state
PDA
PDA
M1
M
Old
accept
states
New
accept
state
qf
Prof. Busch - LSU
31
2. Use new initial stack symbol #
Top of stack
initial stack symbol of M
Z
@
auxiliary stack symbol
#
new initial stack symbol
PDA M2
, @
, Z
PDA M1
M1 still thinks that Z is the initial stack
Prof. Busch - LSU
32
3. On acceptance the stack contains only
stack symbol #
(this symbol is not used in any transition)
PDA M3
Empty stack
x {@, # }
PDA M2
Old
accept
state
, x
,
Prof. Busch - LSU
, @
New
accept
state
qf
33
4. Each transition either pushes a symbol
or pops a symbol but not both together
PDA
PDA
M4
M3
qi
, a b
,
a
qi
Prof. Busch - LSU
qj
, b q
j
34
PDA M3
PDA
,
qi
M4
Where
, q
qi
j
,
qj
is a symbol of the stack alphabet
Prof. Busch - LSU
35
PDA
M4
is the final modified PDA
Note that the new initial stack symbol #
is never used in any transition
Prof. Busch - LSU
36
Example:
M
a, a
b, a
q
M4
, a
, b
, Z
a, a
b, a
q0
, @
q1
q2
, a
, Z
q3
, a
Prof. Busch - LSU
q4
, @
q5
37
Grammar Construction
Variables:
Aqi ,q j
States of PDA
Prof. Busch - LSU
38
PDA
Kind 1: for each state
q
Grammar
Aqq
Prof. Busch - LSU
39
PDA
Kind 2: for every three states
p
q
r
Grammar
Apq Apr Arq
Prof. Busch - LSU
40
PDA
Kind 3: for every pair of such transitions
p
a, t
r
s
b,t q
Grammar
Apq aArs b
Prof. Busch - LSU
41
PDA
Initial state
Accept state
qf
q0
Grammar
Start variable
Aq qf
0
Prof. Busch - LSU
42
Example:
PDA
M4
, a
, b
, Z
a, a
b, a
q0
, @
q1
q2
, a
, Z
q3
, a
Prof. Busch - LSU
q4
, @
q5
43
Grammar
Kind 1: from single states
Aq q
0 0
Aq q
1 1
Aq q
2 2
Aq q
3 3
Aq q
4 4
Aq q
5 5
Prof. Busch - LSU
44
Kind 2: from triplets of states
Aq q Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q
0 0
0 0
0 0
0 1
1 0
0 2
2 0
0 3
3 0
0 4
4 0
0 5
5 0
Aq q Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q
0 1
0 0
0 1
0 1
1 1
0 2
2 1
0 3
3 1
0 4
4 1
0 5
5 1
Aq q Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q
0 5
0 0
0 5
0 1
1 5
0 2
2 5
0 3
3 5
0 4
4 5
0 5
5 5
Aq q Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q | Aq q Aq q
5 5
5 0
0 5
5 1
1 5
5 2
Start variable
2 5
5 3
3 5
5 4
4 5
5 5
5 5
Aq q
0 5
Prof. Busch - LSU
45
Kind 3: from pairs of transitions
M4
q0
, @
q1
1 4
Aq q Aq q
1 4
q2
, a
, Z
Aq q Aq q
0 5
, a
, b
, Z
a, a
b, a
2 4
q3
, a
q4
, @
q5
Aq q aAq q
Aq q aAq q b
Aq q Aq q b
Aq q aAq q
Aq q Aq q
2 4
2 2
2 4
2 2
2 4
2 3
Prof. Busch - LSU
2 2
3 2
Aq q Aq q
2 4
2 4
3 3
3 4
46
Suppose that a PDA M is converted
to a context-free grammar G
We need to prove that
L(G ) L(M )
or equivalently
L(G ) L(M )
L(G ) L(M )
Prof. Busch - LSU
47
L(G ) L(M )
We need to show that if
Aq qf w
0
G
has derivation:
(string of terminals)
Then there is an accepting computation in
M
:
(q0 ,w , # ) (qf , , # )
with input string
w
Prof. Busch - LSU
48
We will actually show that if G has derivation:
Apq w
Then there is a computation in
M
:
( p ,w , ) (q , , )
Prof. Busch - LSU
49
Therefore:
Aq qf w
0
(q0 ,w , ) (qf , , )
Since there is no transition
with the # symbol
(q0 ,w , # ) (qf , , # )
Prof. Busch - LSU
50
Lemma:
If
Apq w (string of terminals)
then there is a computation
from state p to state q on string
which leaves the stack empty:
w
( p ,w , ) (q , , )
Prof. Busch - LSU
51
Proof Intuition:
Apq w
Type 2
Case 1:
Apq Apr Arq w
Type 3
Case 2:
Apq aArs b w
Prof. Busch - LSU
52
Type 2
Case 1:
Apq Apr Arq w
Stack
height
p
Input string
Generated by
Apr
Prof. Busch - LSU
r
Generated by
q
Arq
53
Type 3
Case 2:
Apq aArs b w
Stack
height
r
p
Input string
s
a
Generated by
Prof. Busch - LSU
Ars
b q
54
Formal Proof:
We formally prove this claim
by induction on the number
of steps in derivation:
Apq w
number of steps
Prof. Busch - LSU
55
Induction Basis:
Apq w
(one derivation step)
A Kind 1 production must have been used:
App
Therefore, p q
and
w
This computation of PDA trivially exists:
( p , , ) ( p , , )
Prof. Busch - LSU
56
Induction Hypothesis:
Apq w
k
derivation steps
suppose it holds:
( p ,w , ) (q , , )
Prof. Busch - LSU
57
Induction Step:
Apq w
k 1
derivation steps
We have to show:
( p ,w , ) (q , , )
Prof. Busch - LSU
58
Apq w
k 1
derivation steps
Type 2
Case 1:
Apq Apr Arq w
Type 3
Case 2:
Apq aArs b w
Prof. Busch - LSU
59
Type 2
Case 1:
Apq Apr Arq w
k 1 steps
We can write
w yz
Apr y
Arq z
At most k steps
At most k steps
Prof. Busch - LSU
60
Apr y
Arq z
At most k steps
From induction
hypothesis, in PDA:
( p , y , ) (r , , )
At most k steps
From induction
hypothesis, in PDA:
(r , z , ) (q , , )
Prof. Busch - LSU
61
(r , z , ) (q , , )
( p , y , ) (r , , )
( p , yz , ) (r , z , ) (q , , )
since w yz
( p ,w , ) (q , , )
Prof. Busch - LSU
62
Type 3
Case 2:
Apq aArs b w
k 1
We can write
steps
w ayb
Ars y
At most k steps
Prof. Busch - LSU
63
Ars y
At most k steps
From induction hypothesis,
the PDA has computation:
(r , y , ) (s , , )
Prof. Busch - LSU
64
Type 3
Apq aArs b w
Grammar contains production
Apq aArs b
And PDA Contains transitions
p
a, t
r
s
Prof. Busch - LSU
b,t q
65
p
a, t
r
s
b,t q
(s , b,t ) (q , , )
( p, ayb, ) (r , yb,t )
Prof. Busch - LSU
66
We know
(r , y , ) (s , , )
(r , yb,t ) (s , b ,t )
( p, ayb, ) (r , yb,t )
We also know
(s , b,t ) (q , , )
Therefore:
( p , ayb, ) (r , yb,t ) (s , b ,t ) (q , , )
Prof. Busch - LSU
67
( p , ayb, ) (r , yb,t ) (s , b ,t ) (q , , )
since w ayb
( p ,w , ) (q , , )
END OF PROOF
Prof. Busch - LSU
68
So far we have shown:
L(G ) L(M )
With a similar proof we can show
L(G ) L(M )
Therefore:
L(G ) L(M )
Prof. Busch - LSU
69