Sie sind auf Seite 1von 51

NPDAs Accept

Context-Free Languages

Costas Busch - RPI 1


Theorem:


Context-Free Languages
Languages Accepted by
(Grammars) NPDAs

Costas Busch - RPI 2


Proof - Step 1:


Context-Free Languages
Languages Accepted by
(Grammars) NPDAs

Convert any context-free grammar G


to a NPDA M with: L(G ) L ( M )

Costas Busch - RPI 3


Proof - Step 2:


Context-Free Languages
Languages Accepted by
(Grammars) NPDAs

Convert any NPDA M to a context-free


grammar G with: L(G ) L ( M )

Costas Busch - RPI 4


Proof - step 1

Converting
Context-Free Grammars
to
NPDAs

Costas Busch - RPI 5


We will convert any context-free grammar G

to an NPDA automaton M

Such that:

M Simulates leftmost derivations of G

Costas Busch - RPI 6


Leftmost derivation
G: S 1 2 kV1V2 Vm

Input Stack
processed contents
leftmost variable

M: Simulation of derivation Stack


V1
Input V2
1 2 k k 1
Vm
$
Costas Busch - RPI 7
Leftmost derivation
G: S 1 2 n
string of terminals

M: Simulation of derivation Stack

Input
1 2 n $

end of input is reached


Costas Busch - RPI 8
An example grammar: S aSTb
S b
T Ta
T

What is the equivalent NPDA?

Costas Busch - RPI 9


Grammar:
S aSTb
S b NPDA:
T Ta
, S aSTb
T
, S b
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 10
Grammar: S aSTb
S b
T Ta
T

A leftmost derivation:

S aSTb abTb abTab abab

Costas Busch - RPI 11


Derivation:

Input a b a b

, S aSTb $
Time 0
, S b Stack
, T Ta a, a
, T b, b

, S , $ $
q0 q1 q2
Costas Busch - RPI 12
Derivation: S

Input a b a b
S
, S aSTb $
Time 0
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 13
Derivation: S aSTb a
S
Input a b a b
T
b
, S aSTb $
Time 1
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 14
Derivation: S aSTb a
S
Input a b a b
T
b
, S aSTb $
Time 2
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 15
Derivation: S aSTb abTb
b
Input a b a b
T
b
, S aSTb $
Time 3
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 16
Derivation: S aSTb abTb
b
Input a b a b
T
b
, S aSTb $
Time 4
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 17
Derivation: S aSTb abTb abTab
T
Input a b a b a
b
, S aSTb $
Time 5
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 18
Derivation: S aSTb abTb abTab abab
T
Input a b a b a
b
, S aSTb $
Time 6
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 19
Derivation: S aSTb abTb abTab abab

Input a b a b a
b
, S aSTb $
Time 7
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 20
Derivation:

Input a b a b
b
, S aSTb $
Time 8
, S b Stack
, T Ta a, a
, T b, b

q0 , S q1 , $ $ q2
Costas Busch - RPI 21
Derivation:

Input a b a b

, S aSTb $
Time 9
, S b Stack
, T Ta a, a
, T b, b
accept

q0 , S q1 , $ $ q2
Costas Busch - RPI 22
In general:

Given any grammar G

We can construct a NPDA M

With L(G ) L( M )

Costas Busch - RPI 23


Constructing NPDA M from grammar G:

For any production For any terminal


Aw a

, A w a, a

q0 , S q1 , $ $ q2
Costas Busch - RPI 24
Grammar G generates string w

if and only if

NPDA M accepts w

L(G ) L( M )

Costas Busch - RPI 25


Therefore:

For any context-free language


there is a NPDA
that accepts the same language


Context-Free Languages
Languages Accepted by
(Grammars) NPDAs

Costas Busch - RPI 26


Deterministic PDA

DPDA

Costas Busch - RPI 27


Deterministic PDA: DPDA
Allowed transitions:

a, b w
q1 q2

, b w
q1 q2

(deterministic choices)
Costas Busch - RPI 28
Allowed transitions:

a, b w1 q2 , b w1 q2

q1 q1

a , c w2 q3 , c w2 q3

(deterministic choices)
Costas Busch - RPI 29
Not allowed:

a, b w1 q2 , b w1 q2

q1 q1

a , b w2 q3 a , b w2 q3

(non deterministic choices)


Costas Busch - RPI 30
DPDA example

n n
L( M ) {a b : n 0}

a, a b, a

q0 a, a q1 b, a q2 , $ $ q3

Costas Busch - RPI 31


n n
The language L( M ) {a b : n 0}

is deterministic context-free

Costas Busch - RPI 32


Definition:
A language L is deterministic context-free
if there exists some DPDA that accepts it

Costas Busch - RPI 33


Example of Non-DPDA (NPDA)
R
L( M ) {ww }

a, a a, a
b, b b, b

q0 , q1 , $ $ q2

Costas Busch - RPI 34


Not allowed in DPDAs

a, a a, a
b, b b, b

q0 , q1 , $ $ q2

Costas Busch - RPI 35


NPDAs

Have More Power than

DPDAs

Costas Busch - RPI 36


It holds that:

Deterministic


Context-Free
Context-Free
Languages
Languages
NPDAs
(DPDA)

Since every DPDA is also a NPDA

Costas Busch - RPI 37


We will actually show:

Deterministic


Context-Free
Context-Free
Languages
Languages
(NPDA)
(DPDA)
L L

We will show that there exists


a context-free language L which is not
accepted by any DPDA
Costas Busch - RPI 38
The language is:

n n n 2n
L {a b } {a b } n0

We will show:

L is context-free

L is not deterministic context-free

Costas Busch - RPI 39


n n n 2n
L {a b } {a b }

Language L is context-free

Context-free grammar for L :


S S1 | S 2 {a nb n } {a nb 2n }
n n
S1 aS1b | {a b }

S 2 aS2bb | n 2n
{a b }
Costas Busch - RPI 40
Theorem:
n n n 2n
The language L {a b } {a b }
is not deterministic context-free

(there is no DPDA that accepts L)

Costas Busch - RPI 41


Proof: Assume for contradiction that
n n n 2n
L {a b } {a b }
is deterministic context free

Therefore:

there is a DPDA M that accepts L

Costas Busch - RPI 42


n n n 2n
DPDA M with L( M ) {a b } {a b }

n n
accepts a b

n n n
a b b

n 2n
accepts a
Costas Busch - RPI
b 43
n n n 2n
DPDA M with L( M ) {a b } {a b }

Such a path exists because of the determinism

M
n n n
a b b

Costas Busch - RPI 44


Fact 1: n n n
The language {a b c }
is not context-free

Context-free languages
a nb n

Regular languages
a *b *

(we will prove this at a later class using


pumping lemma for context-free languages)
Costas Busch - RPI 45
n n n
Fact 2: The language L {a b c }
is not context-free

n n n 2n
( L {a b } {a b })

(we can prove this using pumping lemma


for context-free languages)
Costas Busch - RPI 46
We will construct a NPDA that accepts:
n n n
L {a b c }

n n n 2n
( L {a b } {a b })

which is a contradiction!

Costas Busch - RPI 47


n n n 2n
M L( M ) {a b } {a b }
a nb n bn

Modify M Replace b
with c

M L( M ) {a nc n } {a nc 2n }
a nc n cn

Costas Busch - RPI 48


n n n
The NPDA that accepts L {a b c }
Connect final states of M
with final states of M
M
a nc n c n


M
a nb n bn

Costas Busch - RPI 49


n n n
Since L {a b c } is accepted by a NPDA

it is context-free

Contradiction!

n n n
(since L {a b c } is not context-free)

Costas Busch - RPI 50


Therefore:

Not deterministic context free


n n n 2n
L {a b } {a b }
There is no DPDA that accepts

End of Proof
Costas Busch - RPI 51

Das könnte Ihnen auch gefallen