CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 2 · Regular Expressions & Regular Languages

Lesson 2.1.1 · Item 31 of 91

Regular expressions: syntax and meaning

Read at your own pace. Try the questions before opening the answers.

On this page

Recall warm-up

  1. What is L* for L = ∅?
    Stuck? Peek at 0.3.3 Operations on languages
    • L₁L₂ = {xy}; L{ε} = L; L∅ = ∅; not commutative.
    • L⁰ = {ε}; L* = ⋃Lⁿ (n≥0); L⁺ = LL*; L* = L⁺ ∪ {ε}; (L*)* = L*.
    • ∅* = {ε} = {ε}*; ∅⁺ = ∅.
    • L̅ = Σ* − L (include ε!). (L₁L₂)ᴿ = L₂ᴿL₁ᴿ.
    • Counting: C(n,r); fixed positions 2^(free); inclusion–exclusion; complement counting.

    Read lesson 0.3.3

  2. Moore machine output length for input length n?
    Stuck? Peek at 1.4 Mealy and Moore machines
    • Moore: λ : Q → Δ; output length n+1. Mealy: λ : Q × Σ → Δ; output length n.
    • Moore → Mealy: same states, λ'(q,a) = λ(δ(q,a)). Mealy → Moore: ≤ |Q|·|Δ| states.
    • No accepting states; equivalence ignores Moore’s first output.

    Read lesson 1.4

  3. What does Myhill–Nerode say about aⁿbⁿ?
    Stuck? Peek at 1.3.3 Myhill–Nerode theorem
    • x ≡_L y ⇔ ∀z: xz ∈ L ⇔ yz ∈ L. Right-invariant equivalence.
    • L regular ⇔ finite index; index = #states of minimal DFA (incl. dead class).
    • Infinitely many pairwise distinguishable strings ⇒ not regular. aⁿbⁿ: strings aⁱ separated by bⁱ.

    Read lesson 1.3.3

The Hook

You type *.pdf to find files, or a pattern like [0-9]+ in a search box. You are describing a whole language with a tiny formula. In TOC that formula is a regular expression (regex). It describes the same languages DFAs accept — but instead of a machine that checks strings, it is a recipe that builds them. GATE asks you to read, write and compare regexes in nearly every paper.

Predict First

Using only “or”, “followed by” and “repeat any number of times”, can you describe the language “all strings over {a, b} that end with b”?

Core Concept

Prediction check

Yes: “(a or b), repeated any number of times, followed by b” — written (a + b)*b. Anything, then a final b.

aabab(a + b)* — any stringb
(a + b)*b: anything, then a final b.

Building blocks

Regular expressions over Σ are built by these rules:
  • ∅ is a regex; L(∅) = ∅ (no strings).
  • ε is a regex; L(ε) = {ε}.
  • Each symbol a ∈ Σ is a regex; L(a) = {a}.
  • If r, s are regexes, so are: r + s (union, L(r) ∪ L(s)); rs (concatenation, L(r)L(s)); r* (Kleene star, L(r)*); and (r) (grouping).

GATE writes union as +; many books and tools write | or ∪. Same meaning. r⁺ is shorthand for rr* (one or more).

∅
no strings
ε
{ε}
a
{a}
r + s
union
rs
concatenation
r*
zero or more
Three atoms, three operators — every regex is built from these.

Precedence

Like × before + in arithmetic: star binds tightest, then concatenation, then union.

  • ab* means a(b*) — not (ab)*.
  • a + bc means a + (bc) — not (a+b)c.
  • a + b* means a + (b*).
+a·b*c
a + bc* parses as a + (b(c*)): star binds tightest, then concatenation, then union.

Reading a regex in plain English

Translate piece by piece:

  • (a + b)* — “any string over {a,b}”, i.e. Σ*.
  • (a + b)*abb — “ends with abb”.
  • a(a + b)* — “starts with a”.
  • (a + b)*aa(a + b)* — “contains aa”.
  • ((a + b)(a + b))* — “even length”.
  • b*ab*ab* — “exactly two a’s”.
  • (a + ε)(b + ab)* — no two a’s in a row (see 2.1.2).
regexin plain Englishshortest strings
(a+b)*abbends with abbabb, aabb, babb
a(a+b)*starts with aa, aa, ab
(a+b)*aa(a+b)*contains aaaa, aaa, baa
((a+b)(a+b))*even lengthε, aa, ab
b*ab*ab*exactly two a’saa, aab, aba
Reading regexes, piece by piece.

Regex = regular language

Kleene’s theorem: a language is regular (accepted by some DFA) if and only if some regular expression describes it. Chapter 2.2 proves both directions by conversions. So regexes, DFAs, NFAs and ε-NFAs are four faces of one family.

regular expression
ε-NFA
Thompson (2.2.1)
DFA
subset construction
regular expression
state elimination (2.2.2)
Kleene’s theorem: a full circle of conversions, so regexes describe exactly the regular languages.

Putting it together

  • A regex is built from ∅, ε, symbols with +, concatenation and *.
  • Precedence: star, then concatenation, then union.
  • Regexes describe exactly the regular languages (Kleene).

Visual

parse tree of  a + b c*      (star, then concat, then union)

        +
       / \
      a   ·            L = {a} ∪ {b, bc, bcc, bccc, ...}
         / \
        b   *
            |
            c

  regex              plain English                          shortest strings
  (a+b)*b            ends with b                            b, ab, bb
  a(a+b)*b           starts with a and ends with b          ab, aab, abb
  (ab)*              ab repeated                            ε, ab, abab
  a*b*               some a's then some b's                 ε, a, b, aa, ab
  (a*b*)*            anything                               = (a+b)*

Worked Examples

Easy

List all strings of length ≤ 3 in L(a*b).

Show the full solution

b, ab, aab. (Every string is some a’s then exactly one b.)

Medium

Describe L((a + b)*a(a + b)) in English and give the shortest strings.

Show the full solution

Any string, then a, then exactly one more symbol: the second symbol from the end is a. Shortest: aa, ab.

GATE-level

Which strings are in L(1*(0 + 10)*)? Test 0110, 1100, 10010.

Show the full solution

Read it: some 1s, then blocks that are either “0” or “10”. After the leading 1s, every 1 must be immediately followed by 0 — so “11” can only appear at the very start. 0110: 1*=ε, then 0, then “11” is not a block ⇒ no. 1100: 1*=11, then 0, 0 ⇒ yes. 10010: 1*=1, then 0, 0, then 10 ⇒ yes.

GATE Traps

  • ab* ≠ (ab)*: star applies only to the symbol or group directly before it.
  • ε and ∅ are different regexes: L(ε) = {ε}, L(∅) = ∅.
  • + means union here, not “one or more”; r⁺ (superscript) means one or more.
  • a* includes ε — “some a’s” can mean zero a’s.

Memory Hook

Precedence = “Star, String, Sum” (*, concatenation, +) — the order you’d tighten bolts: smallest first.

Connect the Dots

We can read regexes. Next: writing them from descriptions, the identities that simplify them, and the classic GATE task — deciding whether two different-looking regexes describe the same language.

Self-Test

  1. MCQ

    Which string is NOT in L(a(a + b)*b)?

    1. ab
    2. aab
    3. abab
    4. aba
  2. MCQ

    How is a + bc* parsed?

    1. ((a + b)c)*
    2. a + (b(c*))
    3. (a + b)(c*)
    4. (a + bc)*
  3. NAT

    How many strings of length 3 are in L((a + b)*a(a + b)(a + b))?

    Write your answer before opening the key.

  4. MSQ

    Which regexes describe exactly the strings over {a,b} of even length?

    1. ((a + b)(a + b))*
    2. (aa + ab + ba + bb)*
    3. (a + b)*(a + b)*
    4. (a + b)(a + b)*
  5. MCQ

    L(∅*) is:

    1. ∅
    2. {ε}
    3. Σ*
    4. undefined

Rule Card

  • Atoms ∅, ε, a; operators r + s (∪), rs (concat), r* (star); r⁺ = rr*.
  • Precedence: * > concatenation > +.
  • Kleene: regular ⇔ described by a regex.
  • Patterns: Σ* = (a+b)*; ends w = (a+b)*w; starts w = w(a+b)*; contains w = (a+b)*w(a+b)*; even length = ((a+b)(a+b))*; exactly two a’s = b*ab*ab*.

Concept Ledger

  • regular expression (regex)
  • L(r)
  • union r + s
  • concatenation rs
  • Kleene star r*, r⁺
  • precedence (* > concat > +)
  • Kleene’s theorem (regex ⇔ regular)

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. {ε}.
  2. n + 1.
  3. Infinitely many classes (aⁱ separated by bⁱ), so it is not regular.

Self-test answers

  1. D — Must start with a and end with b.
    • A: Fits: a·ε·b.
    • B: Fits.
    • C: Fits.
  2. B — Star, then concatenation, then union.
    • A: Wrong grouping.
    • C: Union binds loosest.
    • D: Star applies only to c.
  3. 4 — Third from the end is a: first symbol a, the other two free: 2² = 4.
  4. AB — Both repeat 2-symbol blocks.
    • C: That is Σ*.
    • D: That is Σ⁺.
  5. B — Star of anything contains ε.
    • A: Star always contains ε.
    • C: Nothing to repeat.
    • D: Defined.

Flashcards

  1. Regex precedence order?
    Reveal answerStar, then concatenation, then union.
  2. Regex for “ends with abb” over {a,b}?
    Reveal answer(a+b)*abb
  3. Regex for “exactly two a’s” over {a,b}?
    Reveal answerb*ab*ab*
  4. Kleene’s theorem?
    Reveal answerA language is regular iff some regular expression describes it.

Ready for the next idea?

Mark this item complete when you’ve finished reading and working through it.