CS Atlas GATE 2027
TOC course
0 / 91 complete

TOC/Phase 2 · Regular Expressions & Regular Languages

Lesson 2.3.2 · Item 38 of 91

Right-linear and left-linear grammars

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

On this page

Recall warm-up

  1. What is a sentential form?
    Stuck? Peek at 2.3.1 What a grammar is
    • G = (V, T, P, S); A → x | y; sentential form; ⇒, ⇒*; L(G) = {w ∈ T* : S ⇒* w}.
    • S → aSb | ε gives aⁿbⁿ (not regular) — general grammars exceed DFAs.
    • Palindromes: S → aSa | bSb | a | b | ε.

    Read lesson 2.3.1

  2. Strict Thompson states for a*?
    Stuck? Peek at 2.2.1 Regex → ε-NFA (Thompson construction)
    • Invariant: one start (no in-moves), one accept (no out-moves).
    • ∅, ε, a: 2 states. r+s: +2 states, 4 ε. rs: ε from accept(r) to start(s). r*: +2 states, 4 ε (enter, skip, loop back, exit).
    • States ≤ 2 × (symbols + operators). Regex → ε-NFA → DFA proves regex ⇒ regular.

    Read lesson 2.2.1

  3. Minimal DFA states for strings ending in a over {a,b}?
    Stuck? Peek at 1.1.3 DFA design patterns
    Language (|w| = k pattern)Minimal states
    starts with wk + 2
    contains w / ends with wk + 1
    |x| ≡ r mod nn
    |x| = k, |x| ≤ kk + 2
    |x| ≥ kk + 1
    #a mod m and/or #b mod nm·n (typical)
    binary value ≡ 0 mod k, k oddk
    binary ÷ 2ʲj + 1
    k-th symbol from start fixedk + 2

    Divisibility update: r ← (base·r + digit) mod k.

    Read lesson 1.1.3

The Hook

A DFA’s arrow “from A on a go to B” reads like a rule: “a string from A is a, followed by a string from B”. Write every arrow as a production and the DFA becomes a grammar. The grammars you get have a very rigid shape — a variable only ever appears at the right end. That shape is exactly what keeps grammars inside the regular languages.

Predict First

DFA: A (start) ─a→ B, B ─b→ A, B is accepting. Write it as grammar rules, one per arrow, plus one rule for “B can stop here”.

Core Concept

Prediction check

A → aB, B → bA, B → ε. Language: a(ba)* — check: A ⇒ aB ⇒ a; A ⇒ aB ⇒ abA ⇒ abaB ⇒ aba ✓.

DFAgrammar
A —a→ BA → aB
B —b→ AB → bA
B acceptingB → ε
One production per arrow, plus ε for accepting states.

Right-linear and left-linear

Right-linear grammar: every production is A → wB or A → w, with w ∈ T* (a string of terminals) and B a variable. The variable, if any, is at the right end.
Left-linear: every production is A → Bw or A → w.
A regular grammar is one that is right-linear, or one that is left-linear.
right-linear
A → wB | w — variable at the right end
left-linear
A → Bw | w — variable at the left end
mixed
both kinds in one grammar — no guarantee
Regular grammars use one side only.

FA → right-linear grammar

  1. One variable per state; start symbol = start state.
  2. For each δ(A, a) = B: add A → aB.
  3. For each accepting A: add A → ε.

A derivation then mirrors the run: each step reads one symbol and moves to the next state.

flowplay illustration
The derivation mirrors the DFA run q0 → q0 → q1 → q2.

Right-linear grammar → NFA

  1. One state per variable plus one accepting state f.
  2. A → aB: arrow A ─a→ B. A → a: arrow A ─a→ f. A → B: ε-arrow. A → ε: make A accepting.
  3. Longer strings A → abcB: insert intermediate states for each symbol.

So right-linear grammars generate exactly the regular languages.

NFA from S → aA | b, A → bS | a

States: S, A, f

Transitions: S>A: a; A>S: b; S>f: b; A>f: a

Grammar → NFA: each A → aB is an arrow; A → a goes to a new accepting state f.

Left-linear grammars

A left-linear grammar for L corresponds to a right-linear grammar for Lᴿ (flip every right side). Since regular languages are closed under reversal (2.4.1), left-linear grammars also generate exactly the regular languages.

Direct conversion: for A → Ba, the string from A is a string from B followed by a — read the grammar “backwards from S”: S is the accepting state, terminal-only rules start the machine.

left-linear grammar for L
flip every body
right-linear for Lᴿ
NFA for Lᴿ, reversed
NFA for L
Why left-linear grammars are regular too.

Mixing breaks it

A grammar with both right-linear and left-linear rules may generate a non-regular language: S → aA, A → Sb | b. Here S ⇒ aA ⇒ ab, S ⇒ aA ⇒ aSb ⇒ aaAb ⇒ aabb: L = { aⁿbⁿ | n ≥ 1 }. Each rule is “linear” (at most one variable), but mixing sides gives non-regular languages.
SaASaAbb
Mixed rules S → aA, A → Sb | b grow from both ends: aabb, and in general aⁿbⁿ.

Putting it together

  • Right-linear (A → wB | w) and left-linear (A → Bw | w) grammars generate exactly the regular languages.
  • DFA arrows become productions; accepting states get A → ε.
  • Mixing left and right rules can escape regularity (aⁿbⁿ).

Visual

DFA for "ends with ab"                      right-linear grammar
 q0 —a→ q1, q0 —b→ q0                       Q0 → aQ1 | bQ0
 q1 —a→ q1, q1 —b→ q2                       Q1 → aQ1 | bQ2
 q2 —a→ q1, q2 —b→ q0   (q2 accepting)      Q2 → aQ1 | bQ0 | ε

derivation of "bab" mirrors the run q0 →b q0 →a q1 →b q2:
 Q0 ⇒ bQ0 ⇒ baQ1 ⇒ babQ2 ⇒ bab

rejected "aba": Q0 ⇒ aQ1 ⇒ abQ2 ⇒ abaQ1 — Q1 has no ε-rule, cannot finish

Worked Examples

Easy

Right-linear grammar for a*b.

Show the full solution

S → aS | b. (Or S → aS | bE, E → ε.)

Medium

Convert S → aA | b, A → bS | a into an NFA.

Show the full solution

States S (start), A, f (accepting). S ─a→ A, S ─b→ f, A ─b→ S, A ─a→ f. Language: (ab)*(b + aa). Trace “abb”: S→A→S→f ✓. “ab”: ends in S, not accepting ✗.

GATE-level

Which grammars generate regular languages? G1: S → aS | Sb | ε. G2: S → Aa, A → Ab | ε. G3: S → aSb | ab. G4: S → abS | ε.

Show the full solution

G2 is left-linear (A → Ab, A → ε, S → Aa): b*a — regular ✓. G4 is right-linear: (ab)* ✓. G3 is not linear-on-one-side, generates aⁿbⁿ — not regular. G1 mixes left and right rules — but its language is a*b* (prepend a’s, append b’s, independently), which is regular. Lesson: a mixed grammar may generate a regular language; it is just not guaranteed. Answer: G1, G2, G4 generate regular languages; only G2 and G4 are regular grammars.

GATE Traps

  • A grammar with both right- and left-linear rules is not a regular grammar, even if each rule has one variable.
  • Accepting states need A → ε rules in the FA → grammar conversion.
  • “The grammar is not regular” ≠ “the language is not regular”. A non-regular grammar can still generate a regular language.

Memory Hook

Right-linear = a train that only ever adds carriages at the back. Mixing front and back additions lets it count — and counting breaks finite memory.

Connect the Dots

Regular languages now have five faces: DFA, NFA, ε-NFA, regex, regular grammar. Next chapter studies the family itself: which operations keep a language regular, what questions about it can be answered by an algorithm, and how to prove a language is NOT regular.

Self-Test

  1. MCQ

    Which production is NOT allowed in a right-linear grammar?

    1. A → abB
    2. A → a
    3. A → Ba
    4. A → ε
  2. MSQ

    Which statements are true?

    1. Right-linear grammars generate exactly the regular languages
    2. Left-linear grammars generate exactly the regular languages
    3. Every grammar with at most one variable per right side generates a regular language
    4. A left-linear grammar for L gives a right-linear one for Lᴿ
  3. NAT

    Converting a 5-state DFA with 2 accepting states over {0,1} to a right-linear grammar (one rule per arrow, plus ε-rules) gives how many productions?

    Write your answer before opening the key.

  4. MCQ

    L(S → aA, A → Sb | b) is:

    1. a*b*
    2. { aⁿbⁿ : n ≥ 1 }
    3. (ab)*
    4. a⁺b⁺

Rule Card

  • Right-linear: A → wB | w. Left-linear: A → Bw | w. Regular grammar = one of the two (not mixed).
  • DFA → grammar: δ(A,a)=B ⇒ A → aB; A accepting ⇒ A → ε.
  • Grammar → NFA: A → aB is an arrow; A → a goes to a new accepting state.
  • Mixed linear can give aⁿbⁿ. Non-regular grammar does not imply non-regular language.

Concept Ledger

  • right-linear grammar
  • left-linear grammar
  • regular grammar
  • linear grammar (at most one variable)
  • DFA ↔ grammar conversion

Answer Vault

Reveal answers when you’re ready

Warm-up answers

  1. Any string of variables and terminals derivable from S.
  2. 4.
  3. 2.

Self-test answers

  1. C — Variable on the left side of the body.
    • A: Terminals then one variable.
    • B: Terminals only.
    • D: Allowed.
  2. ABD — Mixed linear grammars can generate aⁿbⁿ.
    • C: S → aA, A → Sb | b is a counterexample.
  3. 12 — 5 × 2 = 10 arrow rules + 2 ε-rules.
  4. B — Each a is matched by a b added on the right.
    • A: Counts are tied.
    • C: Not alternating.
    • D: Counts must be equal.

Flashcards

  1. Right-linear production shapes?
    Reveal answerA → wB or A → w (w terminals).
  2. Does mixing left- and right-linear rules keep regularity?
    Reveal answerNot guaranteed: S → aA, A → Sb | b gives aⁿbⁿ.
  3. DFA arrow δ(A,a)=B as a production?
    Reveal answerA → aB
  4. How do accepting states appear in the grammar?
    Reveal answerAs A → ε rules.

Ready for the next idea?

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