david evans cs302: theory of computation university of virginia computer science lecture 10:...
TRANSCRIPT
![Page 1: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/1.jpg)
David Evanshttp://www.cs.virginia.edu/evans
cs302: Theory of ComputationUniversity of VirginiaComputer Science
Lecture 10: Lecture 10: Context-Free Context-Free
Languages Languages ContextuallyContextually
![Page 2: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/2.jpg)
2Lecture 10: NDPDAs/CFGs/PDAs
Exam 1
• In class, next Thursday, Feb 28 • Covers:
Lectures 1-10
Sipser Ch 0-2
Problem Sets 1-3 + Comments
Exam 1
Note: unlike nearly all other sets we draw in this class, all of these sets are finite, and the size represents the relative size.
![Page 3: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/3.jpg)
3Lecture 10: NDPDAs/CFGs/PDAs
Exam 1 Notesheet
• For Exam 1, you may not use anything other than – Your own brain and body – A single page (one side) of notes that
you create
• You can work with others to create your notes page
![Page 4: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/4.jpg)
4Lecture 10: NDPDAs/CFGs/PDAs
Menu
• Are DPDAs equivalent to NDPDAs?
• Properties of CFLs
• Equivalence of CFGs and NDPDAs
![Page 5: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/5.jpg)
5Lecture 10: NDPDAs/CFGs/PDAs
Language Classes
Regular Languages
Context-Free Languages
Violates
Pumping
Lemma For RLs
Vio
late
s Pu
mpi
ng
Lem
ma
For
CFL
s
Described by DFA, NFA, RegExp, RegGram
Described by
CFG, N
DPD
A0n1n
0n1n2n
0n
w
wwRww
Where are DPDAs?
![Page 6: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/6.jpg)
6Lecture 10: NDPDAs/CFGs/PDAs
Proving Set Equivalence
A = B A B and B A
Sets A and B are equivalent if A is a subset of B and B is a subset of A.
![Page 7: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/7.jpg)
7Lecture 10: NDPDAs/CFGs/PDAs
Proving Formalism Equivalence
LR(M) = the set of languages that can be recognized by some M
= { l | l P(Σ*) and there is some m M such that L(m) = l }
A = B LR(A) LR(B) and LR(B) LR(A)
![Page 8: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/8.jpg)
8Lecture 10: NDPDAs/CFGs/PDAs
Proving Formalism Non-Equivalence
LR(M) = the set of languages that can be recognized by some M
= { l | l P(Σ*) and there is some m M such that L(m) = l }
A B There is an l P(Σ*) that is in LR(A)but not in LR(B)
or there is an l in LR(B) but not in LR(A)
![Page 9: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/9.jpg)
9Lecture 10: NDPDAs/CFGs/PDAs
Regular Languages
Context-Free Languages
Violates
Pumping
Lemma For RLs
Described by DFA, NFA, RegExp, RegGram
Described by
CFG, N
DPD
A
0n1n0n1n2n
0n
w
wwR
ww
Sensible option 1: LR(NDPDA) LR(DPDA) LR(NFA) = LR(DFA)
Sensible option 2: LR(NDPDA) = LR(DPDA) LR(NFA) = LR(DFA)
To eliminate =, we need to find some language L that can be recognized by an NDPDA and prove it cannot be recognized by a DPDA
![Page 10: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/10.jpg)
10Lecture 10: NDPDAs/CFGs/PDAs
LR(NDPDA) LR(DPDA)
A = { 0i1j | i 0, j = i or j = 2i }
A LR(NDPDA)
ε, ε$0, ε+
ε, εε
1, +εε, $ ε
ε, ε
ε1, +ε
1, εεε, $ ε
![Page 11: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/11.jpg)
11Lecture 10: NDPDAs/CFGs/PDAs
LR(NDPDA) LR(DPDA)
A = { 0i1j | i 0, j = i or j = 2i }
A LR(DPDA)Proof by contradiction.Suppose there is a DPDA P that recognizes A.It must be in accept states only after processing 0i1i and 0i12i
…0, αβ 1, αβ
2i transitions, consuming 0i1i
…1, αβ 1, αβ
i transitions, consuming 1i
![Page 12: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/12.jpg)
12Lecture 10: NDPDAs/CFGs/PDAs
LR(NDPDA) LR(DPDA)
A = { 0i1j | i 0, j = i or j = 2i }
A LR(DPDA)Proof by contradiction.Suppose there is a DPDA P that recognizes A.It must be in accept states only after processing 0i1i and 0i12i
…0, αβ 1, αβ
2i transitions, consuming 0i1i
…1, αβ 1, αβ
i transitions, consuming 2i
22
L(P’) = { 0i1i2i | i 0}
![Page 13: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/13.jpg)
13Lecture 10: NDPDAs/CFGs/PDAs
LR(NDPDA) LR(DPDA)
A = { 0i1j | i 0, j = i or j = 2i }
A LR(DPDA)
Proof by contradiction. If there is a DPDA P that recognizes A, we could construct a DPDA P' that recognizes
A' = L(P') = { 0i1i2i | i 0} But, we know A' is not a CFL! (Prove using pumping lemma)So, there is no NDPDA that can recognize A', so there is no DPDA that can recognize A', so P' must not exist. Hence, P must not exist. This means there is no DPDA that can recognize A.
![Page 14: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/14.jpg)
14Lecture 10: NDPDAs/CFGs/PDAs
Regular Languages
Context-Free Languages
Violates
Pumping
Lemma For RLs
Described by DFA, NFA, RegExp, RegGram
Described by
CFG, N
DPD
A
0n1n0n1n2n
0n
w
Aww
LR(NDPDA) LR(DPDA) LR(NFA) = LR(DFA)
Deterministic Context-Free Languages: recognized by DPDA
A = { 0i1j | i 0, j = i or j = 2i }
![Page 15: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/15.jpg)
15Lecture 10: NDPDAs/CFGs/PDAs
Closure Properties of RLsIf A and B are regular languages then:
AR is a regular languageConstruct the reverse NFA
A* is a regular languageAdd a transition from accept states to start
A is a regular language (complement)F' = Q – F
A B is a regular languageConstruct an NFA that combines DFAs
A B is a regular languageConstruct an DFA combining DFAs that accepts if both accept
![Page 16: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/16.jpg)
16Lecture 10: NDPDAs/CFGs/PDAs
Closure Properties of CFLsIf A and B are context free languages then:
AR is a context-free language ?
A* is a context-free language ?
A is a context-free language (complement)?
A B is a context-free language ?
A B is a context-free language ?
Some of these are true. Some of them are false.
![Page 17: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/17.jpg)
17Lecture 10: NDPDAs/CFGs/PDAs
CFLs Closed Under ReverseGiven a CFL A, is AR a CFL?
Since A is a CFL, there is some CFG G that recognizes A.
Proof-by-construction: There is a CFG GR that recognizes AR.
G = (V, Σ, R, S)
GR = (V, Σ, RR, S) RR = { A αR | A α R }
![Page 18: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/18.jpg)
18Lecture 10: NDPDAs/CFGs/PDAs
CFLs Closed Under *Given a CFL A, is A* a CFL?
Since A is a CFL, there is some CFG G that recognizes A.
Proof-by-construction: There is a CFG G* that recognizes A*.
G = (V, Σ, R, S)
G* = (V {S0}, Σ, R*, S0) R* = R { S0 S } { S0 S0S0 } { S0 ε }
![Page 19: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/19.jpg)
19Lecture 10: NDPDAs/CFGs/PDAs
Closure Properties of CFLsIf A and B are context free languages then: AR is a context-free language TRUE
A* is a context-free language TRUE
A is a context-free language (complement)?
A B is a context-free language ?
A B is a context-free language ?
![Page 20: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/20.jpg)
20Lecture 10: NDPDAs/CFGs/PDAs
CFLs Closed Under Union
Given two CFLs A and B is A B a CFL?
Proof-by-construction: There is a CFG GAUB that recognizes A B.Since A and B are CFLs, there are CFGs GA = (VA, ΣA, RA, SA) and GB = (VB, ΣB, RB, SB) that generate A and B.
GAUB = (VA VB, ΣA ΣB, RAUB, S0) RAUB
= RA RB { S0 SA } { S0 SB }
Assumes VA and VB are disjoint (easy to arrange thisby changing variable names.)
![Page 21: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/21.jpg)
21Lecture 10: NDPDAs/CFGs/PDAs
Closure Properties of CFLsIf A and B are context free languages then: AR is a context-free language TRUE
A* is a context-free language TRUE
A is a context-free language (complement)?
A B is a context-free language TRUE
A B is a context-free language ?
![Page 22: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/22.jpg)
22Lecture 10: NDPDAs/CFGs/PDAs
CFLs Closed Under Complement?
• Try to find a counter-example
{0i1i | i 0 } is a CFL. Is its complement?Yes. We can make a DPDA
that recognizes it: swap accepting states of DPDA that recognizes 0i1i.
Not a counterexample…but not a proof either.
![Page 23: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/23.jpg)
23Lecture 10: NDPDAs/CFGs/PDAs
Complementing Non-CFLs
{ww | w Σ* } is not a CFL. Is its complement?
Yes. This CFG recognizes is: S 0S0 | 1S1 | 0X1 | 1X0
X 0X0 | 1X1 | 0X1 | 1X0 | 0 | 1 | εSo, we have found a pair: P, P where one is a CFL and the other is not. Thus, CFLs are not closed under complement.
Opps! As Danni pointed out in class, this is badly broken. We will fix it next class…
![Page 24: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/24.jpg)
24Lecture 10: NDPDAs/CFGs/PDAs
Closure Properties of CFLsIf A and B are context free languages then:
AR is a context-free language TRUE
A* is a context-free language TRUE
A is not necessarily a context-free language (complement)
A B is a context-free language TRUE
A B is a context-free language ?Left for you to solve (possibly on Exam 1)
![Page 25: David Evans cs302: Theory of Computation University of Virginia Computer Science Lecture 10: Context-Free Languages Contextually](https://reader035.vdocument.in/reader035/viewer/2022070306/551633be550346c6758b4e9e/html5/thumbnails/25.jpg)
25Lecture 10: NDPDAs/CFGs/PDAs
Charge
• Thursday and Tuesday: some interesting applications of CFGs