Regular Grammars and the CFL Hierarchy

Context: FIT2014_MOC · the grammar that corresponds to a finite automaton · pins down exactly where the regular languages sit inside the context-free ones

Quick Revision

  • 🎯 Objective: a regular grammar is a CFG whose rules are restricted to a special shape ➔ it generates exactly the regular languages, so {regular} ⊆ {CFL}.
  • ⚡ Key Constraint: the containment is proper: EQUAL, HALF-AND-HALF, PALINDROME are context-free but not regular. Regular = “a nonterminal remembers only a state”; context-free = “a nonterminal can spawn more nonterminals” (unbounded memory).

📝 Regular grammars

  • Semiword ➔ a string of the form (some terminals then one nonterminal at the end).
  • Regular grammar ➔ a CFG in which every production has one of the forms:
  • The restriction ➔ at most one nonterminal on the right, and it must be rightmost — this is what keeps the memory finite (a nonterminal ≈ an automaton state).

🔧 Building a regular grammar from an NFA

Given an NFA:

  1. Name every state by a nonterminal symbol; call the Start State .
  2. For each transition (arc labelled from state to ), add the rule .
  3. For each Final State , add the rule .
  • Output ➔ terminals = the NFA’s alphabet; nonterminals = the states; rules as above — and this grammar is regular (every right side is a semiword or a terminal string).
  • Special edge forms ➔ a self-loop gives ; an -arc gives .

🎯 The two theorems (and the hierarchy)

  • Every regular language has a regular grammar ➔ it is recognised by some NFA (Kleene’s Theorem), and the construction above turns that NFA into a regular grammar.
  • Every regular grammar generates a regular language ➔ (converse; proof left as exercise) — so regular grammars ⟺ regular languages.
  • Therefore ➔ since every regular grammar is a CFG:
  • Proper containment ➔ the inclusion is strict — EQUAL / HALF-AND-HALF / PALINDROME are CFLs with no regular grammar (proved non-regular via the pumping lemma).
stateDiagram-v2
    direction LR
    all: all languages
    cfl: context-free languages
    reg: regular languages
    all --> cfl: ⊇
    cfl --> reg: ⊋ (proper)

⚠️ Common Mistakes

  • 💡 Regular grammar ≠ any grammar for a regular language ➔ a regular language can be given a non-regular-shaped CFG; “regular grammar” is about the rule form (semiwords), not the language.
  • 💡 One rightmost nonterminal only ➔ a rule like (two nonterminals) is not regular — it is what lifts EQUAL out of the regular class.
  • 💡 The NFA→grammar map needs the arc direction gives (not ); getting the direction wrong generates the reversed language.
  • 💡 Containment is proper, not equal ➔ don’t claim regular = context-free; the pumping lemma supplies explicit separating languages.

🧠 Active Recall