Sub topic 14
Regular grammars and their link to automata
What this chapter covers
This chapter assumes you know DFAs, NFAs, and regular expressions from the earlier chapters. You also need the basic grammar vocabulary: a grammar is G = (V, Σ, P, S), with variables V, terminals Σ, productions P, and a start variable S. The chapter shows that a restricted kind of grammar generates exactly the regular languages. It gives both conversions and explains how they appear in GATE questions.
Right-linear and left-linear grammars
A grammar is right-linear if every production has one of these forms, where A and B are variables and w is a string of terminals:
- A → wB
- A → w
(w may be empty, so A → ε is allowed.)
A grammar is left-linear if every production has the form A → Bw or A → w.
A regular grammar is a grammar that is entirely right-linear or entirely left-linear. In the Chomsky hierarchy these are the Type 3 grammars.
| Grammar form | Example production | Language class |
|---|---|---|
| Right-linear | A → abB, A → ε | Regular |
| Left-linear | A → Bab, A → ε | Regular |
| Mixed (both kinds in one grammar) | S → aB, B → Sb | Can be non-regular |
| Linear in general (one variable per right side, anywhere) | S → aSb, S → ε | Can be non-regular |
Many textbooks restrict w to a single terminal or ε. This is the strict form, and it loses nothing. A production A → abB can be split into A → aX and X → bB with a new variable X.
From a right-linear grammar to an NFA
Each variable becomes a state, and the start variable becomes the start state. Each production becomes a transition or an acceptance mark:
- For A → aB, add the transition δ(A, a) ∋ B.
- For A → ε, make A an accepting state.
- For A → a (a terminal with no variable), add δ(A, a) ∋ F, where F is a new accepting state with no outgoing transitions.
A derivation S ⇒ a₁S₁ ⇒ a₁a₂S₂ ⇒ … ⇒ a₁…aₖ is then exactly a path in the NFA reading a₁…aₖ. Each sentential form (an intermediate string in a derivation) has at most one variable, always at the right end. That variable is the NFA's current state.
From an NFA or DFA to a right-linear grammar
Reverse the construction:
- Make each state a variable. The start state is the start variable.
- For each transition δ(p, a) = q, add the production p → aq.
- For each accepting state q, add q → ε.
Take the NFA for (a|b)\*abb from the earlier chapters, with states 0 to 3 and state 3 accepting. Name the variables A0 to A3:
- A0 → aA0 | bA0 | aA1
- A1 → bA2
- A2 → bA3
- A3 → ε
The string aabb has the derivation A0 ⇒ aA0 ⇒ aaA1 ⇒ aabA2 ⇒ aabbA3 ⇒ aabb. Every step corresponds to one NFA move.
Simulating a grammar as an automaton
Because a right-linear grammar is an NFA in disguise, you can test membership by tracking the set of variables the derivation could currently end in. This code does that for the grammar above. Each production is a pair (terminal string, next variable), and None means there is no next variable.
grammar = {
"A0": [("a", "A0"), ("b", "A0"), ("a", "A1")],
"A1": [("b", "A2")],
"A2": [("b", "A3")],
"A3": [("", None)], # A3 -> epsilon
}
def generates(grammar, start, w):
current = {start}
for ch in w:
current = {
nxt
for A in current
for (t, nxt) in grammar[A]
if t == ch and nxt is not None
}
# accept if some current variable can finish with epsilon
return any(t == "" and nxt is None
for A in current
for (t, nxt) in grammar[A])
print(generates(grammar, "A0", "aabb")) # True
print(generates(grammar, "A0", "abab")) # False
print(generates(grammar, "A0", "abb")) # True
print(generates(grammar, "A0", "")) # FalseThis is the same set-of-states simulation used for NFAs, with variables in place of states. The code assumes the strict form, where each production reads at most one terminal.
Left-linear grammars and reversal
In a left-linear grammar the variable sits at the left end, so a derivation builds the string from right to left. A direct NFA construction is awkward. The standard method uses reversal:
- Reverse the right side of every production. A → Bab becomes A → baB.
- The result is a right-linear grammar G^R with L(G^R) = (L(G))^R.
- Build an NFA for L(G^R).
- Reverse that NFA to get an automaton for L(G). Regular languages are closed under reversal, so this works.
Example: the left-linear grammar
- S → Sa | Sb | abb
generates abb(a|b)\. Each use of S → Sa or S → Sb appends a symbol on the right, and S → abb supplies the leading abb. Reversing the right sides gives the right-linear grammar S → aS | bS | bba. This generates (a|b)\bba, which is the reversal of abb(a|b)\*, as expected.
Why the grammar form matters
The three kinds of grammar are easy to confuse, and GATE questions are built around the confusion.
Two more points to remember:
- Linear is not regular. S → aSb | ε has at most one variable on every right side, but the variable sits in the middle. The language {aⁿbⁿ | n ≥ 0} is not regular, by the pumping lemma.
- Left-linear and right-linear grammars are equally powerful. Both generate exactly the regular languages. They differ only in the direction in which a derivation builds the string.
Working a GATE-style question
Question pattern: "Which language does this grammar generate?" The method is:
- Check that every production is right-linear (or every one is left-linear). If so, the language is regular.
- Treat the variables as states. Find which variables can reach ε.
- Write the language, or draw the small NFA.
Example: S → 0S | 1A, A → 0A | ε.
- S loops on 0, so it reads 0\*.
- S → 1A reads a single 1 and moves to A.
- A loops on 0, then ends with ε, so it reads 0\*.
The language is 0\10\, the strings with exactly one 1. A quick check: from S, "0010" is derived as S ⇒ 0S ⇒ 00S ⇒ 001A ⇒ 0010A ⇒ 0010. The string is accepted, as expected.
A counting shortcut also helps. A strict right-linear grammar with n variables gives an NFA with n states, plus one extra final state if any production has the form A → a. A question that asks for "the minimum number of variables" is therefore the same question as the minimum NFA size, up to the extra final state.
Exercises
Exercise
Convert this right-linear grammar into an NFA and describe its language: S → aS | bA, A → bA | ε.
Show answer
Variables S and A become states, S is the start state, and A is accepting because A → ε. The transitions are δ(S, a) = S, δ(S, b) = A, and δ(A, b) = A. The language is a\bb\ (any number of a's, then one or more b's).
Exercise
Is the grammar S → Sa | Sb | ε a regular grammar? What language does it generate?
Show answer
Yes. Every production is left-linear (the variable is on the left, or there is no variable). Starting from ε, each use of S → Sa or S → Sb appends a symbol on the right, so the language is (a|b)\*, which is regular.
Exercise
A student says "S → aSb | ε is a linear grammar, so its language is regular." Explain the mistake.
Show answer
A linear grammar allows the single variable to sit anywhere on the right side. Only right-linear or left-linear grammars (variable at the end or at the start) are guaranteed to be regular. Here the variable sits in the middle, and the language {aⁿbⁿ | n ≥ 0} is not regular, by the pumping lemma.
Exercise
Give a right-linear grammar for the language of binary strings that end in 01.
Show answer
One answer is S → 0S | 1S | 0A, A → 1B, B → ε. After a guess that the final 01 has started, the grammar reads 0, then 1, then stops. The language is (0|1)\*01.