Sub topic 14
Regular Grammars and Their Link to Automata
Assume you know basic set theory, strings, languages, functions, and elementary proof techniques. You should also know the definition of a deterministic finite automaton (DFA) and a nondeterministic finite automaton (NFA).
What Is a Regular Grammar?
A regular grammar is a formal grammar whose productions have a restricted form. A grammar consists of:
- Nonterminals, such as \(S\) and \(A\)
- Terminals, such as \(a\) and \(b\)
- A start nonterminal
- Production rules
A right-linear grammar uses rules of these forms:
\[ A \rightarrow aB,\qquad A \rightarrow a,\qquad A \rightarrow \epsilon \]
Here, \(A\) and \(B\) are nonterminals, \(a\) is a terminal, and \(\epsilon\) is the empty string. The nonterminal, if present, appears at the right end.
For example:
\[ S \rightarrow aS \mid bA \] \[ A \rightarrow bA \mid \epsilon \]
This grammar generates strings that start with zero or more a symbols, followed by one or more b symbols, depending on the available derivations.
A left-linear grammar instead uses rules such as \(A \rightarrow Ba\). A grammar should normally use one direction consistently. Mixing left-linear and right-linear rules can produce a language that is not regular.
Converting a Regular Grammar to an Automaton
A right-linear grammar maps naturally to an NFA.
- Create one automaton state for each nonterminal.
- Add a new accepting state \(F\).
- For \(A \rightarrow aB\), add the transition \(A \xrightarrow{a} B\).
- For \(A \rightarrow a\), add \(A \xrightarrow{a} F\).
- For \(A \rightarrow \epsilon\), mark state \(A\) as accepting.
- Make the state for the grammar's start symbol the automaton's start state.
Consider this grammar:
\[ S \rightarrow aS \mid bA \] \[ A \rightarrow bA \mid \epsilon \]
The corresponding automaton has transitions \(S \xrightarrow{a} S\), \(S \xrightarrow{b} A\), and \(A \xrightarrow{b} A\). State \(A\) is accepting because \(A \rightarrow \epsilon\).
A derivation in the grammar corresponds to a path in the automaton. For example:
\[ S \Rightarrow aS \Rightarrow abA \Rightarrow abbA \Rightarrow abb \]
The automaton follows the path labeled abb and ends in accepting state \(A\).
Converting an Automaton to a Grammar
Given an automaton, create one nonterminal for each state. Let the start state be the grammar's start symbol.
For every transition
\[ p \xrightarrow{a} q \]
add the production
\[ P \rightarrow aQ \]
For every accepting state \(q\), add
\[ Q \rightarrow \epsilon \]
If a transition enters acceptance directly through a separate final state, the equivalent terminal production \(P \rightarrow a\) may be used.
Common Errors
- Forgetting to make nonterminals with an \(\epsilon\)-production accepting
- Treating every grammar rule as a DFA transition without handling nondeterminism
- Mixing left-linear and right-linear rules carelessly
- Assuming a regular grammar can enforce equal counts, such as \(a^n b^n\)
Exercise
What automaton transition corresponds to the grammar rule A -> 1B?
Show answer
Add a transition from state A to state B labeled 1.