CS181 Lecture 15 -- Fall 2026, UCLA
Overview
Eli Jaffe motivates context-free grammars (CFGs) as a useful middle ground between limited finite automata and powerful but difficult-to-analyze Turing-complete programs. He defines CFG derivations and generated languages, demonstrates grammars for equal-count strings, palindromes, and balanced parentheses, proves that CFGs can represent every regular language, and introduces techniques for designing grammars for structured languages.
Key takeaways
- Rice's theorem makes every non-trivial semantic property of programs in a Turing-complete language uncomputable, including questions such as whether a program halts.
- The grammar A → 0A1 | ε generates 0ⁿ1ⁿ for n ≥ 0, demonstrating that CFGs recognize patterns beyond the capabilities of finite automata.
- The grammar A → AA | (A) | ε generates exactly the well-matched parenthesis strings through concatenation and recursive nesting.
- A DFA can be converted into an equivalent CFG by mapping each state to a variable, each transition i on a to j to Vi → aVj, and each accepting state to an ε production.
- CFGs are useful for programming-language syntax because they can describe valid program text without having to execute the program.
- A productive CFG design approach is to identify linked symbols and recursive substructure, then use separate variables to represent different phases of generation.
Chapters
- Eli Jaffe says Exam 3 is currently planned to cover Lectures 15 through 17, focused on context-free grammars, and proposes polling students about adding uncomputability material.
- Homework 5 is available early and is tentatively due December 7.
- Jaffe recommends submitting work to Gradescope after each work session so technical problems do not erase completed homework.
- Rice's theorem says every non-trivial semantic property of programs in a Turing-complete language is uncomputable.
- A semantic property depends only on input-output behavior, as with whether a program halts or outputs 1 on some input.
- The black-box test is whether observing a program's behavior is enough to determine the property without inspecting its implementation.
- Uncomputable questions arise in number theory, string theory, games, and mathematical logic, not just software engineering.
- Gödel's incompleteness theorem connects limits on mathematical proof to limits associated with computation.
- The broader point is that uncomputability constrains many disciplines that seek definitive answers to mathematical questions.
- Turing machines can simulate higher-level programming languages such as Python with only logarithmic computational overhead, as discussed in the course.
- The Church-Turing thesis proposes that physical computation cannot calculate more functions than Turing machines can.
- A universal Turing machine can simulate other Turing machines, while Turing completeness means a language can compute everything a Turing machine can.
- Turing completeness is both a capability and a drawback: expressive programs can be impossible to analyze completely.
- Jaffe uses SQL injection as an example of the risks of allowing code-like input to interact with a Turing-complete system.
- No algorithm can perfectly verify every possible program's behavior, motivating restricted models that preserve useful expressiveness.
- CFGs are less powerful than Turing machines but more expressive than DFAs, NFAs, and regular expressions.
- Finite automata cannot recognize some useful patterns, including strings with equal numbers of 0s followed by 1s.
- The lecture uses CFGs to explore whether a restricted computational model can remain expressive while supporting useful reasoning.
- A CFG consists of variables V, terminals Σ, production rules R, and a start variable S.
- Variables are expanded into strings of terminals and possibly other variables until only terminals remain.
- Jaffe's first grammar uses A → 0A1 and A → ε over the terminal alphabet {0, 1}.
- A one-step derivation replaces one variable in a string using a rule; a multi-step derivation chains such replacements.
- A string belongs to the language generated by grammar G if it can be derived from G's start variable and contains only terminals.
- The membership function fG(x) returns 1 when x is generated by G and 0 otherwise.
- Applying A → 0A1 repeatedly and then A → ε generates 0ⁿ1ⁿ for every n ≥ 0.
- Each application of 0A1 adds one 0 on the left and one 1 on the right, keeping the counts equal and the blocks ordered.
- This language is not regular, so CFGs can generate a language that DFAs, NFAs, and regular expressions cannot.
- Several productions with the same left-hand variable can be written as alternatives on one line.
- The grammar A → 0A0 | 1A1 | 0 | 1 | ε generates binary palindromes.
- The paired outer symbols match across the center, while 0, 1, or ε supplies an odd-length or even-length center.
- The grammar A → AA | (A) | ε generates well-matched parenthesis strings, including the empty string.
- The production (A) wraps an already generated balanced expression, while AA concatenates two such expressions.
- The recursive structure makes CFGs a compact way to describe infinitely many syntactically valid strings.
- Changing the order of terminals in a production changes the generated language; (A) specifically places the opening parenthesis before A and the closing one after it.
- A start symbol must be a variable, and additional variables can represent distinct phases of a grammar's construction.
- The question period reinforces that CFG rules replace variables without relying on neighboring symbols.
- Jaffe explains that programming languages such as C, Java, Python, and C++ have CFGs describing syntactically valid programs.
- A grammar for Python recognizes whether a program's text is syntactically valid; it does not execute or evaluate that program.
- Separating syntax checking from execution lets compilers reject malformed programs before running them.
- The lecture claims that for every DFA, there is a CFG whose generated language is identical to the DFA's accepted language.
- The construction uses one CFG variable for each DFA state and keeps the DFA's input symbols as grammar terminals.
- The DFA's start state becomes the grammar's start variable, linking the two models' components.
- For a DFA transition from state i on symbol a to state j, add the production Vi → aVj.
- The variable at the end of a partially generated string records the DFA's current state, while terminals to its left record symbols read so far.
- For each accepting DFA state, add a production that replaces its variable with ε, allowing exactly accepted strings to finish.
- Jaffe uses a three-state DFA that tracks the number of 1s modulo 3; 0 transitions leave the remainder unchanged.
- The accepting state represents remainder 0, so its grammar variable receives an ε production.
- The transition-to-production construction shows how the CFG generates exactly the strings accepted by the example DFA.
- The DFA construction establishes that every regular language is context-free.
- The language 0ⁿ1ⁿ is context-free but not regular, giving an example DFA-based models cannot recognize.
- Together, these results show that CFGs are strictly more expressive than DFAs, NFAs, and regular expressions.
- A grammar for L1 ∪ L2 can use a new start variable S with alternatives S → S1 | S2, where S1 and S2 start grammars for the component languages.
- Keeping the existing productions for both grammars lets the new start variable select either language.
- Breaking a target language into simpler cases makes grammar construction more manageable.
- CFGs naturally link symbols, as in matching outer bits for palindromes or pairing parentheses around a central expression.
- Recursive descriptions such as wrapping one balanced expression or concatenating two help turn structural patterns into productions.
- If a target language is regular, its DFA can serve as an intermediate step for constructing a CFG.
- The target language has the form 0ᵐ1 0ⁿ1 0ᵐ⁺ⁿ, where m and n are nonnegative integers.
- The final run of zeros can be viewed as a copy of the first run followed by a copy of the second, exposing two linked counting relationships.
- The alphabet is {0, 1}, and the construction borrows the paired-zero structure from the palindrome grammar.
- A variable can generate a central 0ⁿ1 0ⁿ pattern by repeatedly adding one zero on each side and then replacing the center variable with 1.
- A second variable represents the outer 0ᵐ and 0ᵐ portions, then delegates to the first variable for the inner pattern.
- Jaffe corrects the start variable to B and previews the next lecture's first language that CFGs cannot generate.
Transcript excerpt
0:03 All right. Hello to my fine group of seven who's here today. I know lecture after an exam and the day before Thanksgiving. It's like I didn't even really want people to show up, but thank you for being here. Thank you for everybody who is listening in post virtually. Um, congratulations on finishing exam 2. Woohoo. Round of applause. Um, hopefully you thought it went well. Uh, I have I'm meeting with the TAs on Thursday to discuss. Well, okay, I'll get into that when we talk about statistics. First, let's do everything in order. What did we do last time? What did we do last time? Uh, well, we wrapped up
0:51 our little discussion on uncomputability. this sort of mind-blowing idea that there are things that are completely off limits to us when it comes to computation. And we showed how to compute how to show uncmputability via reductions. And we looked at lots of examples of uncomputable problems. And we talked about how in fact there are many many many many many uncomputable problems in computer science and you know that show up in contexts beyond computer science and we saw one uh characterization of at
1:41 least a subset of the uncomputable problems that are out there with Rice's theorem. No, we saw this idea of meta programming is very very difficult and in fact we can't really reason about what touring machines are able to compute effectively at all. And so today we're going to kind of close the book on that topic. We're going to finish off talking about touring machines. uh review what we discovered and as I teased last time we're going to move backwards for the first time and talk about a more restricted model of computation with
2:28 the motivation being what we've just seen about
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.