Module 7 · 14 min read · Free chapter
Regular languages and the pumping lemma
Regular languages are closed under union, intersection, complement, concatenation, star and reversal, but not under subsets. The pumping lemma says every long string in a regular language has a short loop that can be repeated. It can only prove that a language such as aⁿbⁿ is not regular.
Start from zero: counting with five fingers
A dabba delivery man picks up tiffins from homes in the morning and must hand over exactly the same number at the office. Suppose he is allowed to remember things only on the five fingers of one hand. Up to five tiffins he can keep count, but what happens at the sixth?
He has to reuse a finger, so two different counts now look the same to him. If he picked up 6 tiffins and his finger says "1", he cannot tell 6 from 1. A dishonest office boy could then take one tiffin away and he would never notice.
A finite automaton is exactly this man. It has a fixed number of states, so it cannot keep an unbounded count, and languages that need such a count, such as "the same number of 's and then 's", are not regular. This chapter gives you two kinds of tools: closure properties to build regular languages, and the pumping lemma to prove that a language is not regular.
Why it matters. Deciding "regular or not?" is one of the most common GATE questions in this subject. If you know the tools, you can settle it in under a minute instead of guessing.
Closure properties: building new regular languages
A language is regular if some DFA, NFA or regular expression describes it. Every finite language is regular, since a regular expression can simply list its strings joined by . A family of languages is closed under an operation if applying the operation to members of the family always gives a member again.
Regular languages are closed under almost every operation you meet. Union, concatenation and star come free from regular expressions. Complement is just as easy: take a complete DFA and swap accepting and non-accepting states, so every string that was accepted is now rejected and the other way round.
For intersection we run two DFAs at the same time. If has states and has states, the product DFA has the pairs as states, which is states. It moves both parts together, , and it accepts when both parts accept.
| Operation | Closed for regular languages? |
|---|---|
| Union, intersection, difference | Yes |
| Complement | Yes, by swapping final states |
| Concatenation, Kleene star, reversal | Yes |
| Homomorphism and inverse homomorphism | Yes |
| Subset of a regular language | No |
| Union of infinitely many regular languages | No |
For example, the strings with an even number of 's need a 2-state DFA, and so do the strings with an even number of 's. Their intersection, even 's and even 's, therefore has a product DFA of states, and here all 4 are needed.
Why it matters. Closure lets you prove a language regular without drawing its automaton, by building it out of simple regular pieces. It also gives a quick estimate of the number of states, which GATE often asks.
Where closure stops, and how to use it against a language
Closure does not pass down to subsets. The language is regular, but its subset is not. So "this language sits inside a regular one" proves nothing at all.
Closure also fails for infinite unions. Every language, even a very hard one, is the union of its single strings, and each single-string language is regular. If infinite unions kept regularity, every language would be regular, which is false.
The most useful trick runs closure backwards. Suppose you suspect that , the strings with equally many 's and 's, is not regular. If were regular, then would be regular too, because is regular and intersection is closed.
But that intersection is exactly , which we know is not regular. So cannot be regular. The same trick works for , since removing it from would leave .
Why it matters. A closure argument is often a two-line proof where the pumping lemma would take a page. In multiple-choice questions it is the fastest way to reject a wrong option.
Decision properties: questions a machine can answer
A decision property is a yes-or-no question about a language, given as a DFA or a regular expression. For regular languages, every natural question has an algorithm that always finishes. This is a big contrast with context-free languages and Turing machines, where many such questions have no algorithm at all.
Membership asks whether , and you simply run the DFA on in steps. Emptiness asks whether , and you check whether any accepting state is reachable from the start state by a graph search. Equivalently, a DFA with states accepts something if and only if it accepts some string shorter than .
Finiteness asks whether is finite. The language is infinite exactly when some cycle lies on a path from the start state to an accepting state, because that cycle can be repeated. In terms of lengths, a DFA with states accepts infinitely many strings if and only if it accepts some with .
Equivalence asks whether . One way is to minimise both DFAs and compare them, since the minimal DFA is unique. Another way is to test whether is empty, using closure and the emptiness test.
Why it matters. Tools that check hardware designs and network rules depend on these algorithms, because they can prove two descriptions identical. In GATE, "which of these problems is decidable" questions usually have every regular-language problem marked as decidable.
The Myhill–Nerode idea: how many things must be remembered?
Go back to the delivery man and his fingers. The real question is: how many different situations must a machine keep apart? Two inputs and are the same situation for a language if no future can tell them apart, that is, for every string , exactly when .
We write this as , and it splits all strings into classes. The Myhill–Nerode theorem says that is regular if and only if there are finitely many classes. Moreover, the number of classes equals the number of states in the minimal DFA for .
For strings over ending in , there are exactly three classes: strings ending in , strings ending in , and all the rest. That matches the three-state minimal DFA. Now take and the strings and with .
The future puts in but leaves outside it, so and are in different classes. Since this holds for every pair, there are infinitely many classes, and is not regular. This is the cleanest proof of all, and it also explains why the language fails: the machine would need a new state for every count.
Why it matters. Myhill–Nerode gives a lower bound on states: if you find strings that are pairwise distinguishable, every DFA needs at least states. GATE state-counting questions can be checked this way.
The pumping lemma and two full proofs
Suppose a DFA with states reads a string of length at least . While reading the first symbols it visits states, so by the pigeonhole principle some state repeats. The symbols read between the two visits form a loop, and a loop can be taken zero times, once, twice or any number of times.
That observation is the pumping lemma. If is regular, there is a number such that every with can be split as , where:
The lemma is a necessary condition, not a sufficient one. Some non-regular languages also satisfy it, so it can never prove that a language is regular. We use it only in contrapositive form: if no split works for some long string, the language is not regular.
Treat a proof as a game against an opponent. The opponent picks , you pick a string with , the opponent picks any split that obeys the rules, and you pick an that throws the string out of . You must win against every split, not just one.
Proof 1: is not regular. Assume it is regular with pumping length . Choose , which is in and has length . Because , both and lie inside the block of 's:
Since , the pumped string has more 's than 's, so . This contradicts the lemma, so is not regular.
Proof 2: is not regular. Assume it is regular with pumping length and choose . Any split has with , so pumping once more gives length :
The new length lies strictly between two neighbouring perfect squares, so it is not a perfect square, and . Hence the language is not regular.
Why it matters. Descriptive papers ask for exactly these proofs, and marks go to the correct choice of and the handling of every split. In GATE, the same reasoning helps you spot quickly which options need unbounded counting.
Regular or not: the common traps
The quick test is this: does the machine need to compare two unbounded quantities? If yes, the language is usually not regular. If it only needs a count modulo a fixed number, a fixed-length window, or independent pieces, it is regular.
| Language | Regular? | Reason |
|---|---|---|
| Yes | It is just , with no matching. | |
| No | The two counts must match without bound. | |
| Yes | It is a finite language. | |
| Yes | Only the parity of the length matters. | |
| No | Gaps between squares keep growing. | |
| No | Pumping always reaches a composite length. | |
| No | The second half must copy the first. | |
| No | Even palindromes need unbounded memory. | |
| Yes | The difference is tracked modulo 3. | |
| Equal numbers of and substrings | Yes | This holds exactly when the first and last symbols agree. |
| Yes | Taking as one symbol, it is strings of length at least 3 with equal ends. | |
| No | Its complement inside is . |
The last two regular rows are favourite traps. The language looks like a palindrome, but because may be a single symbol and may be anything, it only checks that the first and last symbols agree. Likewise, "as many as " sounds like counting, yet each switch from to must be followed by a switch back unless the string ends in a different symbol.
Why it matters. GATE options are designed to look non-regular while being regular, and the other way round. Asking "what exactly must be remembered?" before answering protects you from both.
Descriptive lens: how written papers ask this
Written papers ask about regular languages in four ways:
- State the pumping lemma precisely and explain why it cannot prove that a language is regular.
- Prove that a given language is not regular, by the pumping lemma, by Myhill–Nerode or by closure.
- Construct an automaton for a combination of languages, such as an intersection, and give the number of states.
- Explain an algorithm for a decision property, such as emptiness or finiteness.
Practice questions (our own, not previous-year questions).
- State the pumping lemma for regular languages. Use it to prove that is not regular. (10 marks)
- Show that regular languages are closed under intersection and complement. Hence prove that is not regular. (10 marks)
- State the Myhill–Nerode theorem and use it to find the number of states in the minimal DFA for strings over that end in . (15 marks)
Answer plan for question 1.
- State the lemma with all three conditions, , and for every .
- Assume for contradiction that the language is regular, and let be its pumping length.
- Choose , and note that it is in the language and has length .
- Argue that every allowed split puts with inside the first symbols.
- Pump with to get , and show that it is outside the language.
- Conclude that the assumption is false, so the language is not regular.
Quick revision
- Every finite language is regular.
- Regular languages are closed under union, intersection, complement, difference, concatenation, star, reversal and homomorphisms.
- The product DFA for intersection has states, and complement swaps final states of a complete DFA.
- Subsets and infinite unions of regular languages need not be regular.
- To prove non-regular by closure, intersect it with a regular language and reach a known non-regular one.
- Membership, emptiness, finiteness and equivalence are all decidable for regular languages.
- A DFA with states accepts an infinite language exactly when it accepts some with .
- Myhill–Nerode classes equal the states of the minimal DFA, and infinitely many classes mean non-regular.
- The pumping lemma needs and , and it can only prove non-regularity.
- , , palindromes, and with prime are not regular.