Professional Documents
Culture Documents
Context-Free Languages
1
Theorem:
Context-Free Languages
Languages Accepted by
(Grammars) PDAs
2
Proof - Step 1:
Context-Free Languages
Languages Accepted by
(Grammars) PDAs
3
Proof - Step 2:
Context-Free Languages
Languages Accepted by
(Grammars) PDAs
4
Proof - step 1
Converting
Context-Free Grammars
to
PDAs
5
Context-Free Languages
Languages Accepted by
(Grammars) PDAs
6
We will convert grammar G
7
Convert grammar G to PDA M
Production in G Terminal in G
Aw 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 PDA M
If and accepts w
string w
Only if
* (q0 , w,$) (q2 , ,$)
Sw
Therefore L(G ) L( M )
23
Therefore:
Context-Free Languages
Languages Accepted by
(Grammars) PDAs
24
Proof - step 2
Converting
PDAs
to
Context-Free Grammars
25
Context-Free Languages
Languages Accepted by
(Grammars) PDAs
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
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
DPDAs
41
It holds that:
Deterministic
Context-Free
Context-Free
Languages
Languages
PDAs
(DPDA)
42
We will actually show:
Deterministic
Context-Free
Context-Free
Languages
Languages
(PDA)
(DPDA)
L L
n n n 2n
L {a b } {a b } n0
We will show:
• L is context-free
44
n n n 2n
L {a b } {a b }
Language L is context-free
S2 aS2bb | n 2n
{a b }
45
Theorem:
n n n 2n
The language L {a b } {a b }
is not deterministic context-free
46
Proof: Assume for contradiction that
n n n 2n
L {a b } {a b }
is deterministic context free
Therefore:
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 }
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 *
n n n 2n
( L {a b } {a b })
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
n n n
a b b
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
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 } S2 aS2 a | bS2b |
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
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 } S2 aS2 a | bS2b |
Concatenation
n n R S S1S 2
L {a b }{ww }
62
In general:
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:
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
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
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
Automaton for: L1 L2 {a c : n 0} n n
NPDA M
a, 1 c,1
q0 , p0 , q1, p0 , q2 , p0 , q3 , p0
81
In General:
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
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 }
92
Membership Question:
93
Empty Language Question:
Algorithm:
Algorithm:
1. Remove useless variables
2. Remove unit and productions
A C
S
B
96
S AB
A aCb | a
B bB | bb
C cBS