CS181 Lecture 17 -- Fall 2026, UCLA
Overview
Eli Jaffe completes CS181’s context-free grammar unit by finishing a pumping-lemma proof that the duplicate-string language is not context-free, then shows that CFG nonemptiness is computable while fullness and grammar equivalence are not. He connects CFGs to syntax checking through Chomsky normal form and uses the remaining lecture for a practice exam covering grammar construction and pumping-lemma proofs.
Key takeaways
- CFG nonemptiness is decidable by marking terminals good and repeatedly marking any variable whose production yields only good symbols; the process ends when the start variable is marked or no further variables qualify.
- CFG fullness, the question of whether L(G) = Σ*, is uncomputable, and equivalence of two context-free grammars is uncomputable as well.
- A pumping-lemma proof for 0^(n²) uses x = 0^(p²): pumping changes its length by at most p, placing the result between p² and (p + 1)².
- For the language 0^i1^j2^(2i+j), a CFG can independently pair each 0 with two trailing 2s and each 1 with one trailing 2.
- Converting a CFG to Chomsky normal form enables an efficient algorithm to test whether a particular input string is generated by that grammar.
- A CFG proof on an exam can be concise if it gives the productions and explains how their recursive components generate each required block and count.
Chapters
- Eli Jaffe previews the final CS181 topics: finish the duplicate-string pumping-lemma proof, discuss CFG semantic properties, and explain syntax recognition.
- Exam 3 is scheduled for December 10 from 11:30 a.m. to 2:30 p.m. in Young Hall, with 1 hour 50 minutes of designed work time.
- Exam 3 covers lectures 15 to 17 and context-free grammars only; Homework 5 is due December 7 at 9:59 p.m.
- The target language consists of strings made from a string W repeated twice, written WW; Jaffe assumes a pumping length p to derive a contradiction.
- The challenge string is 0^p1^p0^p1^p, which is in the language because its two halves match.
- The CFG pumping lemma bounds the combined length of BCD by p, so the pumped region can lie in one block or cross at most one boundary between the four blocks.
- When BCD crosses the middle boundary, pumping changes the middle zero block, the middle one block, or both, while the outer zero and one blocks stay unchanged.
- After pumping, at least one symbol count differs between the first and second halves, so the resulting string is no longer of the form WW.
- Together with the single-block and outer-boundary cases, the case analysis shows that every valid decomposition can be pumped out of the language.
- Jaffe situates CFGs between finite automata and Turing machines in expressive power.
- CFG languages are closed under union and concatenation, but not under intersection or complement.
- The course has covered CFG construction and the pumping lemma as a method for proving that languages are not context-free.
- A property of a grammar is semantic when it depends only on the language the grammar generates, not on the particular production rules.
- Equivalent grammars must agree on every semantic property because they generate the same strings.
- Examples include whether a grammar generates any strings and whether it generates every string over its alphabet.
- The nonempty property returns 1 when a grammar generates at least one terminal-only string and 0 when its language is empty.
- In Jaffe’s example, S produces AB and B produces BD; because B and D cannot be eliminated into terminals, no terminal-only string is reachable.
- Rules for A and C can produce terminals, but that does not help if an unavoidable variable such as B or D remains.
- The algorithm starts by marking terminals as good, then marks a variable good whenever a production replaces it with only good variables and terminals.
- If the start variable S becomes good, the grammar is nonempty; if no additional variables can be marked, it is empty.
- The marking process terminates because it can only add variables from the grammar’s finite set, making nonemptiness computable by a Turing machine.
- The fullness property asks whether a CFG generates every string in Σ*, such as every binary string when Σ = {0, 1}.
- Jaffe states that fullness is uncomputable, despite the simple-sounding task of checking whether any string is missing.
- Equivalence of two CFGs, meaning whether they generate precisely the same language, is also uncomputable.
- CFGs permit some useful semantic checks, such as nonemptiness, but not all desirable properties, such as fullness or equivalence.
- Jaffe uses smart contracts as an example of systems that limit computational expressiveness to make important security properties easier to verify.
- The choice of a computational model balances the expressive power an application needs against the properties it must be able to guarantee.
- A compiler must determine whether an input string x can be generated by the grammar G that defines a programming language’s syntax.
- Jaffe says every CFG can be converted to Chomsky normal form, abbreviated CNF, after which an efficient algorithm can test whether G generates x.
- The conversion gives grammar-based syntax checking a structured procedure rather than relying on arbitrary production rules.
- CFGs provide benefits over Turing machines for some semantic questions, but they also lack closure under complement and intersection.
- Unlike the Myhill-Nerode theorem for regular languages, the course has no equivalent characterization giving the most efficient CFG for a language.
- After completing the content, Jaffe takes a five-minute break before starting an in-class practice exam session.
- Jaffe introduces the practice exam posted in the EdStem lesson solutions section and recommends attempting it before viewing the answer key.
- The exam format includes eight true-or-false questions worth half a point each, focused on basic CFG properties and direct consequences.
- Sample claims ask about regular languages being context-free, closure under complement and concatenation, and whether subsets of CFLs must be CFLs.
- One construction problem uses strings 0^i1^j2^k with k = 2i + j, requiring a grammar to coordinate two kinds of contributions to the number of twos.
- Another asks for a CFG generating binary strings that are not palindromes.
- A harder construction problem describes 0*1*0*1* strings whose first and second 0/1 blocks have equal combined lengths.
- The first non-CFL problem asks about strings of zeros whose lengths are perfect squares.
- Another language requires the number of ones to equal the maximum of the number of zeros and twos.
- The harder example has the form x2y2x, where x and y are binary strings and the initial and final x blocks match.
- Students work individually or in small groups on at least one CFG-construction problem and one pumping-lemma problem.
- Jaffe advises that grammar solutions should include the rules plus brief explanations of what each variable and production accomplishes, rather than a fully formal correctness proof.
- The class is asked to focus on the simplest construction and pumping problems for the shared solutions.
- Students use the practice period to test strategies for constructing grammars and selecting challenge strings for pumping arguments.
- Jaffe circulates and gives guidance on writing enough explanation to make a grammar’s role clear without over-formalizing the proof.
- The session prepares the class to review one example of each problem style before the final exam.
- Jaffe announces that the class will review the simplest CFG-construction question and the simplest non-CFL question.
- The selected examples are the language with the 2i + j count condition and the perfect-square-length zero strings.
- Students get a final few minutes to examine those two questions before the walkthrough.
- The grammar separates the required 2^k suffix into 2^j and 2^(2i), pairing each generated 1 with one 2 and each 0 with two 2s.
- One recursive component adds a 0 at the left and two 2s at the right; a second component adds a 1 and one 2.
- Jaffe considers a short derivation explanation sufficient when it shows how arbitrary i and j produce 0^i1^j2^(2i+j).
- Assume the language L = {0^(n²) : n ≥ 0} is context-free, so a pumping length p exists.
- Choose the challenge string x = 0^(p²), which belongs to L and has length at least p.
- Because x contains only zeros and |BCD| ≤ p, pumping changes the total zero count by some k with 0 ≤ k ≤ p.
- Pumping up once gives a string of length p² + k, with 1 ≤ k ≤ p when the pumped material is nonempty.
- The next square after p² is (p + 1)² = p² + 2p + 1, so p² + k lies strictly between consecutive squares.
- The argument rules out a perfect-square length; Jaffe notes that the solution key contains the remaining formal details.
- Jaffe closes the final CS181 lecture by thanking students for attending during the quarter.
- He wishes the class luck on Exam 3, which focuses exclusively on context-free grammars.
Transcript excerpt
0:02 Yeah, he was like, "I can't come to the last lectures." But I think it would be really funny, because you write "last class" and then "like today" every time. But like, since it's the last class, if he wrote "last class" and then "the last class," I was like, "Yeah, that's pretty funny." So credit to Beyond. I'll I'll immortalize him here. Let's see. Oh gosh, that's not how that works. Beyond. All right, so today [clears throat] we are going to be finishing up like I said last class we saw more examples of creating context free grammars. We looked at some closure properties or lack thereof as we saw in some cases,
0:49 context free grammars. Unions are good, concatenations are good, intersections are bad, not so bad. And we also saw how to prove that languages are not context free by using our new and improved version of the pumping lemma for context free grammars. And we began a we saw one proof of that in full and we began but did not get the chance to finish our proof that double was not context free. So we're going to finish that up today and then we are going to talk a little bit about semantic properties of context free grammars since that was a big motivation for us. Can we actually compute semantic properties of context free grammars? I'm clear.
1:36 And another big motivation that we saw was I talked about how context free grammars exist in the programming languages and programming languages use them to check if your syntax is valid. And one sort of hole we've left open is well can we actually do that? Like can we take a particular input and check whether it is generatable from a context of particular context-free grammar.
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.