Anda di halaman 1dari 97

PDAs Accept

Context-Free Languages

1
Theorem:
Context-
Languages

=
Free
Accepted
Languages
by
(Grammars
PDAs
)

2
Proof - Step 1:
Context-
Languages


Free
Accepted
Languages
by
(Grammars
PDAs
)

Convert any context-free grammar G


to a PDA M with: L(G ) = L ( M )

3
Proof - Step 2:
Context-
Languages


Free
Accepted
Languages
by
(Grammars
PDAs
)

Convert any PDA M to a context-free


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

4
Proof - step 1
Converting

Context-Free Grammars
to
PDAs

5
Context-
Languages


Free
Accepted
Languages
by
(Grammars
PDAs
)

Convert any context-free grammar G


to a PDA M with: L(G ) = L ( M )

6
We will convert grammar G

to a PDA M such that:

M simulates leftmost derivations of G

7
Convert grammar G to PDA M

Production in G Terminal in G
A→w a

λ, A → w a, a → λ

q0 λ, λ → S q1 λ, $ → $ q2
8
Grammar PDA computation
Simulates grammar
leftmost
leftmost derivations
derivation
(q0 , σ 1 σ kσ k +1 σ n ,$)
S  (q1 , σ 1 σ kσ k +1 σ n , S $)
⇒ 
⇒ σ 1 σ k X 1  X m  (q1 , σ k +1 σ n , X 1  X m $)
⇒ 
⇒ σ 1 σ kσ k +1 σ n  (q2 , λ ,$)

Leftmost
variable
9
Example
Grammar
S → aSTb
S →b PDA

T → Ta λ , S → aSTb
T →λ λ, S → b
λ , T → Ta a, a → λ
λ, T → λ b, b → λ

q0 λ, λ → S q1 λ, $ → $ q2
10
Grammar PDA computation
derivation
(q0 , abab,$)
S  (q1 , abab, S $)
⇒ aSTb  (q1 , bab, STb$)
 (q1 , bab, bTb$)
⇒ abTb
 (q1 , ab, Tb$)
⇒ abTab  (q1 , ab, Tab$)
⇒ abab  (q1 , ab, ab$)
 (q1 , b, b$)
 (q1 , λ ,$)
 (q2 , λ ,$)

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
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
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
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
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
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
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
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
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
20
Derivation: S ⇒ aSTb ⇒ abTb ⇒ abTab ⇒ abab

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

q0 λ, λ → S q1 λ, $ → $ q2
21
Derivation: S ⇒ aSTb ⇒ abTb ⇒ abTab ⇒ abab

Input a b a b

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

q0 λ, λ → S q1 λ, $ → $ q2
22
In general, it can be shown that:

Grammar G
generates If PDA M
and accepts w
string w
Only
*
if (q0 , w,$)  (q2 , λ ,$)
S⇒w

Therefore L(G ) = L( M )
23
Therefore:

For any context-free language L


there is a PDA that accepts L

Context-
Languages


Free
Accepted
Languages
by
(Grammars
PDAs
)
24
Proof - step 2
Converting

PDAs
to
Context-Free Grammars

25
Context-
Languages


Free
Accepted
Languages
by
(Grammars
PDAs
)

Convert any PDA M to a context-free


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

26
We can convert PDA M to

a context-free grammar G such that:

G simulates computations of M
with leftmost derivations

27
1. Modify the PDA so that
the stack is never empty
during computation

Stack

a
$ $
OK OK NOT OK

28
Introduce the new symbol # to mark
the bottom of the stack

a
$ $

a
$ $
# # #
29
At the beginning insert # into the stack
Original PDA
new
initial state
λ ,$ → $#

original
initial state
30
Convert all transitions so that
after popping $ the automaton halts

pop $ σ,ξ → s q
qi j

∀x ∈ Γ − {# }
pop $ qi
λ, x → x σ,ξ → s qj
λ,
#→
#
halting state
31
2. Modify the PDA so that at end
it empties stack and
has a unique accept state
Empty stack
PDA ∀x ∈ Γ − {# }
λλ
, λ, x → λ
→λ
λ ,λ →λ
λ, #→ λ q
f
λ →λ
λ, New
accept
Old accept states state
32
Deterministic PDAs - DPDAs

33
Deterministic PDA: DPDA
Allowed transitions:

a, b → w
q1 q2

λ, b → w
q1 q2

(deterministic choices)
34
Allowed transitions:

a, b → w1 q2 λ , b → w1 q2

q1 q1

a , c → w2 q3 λ , c → w2 q3

(deterministic choices)
35
Not allowed:

a, b → w1 q2 λ , b → w1 q2

q1 q1

a , b → w2 q3 a , b → w2 q3

(non deterministic choices)


36
DPDA example

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

a, λ → a b, a → λ

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

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

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

is deterministic context-free

38
Example of Non-DPDA (PDA)

L( M ) = {vv : v ∈ {a, b} }
R *

a, λ → a a, a → λ
b, λ → b b, b → λ

q0 λ, λ → λ q1 λ, $ → $ q2

39
Not allowed in DPDAs

a, λ → a a, a → λ
b, λ → b b, b → λ

q0 λ, λ → λ q1 λ, $ → $ q2

40
PDAs

Have More Power than

DPDAs

41
It holds that:
Determini
stic Context-
Context-
Free ⊆ Free
Languages
Languages PDAs
(DPDA)

Since every DPDA is also a PDA

42
We will actually show:
Determini
stic Context-
Context-
Free ⊂ Free
Languages
Languages (PDA)
(DPDA)
L∉ L∈

We will show that there exists


a context-free language L which is not
accepted by any DPDA
43
The language is:

n n n 2n
L = {a b } ∪ {a b } n≥0

We will show:

• L is context-free

• L is not deterministic context-free

44
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 }
45
Theorem:
n n n 2n
The language L = {a b } ∪ {a b }
is not deterministic context-free

(there is no DPDA that accepts L)

46
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

47
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 b
48
n n n 2n
DPDA M with L( M ) = {a b } ∪ {a b }

Such a path exists due to determinism

M
n n n
a b b

49
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)
50
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)
51
We will construct a PDA that accepts:
n n n
L ∪ {a b c }

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

which is a contradiction!

52
n n n 2n
DPDA M L( M ) = {a b } ∪ {a b }
a nb n bn

Modify M Replace b
with c

DPDA M′ L( M ′) = {a nc n } ∪ {a nc 2n }
a nc n cn

53
n n n
A PDA that accepts L ∪ {a b c }
Connect the final states of M
with the final states of M ′
M
a nb n bn

λ λ
M′
a nc n cn

54
n n n
Since L ∪ {a b c } is accepted by a PDA

it is context-free

Contradiction!

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

55
Therefore:

n n n 2n
L = {a b } ∪ {a b }
Is not deterministic context free

There is no DPDA that accepts it

End of Proof
56
Positive Properties
of
Context-Free languages

57
Union

Context-free languages
are closed under: Union

L1 is context free
L1 ∪ L2
L2 is context free is context-free

58
Example
Language Grammar

n n
L1 = {a b } S1 → aS1b | λ

R
L2 = {ww } S 2 → aS 2 a | bS 2b | λ

Union
n n R S → S1 | S 2
L = {a b } ∪ {ww }
59
In general:
For context-free languages L1, L2
with context-free grammars G1, G2
and start variables S1, S 2

The grammar of the union L1 ∪ L2


has new start variable S
and additional production S → S1 | S 2

60
Concatenation

Context-free languages
are closed under: Concatenation

L1 is context free
L1L2
L2 is context free is context-free

61
Example
Language Grammar

n n
L1 = {a b } S1 → aS1b | λ

R
L2 = {ww } S 2 → aS 2 a | bS 2b | λ

Concatenation
n n R S → S1S 2
L = {a b }{ww }
62
In general:

For context-free languages L1, L2


with context-free grammars G1, G2
and start variables S1, S 2

The grammar of the concatenation L1L2


has new start variable S
and additional production S → S1S 2

63
Star Operation

Context-free languages
are closed under: Star-operation

L *
is context free L is context-free

64
Example

Language Grammar

n n
L = {a b } S → aSb | λ

Star Operation

n n
L = {a b } * S1 → SS1 | λ
65
In general:

For context-free language L


with context-free grammar G
and start variable S

The grammar of the star operation L *


has new start variable S1
and additional production S1 → SS1 | λ

66
Negative Properties
of
Context-Free Languages

67
Intersection

Context-free languages
are not closed under: intersection

L1 is context free
L1 ∩ L2
L2 is context free not necessarily
context-free

68
Example
n n m n m m
L1 = {a b c } L2 = {a b c }
Context-free: Context-free:
S → AC S → AB
A → aAb | λ A → aA | λ
C → cC | λ B → bBc | λ
Intersection
n n n
L1 ∩ L2 = {a b c } NOT context-free
69
Complement

Context-free languages
are not closed under: complement

L is context free L not necessarily


context-free

70
Example
n n m n m m
L1 = {a b c } L2 = {a b c }
Context-free: Context-free:
S → AC S → AB
A → aAb | λ A → aA | λ
C → cC | λ B → bBc | λ
Complement
n n n
L1 ∪ L2 = L1 ∩ L2 = {a b c }
NOT context-free
71
Intersection
of
Context-free languages
and
Regular Languages

72
The intersection of
a context-free language and
a regular language
is a context-free language

L1 context free
L1 ∩ L2
L2 regular context-free

73
Machine M1 Machine M2
NPDA for L1 DFA for L2
context-free regular

Construct a new NPDA machine M


that accepts L1 ∩ L2

M simulates in parallel M1 and M2


74
NPDA M1 DFA M2
a, b → c a
q1 q2 p1 p2
transition transition

NPDA M

q1, p1 a , b → c
q2 , p2
transition
75
NPDA M1 DFA M2
λ, b → c
q1 q2 p1
transition

NPDA M

q1, p1 λ , b → c
q2 , p1

transition
76
NPDA M1 DFA M2

q0 p0
initial state initial state

NPDA M

q0 , p0
Initial state
77
NPDA M1 DFA M2

q1 p1 p2
final state final states

NPDA M

q1, p1 q1, p2
final states
78
Example: context-free
* *
L1 = {w1w2 : | w1 |=| w2 |, w1 ∈{a, b} , w2 ∈{c, d } }

NPDA M1
a, λ → 1 c,1 → λ
b, λ → 1 d ,1 → λ

q0 λ , λ → λ q1 λ , λ → λ q2 λ , λ → λ q3

79
regular
*
L2 = {a, c}

DFA M2
a, c

p0

80
context-free
n n
Automaton for: L1 ∩ L2 = {a c : n ≥ 0}

NPDA M
a, λ → 1 c,1 → λ

q0 , p0 λ , λ → λ q1, p0 λ , λ → λ q2 , p0 λ , λ → λ q3 , p0

81
In General:

M simulates in parallel M1 and M2

M accepts string w if and only if

M1 accepts string w and


M2 accepts string w

L( M ) = L( M1 ) ∩ L( M 2 )
82
Therefore:
M is NPDA

L ( M1 ) ∩ L ( M 2 ) is context-free

L1 ∩ L2 is context-free

83
Applications
of
Regular Closure

84
The intersection of
a context-free language and
a regular language
is a context-free language

Regular Closure
L1 context free
L1 ∩ L2
L2 regular context-free

85
An Application of Regular Closure

n n
Prove that: L = {a b : n ≠ 100, n ≥ 0}

is context-free

86
We know:

n n
{a b : n ≥ 0} is context-free

87
We also know:

100 100
L1 = {a b } is regular

* 100 100 is regular


L1 = {( a + b) } − {a b }

88
n n * 100 100
{a b } L1 = {( a + b) } − {a b }
context-free regular

n n
(regular closure) {a b } ∩ L1 context-free

n n n n
{a b } ∩ L1 = {a b : n ≠ 100, n ≥ 0} = L
is context-free
89
Another Application of Regular Closure

Prove that: L = {w : na = nb = nc }

is not context-free

90
If L = {w : na = nb = nc } is context-free

(regular closure)
n n n
Then L ∩ {a * b * c*} = {a b c }

context-free regular context-free


Impossible!!!

Therefore, L is not context free


91
Decidable Properties
of
Context-Free Languages

92
Membership Question:

for context-free grammar G


find if string w∈ L (G )

Membership Algorithms: Parsers

• Exhaustive search parser

• CYK parsing algorithm

93
Empty Language Question:

for context-free grammar G


find if L (G ) = ∅

Algorithm:

1. Remove useless variables

2. Check if start variable S is useless


94
Infinite Language Question:
for context-free grammar G
find if L (G ) is infinite

Algorithm:
1. Remove useless variables
2. Remove unit and λ productions

3. Create dependency graph for variables

4. If there is a loop in the dependency graph


then the language is infinite
95
Example: S → AB
A → aCb | a
B → bB | bb
C → cBS
Dependency graph Infinite language

A C
S
B
96
S → AB
A → aCb | a
B → bB | bb
C → cBS

S ⇒ AB ⇒ aCbB ⇒ acBSbB ⇒ acbbSbbb


∗ ∗
2 2
S ⇒ acbbSbbb ⇒(acbb) S (bbb)

i i
⇒(acbb) S (bbb)
97