Midterm 1 study guide

Everything on the Midterm 1 announcement, in exam order, with lecture-only material marked From lecture. Kept here for final exam review, along with the practice test and questions from the midterm itself.

Exam at a glance

Midterm 1 was written on Thursday, October 8: 29 five-option multiple-choice questions on Sipser Chapter 0, all of Chapter 1, and the start of Chapter 2, plus what the lectures covered. Bring HB pencils and an eraser, arrive early, and print your first and last names in capitals on both the bubble sheet and the question paper.

#TopicSipserPagesLecture additions
1Introduction0.1–0.41–26Set exercises, 2S notation, recursive definitions, induction examples
2Finite automata1.131–46DFA minimization (table filling)
3NFAs and regular expressions1.2–1.347–76Lexical analysis: tokens, lexemes, regular definitions
4Pumping lemma and CFGs1.4, 2.177–87, 102–107(V, T, P, S) notation, leftmost and rightmost derivations

What the Oct 6 practice test tells us

The prof said the midterm questions would be similar to the practice test, and they were. It shows his format: five choices (a)–(e), shared setups (one NFA or DFA table used for two or three questions), automata given as transition tables, and minimization asked in terms of partitions. Option (e) is often “None of the above” or “Both (a) and (b)”, and Q8’s answer was (e).

QAskedAnswerSkillIn this guide
1A − B for A = {1,2,3,4}, B = {3,4,5}(d) {1, 2}Set differenceSets
2Which is true about 2S, S = {a,b,c}(b) {a, b} ∈ 2S∈ vs ⊆ with power setsSets
3Properties of a relation on {1,2,3}(d) Equivalence relationReflexive, symmetric, transitiveRelations
4Subset construction: {q₀, q₁} on 1(c) {q₀, q₂}One step of the constructionSubset construction
5Which DFA state accepts(d) {q₀, q₂}Subsets containing an accept stateSubset construction
6States after minimizing that DFA(d) 4Subset construction + refinementSubset construction
7Initial partition for minimization(b) {{q₀,q₁}, {q₂,q₃}}Accepting vs non-acceptingMinimization
8Which states merge(e) Both (a) and (b)Partition refinementMinimization
9Two parse trees for a + a × a(b) AmbiguousDefinition of ambiguityAmbiguity

Nothing on it covered regular expressions or the pumping lemma, but both are on the announcement, so don’t skip them. Take the practice test under exam timing, or quiz yourself on 70 questions in the same style.

Questions from the real midterm

Questions 8–29 of the Oct 8 midterm were captured in photos (1–7 were not). Themes it added beyond the practice test: reading functions from tables, naming the conversion methods, deciding FA equivalence, intersecting regex languages, and a large ε-NFA worked through subset construction, minimization and back to a regex. Retake them under exam timing or quiz yourself on them.

QAskedAnswerKey idea
8g(2, 3) from a table(b) 3First argument picks the row, second the column
9g(1, f(4))(e) None (it is 1)Inside out: f(4) = 2, then g(1, 2) = 1
10Domain of g(c) {0,1,2,3} × {2,3,4,6}Two arguments: a Cartesian product, in order
11Range of f(b) {2, 3, 4}The set of values; (2, 3, 3, 4) is a tuple
12NFA → DFA method(c) Subset constructionSubset construction
13Regex → NFA method(b) Thompson’s constructionRegex → NFA
14Regex for an ε-NFA(b) (0*1)*1Loop of 0*1 blocks via ε, then a final 1: “1” or anything ending in 11
15–17Match: length ÷ 3, same first/last, ∅(d), (c), (b)(ΣΣΣ)*; 0Σ*0 ∪ 1Σ*1 ∪ 0 ∪ 1; 1*∅ = ∅
18String a 3-cycle DFA rejects(a) εAccepts lengths 2, 5, 8, 11, …
19DFA vs NFA(c)One move per state and symbol vs 0, 1 or more
20L(a*ba) ∩ L(b*a)(c) {ba}List short strings of each and compare
21Non-members of (a*b)⁺(e) All are membersEvery nonempty string ending in b
22Definition of ambiguous(b) two rightmost derivationsRightmost works as well as leftmost
23Testing two FAs for equivalence(d) convert, then minimize and compareMinimization
24What is a GNFA(b) labels may be regexesGNFAs
25–277-state ε-NFA: trace aba, reachable states, count(a), (a), (d) 5Start at E({q₀}) = {q₀, q₅}; take closures after every move
28States after minimizing(b) 3The machine counts a’s mod 3
29Regex for the NFA(a) b*ab*ab*(ab*ab*ab*)*Number of a’s is 2 more than a multiple of 3

How the topics fit together

The arrows form a cycle (regex → NFA → DFA → regex), which is why all three describe exactly the regular languages. CFGs generate a strictly larger class; the pumping lemma shows a language is outside the regular box.

Topic 1: Mathematical preliminaries

Chapter 0 and the first lecture supply the vocabulary every later question uses. Expect quick definition, set-calculation and counting questions.

The three areas of the theory

AreaCentral question
Automata theoryWhat are the properties of formal models of computation (finite automata, grammars)? This exam.
Computability theoryWhich problems can a computer solve at all?
Complexity theoryWhat makes some solvable problems hard and others easy?

Sets

  • A set is unordered with no repeats: {7, 21, 57} = {57, 7, 21, 21}. A multiset counts repeats.
  • A ⊆ B: every element of A is in B. A ⊊ B (proper subset) also needs A ≠ B. For every set A, ∅ ⊆ A and A ⊆ A.
  • X = Y if and only if X ⊆ Y and Y ⊆ X.
  • Union, intersection, difference X − Y, complement X̄ = U − X relative to a universe U.
  • De Morgan: the complement of X ∪ Y is X̄ ∩ Ȳ; the complement of X ∩ Y is X̄ ∪ Ȳ.
  • Power set P(X), also written 2X in class: all subsets of X. |P(X)| = 2|X|. P(∅) = {∅} has one element.
  • Cardinality |S| counts top-level elements only: |{a, {a, b}, {a, {a, b, c}}}| = 3.
Practice test Q2: ∈ versus ⊆ with power sets

The elements of 2S are the subsets of S. For S = {a, b, c}:

  • {a, b} ∈ 2S ✓ (a subset of S is an element of 2S)
  • a ∈ 2S ✗ (a is an element of S, not a subset; {a} ∈ 2S is the true version)
  • {a, b, {c}} ∈ 2S ✗ ({c} is not an element of S)
  • 2S ⊆ S ✗ (the elements of 2S are sets)
  • ε ∈ 2S ✗ (ε is a string; ∅ ∈ 2S is the true version)
  • {a} ⊆ 2S ✗ but {{a}} ⊆ 2S ✓. To test X ⊆ 2S, check each element of X is a subset of S.
From lecture: the class exercises, worked
  • ({8,9,14,7,5} ∪ {6,17,14,8}) ∩ {8,3,5} = {5, 8}
  • ({1,2,5} − {5,7,9}) ∪ ({5,7,9} − {1,2,5}) = {1, 2, 7, 9}
  • 2{5,4,2} − 2{4,5} = {{2}, {2,4}, {2,5}, {2,4,5}}: the subsets that contain 2
  • ∅ ∪ {∅} = {∅}; ∅ ∩ {∅} = ∅
  • {∅} ∩ {177, {∅}, {∅, {∅}}} = ∅, so “= {∅}” is false: ∅ itself is not an element of the second set
  • {(1,2), (x,x), (x,y), (y,1)} ⊆ {1,x,2,y} × {2,y,1,x}: true
  • ∅ ⊆ ∅ true; ∅ ∈ ∅ false; ∅ ∈ {∅} true; ∅ ⊆ {∅} true
Watch the definition of N

Sipser uses N = {1, 2, 3, …}. One lecture slide writes N = {0, 1, 2, …} and a later one {1, 2, 3, …}. If a question depends on it, use the definition the question gives.

Tuples, functions and relations

  • A sequence or tuple is ordered and may repeat: (7, 21, 57) ≠ (57, 7, 21).
  • Cartesian product X × Y = {(x, y) | x ∈ X, y ∈ Y}; |X × Y| = |X|·|Y|. With X = {1, 2} and Y = {a, b, c}, X × Y has 6 pairs, each starting with 1 or 2.
  • A function f: D → R maps each input to exactly one output. One-to-one: distinct inputs give distinct outputs. Onto: every element of R is hit. Correspondence: both. There are |B||A| functions from A to B.
  • A function on X × Y can be shown as a table. The class example g: Z₄ × Z₄ → Z₄ is addition mod 4, so g(3, 2) = 1.
  • A binary relation on X and Y is a subset of X × Y; an n-ary relation is a subset of X₁ × … × Xₙ. A predicate is a function to {TRUE, FALSE}.
  • An equivalence relation is reflexive, symmetric and transitive.
From lecture: scissors-paper-stone

Beats = {(Scissors, Paper), (Paper, Stone), (Stone, Scissors)} ⊆ SPS × SPS. It is not reflexive, not symmetric and not transitive (Scissors beats Paper and Paper beats Stone, but Scissors does not beat Stone).

Practice test Q3: checking a relation fast
  1. Diagonal. Is every (x, x) present? If one is missing, R is not reflexive and not an equivalence relation.
  2. Mirror. For every off-diagonal pair (x, y), is (y, x) present?
  3. Chains. For every (x, y) and (y, z), is (x, z) present? Only off-diagonal pairs can fail, so check those.

R = {(1,1), (2,2), (3,3), (1,2), (2,1)} passes all three, so it is an equivalence relation (classes {1, 2} and {3}). Add (2,3) and (3,2) and it stops being transitive, because (1,2), (2,3) would need (1,3). The prof’s options are “Reflexive but not symmetric”, “Reflexive and symmetric but not transitive”, “Symmetric but not reflexive”, “An equivalence relation”, and “None of the above”. A relation that is only transitive is “None of the above”.

Graphs

  • Degree of a node = number of edges at it. The sum of all degrees = 2|E|.
  • Path, simple path (no repeated nodes), connected, cycle, simple cycle (at least 3 nodes).
  • A tree is connected with no simple cycles; n nodes ⇒ n − 1 edges.
  • Directed graphs have indegree and outdegree; strongly connected means a directed path joins every ordered pair. A DFA’s state diagram is a labelled directed graph.

Strings and languages

  • Alphabet Σ: a nonempty finite set of symbols. String: a finite sequence of symbols. Language: a set of strings.
  • |w| is the length; the empty string ε has |ε| = 0.
  • Concatenation xy; wk = w repeated k times, w0 = ε; reversal wR (abdR = dba).
  • Prefix x of w = xz, suffix z, substring: consecutive symbols. aaabc has 6 prefixes (ε through aaabc).
  • Shortlex order: by length, then dictionary order: ε, 0, 1, 00, 01, 10, 11, 000, …
  • A language is prefix-free if no member is a proper prefix of another member.
From lecture: Σᵏ, Σ* and Σ⁺
  • Σk = all strings of length k over Σ; there are |Σ|k. For Σ = {a, b}, Σ² = {aa, ab, ba, bb} and Σ⁰ = {ε}.
  • Σ1 looks like Σ but differs in kind: Σ1 is a set of strings of length 1, Σ is a set of symbols.
  • Kleene closure Σ* = Σ⁰ ∪ Σ¹ ∪ Σ² ∪ …, all strings including ε. Σ⁺ = Σ¹ ∪ Σ² ∪ … = Σ* without ε.
  • ∅ is the language with no strings; {ε} is a language with one string.
  • A problem, in automata theory, means deciding whether a given string is in a given language.
  • Example: L = {0i1j | j ≥ i ≥ 0} is some 0s followed by at least as many 1s, so 0111 ∈ L but 001 ∉ L.

Boolean logic

  • P → Q is false only when P is true and Q is false; it equals ¬P ∨ Q.
  • Contrapositive ¬Q → ¬P is equivalent to P → Q. The converse Q → P is not.
  • De Morgan: ¬(P ∧ Q) = ¬P ∨ ¬Q; ¬(P ∨ Q) = ¬P ∧ ¬Q.

Recursive definitions

From lecture

A recursive definition of a set X has three parts:

  1. Basis: the starting elements (1 ∈ N).
  2. Recursive step: how to build new elements (if n ∈ N then s(n) ∈ N, s = successor).
  3. Closure: nothing is in X unless it comes from the basis by finitely many recursive steps.

This generates 1, s(1), s(s(1)), … = 1, 2, 3, …. The Prolog maternal_ancestor example has the same shape: a base rule plus a recursive rule.

Proofs and induction

  • Definition, theorem, lemma, corollary. “P if and only if Q” needs both P ⇒ Q and Q ⇒ P.
  • Construction: build the object (a 3-regular graph on every even n > 2 nodes).
  • Contradiction: assume the statement is false and derive the impossible (√2 is irrational). Pumping-lemma proofs work this way.
  • Induction: basis P(b); induction hypothesis P(k); inductive step P(k) ⇒ P(k + 1). Without a basis the proof is invalid.
From lecture: induction examples to know
  • 1 + 2 + … + n = n(n + 1)/2, and 1² + 2² + … + n² = n(n + 1)(2n + 1)/6.
  • 3 | (4ⁿ − 1) for n ≥ 0. Basis: 4⁰ − 1 = 0 = 3·0. Step: 4ᵏ⁺¹ − 1 = 4(4ᵏ − 1) + 3 = 4(3z) + 3 = 3(4z + 1).
  • Every amount ≥ 8¢ from 3¢ and 5¢. Basis: 8 = 3 + 5 (also 9 = 3+3+3, 10 = 5+5). Step, by cases: if there is a 5¢ coin, replace it with two 3¢ (+1¢); otherwise there are at least three 3¢ coins, replace them with two 5¢ (+1¢).
  • Class exercise: every postage ≥ 12¢ from 3¢ and 7¢ (11¢ is impossible). Also: 43 divides 6ⁿ⁺² + 7²ⁿ⁺¹ for n ≥ 0.

Topic 2: Finite automata

A DFA reads its input once, left to right, with fixed finite memory (its current state), and accepts if it ends in an accept state. DFAs recognize exactly the regular languages.

Formal definition and computation

A DFA is a 5-tuple M = (Q, Σ, δ, q₀, F):

PartMeaningRemember
QStatesFinite
ΣInput alphabetFinite
δδ: Q × Σ → QExactly one move per (state, symbol); no ε-moves
q₀Start stateExactly one
FAccept statesF ⊆ Q; F = ∅ gives L(M) = ∅

M accepts w = w₁…wₙ if there are states r₀, …, rₙ with r₀ = q₀, rᵢ₊₁ = δ(rᵢ, wᵢ₊₁), and rₙ ∈ F. L(M) is the language M recognizes; M accepts ε exactly when q₀ ∈ F. A missing arrow is not allowed in a DFA: add a dead state that loops on every symbol.

Designing DFAs

Ask what you must remember about the prefix read so far; each distinct thing to remember is one state.

Language over {0,1}RememberStates
Ends in 1Last symbol2
Even number of 1sParity of 1s2
Ends in 00 lectureTrailing 0s: none, one, two or more3
Length divisible by kLength mod kk
Binary value divisible by 3Value mod 3; reading b maps v to 2v + b3
Begins with 1 and value divisible by 3 lectureStart, value mod 3, dead (began with 0)5
Contains 001Progress: none, 0, 00, found4
Exactly k 1sCount 0…k, plus “too many”k + 2

Worked example, strings containing 001:

Stateon 0on 1
→ q (start)q₀q
q₀q₀₀q
q₀₀q₀₀q₀₀₁
q₀₀₁ (accept)q₀₀₁q₀₀₁

Regular operations and the product construction

  • Union A ∪ B; concatenation A ∘ B = {xy | x ∈ A, y ∈ B}; star A* = {x₁…xₖ | k ≥ 0, xᵢ ∈ A}. A* always contains ε, so ∅* = {ε}.
  • |A ∘ B| ≤ |A|·|B|: different pairs can give the same string. {ab, a} ∘ {b, bb} = {ab, abb, abbb}.

Theorem 1.25 (closure under union): run both DFAs in parallel. States Q₁ × Q₂; δ((r₁, r₂), a) = (δ₁(r₁, a), δ₂(r₂, a)); start (q₁, q₂). Change only the accept states for related operations:

OperationAccept when
A₁ ∪ A₂r₁ ∈ F₁ or r₂ ∈ F₂
A₁ ∩ A₂r₁ ∈ F₁ and r₂ ∈ F₂
A₁ − A₂r₁ ∈ F₁ and r₂ ∉ F₂
Complement of A₁Swap F₁ and Q₁ − F₁ (DFAs only)

DFA minimization From lecture

Not in the assigned Sipser pages, but covered in two lectures. Every regular language has a unique minimum-state DFA up to renaming states (isomorphism), so two DFAs are equivalent exactly when their minimized versions match.

  1. Remove states that cannot be reached from the start state.
  2. Merge equivalent states.

States p and q are equivalent if for every string w, reading w from p ends in an accept state exactly when reading w from q does. Otherwise they are distinguishable.

Method 1: partition refinement (the practice test’s method)

The practice test asks for the “initial partition”, so expect questions phrased this way.

  1. Initial partition: two groups, the non-accepting states Q − F and the accepting states F.
  2. Signatures: for each state, write which group each symbol sends it to.
  3. Split any group whose members have different signatures.
  4. Repeat until a round changes nothing. Each group is one state of the minimal DFA.

Practice test Q7–8. Start q₀, F = {q₂, q₃}:

Stateon 0on 1Signature (groups: N = {q₀,q₁}, F = {q₂,q₃})
q₀q₁q₂(N, F)
q₁q₀q₃(N, F)
q₂ (accept)q₃q₀(F, N)
q₃ (accept)q₂q₁(F, N)

Initial partition {{q₀, q₁}, {q₂, q₃}} (Q7, answer b). Signatures match within each group, so nothing splits: q₀ ≡ q₁ and q₂ ≡ q₃ (Q8, answer e, “Both (a) and (b)”). The minimal DFA has 2 states and accepts strings with an odd number of 1s.

Watch for a DFA that needs more than one round. With the counter q₀ → q₁ → q₂ → q₃ → q₀ on 1 (loops on 0) and F = {q₀}: round 0 gives {q₀}, {q₁, q₂, q₃}; round 1 splits off q₃ (it reaches q₀ on 1); round 2 splits q₁ from q₂. All 4 states survive. Same table with F = {q₀, q₂} gives 2 states.

Method 2: table filling

The lecture slides also show the table-filling (Myhill–Nerode) method. It finds the same answer by marking every distinguishable pair:

  • Basis: mark {p, q} when exactly one of them is accepting.
  • Inductive step: mark {p, q} when, for some symbol a, the pair {δ(p, a), δ(q, a)} is already marked.
  • Repeat until nothing changes. Unmarked pairs are equivalent; merge them.

Worked example (class). Accept states A and C:

Stateon 0on 1
→ A (accept)BC
BAD
C (accept)DA
DCB

Basis marks every accepting/non-accepting pair: {A,B}, {A,D}, {B,C}, {C,D}. Check {A,C}: on 0 they go to {B,D}, on 1 to {C,A}; neither pair is marked. Check {B,D}: on 0 → {A,C}, on 1 → {D,B}; unmarked. Nothing new gets marked, so A ≡ C and B ≡ D. The minimal DFA has 2 states and accepts strings with an even number of 0s.

Larger class example. States a–h, start a, accept c:

Stateabc*defgh
on 0bgachcgg
on 1fccgfgec

Result: [a, e], [b, h], [d, f], [c], [g], five states. d is unreachable from a, so step 1 removes it anyway. a and b are distinguished by the string 1 (a → f rejects, b → c accepts).

Topic 3: NFAs and regular expressions

NFAs

An NFA may have zero, one or many moves on a symbol, plus ε-moves, and accepts if any branch ends in an accept state. Think of parallel threads, a tree of branches, or a machine that always guesses right.

FeatureDFANFA
δQ × Σ → QQ × Σ_ε → P(Q)
Moves per (state, symbol)Exactly oneZero, one or many
ε-arrowsNoYes
No move availableImpossibleThat branch dies
Complement by swapping FWorksDoes not work

Acceptance: w = y₁…yₘ with each yᵢ ∈ Σ_ε, and states r₀ = q₀, rᵢ₊₁ ∈ δ(rᵢ, yᵢ₊₁), rₘ ∈ F. Every DFA is already an NFA. NFAs can be much smaller: “third symbol from the end is 1” needs 4 NFA states but 8 DFA states (generally k + 1 versus 2k).

NFA → DFA: subset construction

  • ε-closure E(R): states reachable from R by zero or more ε-arrows.
  • DFA states: subsets of Q (at most 2k). Start: E({q₀}).
  • δ′(R, a) = E(union of δ(r, a) over r ∈ R).
  • Accept: every subset containing at least one NFA accept state. ∅ is the dead state.

Example: NFA for strings ending in 01 (p loops on 0, 1; p → q on 0; q → r on 1; accept r) gives reachable subsets {p}, {p, q}, {p, r}, with {p, r} accepting. Corollary 1.40: a language is regular if and only if some NFA recognizes it.

Practice test Q4–6, worked

NFA: q₀ on 0 → {q₀, q₁}, on 1 → {q₀}; q₁ on 0 → ∅, on 1 → {q₂}; q₂ on 0 → {q₂}, on 1 → ∅. Start q₀, accept q₂. Build only the reachable subsets, starting from {q₀}:

DFA stateon 0on 1Accepts?
→ {q₀}{q₀, q₁}{q₀}No
{q₀, q₁}{q₀, q₁}{q₀, q₂} (Q4)No
{q₀, q₂}{q₀, q₁, q₂}{q₀}Yes (Q5)
{q₀, q₁, q₂}{q₀, q₁, q₂}{q₀, q₂}Yes

Q6, minimizing: initial partition {{q₀}, {q₀,q₁}} and {{q₀,q₂}, {q₀,q₁,q₂}}. On 1, {q₀} stays non-accepting but {q₀,q₁} reaches an accepting state, so they split. On 1, {q₀,q₂} goes to a non-accepting state but {q₀,q₁,q₂} does not, so they split too. Answer: 4 states. Don’t assume minimization always shrinks the DFA. For “contains 11”, the same steps give 4 subset states but only 3 after minimizing.

ClosureNFA construction
A₁ ∪ A₂New start state with ε-arrows to both start states
A₁ ∘ A₂ε-arrows from N₁’s accept states to N₂’s start; N₁’s accept states stop accepting
A₁*New start state that is accepting, ε to the old start; ε from accept states back to the old start

Regular expressions

R is a regular expression if it is a (for a ∈ Σ), ε, ∅, R₁ ∪ R₂, R₁R₂, or R₁*. R⁺ = RR*. Precedence: star, then concatenation, then union, so ab* ∪ c = (a(b*)) ∪ c.

IdentityNote
R ∪ ∅ = R, Rε = RIdentities for ∪ and concatenation
R∅ = ∅Nothing to concatenate with
∅* = {ε}, ε* = {ε}Star always includes ε
R ∪ ε ≠ R in generalEqual only if ε ∈ L(R)
(R ∪ S)* ≠ R* ∪ S*01 ∈ (0 ∪ 1)* but not in 0* ∪ 1*
Regex (Σ = {0,1})Language
0*10*Exactly one 1
Σ*1Σ*At least one 1
Σ*001Σ*Contains 001
(ΣΣ)*Even length
(ΣΣΣ)*Length a multiple of 3
1*(01⁺)*Every 0 is followed by at least one 1
0Σ*0 ∪ 1Σ*1 ∪ 0 ∪ 1Starts and ends with the same symbol
(0 ∪ ε)(1 ∪ ε){ε, 0, 1, 01}
(0 ∪ 10)*(ε ∪ 1)No two consecutive 1s

Regex → NFA (Lemma 1.55, p. 67): a is two states with one a-arrow; ε is one accepting state; ∅ is one non-accepting state; then use the union, concatenation and star constructions. For (ab ∪ a)*, build a and b, concatenate, union with a, then star last.

DFA → regex via GNFAs

A GNFA labels arrows with regular expressions and is put in special form: the start state has only outgoing arrows; one accept state, different from the start, has only incoming arrows; every other pair has one arrow each way (∅ for “none”).

  1. From a k-state DFA, add a new start (ε to the old start) and a new accept state (ε from old accept states): k + 2 states.
  2. Rip out states one at a time; the last label is the answer.
new label(qᵢ → qⱼ) = (R₁)(R₂)*(R₃) ∪ (R₄)

R₁: qᵢ → q_rip, R₂: q_rip’s self-loop, R₃: q_rip → qⱼ, R₄: the direct arrow qᵢ → qⱼ. Example: state 1 (start) loops on a, goes to 2 on b; state 2 (accept) loops on a, b. Rip 2: b(a ∪ b)*. Rip 1: a*b(a ∪ b)*. A different rip order gives an equivalent regex that may look different.

Lexical analysis From lecture

The lexical analyzer is the first phase of a compiler: it reads characters and outputs tokens for the parser. Tools such as grep, lex, sed and awk turn a regex into a DFA to scan text.

TermMeaningExample
LexemeThe actual character sequence matched in the sourcepi, 17, =
TokenThe category of a lexemeidentifier, int_literal, equal_sign
PatternThe rule describing which lexemes belong to a tokenletter(letter | digit)*
  • Lex notation writes union as |. Precedence: * highest, then concatenation, then |; all left associative. (a)|((b)*(c)) = a|b*c.
  • Shorthand: r⁺ = rr*, r* = r⁺ | ε, r? = r | ε, [abc] = a | b | c, [a–z] = a | … | z.
  • Identifier: [A–Za–z][A–Za–z0–9]*. So x1 is an identifier and 9abc is not.
  • A regular definition d₁ → r₁, …, dₙ → rₙ lets each rᵢ use basic symbols and earlier names d₁ … dᵢ₋₁ only.
  • Thompson’s construction turns a regex into an NFA (the same idea as Lemma 1.55).

Topic 4: Pumping lemma and CFGs

The pumping lemma

If A is regular, there is a number p (the pumping length) such that every s ∈ A with |s| ≥ p can be split s = xyz with:

  1. xyiz ∈ A for every i ≥ 0;
  2. |y| > 0;
  3. |xy| ≤ p.
Errata on one class slide

The first pumping-lemma slide in the lecture states “|xy| > p” and “for any i > 0”. Those are typos. The correct conditions are |xy| ≤ p and i ≥ 0, as in Sipser and the later slide. Pumping down (i = 0) is often the key move.

Why it is true. A DFA with p states reading at least p symbols visits at least p + 1 states, so by the pigeonhole principle some state repeats within the first p symbols. The loop between visits is y.

It is a necessary condition only: use it to prove a language is not regular, never to prove one is.

StepWho choosesWhat happens
1YouAssume A is regular
2AdversaryPicks p (you may not pick a number)
3YouPick s ∈ A with |s| ≥ p, written in terms of p
4AdversaryPicks any split with |y| > 0 and |xy| ≤ p
5YouShow some i gives xyiz ∉ A, for every allowed split
LanguageChoose sWhy pumping fails
{0ⁿ1ⁿ}0ᵖ1ᵖy = 0ᵏ; xy²z has extra 0s
Equal 0s and 1s0ᵖ1ᵖSame; or intersect with 0*1*
{ww}0ᵖ10ᵖ1Pumping breaks the halves (0ᵖ0ᵖ is a bad choice: it pumps)
{1^(n²)}1^(p²)New length lies strictly between p² and (p + 1)²
{0ⁱ1ʲ | i > j}0ᵖ⁺¹1ᵖPump down (i = 0)
Palindromes0ᵖ10ᵖExtra 0s on the left only
{0ⁿ | n prime} lecture0ᵐ, m prime > pPumping k = |u| + |w| copies gives length (|u| + |w|)(1 + |v|), not prime
{aⁱbⁱcⁱ} homeworkaᵖbᵖcᵖy is all a’s; pumping up breaks equality
From lecture
  • The lecture’s first proof that {0ⁱ1ⁱ} is not regular uses three cases (y all 0s, all 1s, or mixed). With |xy| ≤ p only the first case can happen, which is why the later slide is shorter.
  • The class sometimes writes the split as uvw instead of xyz: same thing.
  • Closure trick: B = {equal 0s and 1s}. If B were regular, B ∩ 0*1* = {0ⁿ1ⁿ} would be regular. Contradiction.
  • Intuition can be wrong: C = {w : equally many 01 and 10 substrings} looks like counting but is regular (they are equal exactly when w is empty or starts and ends with the same symbol). “I couldn’t find a DFA” is not a proof.

Also regular: any finite language (such as {0ⁿ1ⁿ | n ≤ 100}), 0*1*, and counting mod k. Common mistakes: choosing p or the split yourself, choosing s ∉ A or shorter than p, and forgetting i = 0.

Context-free grammars

A CFG is a 4-tuple. Sipser writes (V, Σ, R, S); the lectures write (V, T, P, S): variables, terminals, productions, start variable. Every rule is A → α with A a single variable and α ∈ (V ∪ T)*. A → α₁ | α₂ | … is shorthand for several rules.

  • α ⇒ β: one rule applied once. α ⇒* β: zero or more steps.
  • L(G) = {w ∈ T* | S ⇒* w}: strings of terminals only, derived from S. Such a language is context-free.
  • CFGs began as a way to describe natural language: ⟨sentence⟩ → ⟨noun phrase⟩ ⟨verb phrase⟩.
aⁿbⁿ, n ≥ 1:            S → aSb | ab
0ᵏ1ᵏ, k ≥ 0 (G₁):       S → 0S1 | R      R → ε
0ᵃ1ᵃ0ᵇ1ᵇ (G₂):          S → RR           R → 0R1 | ε
Balanced parentheses:   S → SS | (S) | ε
Palindromes over {a,b}: S → aSa | bSb | a | b | ε
Equal a’s and b’s (≥1): S → aB | bA   A → a | aS | bAA   B → b | bS | aBB
aⁱbʲ with i ≥ j:        S → aS | T       T → aTb | ε
Arithmetic (class G₃):  E → E + E | E − E | (E) | V     V → x | y | z

In G₃ the variables are {E, V} and the terminals are {+, −, (, ), x, y, z}.

Derivations and parse trees From lecture

Deriving x + (y − z) in G₃:

Leftmost (expand leftmost variable)Rightmost (expand rightmost variable)
E ⇒ E + E
⇒ V + E
⇒ x + E
⇒ x + (E)
⇒ x + (E − E)
⇒ x + (V − E)
⇒ x + (y − E)
⇒ x + (y − V)
⇒ x + (y − z)
E ⇒ E + E
⇒ E + (E)
⇒ E + (E − E)
⇒ E + (E − V)
⇒ E + (E − z)
⇒ E + (V − z)
⇒ E + (y − z)
⇒ V + (y − z)
⇒ x + (y − z)

Both derivations give the same parse tree. Its rules: the root is labelled S; every internal vertex is a variable; if a vertex A has children X₁…Xₖ then A → X₁…Xₖ is a production; a vertex labelled ε is a leaf with no siblings. Reading the leaves left to right gives the yield.

Designing grammars, ambiguity, CNF

  • Union: S → S₁ | S₂. From a DFA: Rᵢ → aRⱼ when δ(qᵢ, a) = qⱼ, Rᵢ → ε for accept states, start R₀. So every regular language is context-free. Linked parts: R → uRv. Nesting: let a variable appear inside its own rules.
  • Practice test Q9 showed these two trees for a + a × a and asked what they prove: the grammar is ambiguous, because one string has two distinct parse trees.
  • A grammar is ambiguous if some string has two parse trees, equivalently two leftmost derivations. The class grammar E → E + E | E * E | (E) | id is ambiguous: id + id * id groups two ways. Two different derivations alone prove nothing.
  • A language is inherently ambiguous if every grammar for it is ambiguous, e.g. {aⁱbʲcᵏ | i = j or j = k}.
  • Chomsky normal form (A → BC or A → a, plus S → ε) starts around page 107 and is not in the lecture slides. Low priority.

Cheat sheet and traps

ConceptKey fact
Power set size2ⁿ for an n-element set
Strings of length n over Σ|Σ|ⁿ
Functions A → B|B|^|A|
ε, ∅, {ε}String of length 0; empty language; language with one string
DFAδ: Q × Σ → Q, one move per symbol
NFAδ: Q × Σ_ε → P(Q), accept if any branch accepts
Subset constructionk NFA states → at most 2ᵏ DFA states; start E({q₀})
Product constructionm × n states for ∪, ∩, −
MinimizationRemove unreachable; initial partition {Q − F, F}; refine until stable. Minimal DFA unique up to renaming
Regex precedenceStar, concatenation, union
Regular =DFA = NFA = regular expression
Closed under∪, ∘, *, ∩, complement, difference
Pumping lemma|y| > 0, |xy| ≤ p, xyⁱz ∈ A for all i ≥ 0; proves non-regularity only
CFG(V, T, P, S); left side is one variable
AmbiguousTwo parse trees = two leftmost derivations
  • Swapping accept states complements a DFA, never an NFA.
  • Forgetting the ε-closure of the start state in the subset construction.
  • A* always contains ε. Finite languages are always regular.
  • In pumping proofs, the adversary picks p and the split; you pick s and i.
  • Converse ≠ contrapositive. Induction needs a basis.