Recall warm-up
- 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 | ε.
- 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.
- 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 w k + 2 contains w / ends with w k + 1 |x| ≡ r mod n n |x| = k, |x| ≤ k k + 2 |x| ≥ k k + 1 #a mod m and/or #b mod n m·n (typical) binary value ≡ 0 mod k, k odd k binary ÷ 2ʲ j + 1 k-th symbol from start fixed k + 2 Divisibility update: r ← (base·r + digit) mod k.
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 ✓.
| DFA | grammar |
|---|---|
| A —a→ B | A → aB |
| B —b→ A | B → bA |
| B accepting | B → ε |
Right-linear and left-linear
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.
FA → right-linear grammar
- One variable per state; start symbol = start state.
- For each δ(A, a) = B: add A → aB.
- For each accepting A: add A → ε.
A derivation then mirrors the run: each step reads one symbol and moves to the next state.
Right-linear grammar → NFA
- One state per variable plus one accepting state f.
- A → aB: arrow A ─a→ B. A → a: arrow A ─a→ f. A → B: ε-arrow. A → ε: make A accepting.
- Longer strings A → abcB: insert intermediate states for each symbol.
So right-linear grammars generate exactly the regular languages.
States: S, A, f
Transitions: S>A: a; A>S: b; S>f: b; A>f: a
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.
Mixing breaks it
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
Right-linear grammar for a*b.
Show the full solution
S → aS | b. (Or S → aS | bE, E → ε.)
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 ✗.
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
- MCQ
Which production is NOT allowed in a right-linear grammar?
- A → abB
- A → a
- A → Ba
- A → ε
- MSQ
Which statements are true?
- Right-linear grammars generate exactly the regular languages
- Left-linear grammars generate exactly the regular languages
- Every grammar with at most one variable per right side generates a regular language
- A left-linear grammar for L gives a right-linear one for Lᴿ
- 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.
- MCQ
L(S → aA, A → Sb | b) is:
- a*b*
- { aⁿbⁿ : n ≥ 1 }
- (ab)*
- 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
- Any string of variables and terminals derivable from S.
- 4.
- 2.
Self-test answers
- C — Variable on the left side of the body.
- A: Terminals then one variable.
- B: Terminals only.
- D: Allowed.
- ABD — Mixed linear grammars can generate aⁿbⁿ.
- C: S → aA, A → Sb | b is a counterexample.
- 12 — 5 × 2 = 10 arrow rules + 2 ε-rules.
- 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
- Right-linear production shapes?
Reveal answer
A → wB or A → w (w terminals). - Does mixing left- and right-linear rules keep regularity?
Reveal answer
Not guaranteed: S → aA, A → Sb | b gives aⁿbⁿ. - DFA arrow δ(A,a)=B as a production?
Reveal answer
A → aB - How do accepting states appear in the grammar?
Reveal answer
As A → ε rules.