View
218
Download
1
Tags:
Embed Size (px)
Citation preview
Costas Busch - RPI 3
Context-Free
Languages(Grammars)
LanguagesAccepted
byNPDAs
Proof - Step 1:
Convert any context-free grammar to a NPDA with:
GM )()( MLGL
Costas Busch - RPI 4
Context-Free
Languages(Grammars)
LanguagesAccepted
byNPDAs
Proof - Step 2:
Convert any NPDA to a context-free grammar with: G
M)()( MLGL
Costas Busch - RPI 6
to an NPDA automaton M
We will convert any context-free grammar G
Such that:
M Simulates leftmost derivations of G
Costas Busch - RPI 7
mk VVVS 2121
1 2 k 1k
$
1V2V
mV
Inputprocessed
Stackcontents
:G
:M
Input
Stack
leftmost variable
Leftmost derivation
Simulation of derivation
Costas Busch - RPI 8
nS 21
1 2 n $
:G
:M
Input
Stack
Leftmost derivation
Simulation of derivation
string of terminals
end of input is reached
Costas Busch - RPI 10
q0 q1 2qS, , $ $
Grammar:
NPDA:
T
TaT
bS
aSTbS
,
,
,
,
T
TaT
bS
aSTbS
bb
aa
,
,
Costas Busch - RPI 120q q1 2q
S, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab
Time 0
b
Derivation:
Costas Busch - RPI 13
q0 q1 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
S
Time 0
Derivation: S
Costas Busch - RPI 14
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
a
b
ST
q1
Time 1
Derivation: aSTbS
Costas Busch - RPI 15
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
a
b
ST
q1
Time 2
aSTbS Derivation:
Costas Busch - RPI 16
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
bTb
q1
Time 3
Derivation: abTbaSTbS
Costas Busch - RPI 17
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
bTb
q1
Time 4
Derivation: abTbaSTbS
Costas Busch - RPI 18
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
b
Ta
q1
Time 5
Derivation: abTababTbaSTbS
Costas Busch - RPI 19
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
b
Ta
q1
Time 6
Derivation: abababTababTbaSTbS
Costas Busch - RPI 20
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
ba
q1
Time 7
Derivation: abababTababTbaSTbS
Costas Busch - RPI 21
q0 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
b
q1
Time 8
Derivation:
Costas Busch - RPI 22
q0 q1 2qS, , $ $
T
TaT
bS
aSTbS
,
,
,
,
bb
aa
,
,
Input
Stack
$
a ab b
accept
Time 9
Derivation:
Costas Busch - RPI 24
Constructing NPDA from grammar :
q0 q1 2qS, , $ $
wA, aa,
M
For any production
wAFor any terminal
a
G
Costas Busch - RPI 26
Therefore:
For any context-free languagethere is a NPDAthat accepts the same language
Context-Free
Languages(Grammars)
LanguagesAccepted
byNPDAs
Costas Busch - RPI 29
Intuition:
G
The grammar simulates the machine
A derivation in Grammar :
Current configuration in NPDA M
abcABCabcS
Input processed Stack contents
terminals variables
Costas Busch - RPI 30
Some Necessary Modifications
Modify (if necessary) the NPDA so that:
1) The stack is never empty
2) It has a single final state and empties the stack when it accepts a string
3) Has transitions in a special form
Costas Busch - RPI 33
Original NPDA
#,
At the beginning push into the stack#
original initial state
new initial state
Costas Busch - RPI 34
iq jqba $,
iq jqba #,
In transitions: replace every instance of with $ #
Example:
Costas Busch - RPI 35
if the automaton attempts to popor replace it will halt $
Convert all transitions so that:
Costas Busch - RPI 37
NPDA
fq
x,
,
Empty the stack{$}x
2) Modify the NPDA so that it empties the stack and has a unique final state
,
,
Old final states
$,
Costas Busch - RPI 38
3) modify the NPDA so that transitions have the following forms:
iq jq B,
OR
iq jqCDB,
symbolsstack :,, DCB
Costas Busch - RPI 42
$,0q fq
a, $ 0$a, 0 00a,1
b, $ 1$b, 1 11b, 0
}:{)( ba nnwML
Example of a NPDA in correct form:
symbolstack initial :$
Costas Busch - RPI 43
The Grammar Construction
)( jiBqq
In grammar :G
Terminals:Input symbols of NPDA
states
Stack symbol
Variables:
Costas Busch - RPI 45
For each transition
We add productions
))(()( klljki DqqCqqaBqq
iq jqCDBa ,
For all possible statesin the automaton
lk qq ,
Costas Busch - RPI 47
Example:
$,0q fq
a, $ 0$a, 0 00a,1
b, $ 1$b, 1 11b, 0
aqq )1( 00Grammar production:
Costas Busch - RPI 48
Example:
$,0q fq
a, $ 0$a, 0 00a,1
b, $ 1$b, 1 11b, 0
)$)(1(|)$)(1()$(
)$)(1(|)$)(1()$(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
Grammar productions:
Costas Busch - RPI 49
Example:
$,0q fq
a, $ 0$a, 0 00a,1
b, $ 1$b, 1 11b, 0
Grammar production: )$( 0 fqq
Costas Busch - RPI 50
)$)(1(|)$)(1()$(
)$)(1(|)$)(1()$(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
)1)(1(|)1)(1()1(
)1)(1(|)1)(1()1(
00000
00000000
fffff
ff
qqqqbqqqqbqq
qqqqbqqqqbqq
)$)(0(|)$)(0()$(
)$)(0(|)$)(0()$(
00000
00000000
fffff
ff
qqqqaqqqqaqq
qqqqaqqqqaqq
Resulting Grammar: ablestart vari:)$( 0 fqq
Costas Busch - RPI 51
)0)(0(|)0)(0()0(
)0)(0(|)0)(0()0(
00000
00000000
fffff
ff
qqqqaqqqqaqq
qqqqaqqqqaqq
bqq
aqq
)0(
)1(
00
00
)$( 0 fqq
Costas Busch - RPI 52
Derivation of string abba
)$( 0 fqq )$)(0( 000 fqqqqa
)$( 0 fqqab
)$)(1( 000 fqqqqabb
)$( 0 fqqabba abba
Costas Busch - RPI 53
In general:
wAqq ji
)(
if and only if
the NPDA goes from toby reading string andthe stack doesn’t change belowand then is removed from stack
iq jq
AA
w
Costas Busch - RPI 55
Therefore:
For any NPDAthere is a context-free grammarthat accepts the same language
Context-Free
Languages(Grammars)
LanguagesAccepted
byNPDAs
Costas Busch - RPI 57
Deterministic PDA: DPDA
q1 q2wba ,
1q 2qwb,
Allowed transitions:
(deterministic choices)
Costas Busch - RPI 58
Allowed transitions:
q1
q21, wb
q32, wc
q1
q21, wba
q32, wca
(deterministic choices)
Costas Busch - RPI 59
q1
q21, wba
q32, wba
Not allowed:
(non deterministic choices)
q1
q21, wb
q32, wba
Costas Busch - RPI 62
Definition:
A language is deterministic context-freeif there exists some DPDA that accepts it
L
Costas Busch - RPI 66
Deterministic
Context-FreeLanguages
(DPDA)
Context-Free
LanguagesNPDAs
Since every DPDA is also a NPDA
It holds that:
Costas Busch - RPI 67
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(NPDA)
Costas Busch - RPI 68
The language is:
}{}{ 2nnnn babaL 0n
We will show:
L• is context-free
L• is not deterministic context-free
Costas Busch - RPI 69
}{}{ 2nnnn babaL
Language is context-freeL
Context-free grammar for : L
21 | SSS
|11 baSS
|22 bbaSS
}{ nnba
}{ 2nnba
}{}{ 2nnnn baba
Costas Busch - RPI 70
}{}{ 2nnnn babaL
is not deterministic context-free
Theorem:
The language
(there is no DPDA that accepts ) L
Costas Busch - RPI 71
Proof: Assume for contradiction that
}{}{ 2nnnn babaL
is deterministic context free
Therefore:
there is a DPDA that accepts M L
Costas Busch - RPI 73
DPDA with M
nnba nb
}{}{)( 2nnnn babaML
Such a path exists because of the determinism
M
Costas Busch - RPI 74
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
Costas Busch - RPI 75
The language is not context-free
}{ nnn cbaL
}){}{( 2nnnn babaL
Fact 2:
(we can prove this using pumping lemma for context-free languages)
Costas Busch - RPI 76
We will construct a NPDA that accepts:
}{ nnn cbaL
}){}{( 2nnnn babaL
which is a contradiction!
Costas Busch - RPI 77
Modify M
M ncnnca
}{}{)( 2nnnn babaML
}{}{)( 2nnnn cacaML
Replacewith
bc
nnba nb
M
Costas Busch - RPI 78
The NPDA that accepts }{ nnn cbaL
nnba nb
nc
M
M
nnca
Connect final states of with final states of
M
M
Costas Busch - RPI 79
Since is accepted by a NPDA }{ nnn cbaL
it is context-free
Contradiction!
(since is not context-free)}{ nnn cbaL