Post on 26-Mar-2015
3
Context-Free
Languages(Grammars)
LanguagesAccepted
byPDAs
Proof - Step 1:
Convert any context-free grammar to a PDA with:
GM )()( MLGL
4
Context-Free
Languages(Grammars)
LanguagesAccepted
byPDAs
Proof - Step 2:
Convert any PDA to a context-free grammar with: G
M)()( MLGL
6
Context-Free
Languages(Grammars)
LanguagesAccepted
byPDAs
Convert any context-free grammar to a PDA with:
GM )()( MLGL
9
nkk
mk XX
S
11
11
Grammarleftmost derivation
,$),(
$),,(
$),,(
,$),(
2
111
111
110
q
XXq
Sq
q
mnk
nkk
nkk
PDA computationSimulates grammar leftmost derivations
Leftmostvariable
11
abab
abTab
abTb
aSTb
S
,$),(
,$),(
$),,(
$),,(
$),,(
$),,(
$),,(
$),,(
$),,(
,$),(
2
1
1
1
1
1
1
1
1
0
q
q
bbq
ababq
Tababq
Tbabq
bTbbabq
STbbabq
Sababq
ababq
Grammarderivation
PDA computation
14
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
a
b
ST
q1
Time 1
Derivation: aSTbS
15
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
a
b
ST
q1
Time 2
aSTbS Derivation:
16
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
bTb
q1
Time 3
Derivation: abTbaSTbS
17
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
bTb
q1
Time 4
Derivation: abTbaSTbS
18
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
b
Ta
q1
Time 5
Derivation: abTababTbaSTbS
19
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
b
Ta
q1
Time 6
Derivation: abababTababTbaSTbS
20
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
ba
q1
Time 7
Derivation: abababTababTbaSTbS
21
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
b
q1
Time 8
Derivation: abababTababTbaSTbS
22
q0 q1 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
accept
Time 9
Derivation: abababTababTbaSTbS
23
Grammar generates string
G
wS*
PDA accepts
M
,$),(,$),( 20 qwq
In general, it can be shown that:
w wIf andOnly if
Therefore )()( MLGL
24
Therefore:
For any context-free language there is a PDA that accepts
Context-Free
Languages(Grammars)
LanguagesAccepted
byPDAs
LL
26
Context-Free
Languages(Grammars)
LanguagesAccepted
byPDAs
Convert any PDA to a context-free grammar with: G
M)()( MLGL
27
We can convert PDA toM
a context-free grammar such that:G
Msimulates computations ofwith leftmost derivations
G
28
PDA
fq
x,
,
Empty stack
}{#x
Modify the PDA so that at end it empties stack and has a unique accept state
,
,
Old accept states
#,
New accept state
34
}0:{)( nbaML nnThe language
is deterministic context-free
Definition:
A language is deterministic context-freeif there exists some DPDA that accepts it
L
Example:
38
Deterministic
Context-FreeLanguages
(DPDA)
Context-Free
LanguagesPDAs
Since every DPDA is also a PDA
It holds that:
39
We will actually show:
We will show that there existsa context-free language which is notaccepted by any DPDA
L
LL
Deterministic
Context-FreeLanguages
(DPDA)
Context-Free
Languages(PDA)
40
The language is:
}{}{ 2nnnn babaL 0n
We will show:
L• is context-free
L• is not deterministic context-free
41
}{}{ 2nnnn babaL
Language is context-freeL
Context-free grammar for : L
21 | SSS
|11 baSS
|22 bbaSS
}{ nnba
}{ 2nnba
}{}{ 2nnnn baba
42
}{}{ 2nnnn babaL
is not deterministic context-free
Theorem:
The language
(there is no DPDA that accepts ) L
43
Proof: Assume for contradiction that
}{}{ 2nnnn babaL
is deterministic context free
Therefore:
there is a DPDA that accepts M L
46
The language is not context-free
}{ nnn cba
(we will prove this at a later class usingpumping lemma for context-free languages)
Fact 1:
Regular languages**ba
Context-free languagesnnba
47
The language is not context-free
}{ nnn cbaL
}){}{( 2nnnn babaL
Fact 2:
(we can prove this using pumping lemma for context-free languages)
50
A PDA that accepts }{ nnn cbaL
nnba nb
nc
M
M
nnca
Connect the final states of with the final states of M
M
51
Since is accepted by a PDA }{ nnn cbaL
it is context-free
Contradiction!
(since is not context-free)}{ nnn cbaL
52
Therefore:
}{}{ 2nnnn babaL
There is no DPDA that accepts it
End of Proof
Is not deterministic context free
54
Context-free languages are closed under: Union
1L is context free
2L is context free
21 LL
is context-free
Union
56
In general:
The grammar of the union has new start variableand additional production 21 | SSS
For context-free languageswith context-free grammarsand start variables
21, LL
21, GG
21, SS
21 LL S
57
Context-free languages are closed under: Concatenation
1L is context free
2L is context free
21LL
is context-free
Concatenation
58
Example
|11 baSS
|| 222 bbSaaSS
Concatenation
}{1nnbaL
}{2RwwL
21SSS }}{{ Rnn wwbaL
Language Grammar
59
In general:
The grammar of the concatenation has new start variableand additional production 21SSS
For context-free languageswith context-free grammarsand start variables
21, LL
21, GG
21, SS
21LLS
60
Context-free languages are closed under: Star-operation
L is context free*L is context-free
Star Operation
62
In general:
The grammar of the star operation has new start variableand additional production
For context-free languagewith context-free grammarand start variable
LGS
*L1S
|11 SSS
64
Context-free languagesare not closed under: intersection
1L is context free
2L is context free
21 LL
not necessarilycontext-free
Intersection
65
Example
}{1mnn cbaL
|
|
cCC
aAbA
ACS
Context-free:
}{2mmn cbaL
|
|
bBcB
aAA
ABS
Context-free:
}{21nnn cbaLL NOT context-free
Intersection
66
Context-free languagesare not closed under: complement
L is context free L not necessarilycontext-free
Complement
67
}{2121nnn cbaLLLL
NOT context-free
Example
}{1mnn cbaL
|
|
cCC
aAbA
ACS
Context-free:
}{2mmn cbaL
|
|
bBcB
aAA
ABS
Context-free:
Complement
69
The intersection of a context-free language and a regular languageis a context-free language
1L context free
2L regular
21 LL
context-free
70
1Lfor for 2LNPDA
1M
DFA
2M
Construct a new NPDA machinethat accepts
Machine Machine
M21 LL
context-free regular
M simulates in parallel and 1M 2M
75
Example:
, , ,
1, b1, a
1,d1,c
0q 1q 2q 3q
1M
}},{,},{|,|||:{ *2
*121211 dcwbawwwwwL
NPDA
context-free
78
M simulates in parallel and 1M 2M
M accepts string w if and only if
accepts string and w1M
accepts string w2M
)()()( 21 MLMLML
In General:
81
The intersection of a context-free language and a regular languageis a context-free language
1L context free
2L regular
21 LL
context-free
Regular Closure
85
}{}){( 100100*1 babaL
regular
}{ nnbacontext-free
1}{ Lba nn context-free
LnnbaLba nnnn }0,100:{}{ 1
is context-free
(regular closure)
87
}:{ cba nnnwL
}{*}**{ nnn cbacbaL
context-free regular context-free
If is context-free
Then
Impossible!!!
Therefore, is not context free L
(regular closure)
89
Membership Question:
for context-free grammarfind if string
G)(GLw
Membership Algorithms: Parsers
• Exhaustive search parser
• CYK parsing algorithm
90
Empty Language Question:
for context-free grammar find if
G)(GL
Algorithm:
S
1. Remove useless variables
2. Check if start variable is useless
91
Infinite Language Question:
for context-free grammar find if is infinite
G)(GL
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