CS181 Lecture 16 -- Fall 2026, UCLA
Overview
Eli Jaffe develops context-free grammars through constructions for balanced symbol counts and strings that are not of the form ww, then uses the context-free pumping lemma to show that 0^n1^n2^n is not context-free. The lecture highlights that context-free languages are closed under union and concatenation but not, in general, under intersection or complement; the pumping-lemma proof that the language of doubles is not context-free begins but is left unfinished.
Key takeaways
- A CFG for 0^n1^n2^m can combine A → 0A1 | ε with B → 2B | ε, separating equal-count generation from an independent suffix.
- To build a CFG for even-length strings that are not ww, locate a mismatched corresponding bit and express the string as two balanced pieces centered on different bits.
- Context-free languages are closed under union and concatenation but not under intersection: intersecting the grammars for 0^n1^n2^m and 0^m1^n2^n yields the non-context-free language 0^n1^n2^n.
- The CFG pumping lemma decomposes long strings as A B C D E and pumps B and D together, with |BD| > 0 and |BCD| ≤ p.
- For 0^p1^p2^p, the pumping region is too short to touch all three symbol blocks, so pumping necessarily changes some counts while leaving another block unchanged.
Chapters
- Eli Jaffe identifies Lecture 16 as the penultimate CS181 meeting and plans a practice-problem review for the next class.
- Students will vote on whether the December 10 final covers context-free grammars alone or also uncomputability.
- The exam is scheduled for 11:30 a.m. to 2:30 p.m. in Young Hall CS76, with a design target of about 1 hour 50 minutes.
- The prior example generates 0^m 1 0^n 1 0^(m+n) by separating the m-balanced and n-balanced portions.
- Jaffe emphasizes that CFG variables can represent stages of a construction, with productions adding matching symbols around a center.
- The design heuristic is to break a language into simpler components and look for balanced structure.
- L1 consists of 0^n1^n2^m, where n and m are independently nonnegative.
- The proposed construction first generates the equal-count zero and one block, then appends an independently generated run of twos.
- The example illustrates CFG support for concatenating languages with separate counting constraints.
- The grammar uses S → AB, A → 0A1 | ε, and B → 2B | ε to generate 0^n1^n2^m.
- A adds one zero and one per recursive step; B independently adds any number of twos.
- A new start symbol concatenating two component grammars provides a general construction for concatenated context-free languages.
- L2 has the form 0^m1^n2^n, with arbitrary leading zeros and equal counts of ones and twos.
- The grammar separates the unrestricted zero prefix from the balanced one-and-two portion.
- A derivation can expand the zero and balanced components in interleaved order without changing the generated language.
- A double is a bit string of the form ww, where w is any bit string; examples include 010010 and 11101110.
- The target language is the complement of the doubles among strings over {0,1}.
- Jaffe proposes handling easy and difficult cases separately, then combining their grammars with union.
- Every odd-length bit string cannot be written as ww because a doubled string must have even length.
- A grammar can add any pair of bits around a central variable, then finish with either 0 or 1.
- This construction generates every odd-length bit string, providing one subset of the non-double language.
- An even-length string can be split into equal first and second halves; it is not a double exactly when those halves differ somewhere.
- The proof selects one corresponding position where the bits differ, such as 1 in the first half and 0 in the second.
- The remaining bits are unconstrained, so the construction focuses on encoding that guaranteed mismatch.
- Jaffe labels the unmatched position using i preceding bits and j intervening bits, with each half having length i + j + 1.
- Reordering the description, not the string's bits, reveals two centered components: one with 1 at its center and one with 0.
- Concatenating those components places the differing bits at corresponding positions in the two halves.
- Separate variables generate strings with a 1 or a 0 exactly in the middle by adding arbitrary bit pairs around the center.
- The grammar concatenates a centered-1 string with a centered-0 string, and includes the reverse ordering for the opposite mismatch.
- A derivation demonstrates how the two centered blocks guarantee unequal corresponding bits after the result is split in half.
- The full grammar begins with a start symbol that branches to the odd-length construction or the even-length construction.
- Every non-double is covered because it has either odd length or even length with at least one mismatched position.
- Jaffe distinguishes proving a language context-free by describing component grammars from explicitly listing every production.
- The lecture reviews union and concatenation as operations that preserve context-freeness.
- L1 is 0^n1^n2^m, while L2 is 0^m1^n2^n; their intersection requires all three counts to match.
- The intersection therefore equals {0^n1^n2^n : n ≥ 0}, setting up a counterexample to closure under intersection.
- Jaffe states that {0^n1^n2^n : n ≥ 0} is not context-free, despite being the intersection of two context-free languages.
- This example proves that context-free languages are not closed under intersection in general.
- The language of doubles is also asserted to be non-context-free, even though its complement was constructed as context-free.
- For every context-free grammar, the pumping lemma supplies a threshold p for sufficiently long strings in its language.
- A qualifying string decomposes as A B C D E, and pumping gives A B^i C D^i E for every i ≥ 0.
- The paired B and D regions reflect recursive expansion on both sides of a central grammar-tree node.
- The lemma requires |BD| > 0, while any one of A, B, C, D, or E may individually be empty.
- It also requires |BCD| ≤ p, which confines the pumped region to a short contiguous section.
- Jaffe relates the decomposition to repeated variables in a CFG parse tree: C marks a subtree root, while B and D capture its left and right growth.
- Assuming the language is context-free, the proof receives a pumping threshold p and chooses x = 0^p1^p2^p.
- The string is in the language and has length 3p, satisfying the lemma's minimum-length requirement.
- The proof must show that every valid A B C D E split fails after some allowed pumping choice.
- Because |BCD| ≤ p, the pumped region cannot reach all three p-symbol blocks of 0^p1^p2^p.
- If BCD lies within one block, pumping down with i = 0 changes that block's count while leaving the others unchanged.
- If BCD straddles a boundary, at least one of the two touched blocks changes while the third block remains untouched, again violating equal counts.
- The next target is D = {ww : w ∈ {0,1}*}, which Jaffe claims is not context-free.
- The initial candidate x = 1^p1^p fails because pumping any valid regions still produces an even-length string of all ones.
- A useful challenge string must contain multiple symbol blocks so that short pumped regions disrupt the matching halves.
- The proposed string is x = 0^p1^p0^p1^p, whose first half equals its second half.
- Since |BCD| ≤ p, the pumped region can lie in one block or span at most one boundary between blocks.
- The lecture sketches how pumping inside a block or across the first or third boundary disrupts the double structure.
- The remaining case has BCD straddling the middle boundary between the second and third blocks.
- Jaffe says pumping must make either the zero or one counts fail to match, but postpones the detailed argument because class time is nearly over.
- The next class is scheduled to finish the pumping-lemma proof and work through practice problems.
Transcript excerpt
0:03 All right, let's get started on our penultimate CS181 lecture. Very exciting. Very exciting. Welcome to week 10. Hope everybody had a good Thanksgiving. Who watched Tana Montana? >> Wow. >> Wow. You have seen it. >> A little bit. >> Okay, that count. That counts. Well, today is exciting. Uh, we are mostly going to finish our content today, which is pretty crazy. Um, what we did last time was we essentially introduced contextf free grammars, saw examples of contextfree
0:53 grammarss. Uh, we showed that they're more powerful than DFAs. So they can do everything DSAs can do and NFAs and regex uh plus some extra stuff and we talked about how they are important for programming languages among other things. Now they help us show that programs adhere to some kind of syntax. And today we're going to round it all out. We're going to look at more examples of contextf free grammarss and we're going to talk about what we've talked about with all our other uh methods of computation which are closure properties
1:43 or you know lack thereof in some cases as we saw with NFA sometimes closure is not so easy and we will talk a bit about what motivated us to do this in the first place which was computing semantic properties of in this case context program. This is what was so hard with touring machines. It was hard to figure out what they did on inputs. But as we're going to see, we can at least do some of that with contextf free grammar. And we are mostly this is basically going to constitute the end of the content. We're going to maybe talk about a couple other tiny little details depending on how far we get today. And next class, one of the pieces of
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Eli Jaffe.