SlideShare a Scribd company logo
© 2020 Wael Badawy
1
PDAs Accept
Context-Free Languages
1
DISCLAIMER:
This video is optimized for HD large display using
patented and patent-pending “Nile Codec”, the first
Egyptian Video Codec for more information,
PLEASE check https://NileCodec.com
Also available as a PodCast
© 2020 Wael Badawy
Copyright © 2020 Wael Badawy. All rights reserved
n This video is subject to copyright owned by Wael Badawy “WB”. Any
reproduction or republication of all or part of this video is expressly prohibited
unless WB has explicitly granted its prior written consent. All other rights
reserved.
n This video is intended for education and information only and is offered AS IS,
without any warranty of the accuracy or the quality of the content. Any other
use is strictly prohibited. The viewer is fully responsible to verify the accuracy
of the contents received without any claims of costs or liability arising .
n The names, trademarks service marked as logos of WB or the sponsors
appearing in this video may not be used in any any product or service,
without prior express written permission from WB and the video sponsors
n Neither WB nor any party involved in creating, producing or delivering
information and material via this video shall be liable for any direction,
incidental, consequential, indirect of punitive damages arising out of access
to, use or inability to use this content or any errors or omissions in the
content thereof.
n If you will continue to watch this video, you agree to the terms above and
other terms that may be available on http://nu.edu.eg & https://caiwave.net
2
© 2020 Wael Badawy
© 2020 Wael Badawy
4
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
=
Theorem:
© 2020 Wael Badawy
5
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
Í
Proof - Step 1:
Convert any context-free grammar
to a PDA with:
G
M )()( MLGL =
© 2020 Wael Badawy
6
Context-Free
Languages
(Grammars)
Languages
Accepted by
PDAs
Ê
Proof - Step 2:
Convert any PDA to a context-free
grammar with:G
M
)()( MLGL =
7
Convert
Context-Free Grammars
to
PDAs
7
Proof - step 1
© 2020 Wael Badawy
8
MWe will convert to a PDA such that:G
Take an arbitrary context-free grammar G
)()( MLGL =
© 2020 Wael Badawy
9
q0 q1 2qS®ee, $$, ®e
wA ®,e e®aa,
production in
wA ®
terminal in
a
G G
Conversion Procedure:
For each For each
Add transitions
© 2020 Wael Badawy
10
q0 q1 2q
Grammar
PDA
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e®
®
®
®
T
TaT
bS
aSTbS
e
e
®
®
bb
aa
,
,
Example
S®ee, $$, ®e
© 2020 Wael Badawy
11
nkk
mk XX
S
ssss
ss
!!
!
!!
!
11
11
+Þ
Þ
Þ
Þ
,$),(
$),,(
$),,(
,$),(
2
111
111
110
e
ss
ssss
ssss
q
XXq
Sq
q
mnk
nkk
nkk
!
"!
""!
"!
""!
""
+
+
+
PDA simulates leftmost derivations
Grammar
Leftmost Derivation
PDA Computation
Scanned
symbols
Stack
contents
© 2020 Wael Badawy
12
Bzyx
xAy
ji ss !
!
Þ
Þ
Þ
Grammar
Leftmost Derivation
BzA ji ss !®
Production applied
Terminals
Leftmost
variable
Variables
or terminals
Terminals Variable
Variables
or terminals
© 2020 Wael Badawy
13
Bzyx
xAy
ji ss !
!
Þ
Þ
Þ
$),,(
$),,(
1
1
Bzyq
Ayq
jini
ni
ssss
ss
!!"
!"
!"
Grammar
Leftmost Derivation
PDA Computation
BzA ji ss !®
Production applied
q0 q1 2qS®ee, $$, ®e
BzA ji sse !®,
Transition applied
© 2020 Wael Badawy
14
Bzyx
xAy
ji ss !
!
Þ
Þ
Þ
$),,(
$),,(
$),,(
111
1
1
Bzyq
Bzyq
Ayq
jini
jini
ni
ssss
ssss
ss
!!"
!!"
!"
!"
++
Grammar
Leftmost Derivation
PDA Computation
q0 q1 2q
ess ®ii ,
Transition applied
Read from input
and remove it from stack
is
S®ee, $$, ®e
© 2020 Wael Badawy
15
Bzyx
xAy
ji ss !
!
Þ
Þ
Þ
$),,(
$),,(
$),,(
$),,(
11
111
1
1
Bzyq
Bzyq
Bzyq
Ayq
nj
jini
jini
ni
ss
ssss
ssss
ss
!"
!"
!!"
!!"
!"
!"
+
++
Grammar
Leftmost Derivation
PDA Computation
q0 q1 2q
ess ®jj ,
Last Transition applied
All symbols
have been removed
from top of stack
ji ss !
S®ee, $$, ®e
© 2020 Wael Badawy
16
Cpzyx
Bzyx
xAy
kjji
ji
ssss
ss
!!
!
!
1+Þ
Þ
Þ
Þ
$),,(
$),,(
$),,(
11
111
11
Cpzyq
Cpzyq
Bzyq
nk
kjnj
nj
ss
ssss
ss
!"
!"
!!"
!"
!"
+
++
+
CpB kj ss !1+®
Production applied
The process repeats with the next
leftmost variable
And so on……
© 2020 Wael Badawy
17
0q q1 2q
Input
Stack
$
a ab
Time 0
b
Example:
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
18
q0 q1 2q
Input
Stack
$
a ab b
S
Time 1
Derivation: S
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
19
q0 2q
Input
Stack
$
a ab b
a
b
S
T
q1
Time 2
Derivation: aSTbS Þ
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
20
q0 2q
Input
Stack
$
a ab b
a
b
S
T
q1
Time 3
aSTbS ÞDerivation:
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
21
q0 2q
Input
Stack
$
a ab b
b
T
b
q1
Time 4
Derivation: abTbaSTbS ÞÞ
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
22
q0 2q
Input
Stack
$
a ab b
b
T
b
q1
Time 5
Derivation: abTbaSTbS ÞÞ
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
23
q0 2q
Input
Stack
$
a ab b
b
T
a
q1
Time 6
Derivation: abTababTbaSTbS ÞÞÞ
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
24
q0 2q
Input
Stack
$
a ab b
b
T
a
q1
Time 7
Derivation: abababTababTbaSTbS ÞÞÞÞ
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
25
q0 2q
Input
Stack
$
a ab b
b
a
q1
Time 8
Derivation: abababTababTbaSTbS ÞÞÞÞ
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
26
q0 2q
Input
Stack
$
a ab b
b
q1
Time 9
Derivation: abababTababTbaSTbS ÞÞÞÞ
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
27
q0 q1 2q
Input
Stack
$
a ab b
accept
Time 10
Derivation: abababTababTbaSTbS ÞÞÞÞ
ee
e
e
e
®
®
®
®
T
TaT
bS
aSTbS
,
,
,
,
e
e
®
®
bb
aa
,
,
S®ee, $$, ®e
© 2020 Wael Badawy
28
abab
abTab
abTb
aSTb
S
Þ
Þ
Þ
Þ
,$),(
,$),(
$),,(
$),,(
$),,(
$),,(
$),,(
$),,(
$),,(
,$),(
2
1
1
1
1
1
1
1
1
0
e
e
q
q
bbq
ababq
Tababq
Tbabq
bTbbabq
STbbabq
Sababq
ababq
!
!
!
!
!
!
!
!
!
Grammar
Leftmost Derivation
PDA Computation
© 2020 Wael Badawy
29
Grammar
generates
string
G
wS
*
Þ
PDA
accepts
M
,$),(,$),( 2
*
0 eqwq !
In general, it can be shown that:
w wIf and
Only if
Therefore )()( MLGL =
30
Convert
PDAs
to
Context-Free Grammars
30
Proof - step 2
© 2020 Wael Badawy
31
We will convert
to a context-free grammar such that:G
Take an arbitrary PDA
)()( GLML =
M
M
© 2020 Wael Badawy
32
First modify PDA so that:M
1. The PDA has a single accept state
4. Each transition either pushes a symbol
or pops a symbol but not both together
3. On acceptance the stack contains only
stack symbol # (this symbol is not used in any transition)
2. Use new initial stack symbol #
© 2020 Wael Badawy
33
PDA
fq
eee ®,
eee ®,
e
ee
®,
Old
accept
states
New
accept
state
M
1. The PDA has a single accept state
PDA 1M
© 2020 Wael Badawy
34
Z®ee,
PDA 1M
2. Use new initial stack symbol #
new initial stack symbol
initial stack symbol of M
#
Z
Top of stack
still thinks that Z is the initial stack
PDA 2M
1M
@, ®ee
@ auxiliary stack symbol
© 2020 Wael Badawy
35
fq
ee ®x,
Empty stack
}#{@,-GÎ"x
ee ®@,
New
accept
state
PDA 2M
eee ®,
Old
accept
state
PDA 3M
3. On acceptance the stack contains only
stack symbol #
(this symbol is not used in any transition)
© 2020 Wael Badawy
36
iq jq
ba ®,s
iq jq
es ®a, b®ee,
4. Each transition either pushes a symbol
or pops a symbol but not both together
PDA 3M
PDA 4M
© 2020 Wael Badawy
37
iq jq
ees ®,
iq jq
des ®, ede ®,
PDA 3M
PDA 4M
Where is a symbol of the stack alphabetd
© 2020 Wael Badawy
38
4MPDA is the final modified PDA
Note that the new initial stack symbol #
is never used in any transition
© 2020 Wael Badawy
39
Example: aa ®e,
e®ab,
M
aa ®e,
e®ab,
a®ee, ee ®a,
ee ®a,
ee ®b,
ee ®Z,
ee ®@,
4M
q
0q
2q 3q 4q 5q
1q
@, ®ee
Z®ee,
© 2020 Wael Badawy
Grammar Construction
40
ji qqA ,
Variables:
States of PDA
© 2020 Wael Badawy
41
q
PDA
Grammar
e®qqA
Kind 1: for each state
© 2020 Wael Badawy
42
p q r
PDA
Grammar
rqprpq AAA ®
Kind 2: for every three states
© 2020 Wael Badawy
43
p qr
PDA
Grammar
baAA rspq ®
sta ®e, e®tb,
Kind 3: for every pair of such transitions
© 2020 Wael Badawy
44
0q
PDA
Grammar
fqqA 0
fq
Initial state Accept state
Start variable
© 2020 Wael Badawy
45
Example: PDA
aa ®e,
e®ab,
a®ee, ee ®a,
ee ®a,
ee ®b,
ee ®Z,
ee ®@,
4M
0q
2q 3q 4q 5q
1q
@, ®ee
Z®ee,
© 2020 Wael Badawy
46
Grammar
e
e
e
e
e
e
®
®
®
®
®
®
55
44
33
22
11
00
qq
qq
qq
qq
qq
qq
A
A
A
A
A
A
Kind 1: from single states
© 2020 Wael Badawy
47
50qqAStart variable
55555445533552255115500555
55505440533052205110500050
15501440133012201110100010
05500440033002200110000000
|||||
|||||
|||||
|||||
qqqqqqqqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqqqqqqqq
qqqqqqqqqqqqqqqqqqqqqqqqqq
AAAAAAAAAAAAA
AAAAAAAAAAAAA
AAAAAAAAAAAAA
AAAAAAAAAAAAA
®
®
®
®
!
!
Kind 2: from triplets of states
© 2020 Wael Badawy
48
Kind 3: from pairs of transitions
4150 qqqq AA ®
baAA qqqq 2222
®
3242 qqqq aAA ®
4242 qqqq aAA ® bAA qqqq 2322
®
4241 qqqq AA ® 3342 qqqq AA ®
4342 qqqq AA ®
aa ®e,
e®ab,
a®ee, ee ®a,
ee ®a,
ee ®b,
ee ®Z,
ee ®@,
4M
0q
2q 3q 4q 5q
1q
@, ®ee
Z®ee,
© 2020 Wael Badawy
49
We need to prove that
Suppose that a PDA is converted
to a context-free grammar G
M
)()( MLGL =
)()( MLGL Í )()( MLGL Ê
or equivalently
© 2020 Wael Badawy
50
)()( MLGL Í
We need to show that if has derivation:
wA fqq
*
Þ0
Then there is an accepting computation in :M
)#,,()#,,( 0 efqwq
*
!
G
(string of terminals)
with input string w
© 2020 Wael Badawy
51
We will actually show that if has derivation:
wApq
*
Þ
Then there is a computation in :M
),,(),,( eee qwp
*
!
G
© 2020 Wael Badawy
52
wA fqq
*
Þ0
)#,,()#,,( 0 efqwq
*
!
),,(),,( 0 eee fqwq
*
!
Therefore:
Since there is no transition
with the # symbol
© 2020 Wael Badawy
53
Lemma:
wApq
*
ÞIf (string of terminals)
then there is a computation
from state to state on string
which leaves the stack empty:
),,(),,( eee qwp
*
!
p q w
© 2020 Wael Badawy
54
Proof Intuition:
wApq ÞÞ !
wbaAA rspq ÞÞÞ !
wAAA rqprpq ÞÞÞ !Case 1:
Case 2:
Type 2
Type 3
© 2020 Wael Badawy
55
wAAA rqprpq ÞÞÞ !Case 1:
Type 2
Stack
height
Input string Generated by
prA
p r q
Generated by
rqA
© 2020 Wael Badawy
56
Stack
height
Input string Generated by
rsA
p q
wbaAA rspq ÞÞÞ !Case 2:
Type 3
a b
r s
© 2020 Wael Badawy
57
We formally prove this claim
by induction on the number
of steps in derivation:
wApq ÞÞ !
Formal Proof:
number of steps
© 2020 Wael Badawy
58
Induction Basis: wApq Þ
(one derivation step)
A Kind 1 production must have been used:
e®ppA
Therefore, and e=wqp =
),,(),,( eeee pp
*
!
This computation of PDA trivially exists:
© 2020 Wael Badawy
59
Induction Hypothesis:
wApq ÞÞ !
k derivation steps
),,(),,( eee qwp
*
!
suppose it holds:
© 2020 Wael Badawy
60
Induction Step:
wApq ÞÞ !
1+k derivation steps
),,(),,( eee qwp
*
!
We have to show:
© 2020 Wael Badawy
61
wApq ÞÞ !
1+k derivation steps
wbaAA rspq ÞÞÞ !
wAAA rqprpq ÞÞÞ !Case 1:
Case 2:
Type 2
Type 3
© 2020 Wael Badawy
62
wAAA rqprpq ÞÞÞ !Case 1:
Type 2
yzw =We can write
yApr ÞÞ !
kAt most steps
zArq ÞÞ !
kAt most steps
1+k steps
© 2020 Wael Badawy
63
yApr ÞÞ !
kAt most steps
zArq ÞÞ !
kAt most steps
From induction
hypothesis, in PDA:
),,(),,( eee ryp
*
! ),,(),,( eee qzr
*
!
From induction
hypothesis, in PDA:
© 2020 Wael Badawy
64
),,(),,( eee ryp
*
! ),,(),,( eee qzr
*
!
),,(),,(),,( eeee qzryzp
**
!!
),,(),,( eee qwp
*
!
yzw =since
© 2020 Wael Badawy
65
wbaAA rspq ÞÞÞ !Case 2:
Type 3
aybw =We can write
yArs ÞÞ !
kAt most steps
1+k steps
© 2020 Wael Badawy
66
yArs ÞÞ !
kAt most steps
From induction hypothesis,
the PDA has computation:
),,(),,( eee syr
*
!
© 2020 Wael Badawy
67
p qr sta ®e, e®tb,
And PDA Contains transitions
Type 3
baAA rspq ®
Grammar contains production
wbaAA rspq ÞÞÞ !
© 2020 Wael Badawy
68
p qr sta ®e, e®tb,
),,(),,( tybraybp !e ),,(),,( eeqtbs !
© 2020 Wael Badawy
69
),,(),,( tybraybp !e
),,(),,( eee syr
*
!
),,(),,( eeqtbs !
),,(),,( tbstybr
*
!
We know
We also know
Therefore:
),,(),,(),,(),,( eee qtbstybraybp !!!
*
© 2020 Wael Badawy
70
),,(),,(),,(),,( eee qtbstybraybp !!!
*
),,(),,( eee qwp
*
!
aybw =since
END OF PROOF
© 2020 Wael Badawy
71
So far we have shown:
)()( MLGL Í
With a similar proof we can show
)()( MLGL Ê
Therefore: )()( MLGL =

More Related Content

PDF
Pushdown Automota
PDF
Regular properties
PPTX
unit 3.pptx-theory of computation unit 3 notes
PDF
Theory of Computation Basic Concepts and Grammar
PDF
Context Free Grammar
PDF
Context free langauges
PPTX
Theory of Computation Unit 3
DOCX
V cse cs6503 model qb1 1
Pushdown Automota
Regular properties
unit 3.pptx-theory of computation unit 3 notes
Theory of Computation Basic Concepts and Grammar
Context Free Grammar
Context free langauges
Theory of Computation Unit 3
V cse cs6503 model qb1 1

Similar to Pda accept context free (20)

PPTX
RegularLanguageProperties [Autosaved].pptx
DOCX
Pushdown AutomataChapter 12Recognizing Context-F.docx
PPT
Normal-forms-for-Context-Free-Grammars.ppt
PPT
Lecture_Notes_AutomatContext Free Languages.ppt
PPTX
Theory of computing presentation
PDF
Theory of Computation Grammar Concepts and Problems
PPT
163692498 grammar
PDF
Formal Languages and Automata Theory unit 3
PDF
TOC question bank.pdf
PDF
CS4200 2019 | Lecture 3 | Parsing
PPTX
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
PPT
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
PPT
computer Languages and Grammars.pptx and
PPTX
theory of computation lecture 02
PPTX
Open course(programming languages) 20150211
PPTX
Open course(programming languages) 20150211
PDF
Compiler Construction | Lecture 4 | Parsing
PPTX
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
PPT
context-freelanguages and properties lecture note.ppt
PDF
Declarative Syntax Definition - Grammars and Trees
RegularLanguageProperties [Autosaved].pptx
Pushdown AutomataChapter 12Recognizing Context-F.docx
Normal-forms-for-Context-Free-Grammars.ppt
Lecture_Notes_AutomatContext Free Languages.ppt
Theory of computing presentation
Theory of Computation Grammar Concepts and Problems
163692498 grammar
Formal Languages and Automata Theory unit 3
TOC question bank.pdf
CS4200 2019 | Lecture 3 | Parsing
Unit-1 PPL PPTvvhvmmmmmmmmmmmmmmmmmmmmmm
Integrated Fundamental and Technical Analysis of Select Public Sector Oil Com...
computer Languages and Grammars.pptx and
theory of computation lecture 02
Open course(programming languages) 20150211
Open course(programming languages) 20150211
Compiler Construction | Lecture 4 | Parsing
a7f0bc7785844884c4de624f61ce5978_MIT18_404f20_lec4.pptx
context-freelanguages and properties lecture note.ppt
Declarative Syntax Definition - Grammars and Trees
Ad

More from Wael Badawy (20)

PDF
HTML introduction
PPTX
Np complete reductions
PPTX
N F A - Non Deterministic Finite Automata
PPTX
Parsers -
PPTX
Computer Vision Cameras
PPTX
Computer Vision Gans
PPTX
Computer Vision image classification
PPTX
Computer Vision Structure from motion
PDF
Universal turing
PDF
Turing Machine
PDF
Turing variations
PDF
Time complexity
PDF
Regular pumping
PDF
Regular pumping examples
PDF
Regular expressions
PPTX
Computer Vision sfm
PPTX
Computer vision - photometric
PPTX
Computer vision - light
PPTX
Computer vision - two view geometry
PPTX
Computer Vision - Stereo Vision
HTML introduction
Np complete reductions
N F A - Non Deterministic Finite Automata
Parsers -
Computer Vision Cameras
Computer Vision Gans
Computer Vision image classification
Computer Vision Structure from motion
Universal turing
Turing Machine
Turing variations
Time complexity
Regular pumping
Regular pumping examples
Regular expressions
Computer Vision sfm
Computer vision - photometric
Computer vision - light
Computer vision - two view geometry
Computer Vision - Stereo Vision
Ad

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Complications of Minimal Access Surgery at WLH
PDF
Classroom Observation Tools for Teachers
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Institutional Correction lecture only . . .
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Lesson notes of climatology university.
PPTX
Cell Structure & Organelles in detailed.
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Pharma ospi slides which help in ospi learning
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Microbial disease of the cardiovascular and lymphatic systems
Complications of Minimal Access Surgery at WLH
Classroom Observation Tools for Teachers
Computing-Curriculum for Schools in Ghana
Final Presentation General Medicine 03-08-2024.pptx
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Institutional Correction lecture only . . .
O5-L3 Freight Transport Ops (International) V1.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
Lesson notes of climatology university.
Cell Structure & Organelles in detailed.
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pharma ospi slides which help in ospi learning
Chinmaya Tiranga quiz Grand Finale.pdf
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf

Pda accept context free

  • 1. © 2020 Wael Badawy 1 PDAs Accept Context-Free Languages 1 DISCLAIMER: This video is optimized for HD large display using patented and patent-pending “Nile Codec”, the first Egyptian Video Codec for more information, PLEASE check https://NileCodec.com Also available as a PodCast
  • 2. © 2020 Wael Badawy Copyright © 2020 Wael Badawy. All rights reserved n This video is subject to copyright owned by Wael Badawy “WB”. Any reproduction or republication of all or part of this video is expressly prohibited unless WB has explicitly granted its prior written consent. All other rights reserved. n This video is intended for education and information only and is offered AS IS, without any warranty of the accuracy or the quality of the content. Any other use is strictly prohibited. The viewer is fully responsible to verify the accuracy of the contents received without any claims of costs or liability arising . n The names, trademarks service marked as logos of WB or the sponsors appearing in this video may not be used in any any product or service, without prior express written permission from WB and the video sponsors n Neither WB nor any party involved in creating, producing or delivering information and material via this video shall be liable for any direction, incidental, consequential, indirect of punitive damages arising out of access to, use or inability to use this content or any errors or omissions in the content thereof. n If you will continue to watch this video, you agree to the terms above and other terms that may be available on http://nu.edu.eg & https://caiwave.net 2
  • 3. © 2020 Wael Badawy
  • 4. © 2020 Wael Badawy 4 Context-Free Languages (Grammars) Languages Accepted by PDAs = Theorem:
  • 5. © 2020 Wael Badawy 5 Context-Free Languages (Grammars) Languages Accepted by PDAs Í Proof - Step 1: Convert any context-free grammar to a PDA with: G M )()( MLGL =
  • 6. © 2020 Wael Badawy 6 Context-Free Languages (Grammars) Languages Accepted by PDAs Ê Proof - Step 2: Convert any PDA to a context-free grammar with:G M )()( MLGL =
  • 8. © 2020 Wael Badawy 8 MWe will convert to a PDA such that:G Take an arbitrary context-free grammar G )()( MLGL =
  • 9. © 2020 Wael Badawy 9 q0 q1 2qS®ee, $$, ®e wA ®,e e®aa, production in wA ® terminal in a G G Conversion Procedure: For each For each Add transitions
  • 10. © 2020 Wael Badawy 10 q0 q1 2q Grammar PDA ee e e e ® ® ® ® T TaT bS aSTbS , , , , e® ® ® ® T TaT bS aSTbS e e ® ® bb aa , , Example S®ee, $$, ®e
  • 11. © 2020 Wael Badawy 11 nkk mk XX S ssss ss !! ! !! ! 11 11 +Þ Þ Þ Þ ,$),( $),,( $),,( ,$),( 2 111 111 110 e ss ssss ssss q XXq Sq q mnk nkk nkk ! "! ""! "! ""! "" + + + PDA simulates leftmost derivations Grammar Leftmost Derivation PDA Computation Scanned symbols Stack contents
  • 12. © 2020 Wael Badawy 12 Bzyx xAy ji ss ! ! Þ Þ Þ Grammar Leftmost Derivation BzA ji ss !® Production applied Terminals Leftmost variable Variables or terminals Terminals Variable Variables or terminals
  • 13. © 2020 Wael Badawy 13 Bzyx xAy ji ss ! ! Þ Þ Þ $),,( $),,( 1 1 Bzyq Ayq jini ni ssss ss !!" !" !" Grammar Leftmost Derivation PDA Computation BzA ji ss !® Production applied q0 q1 2qS®ee, $$, ®e BzA ji sse !®, Transition applied
  • 14. © 2020 Wael Badawy 14 Bzyx xAy ji ss ! ! Þ Þ Þ $),,( $),,( $),,( 111 1 1 Bzyq Bzyq Ayq jini jini ni ssss ssss ss !!" !!" !" !" ++ Grammar Leftmost Derivation PDA Computation q0 q1 2q ess ®ii , Transition applied Read from input and remove it from stack is S®ee, $$, ®e
  • 15. © 2020 Wael Badawy 15 Bzyx xAy ji ss ! ! Þ Þ Þ $),,( $),,( $),,( $),,( 11 111 1 1 Bzyq Bzyq Bzyq Ayq nj jini jini ni ss ssss ssss ss !" !" !!" !!" !" !" + ++ Grammar Leftmost Derivation PDA Computation q0 q1 2q ess ®jj , Last Transition applied All symbols have been removed from top of stack ji ss ! S®ee, $$, ®e
  • 16. © 2020 Wael Badawy 16 Cpzyx Bzyx xAy kjji ji ssss ss !! ! ! 1+Þ Þ Þ Þ $),,( $),,( $),,( 11 111 11 Cpzyq Cpzyq Bzyq nk kjnj nj ss ssss ss !" !" !!" !" !" + ++ + CpB kj ss !1+® Production applied The process repeats with the next leftmost variable And so on……
  • 17. © 2020 Wael Badawy 17 0q q1 2q Input Stack $ a ab Time 0 b Example: ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 18. © 2020 Wael Badawy 18 q0 q1 2q Input Stack $ a ab b S Time 1 Derivation: S ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 19. © 2020 Wael Badawy 19 q0 2q Input Stack $ a ab b a b S T q1 Time 2 Derivation: aSTbS Þ ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 20. © 2020 Wael Badawy 20 q0 2q Input Stack $ a ab b a b S T q1 Time 3 aSTbS ÞDerivation: ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 21. © 2020 Wael Badawy 21 q0 2q Input Stack $ a ab b b T b q1 Time 4 Derivation: abTbaSTbS ÞÞ ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 22. © 2020 Wael Badawy 22 q0 2q Input Stack $ a ab b b T b q1 Time 5 Derivation: abTbaSTbS ÞÞ ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 23. © 2020 Wael Badawy 23 q0 2q Input Stack $ a ab b b T a q1 Time 6 Derivation: abTababTbaSTbS ÞÞÞ ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 24. © 2020 Wael Badawy 24 q0 2q Input Stack $ a ab b b T a q1 Time 7 Derivation: abababTababTbaSTbS ÞÞÞÞ ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 25. © 2020 Wael Badawy 25 q0 2q Input Stack $ a ab b b a q1 Time 8 Derivation: abababTababTbaSTbS ÞÞÞÞ ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 26. © 2020 Wael Badawy 26 q0 2q Input Stack $ a ab b b q1 Time 9 Derivation: abababTababTbaSTbS ÞÞÞÞ ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 27. © 2020 Wael Badawy 27 q0 q1 2q Input Stack $ a ab b accept Time 10 Derivation: abababTababTbaSTbS ÞÞÞÞ ee e e e ® ® ® ® T TaT bS aSTbS , , , , e e ® ® bb aa , , S®ee, $$, ®e
  • 28. © 2020 Wael Badawy 28 abab abTab abTb aSTb S Þ Þ Þ Þ ,$),( ,$),( $),,( $),,( $),,( $),,( $),,( $),,( $),,( ,$),( 2 1 1 1 1 1 1 1 1 0 e e q q bbq ababq Tababq Tbabq bTbbabq STbbabq Sababq ababq ! ! ! ! ! ! ! ! ! Grammar Leftmost Derivation PDA Computation
  • 29. © 2020 Wael Badawy 29 Grammar generates string G wS * Þ PDA accepts M ,$),(,$),( 2 * 0 eqwq ! In general, it can be shown that: w wIf and Only if Therefore )()( MLGL =
  • 31. © 2020 Wael Badawy 31 We will convert to a context-free grammar such that:G Take an arbitrary PDA )()( GLML = M M
  • 32. © 2020 Wael Badawy 32 First modify PDA so that:M 1. The PDA has a single accept state 4. Each transition either pushes a symbol or pops a symbol but not both together 3. On acceptance the stack contains only stack symbol # (this symbol is not used in any transition) 2. Use new initial stack symbol #
  • 33. © 2020 Wael Badawy 33 PDA fq eee ®, eee ®, e ee ®, Old accept states New accept state M 1. The PDA has a single accept state PDA 1M
  • 34. © 2020 Wael Badawy 34 Z®ee, PDA 1M 2. Use new initial stack symbol # new initial stack symbol initial stack symbol of M # Z Top of stack still thinks that Z is the initial stack PDA 2M 1M @, ®ee @ auxiliary stack symbol
  • 35. © 2020 Wael Badawy 35 fq ee ®x, Empty stack }#{@,-GÎ"x ee ®@, New accept state PDA 2M eee ®, Old accept state PDA 3M 3. On acceptance the stack contains only stack symbol # (this symbol is not used in any transition)
  • 36. © 2020 Wael Badawy 36 iq jq ba ®,s iq jq es ®a, b®ee, 4. Each transition either pushes a symbol or pops a symbol but not both together PDA 3M PDA 4M
  • 37. © 2020 Wael Badawy 37 iq jq ees ®, iq jq des ®, ede ®, PDA 3M PDA 4M Where is a symbol of the stack alphabetd
  • 38. © 2020 Wael Badawy 38 4MPDA is the final modified PDA Note that the new initial stack symbol # is never used in any transition
  • 39. © 2020 Wael Badawy 39 Example: aa ®e, e®ab, M aa ®e, e®ab, a®ee, ee ®a, ee ®a, ee ®b, ee ®Z, ee ®@, 4M q 0q 2q 3q 4q 5q 1q @, ®ee Z®ee,
  • 40. © 2020 Wael Badawy Grammar Construction 40 ji qqA , Variables: States of PDA
  • 41. © 2020 Wael Badawy 41 q PDA Grammar e®qqA Kind 1: for each state
  • 42. © 2020 Wael Badawy 42 p q r PDA Grammar rqprpq AAA ® Kind 2: for every three states
  • 43. © 2020 Wael Badawy 43 p qr PDA Grammar baAA rspq ® sta ®e, e®tb, Kind 3: for every pair of such transitions
  • 44. © 2020 Wael Badawy 44 0q PDA Grammar fqqA 0 fq Initial state Accept state Start variable
  • 45. © 2020 Wael Badawy 45 Example: PDA aa ®e, e®ab, a®ee, ee ®a, ee ®a, ee ®b, ee ®Z, ee ®@, 4M 0q 2q 3q 4q 5q 1q @, ®ee Z®ee,
  • 46. © 2020 Wael Badawy 46 Grammar e e e e e e ® ® ® ® ® ® 55 44 33 22 11 00 qq qq qq qq qq qq A A A A A A Kind 1: from single states
  • 47. © 2020 Wael Badawy 47 50qqAStart variable 55555445533552255115500555 55505440533052205110500050 15501440133012201110100010 05500440033002200110000000 ||||| ||||| ||||| ||||| qqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqq qqqqqqqqqqqqqqqqqqqqqqqqqq AAAAAAAAAAAAA AAAAAAAAAAAAA AAAAAAAAAAAAA AAAAAAAAAAAAA ® ® ® ® ! ! Kind 2: from triplets of states
  • 48. © 2020 Wael Badawy 48 Kind 3: from pairs of transitions 4150 qqqq AA ® baAA qqqq 2222 ® 3242 qqqq aAA ® 4242 qqqq aAA ® bAA qqqq 2322 ® 4241 qqqq AA ® 3342 qqqq AA ® 4342 qqqq AA ® aa ®e, e®ab, a®ee, ee ®a, ee ®a, ee ®b, ee ®Z, ee ®@, 4M 0q 2q 3q 4q 5q 1q @, ®ee Z®ee,
  • 49. © 2020 Wael Badawy 49 We need to prove that Suppose that a PDA is converted to a context-free grammar G M )()( MLGL = )()( MLGL Í )()( MLGL Ê or equivalently
  • 50. © 2020 Wael Badawy 50 )()( MLGL Í We need to show that if has derivation: wA fqq * Þ0 Then there is an accepting computation in :M )#,,()#,,( 0 efqwq * ! G (string of terminals) with input string w
  • 51. © 2020 Wael Badawy 51 We will actually show that if has derivation: wApq * Þ Then there is a computation in :M ),,(),,( eee qwp * ! G
  • 52. © 2020 Wael Badawy 52 wA fqq * Þ0 )#,,()#,,( 0 efqwq * ! ),,(),,( 0 eee fqwq * ! Therefore: Since there is no transition with the # symbol
  • 53. © 2020 Wael Badawy 53 Lemma: wApq * ÞIf (string of terminals) then there is a computation from state to state on string which leaves the stack empty: ),,(),,( eee qwp * ! p q w
  • 54. © 2020 Wael Badawy 54 Proof Intuition: wApq ÞÞ ! wbaAA rspq ÞÞÞ ! wAAA rqprpq ÞÞÞ !Case 1: Case 2: Type 2 Type 3
  • 55. © 2020 Wael Badawy 55 wAAA rqprpq ÞÞÞ !Case 1: Type 2 Stack height Input string Generated by prA p r q Generated by rqA
  • 56. © 2020 Wael Badawy 56 Stack height Input string Generated by rsA p q wbaAA rspq ÞÞÞ !Case 2: Type 3 a b r s
  • 57. © 2020 Wael Badawy 57 We formally prove this claim by induction on the number of steps in derivation: wApq ÞÞ ! Formal Proof: number of steps
  • 58. © 2020 Wael Badawy 58 Induction Basis: wApq Þ (one derivation step) A Kind 1 production must have been used: e®ppA Therefore, and e=wqp = ),,(),,( eeee pp * ! This computation of PDA trivially exists:
  • 59. © 2020 Wael Badawy 59 Induction Hypothesis: wApq ÞÞ ! k derivation steps ),,(),,( eee qwp * ! suppose it holds:
  • 60. © 2020 Wael Badawy 60 Induction Step: wApq ÞÞ ! 1+k derivation steps ),,(),,( eee qwp * ! We have to show:
  • 61. © 2020 Wael Badawy 61 wApq ÞÞ ! 1+k derivation steps wbaAA rspq ÞÞÞ ! wAAA rqprpq ÞÞÞ !Case 1: Case 2: Type 2 Type 3
  • 62. © 2020 Wael Badawy 62 wAAA rqprpq ÞÞÞ !Case 1: Type 2 yzw =We can write yApr ÞÞ ! kAt most steps zArq ÞÞ ! kAt most steps 1+k steps
  • 63. © 2020 Wael Badawy 63 yApr ÞÞ ! kAt most steps zArq ÞÞ ! kAt most steps From induction hypothesis, in PDA: ),,(),,( eee ryp * ! ),,(),,( eee qzr * ! From induction hypothesis, in PDA:
  • 64. © 2020 Wael Badawy 64 ),,(),,( eee ryp * ! ),,(),,( eee qzr * ! ),,(),,(),,( eeee qzryzp ** !! ),,(),,( eee qwp * ! yzw =since
  • 65. © 2020 Wael Badawy 65 wbaAA rspq ÞÞÞ !Case 2: Type 3 aybw =We can write yArs ÞÞ ! kAt most steps 1+k steps
  • 66. © 2020 Wael Badawy 66 yArs ÞÞ ! kAt most steps From induction hypothesis, the PDA has computation: ),,(),,( eee syr * !
  • 67. © 2020 Wael Badawy 67 p qr sta ®e, e®tb, And PDA Contains transitions Type 3 baAA rspq ® Grammar contains production wbaAA rspq ÞÞÞ !
  • 68. © 2020 Wael Badawy 68 p qr sta ®e, e®tb, ),,(),,( tybraybp !e ),,(),,( eeqtbs !
  • 69. © 2020 Wael Badawy 69 ),,(),,( tybraybp !e ),,(),,( eee syr * ! ),,(),,( eeqtbs ! ),,(),,( tbstybr * ! We know We also know Therefore: ),,(),,(),,(),,( eee qtbstybraybp !!! *
  • 70. © 2020 Wael Badawy 70 ),,(),,(),,(),,( eee qtbstybraybp !!! * ),,(),,( eee qwp * ! aybw =since END OF PROOF
  • 71. © 2020 Wael Badawy 71 So far we have shown: )()( MLGL Í With a similar proof we can show )()( MLGL Ê Therefore: )()( MLGL =