Theory of Computation
Regular Expressions
Regular expressions are the algebraic face of finite automata. Master the identities, solve state equations with Arden’s lemma, and you can move between regex, NFA and DFA in the time GATE gives you.
GATE CSE regular expressions — operators and precedence, identities, Arden’s lemma, regex → NFA → DFA, DFA → regex by state elimination, and complement/intersection via DFA, with sample questions, flashcards and drills.
Learning outcomes
- Write regular expressions for standard languages and decide whether two expressions are equivalent using identities
- Apply Arden’s lemma R = Q + RP ⇒ R = QP* to derive a regex from a DFA
- Convert a regular expression to an ε-NFA (Thompson) and then to a DFA, and a DFA back to a regex by state elimination
- Obtain complement and intersection of regular languages through the DFA and count strings of a given length in a regex
Regular expressions — operators and precedence
- Base cases: ∅ denotes the empty language, ε denotes {ε}, and each symbol a ∈ Σ denotes {a}.
- Union r + s (also written r | s): L(r) ∪ L(s). Concatenation rs: { xy | x ∈ L(r), y ∈ L(s) }. Kleene star r*: zero or more concatenations of strings of L(r), so ε is always in L(r*).
- Precedence from highest to lowest: star (*), then concatenation, then union (+). So ab* + c means (a(b*)) + c.
- r+ (positive closure) = rr* and r? = ε + r are shorthand, not extra power.
- A language is regular iff it is described by some regular expression (Kleene’s theorem), iff some DFA / NFA accepts it.
- Strings of length n in (a+b)* over {a, b}: 2^n. For a regular expression, count by splitting on union and multiplying along concatenation, but watch out for ambiguity that double counts.
- Common patterns: strings over {a,b} ending in ab → (a+b)*ab; containing aa → (a+b)*aa(a+b)*; no two consecutive 0s → (1 + 01)*(ε + 0); even number of 0s → 1*(01*01*)*; length divisible by 3 → ((a+b)(a+b)(a+b))*.
- Regular expressions are over a finite alphabet and use only three operators (union, concatenation, star). Intersection, complement and “exactly n times” are NOT basic operators — extended regexes (with ∩ or ¬) still describe only regular languages but are not part of the GATE syllabus definition.
- Equivalent notations: a|b, a+b and {a,b} all mean union. Do not confuse the union “+” with the positive closure superscript r⁺ — in typeset GATE questions the superscript is a Kleene plus.
- Language sizes: L(r) is finite iff no star is applied to an expression that can generate a nonempty string. Example: a + bc is finite (2 strings), but (a + b)* and a*b are infinite.
- Shortest string in L(r): ε if r can match empty (star of anything, or ε inside the concatenation chain), else minimise lengths — min over union, sum over concatenation, 0 for star.
- Pattern for “a occurs exactly twice”: b*ab*ab*; “at most one a”: b*(ε + a)b*; “at least one a”: (a+b)*a(a+b)*; “the third symbol from the left is a”: (a+b)(a+b)a(a+b)*.
Formulas to remember
-
L(r + s) = L(r) ∪ L(s) -
L(rs) = L(r)·L(s) -
L(r*) = ∪_{i ≥ 0} L(r)^i, includes ε -
precedence: * > concatenation > +
| Language | Regular expression |
|---|---|
| all strings over {a,b} | (a+b)* |
| ends with ab | (a+b)*ab |
| contains aa | (a+b)*aa(a+b)* |
| no two consecutive 0s over {0,1} | (1+01)*(ε+0) |
| even number of 0s over {0,1} | 1*(01*01*)* |
| starts and ends with the same symbol (nonempty) | a(a+b)*a + b(a+b)*b + a + b |
Worked example
Problem. Write a regular expression for binary strings with no two consecutive 0s.
Solution. Every 0 must be followed by a 1 except possibly the last symbol. Break the string into blocks 1 or 01, then an optional final 0: (1 + 01)*(ε + 0).
Worked examples — designing regular expressions
- Method: (1) identify the repeating structure, (2) split the string into a fixed skeleton and free filler (a+b)*, (3) test the expression on ε, a one-symbol string and a boundary string.
- Substring conditions use filler on both sides: “contains abb” → (a+b)*abb(a+b)*. “Ends in” keeps filler only on the left, “starts with” only on the right.
- Forbidden-substring conditions are hardest. “No aa” over {a, b}: every a is followed by b or the end, so (b + ab)*(ε + a). “No 11” over {0,1}: (0 + 10)*(ε + 1).
- Parity / modulo conditions repeat a cycle: even number of a’s → b*(ab*ab*)*; number of a’s divisible by 3 → b*(ab*ab*ab*)*; “even length, ends in a” → ((a+b)(a+b))*(a+b)a (pairs of symbols, then one free symbol and a final a).
- Position conditions: “second symbol from the left is b” → (a+b)b(a+b)*; “length exactly 3” → (a+b)(a+b)(a+b); “length at least 2” → (a+b)(a+b)(a+b)*.
- Strings that start and end with different symbols: a(a+b)*b + b(a+b)*a. Use a separate union branch for each case — never try to cover two cases with one star.
- A regex is not unique: dozens of different expressions describe the same language. GATE therefore asks “which of the following describes L” and expects you to test options on short strings.
| Language over {a, b} | Regular expression |
|---|---|
| contains abb | (a+b)*abb(a+b)* |
| no two consecutive a’s | (b + ab)*(ε + a) |
| exactly two a’s | b*ab*ab* |
| number of a’s divisible by 3 | b*(ab*ab*ab*)* |
| starts and ends with different symbols | a(a+b)*b + b(a+b)*a |
| third symbol from the left is a | (a+b)(a+b)a(a+b)* |
Worked example
Problem. Write a regular expression for binary strings in which every 1 is immediately followed by at least one 0 (no 11 and not ending in 1).
Solution. Each 1 must be glued to a following 0, so treat “10” as one block. Zeros may appear freely, and every block is 0 or 10: (0 + 10)*. Check: ε ∈ L (fine), 0, 10, 0100 ∈ L; 1, 11, 101 ∉ L because they end in 1 or contain 11.
Identities of regular expressions
- Quick test of an identity: it must hold for the empty language ∅, for {ε}, and for single symbols. If it fails for r = a, s = b you have a non-identity.
- Absorption in star: (r + s*)* = (r + s)* and (r*s)*r* = (r + s)* — a star inside a star absorbs everything it already generates.
- Both r*(r + s)* and (r + s)*r* equal (r + s)*, because the extra r* adds nothing new.
- Union is commutative and associative: r + s = s + r; r + r = r; r + ∅ = r. Concatenation is associative but NOT commutative: rs ≠ sr in general.
- Identity and annihilator: εr = rε = r; ∅r = r∅ = ∅. Star facts: ∅* = ε, ε* = ε.
- Distributive laws: r(s + t) = rs + rt and (s + t)r = sr + tr.
- Star identities: (r*)* = r*; r*r* = r*; rr* = r*r = r+; ε + rr* = r*; (ε + r)* = r*; (ε + r)+ = r*.
- Key GATE identities: (r + s)* = (r*s*)* = (r* + s*)* = (r*+s)* = r*(sr*)*. Also (rs)*r = r(sr)*.
- Non-identities: (rs)* ≠ r*s*, (r + s)* ≠ r* + s*, r* + s* ≠ (r+s)*. Test by trying small strings such as ab or ba.
- Prove two expressions are equal by showing both describe the same language; to refute, give one string in exactly one language.
Formulas to remember
-
(r + s)* = (r*s*)* = (r* + s*)* -
(rs)*r = r(sr)* -
ε + rr* = r* -
(r*)* = r*; ∅* = ε
| Expression | Equals | Remark |
|---|---|---|
| (a*)* | a* | star is idempotent |
| (a + ε)* | a* | ε inside star is redundant |
| a* + b* | (a + b)* ? | NOT equal (ab is missing) |
| (ab)*a | a(ba)* | shifting identity |
| a+ | aa* | does not contain ε |
Worked example
Problem. Are (a + b)* and (a*b*)* equivalent?
Solution. Yes. (a*b*)* contains a and b separately and any concatenation of them, so it produces every string over {a, b}; (a + b)* is the same language. This is the identity (r + s)* = (r*s*)*.
Arden’s lemma and DFA → regular expression
- Intuition for Arden: in R = Q + RP, the string is either one of the base strings Q, or a shorter solution R followed by one more P. Unrolling gives Q + QP + QPP + … = QP*.
- Which method is faster? For 2–3 states Arden’s equations are fastest; for graphs with several back-edges state elimination is more systematic. Both are fine for GATE.
- Arden’s lemma: if P does not contain ε, the equation R = Q + RP has the UNIQUE solution R = QP*. Equivalently R = Q + PR gives R = P*Q.
- Method: for each DFA state qi write an equation qi = (ε if qi is the start) + Σ qj·a over transitions qj —a→ qi. Eliminate states one by one using Arden’s lemma (substitute and simplify).
- The regular expression of the DFA is the union of the expressions of its final states.
- State elimination (GNFA method): add a new start and a new final state with ε-edges, then remove each original state q: for every pair (p, r) replace the path p → q → r by a new edge labelled R_pq R_qq* R_qr (union it with any existing edge p → r).
- Both methods give equivalent (not necessarily identical) expressions. Different elimination orders give different-looking but equal expressions.
- Example: R = a + Rb gives R = ab*; R = ε + R01 gives R = (01)*; R = 0 + R1 gives R = 01*.
- Arden’s lemma needs P without ε. If P contains ε then R = Q + RP has many solutions (for instance R = Σ* works whenever Q ⊆ Σ*), so uniqueness fails.
Formulas to remember
-
R = Q + RP, ε ∉ L(P) ⇒ R = QP* -
state elimination: new edge = R_pq · R_qq* · R_qr
| Equation | Solution |
|---|---|
| R = a + Rb | ab* |
| R = ε + R01 | (01)* |
| R = 0 + R1 | 01* |
| R = ε + 0R | 0* |
Worked example
Problem. DFA: states q0 (start, final) and q1; q0 —a→ q1, q1 —b→ q0, q0 —b→ dead, q1 —a→ dead. Find the regex.
Solution. Ignoring the dead state: q1 = q0·a and q0 = ε + q1·b = ε + q0·ab. By Arden’s lemma q0 = ε·(ab)* = (ab)*. The final state is q0, so the language is (ab)*.
Worked example — Arden’s lemma on a 3-state DFA
- DFA for “binary strings ending in 01”: states A (start), B (last symbol 0), C (last two symbols 01, final). Transitions: A —0→ B, A —1→ A, B —0→ B, B —1→ C, C —0→ B, C —1→ A.
- Write one equation per state from its INCOMING edges: A = ε + A1 + C1; B = A0 + B0 + C0; C = B1.
- Step 1: substitute C = B1 into B’s equation: B = A0 + B0 + B10 = A0 + B(0 + 10). Arden (R = Q + RP) gives B = A0(0 + 10)*.
- Step 2: substitute C = B1 and B into A: A = ε + A1 + B11 = ε + A1 + A0(0 + 10)*11 = ε + A(1 + 0(0 + 10)*11), hence A = (1 + 0(0 + 10)*11)*.
- Step 3: the language is the equation of the final state, C = B1 = A0(0 + 10)*1 = (1 + 0(0 + 10)*11)* 0 (0 + 10)* 1.
- This looks nothing like (0 + 1)*01, but both describe the same language — different elimination orders produce different-looking, equivalent expressions.
- Checklist: (a) the start state’s equation carries the ε term, (b) use INCOMING transitions, (c) always put the unknown on the LEFT of the loop term when applying R = Q + RP, (d) the answer is the union of equations of all final states.
| Step | Equation | Result |
|---|---|---|
| 0 | A = ε + A1 + C1, B = A0 + B0 + C0, C = B1 | system of 3 equations |
| 1 | B = A0 + B(0 + 10) | B = A0(0 + 10)* |
| 2 | A = ε + A(1 + 0(0 + 10)*11) | A = (1 + 0(0 + 10)*11)* |
| 3 | C = B1 | L = A0(0 + 10)*1 |
Worked example
Problem. Use state elimination to obtain a regex for the DFA with q0 (start) —0→ q1 —1→ q2 (final), q1 —0→ q1, and q2 —0,1→ q2 (all other moves go to a dead state).
Solution. This DFA accepts 0 0* 1 (0 + 1)*. Eliminate q1: edge q0 → q2 becomes 0 · 0* · 1 (R_pq R_qq* R_qr). Then q2 keeps its self loop (0 + 1), so the regex is 00*1(0 + 1)* = 0⁺1(0 + 1)* — all strings that start with one or more 0s followed by a 1.
Regex → NFA → DFA, complement and intersection
- Size of the Thompson ε-NFA for (a+b)*abb: each symbol gives 2 states, each operator adds up to 2 more; after removing ε-moves the NFA shrinks to 4 states (q0 loops on a, b; q0 —a→ q1 —b→ q2 —b→ q3).
- Subset construction on that 4-state NFA yields a DFA with 4 states (strings that end in abb need to remember the longest matched prefix of “abb”), so NFA size = DFA size here.
- Thompson’s construction builds an ε-NFA from a regex by induction: one new start and one new final state per operator. The ε-NFA has at most 2·|r| states for a regex with |r| symbols and operators, and each state has at most two outgoing transitions.
- Union: new start with ε-edges to both sub-automata and ε-edges from both to a new final state. Concatenation: connect the final state of the first to the start of the second with ε. Star: add ε-loop back and an ε-bypass.
- To get a DFA: remove ε-moves (ε-closure), then run subset construction, then minimize. The size may be exponential in the regex size.
- Complement of a regular language: build a complete DFA, swap final and non-final states, and read off a regex. Complement of a regex is not an operator of basic regexes — it is done through the automaton.
- Intersection: product construction on two DFAs (m·n states) or De Morgan: L1 ∩ L2 = complement(complement(L1) ∪ complement(L2)).
- Typical intersections: L(a*b*) ∩ L(b*a*) = a* + b*. Strings with an even number of a’s intersected with strings ending in b: product DFA with 2 × 2 = 4 states.
- Emptiness, finiteness, membership and equivalence of regexes are all decidable (convert to DFA).
| Operation | Construction | State bound |
|---|---|---|
| Regex → ε-NFA | Thompson | ≤ 2·|r| |
| ε-NFA → DFA | ε-closure + subset | ≤ 2^n |
| Complement | Complete DFA, swap F | same |
| Intersection / union | Product DFA | m·n |
| Reversal | Reverse arrows, swap start/final | n (NFA) |
Worked example
Problem. Find L(a*b*) ∩ L(b*a*) over {a, b}.
Solution. A string in a*b* has all a’s before all b’s; in b*a* all b’s before all a’s. A string in both cannot contain both a and b, so the intersection is a* + b* (ε included).
GATE exam tricks — regular expressions
- To decide whether two regexes are equal, find a shortest distinguishing string (try ε, a, b, ab, ba, aa). One counter-example is enough to prove inequality.
- Remember ε: a+ lacks ε but a* has it; (a + ε)* = a*; (ε + a)(ε + b) = ε + a + b + ab.
- When a question asks “the language of the regex has how many strings of length n”, list the structure of the strings first. For (a+b)*ab(a+b)* of length 3 count strings containing ab (4 of 8; the 4 without ab are b^i a^j: bbb, bba, baa, aaa).
- Regex for “even number of a’s” is b*(ab*ab*)*; for “odd number” is b*ab*(ab*ab*)*. Use the symmetric structure for other parity conditions.
- Option elimination: test every option on ε and on two short strings that must be in and out of the language. Wrong options usually fail on ε or on a boundary string.
- For “second last symbol is a” use (a+b)*a(a+b); its minimal DFA needs 4 states. For “n-th from last” the DFA needs 2^n states.
- Arden’s lemma is for right-linear equations written as R = Q + RP. If the equation is R = Q + PR, the answer is P*Q — orientation matters.
Worked example
Problem. How many strings of length 3 over {a, b} are in L((a + b)*ab(a + b)*)?
Solution. The language is “contains ab”. Strings with no ab have the form b^i a^j, giving 4 strings of length 3 (bbb, bba, baa, aaa). So 8 − 4 = 4 strings contain ab.
Exam tips
- Memorise (r+s)* = (r*s*)* = (r*+s*)* and (rs)*r = r(sr)* — they decide most “equivalent expressions” questions.
- Use Arden’s lemma only when P does not contain ε; always check orientation (R = Q + RP ⇒ QP*).
- For equality tests, try ε, single symbols and two-symbol strings before algebra.
- Convert to a DFA, complement it, then write the regex — never complement a regex directly.
- Precedence is star > concatenation > union; parenthesize before comparing options.
10 GATE-style MCQs for Regular Expressions with full explanations. Attempt each question before revealing the answer.
Which of the following regular expressions is equivalent to (a + b)*?
Show answer & explanation
Answer: B
By the identity (r + s)* = (r*s*)*, (a*b*)* generates every string over {a, b}. The others miss strings such as ba.
Using Arden’s lemma, the solution of R = a + Rb is:
Show answer & explanation
Answer: B
R = Q + RP with Q = a and P = b (ε ∉ P) gives R = QP* = ab*.
The minimum number of states in a DFA accepting the language of (a + b)*abb is:
Show answer & explanation
Answer: B
The language is “ends with abb”, a suffix of length 3, so the minimal DFA has k + 1 = 4 states.
Which of the following is NOT equal to a*?
Show answer & explanation
Answer: D
a+ = aa* does not contain ε. (a*)* = a*, ε + aa* = a*, and (a + ε)* = a*.
Which regular expression describes binary strings with no two consecutive 0s?
Show answer & explanation
Answer: A
Split the string into blocks 1 and 01, then allow a final 0. B allows 00 (empty 1* between zeros), C requires pairs 00, and D misses strings like 11011.
The language L(a*b*) ∩ L(b*a*) over {a, b} is:
Show answer & explanation
Answer: C
A string in both must have all a’s before b’s and all b’s before a’s, so it contains only one kind of symbol (or is empty): a* + b*.
The number of strings of length 3 in L((a + b)*ab(a + b)*) is:
Show answer & explanation
Answer: C
Strings without ab have the form b^i a^j: bbb, bba, baa, aaa (4 strings). Hence 8 − 4 = 4 strings contain ab.
A regular expression for the complement of L(a*) over Σ = {a, b} is:
Show answer & explanation
Answer: B
The complement of a* consists of all strings that contain at least one b: (a + b)*b(a + b)*.
The language described by (0 + 1)*1(0 + 1)(0 + 1) is the set of binary strings in which:
Show answer & explanation
Answer: B
After the final 1 there are exactly two more symbols, so that 1 is the third symbol from the right.
The minimum number of states in a DFA for (a + b)*a(a + b) (second symbol from the right is a) is:
Show answer & explanation
Answer: C
The DFA must remember the last two symbols: 2² = 4 states.
Extra page-specific practice beyond the GATE samples. Attempt all 10, then reveal answers to check your reasoning.
The solution of R = ε + R·01 given by Arden’s lemma is:
Show answer & explanation
Answer: B) (01)*
Q = ε, P = 01, so R = QP* = (01)*.
Which of the following strings is NOT in L((ab + a)*b)?
Show answer & explanation
Answer: D) bb
The language is (ab + a)* followed by a single b. “bb” would need a leading b inside (ab + a)*, which is impossible; b, aab and abb are all in the language.
A regular expression for binary strings with an even number of 0s is:
Show answer & explanation
Answer: A) 1*(01*01*)*
Zeros are grouped in pairs with any number of 1s before, between and after them. B rejects 11 (zero 0s, which is even), C rejects 1010 (it allows 1s only at the end), and D forces the string to end in 00.
The minimum number of states (including the dead state) in a DFA over {a, b} for L(a*b*) is:
Show answer & explanation
Answer: B) 3
State 1: still reading a’s (final); state 2: reading b’s (final); dead state: an a after a b.
The regular expression (ab)*a is equivalent to:
Show answer & explanation
Answer: A) a(ba)*
By the identity (rs)*r = r(sr)* with r = a and s = b.
The language denoted by ∅* is:
Show answer & explanation
Answer: B) {ε}
Kleene star always includes zero repetitions, giving ε. Since no non-empty string can be built from ∅, the language is exactly {ε}.
A DFA has start and final state q0, with q0 —a→ q1, q1 —b→ q0 and all other transitions going to a dead state. Its regular expression is:
Show answer & explanation
Answer: B) (ab)*
The only way to return to q0 is by reading ab, and q0 is final, so the language is (ab)*.
The number of strings of length 4 in L(a(a + b)*b) is:
Show answer & explanation
Answer: B) 4
Strings of length 4 have the form a _ _ b with 2 free middle symbols: 2² = 4.
The set of strings over {a, b} with equal numbers of a’s and b’s:
Show answer & explanation
Answer: C) cannot be described by any regular expression
The language is not regular (it needs unbounded counting), so no regular expression describes it.
Which of the following identities is correct for regular expressions r and s?
Show answer & explanation
Answer: C) r(sr)* = (rs)*r
C is the shifting identity. For A and B take r = a, s = b: ab is in the first but (rs)* = (ab)* misses a, and (r + s)* contains ab while r* + s* does not. D fails since r*r* = r*.
Topic FAQs
What does Arden’s lemma state?
If P is a regular expression with ε ∉ L(P), then R = Q + RP has the unique solution R = QP*. It lets you eliminate a state with a self-loop when converting a DFA to a regular expression.
Is (a + b)* the same as a* + b*?
No. (a + b)* contains every string over {a, b}, but a* + b* contains only strings made of a single repeated symbol, so ab is missing.
How do you find a regular expression for the complement of a language?
Build a complete DFA for the language, swap final and non-final states, and convert the resulting DFA to a regex by state elimination or Arden’s lemma.
Does every regular expression have a DFA of polynomial size?
No. A regex of length O(n) can need a DFA of 2^n states, for example (a+b)*a(a+b)^(n−1). The ε-NFA from Thompson’s construction, however, is linear in the regex size.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.