Title

ECS 120 Theory of Computation
Equivalence of regular grammars and NFAs
David Doty (adapted from slides authored by Julian Panetta)
University of California, Davis

Equivalence of regular grammars and DFAs

  • We’ve proved that NFAs and DFAs have equivalent computational power.
    • Any DFA “is” an NFA.
    • Any NFA can be simulated by a DFA using the “subset construction”.
  • What about Context-Free Grammars?
    • CFGs will turn out to be more powerful than DFAs.
    • Any DFA can be “simulated by” a CFG.
    • The CFG-decidable language \(\{0^n 1^n \mid n \geq 0\}\) is not DFA-decidable.

Recall right-regular grammars (RRG)

A right-regular grammar (RRG) is a CFG whose rules are all of the form: \[ X \to a Y \qquad \text{or} \qquad X \to \emptystring \]

Theorem: Every DFA-decidable language is RRG-decidable (and thus CFG-decidable).

Given any DFA \(D\), we can convert it into RRG \(G\) such that \(L(G) = L(D).\) In other words \(G\) generates string \(w\) iff \(D\) accepts \(w\).

Construction Example:

data/images/dfa_rrg/example_dfa/diagram.svg

Equivalent grammar \(G\): \[\begin{align*} P &\to \fragment{0T\;|\;} \fragment{1R} \\ T &\to 0R\;|\; 1P \\ R &\to 0P\;|\; 1R \\ P &\to \emptystring \\ T &\to \emptystring \end{align*}\]

Parse tree for \(0110\):
0 P 1 0 0->1 2 T 0->2 3 1 2->3 4 P 2->4 5 1 4->5 6 R 4->6 7 0 6->7 8 P 6->8

Derivation of \(0110\): \[ \fragment{P} \fragment{\yields 0T} \fragment{\yields 01P} \fragment{\yields 011R} \fragment{\yields 0110P} \fragment{\yields 0110} \]

Computation sequence accepting \(0011\): \[ p, t, p, r, p \]

  • Intermediate strings of the derivation have a single nonterminal end representing the current “state”.
  • This nonterminal can be removed only if it represents an accept state.

Simulating DFAs with RRGs

A right-regular grammar (RRG) is a CFG whose rules are all of the form: \[ X \to a Y \qquad \text{or} \qquad X \to \emptystring \]

Theorem: Every DFA-decidable language is RRG-decidable (and thus CFG-decidable).

Given any DFA \(D\), we can convert it into RRG \(G\) such that \(L(G) = L(D).\) In other words \(G\) generates string \(w\) iff \(D\) accepts \(w\).

Proof

  • Let \(D = (Q, \Sigma, \delta, s, F)\) be a DFA.
  • Construct grammar \(G = (\Gamma, \Sigma, S, \rho)\) such that:
    • \(\Gamma=Q\)
    • Start symbol \(S \in \Gamma\) corresponds to start state \(s \in Q\)
    • Production rules \(\rho\) are defined as follows:
      • For each state \(X \in Q\) and \(a \in \Sigma\) add the rule \(X \to a Y\), where \(Y = \delta(X, a)\)
      • For each accept state \(X \in F\), add the rule \(X \to \emptystring\)

We now prove \(L(G) = L(D)\).

Simulating DFAs with RRGs

A right-regular grammar (RRG) is a CFG whose rules are all of the form: \[ X \to a Y \qquad \text{or} \qquad X \to \emptystring \]

Theorem: Every DFA-decidable language is RRG-decidable (and thus CFG-decidable).

For \(X \in Q, a \in \Sigma\): \(\ X \to a Y\), where \(Y = \delta(X, a)\)
For \(X \in F\): \(\qquad \quad X \to \emptystring\)

Proof

  • \(L(G) \subseteq L(D)\):
    • For any \(x \in L(G)\), there is a derivation \[ S \yields u_1 \yields u_2 \yields \ldots \yields u_n \yields x \]
    • Due to \(G\)’s special production rule structure,
      each \(u_i = x_i R_i\), where \(x_i \sqsubseteq x\) is a prefix of \(x\).
    • The state \(R_i\) is the state of \(D\) after reading \(x_i\).
    • State \(R_n\) must be an accept state for the last nonterminal to be erased via \(R_n \to \emptystring\) in \(u_n = x R_n\).
    • Thus the computation sequence \(S, R_1, R_2, \ldots, R_n\) accepts \(x\), showing \(L(G) \subseteq L(D)\).

Simulating DFAs with RRGs

A right-regular grammar (RRG) is a CFG whose rules are all of the form: \[ X \to a Y \qquad \text{or} \qquad X \to \emptystring \]

Theorem: Every DFA-decidable language is RRG-decidable (and thus CFG-decidable).

For \(X \in Q, a \in \Sigma\): \(\ X \to a Y\), where \(Y = \delta(X, a)\)
For \(X \in F\): \(\qquad \quad X \to \emptystring\)

Proof

  • \(L(D) \subseteq L(G)\):
    • For any \(x \in L(D)\), there is an accepting
      computation sequence (i.e., \(R_n \in F\)): \(\quad S, R_1, R_2, \dots, R_n\)
    • This can be translated into a derivation of \(x\) by applying the corresponding production rules in order: \[\begin{equation*} \fragment{ \begin{aligned} S &\to x[1] R_1 \\ R_1 &\to x[2] R_2 \\ R_2 &\to x[3] R_3 \\ &\cdots \\ R_{n-1} &\to x[n] R_n \\ R_{n} &\to \emptystring \\ \end{aligned} \quad \quad \quad \begin{aligned} S & \yields x[1] R_1 \\& \yields x[1] x[2] R_2 \\& \yields x[1] x[2] x[3] R_3 \\& \yields \ldots \\& \yields x[1] x[2] \cdots x[n] R_n \\& \yields x[1] x[2] \cdots x[n] = x \end{aligned} } \end{equation*}\]

Thus \(x \in L(G)\), showing \(L(D) \subseteq L(G)\).

Simulating RRGs with NFAs

Theorem: Any RRG-decidable language is NFA-decidable (and thus DFA-decidable).

Proof

  • Let \(G = (\Gamma, \Sigma, S, \rho)\) be a RRG.
  • Construct NFA \(N = (Q, \Sigma, \Delta, s, F)\) as follows:
    • \(Q = \Gamma\)
    • \(s = S\)
    • \(F = \setbuild{X \in Q}{\fragment{(X, \emptystring) \in \rho}}\)
      (For each production rule of the form \(X \to \emptystring\), let state \(X\) be accepting)
    • For all \(X \in Q\) and \(a \in \Sigma\): \(\Delta(X, a) = \setbuild{Y \in Q}{\fragment{(X, a Y) \in \rho}}\)
      (For each production rule of the form \(X \to a Y\), add the transition \(X \stackrel{a}{\to} Y\))
  • Paths from \(s\) to an accept state of \(N\) correspond exactly to a sequence of rules in \(G\), all but the last of which produce a single new terminal.
  • Therefore \(L(N) = L(G)\).

Corollary: RRGs, DFAs, and NFAs have equivalent computational (decision) power.

Note this is almost the same as the DFA-to-RRG in reverse; nondeterministic transitions like \(X \stackrel{0}{\to} Y\) and \(X \stackrel{0}{\to} Z\) come from rules like \(X \to 0Y \or 0Z\).
What if there are also rules like \(X \to Y\)? Correspond to \(\emptystring\)-transitions \(X \stackrel{\emptystring}{\to} Y\)

Left regular grammars

A left-regular grammar (LRG) is a CFG whose rules are all of the form: \[ X \to Y a \qquad \text{or} \qquad X \to \emptystring \]

  • Left Regular Grammars are also equivalent in power to DFAs/NFAs.
  • We could prove this using similar arguments to the RRG case.
  • Or we can notice:
    • Consider converting an RRG \(G\) to an LRG \(G'\) by replacing each rule \(X \to aY\) with \(X \to Ya\).
    • What is the relationship between \(L(G)\) and \(L(G')\)? \(\quad L(G') = \reverse{L(G)} = \setbuild{\reverse{w}}{w \in L(G)}\)
    • NFA-decidable languages are closed under reversal! (exercise at home)
    • Thus \(L(G')\) is NFA-decidable if and only if \(L(G)\) is.

Left and right regular grammars: Don’t mix the rules!

  • What language does the following grammar generate?

\[\begin{align*} A &\to 0B \\ B &\to A1 \\ A &\to \emptystring \end{align*}\]

\(\setbuild{0^n1^n}{n \in \N}\)… not regular!