To convert a left-linear grammar to a right-linear grammar, first translate its productions into an NFA whose transition direction reflects the left-linear derivation. Then read that NFA as right-linear productions. This avoids informal symbol reversal, which can accidentally produce the reversed language.
Left-linear and right-linear productions
A simple left-linear production has the form A → B a or A → a, where uppercase symbols are nonterminals and a is a terminal. A right-linear production has the form A → a B or A → a.
Both restrictions describe regular languages when applied consistently across the grammar. The conversion can therefore pass through a deterministic or nondeterministic finite automaton.
NFA conversion method
- Create one state for every nonterminal and add a fresh state
q₀. - Make
q₀the NFA start state. - Make the original grammar start symbol the accepting state.
- For each production
A → B a, add transitionB —a→ A. - For each production
A → a, add transitionq₀ —a→ A. - Translate every NFA transition
P —a→ Qinto right-linear productionP → a Q. - Add
S → εfor each accepting stateS.
Productions containing longer terminal strings can be normalized by inserting intermediate states. Nullable nonterminals and epsilon productions require the corresponding epsilon-closure or accepting-state adjustments.
Worked conversion example
Consider this left-linear grammar with start symbol S:
S → A b | a
A → B a | b
B → aCreate states q₀, S, A and B. The accepting state is S. The productions produce these NFA transitions:
A —b→ S
q₀ —a→ S
B —a→ A
q₀ —b→ A
q₀ —a→ BReading those transitions as a right-linear grammar gives:
q₀ → a S | b A | a B
A → b S
B → a A
S → εThe new grammar starts at q₀. Each derivation follows an NFA path and terminates when it reaches the accepting state S.
Verify language equivalence
List strings up to a small length from both grammars and compare the sets. This catches swapped transition direction, an incorrect accepting state and omitted terminal-only productions. For a proof-oriented check, minimize or compare the finite automata derived from both grammars.
Do not simply move a nonterminal from the left side of the terminal string to the right. Grammar derivations are directional, and that shortcut generally does not preserve the language.
Frequently asked questions
01What is a left-linear grammar?
A left-linear grammar has at most one nonterminal in each production right-hand side, and when present that nonterminal appears to the left of the terminals.
02What is a right-linear grammar?
A right-linear grammar has at most one nonterminal in each production right-hand side, and that nonterminal appears to the right of the terminals.
03Do left-linear and right-linear grammars describe regular languages?
Yes. Both forms generate regular languages and can be converted through finite automata.
04Can left-linear and right-linear productions be mixed?
A grammar that mixes the two forms is not automatically regular under the usual linear-grammar theorem. The global production restrictions matter.
05Why use an NFA during conversion?
The automaton makes transition direction and accepting states explicit. A right-linear grammar can then be read directly from the NFA transitions.
06How are epsilon productions handled?
They affect initial or accepting-state treatment. Remove avoidable epsilon productions first or carry nullable behavior explicitly in the automaton construction.
07Does the converted grammar have the same nonterminal names?
It can reuse names, but adding a fresh automaton start state is common. Only language equivalence matters; internal names do not need to match.
08How can the conversion be checked?
Generate short strings from both grammars or convert both to automata and test equivalence with a regular-language tool.