Regular Expressions & Pumping Lemma
988 words
5 min read
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
# Regular Expressions & Pumping Lemma ## 🎯 Learning Objectives - Write regular expressions for regular languages - Convert between regex and DFA/NFA - Apply the pumping lemma to prove non-regularity - Use closure properties to construct regular languages * * * ## 1. Regular Expressions ### 1.1 Intuition Regular exp...

Regular Expressions & Pumping Lemma
🎯 Learning Objectives
- Write regular expressions for regular languages
- Convert between regex and DFA/NFA
- Apply the pumping lemma to prove non-regularity
- Use closure properties to construct regular languages
1. Regular Expressions
1.1 Intuition
Regular expressions describe patterns in text. They're equivalent to finite automata — every regex corresponds to some DFA/NFA, and vice versa.
1.2 Operations
| Operation | Notation | Example |
|---|---|---|
| Union | $R_1 | R_2$ |
| Concatenation | R1⋅R2 | ab matches "ab" |
| Kleene star | R∗ | a* matches "", "a", "aa", ... |
| Grouping | (R) | (ab)* matches "", "ab", "abab", ... |
1.3 Examples
| Language | Regex |
|---|---|
| Strings starting with 'a' | $a(a |
| Strings ending with '01' | $(0 |
| Strings with even number of 0s | (1∗01∗01∗)∗ |
| Strings of length exactly 3 | $(a |
| Strings with no consecutive 0s | $(1 |
1.4 Algebraic Laws
| Law | Expression |
|---|---|
| Associativity | $(R_1 |
| Commutativity | $R_1 |
| Distributivity | $R_1(R_2 |
| Identity | ∅∗=ε , εR=R , ∅R=∅ |
| Idempotent | $R |
| Star laws | (R∗)∗=R∗ , ∅∗=ε , ε∗=ε |
2. Equivalence of Regex and FA
2.1 Regex → NFA (Thompson's Construction)
Every regex can be systematically converted to an NFA:
(Diagram)
2.2 DFA → Regex (State Elimination)
- Add a new start state with ε to old start
- Add a new accept state with ε from old accepts
- Eliminate states one by one, updating edge labels
- The remaining edge from start to accept is the regex
3. Pumping Lemma for Regular Languages
3.1 Intuition
The pumping lemma says: every sufficiently long string in a regular language can be "pumped" — a middle section can be repeated indefinitely while staying in the language. If a language violates this, it's not regular.
3.2 Formal Statement
For every regular language L, there exists a constant p (pumping length) such that for any string w∈L with ∣w∣≥p, we can write w=xyz where:
- ∣xy∣≤p (the pumpable part is near the start)
- ∣y∣≥1 (y is non-empty)
- xyiz∈L for all i≥0 (pumping works)
3.3 Proof Template
To prove L is not regular:
pseudo1. Assume L is regular (for contradiction) 2. Let p be the pumping length 3. Choose w ∈ L with |w| ≥ p (be strategic!) 4. For ALL ways to split w = xyz with |xy| ≤ p, |y| ≥ 1: Show there exists i such that xy^iz ∉ L 5. Contradiction → L is not regular
3.4 Worked Example 1: L={anbn∣n≥0}
- Assume L is regular
- Let p be pumping length
- Choose w=apbp
- Since ∣xy∣≤p, y consists only of a's: y=ak for k≥1
- Pump i=2: xy2z=ap+kbp
- More a's than b's → not in L
- Contradiction → L is not regular ✓
3.5 Worked Example 2: L={ww∣w∈{0,1}∗}
- Choose w=0p10p1
- ∣xy∣≤p, so y is within the first 0p block
- y=0k, pump i=2: xy2z=0p+k10p1
- The string before the middle is 0p+k1 and after is 0p1 — not equal
- Result: not regular ✓
3.6 When Pumping Lemma Fails
Some non-regular languages CAN be pumped! Example:
L={anbm∣n=m} is not regular, but the pumping lemma can't disprove it directly (you need more powerful tools like the Myhill-Nerode theorem).
4. Common Pitfalls
Pitfall 1: Pumping the wrong string
Mistake: Choosing a string that doesn't force a contradiction.
Fix: Choose a string where every possible y (within first p characters) breaks the language.
Pitfall 2: Thinking pumping lemma can prove regularity
Mistake: "I pumped the string and it stayed in L, so L is regular."
Correction: Pumping lemma gives a necessary condition for regularity, not a sufficient one. Some non-regular languages also satisfy pumping.
Pitfall 3: Forgetting to consider all splits
Mistake: Only checking one possible split of w=xyz.
Fix: Your proof must work for all valid splits (|xy| ≤ p, |y| ≥ 1).
5. 📝 Practice Questions
Q1: Write a regex for binary strings that do NOT contain "00".Answer: (1∣01)∗(0∣ε) — strings where every 0 is followed by 1 (or at end). Q2: Prove L={0n2∣n≥0} is not regular.Answer: Assume regular with pumping length p. Choose w=0p2. Then y=0k for 1≤k≤p. Pump i=2: xy2z=0p2+k. But (p+1)2=p2+2p+1>p2+k≥p2+1, so p2<p2+k<(p+1)2. Thus p2+k is not a perfect square → not in L. Contradiction. Q3: Convert regex (0∣1)∗00 to an NFA.Answer: NFA with states q0 (start), q1 (after first 0), q2 (accept, after "00"). Transitions: q0—0,1→q0; q0—0→q1; q1—0→q2; q2—0→q2; q2—1→q0. Q4: Is the language L = {w ∈ {0,1} | w has equal number of 0s and 1s} regular?*Answer: No. Proof using pumping lemma: choose w=0p1p. y=0k. Pump i=2: 0p+k1p has more 0s than 1s → not in L. Not regular. Q5: Show that regular languages are closed under intersection.Answer: Given DFAs for L₁ and L₂, construct a product DFA where states are pairs (q₁, q₂), transitions are δ((q₁,q₂), a) = (δ₁(q₁,a), δ₂(q₂,a)), and accept states are (q₁,q₂) where q₁ ∈ F₁ AND q₂ ∈ F₂. This DFA accepts exactly L₁ ∩ L₂.
6. 🔗 Cross-References
- Week 1 - DFA/NFA: Equivalence with regex
- Week 3 - DFA Minimization: Minimization uses equivalence classes
- BSCS4032 (Compiler Design): Lexical analysis uses regex → NFA → DFA Join Discord PreviousNFA to DFANextRegex to DFA