Title

ECS 120 Theory of Computation
Equivalence of Regular Grammars and DFAs
Julian Panetta
University of California, Davis

Equivalence of Regular Grammars and DFAs

  • Last lecture we proved 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: \[ A \to a B \or \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 \fragment{0R\;|\;} \fragment{1P} \\ R &\to \fragment{0P\;|\;} \fragment{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} \]

Computational 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: \[ A \to a B \or \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\) contains a variable \(\varforstate(q)\) for each state in \(q \in Q\)
      (\(p, t, r \in Q\) became \(P, T, R \in \Gamma\), with \(P = \varforstate(p)\), etc.)
    • \(\Sigma\) is the same
    • Start symbol \(S \in \Gamma\) corresponds to start state \(s \in Q\)
    • Production rules \(\rho\) are defined as follows:
      • For each \(q \in Q\) and \(a \in \Sigma\) add the rule \(\varforstate(q) \to a \varforstate(\delta(q, a))\)
      • For each accept state \(q \in F\), add the rule \(\varforstate(q) \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: \[ A \to a B \or \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

  • \(L(G) \subseteq L(D)\):
    • For any \(w \in L(G)\), there exists a derivation \[ S \yields u_1 \yields u_2 \yields \ldots \yields u_n \yields w \]
    • Due to \(G\)’s special production rule structure,
      each \(u_i = x_i R_i\), where \(x_i \sqsubseteq w\) is a prefix of \(w\).
    • The state \(r_i = \varforstate^{-1}(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\).
    • Thus the computation sequence \(s, r_1, r_2, \ldots, r_n\) accepts \(w\).

Simulating DFAs with RRGs

A right-regular grammar (RRG) is a CFG whose rules are all of the form: \[ A \to a B \or \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

  • \(L(D) \subseteq L(G)\):
    • For any \(w \in L(D)\), there is an accepting
      computation sequence (i.e., \(r_n \in F\)): \[ s, r_1, r_2, \ldots, r_n \]
    • This can be translated into a derivation of \(w\) by applying the corresponding production rules in order: \[\begin{equation*} \begin{aligned} S &\to w[1] \varforstate(r_1) \\ \varforstate(r_1) &\to w[2] \varforstate(r_2) \\ \varforstate(r_2) &\to w[3] \varforstate(r_3) \\ &\cdots \\ \varforstate(r_n) &\to w[n] \\ \end{aligned} \quad \quad \quad \begin{aligned} \fragment{S} \fragment{\yields w[1] \varforstate(r_1)} &\fragment{\yields w[1] w[2] \varforstate(r_2)} \fragment{\yields \ldots} \\ &\fragment{\yields w[1] w[2] \cdots w[n] \varforstate(r_n)} \fragment{\yields w} \end{aligned} \end{equation*}\]

Thus \(w \in L(G)\), showing \(L(D) = 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 all \(X \in Q\) and \(a \in \Sigma\): \(\Delta(X, a) = \setbuild{B \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 for all \(w \in \Sigma^*\), \(\quad w \in L(N) \iff w \in L(G)\).

Corollary: a language is DFA-decidable if and only if it is DFA-decidable.

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

Left Regular Grammars

A left-regular grammar (LRG) is a CFG whose rules are all of the form: \[ A \to B a \or \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:
    • Any RRG \(G\) can be converted to an LRG \(G'\) by reversing the order of the symbols in each production rule.
      Moving the “insertion cursor” to the left of the string instead of the right.
    • What is the relationship between \(L(G)\) and \(L(G')\)? \(\quad L(G') = \reverse{L(G)} \fragment{= \setbuild{\reverse{w}}{w \in L(G)}}\)
    • NFA-decidable languages are closed under reversal!
    • 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}\qquad\) not regular!