DFA To Regular Grammar Conversion Module
DFA To Regular Grammar Conversion Module
A DFA may be easily converted into a regular grammar. Let us consider the following DFA which models one
round of the classic game Rock-‐Paper-‐Scissors. The DFA accepts two-‐lettered inputs where the first
letter is Player 1’s input and the second letter is Player 2’s input. The input alphabet is {R, P, S}
representing rock, paper, and scissors. For example, RP represents rock for Player 1 and paper for
Player 2; this string ends in a final state in which Player 2 wins since rock crushes scissors.
Try It! Open the file RockPaperScissors_DFA.jflap in JFLAP. Using Input > Multiple Run, run the valid and
invalid inputs: PP, SP, RS, RSP, RSSP, and RT.
Converting a DFA, defined by M = (Q, Σ, δ, q0, F), to a regular grammar, defined by G = (V, T, S, P) is
straight forward. The rules are summarized below:
1. The start symbol of the grammar is q0, the non-‐terminal corresponding to the start state of the
DFA.
2. For each transition from state qi to state qj on some symbol ‘a’, create a production rule of the
form: qi a qj.
3. For each state qi of the DFA which is a final state, create a production rule of the form: qi λ.
Next, we work on converting the DFA into a grammar. Using the DFA above, we convert each transition
(between two states) into a production rule for the resulting regular grammar. Since there are twelve
transitions, we end up with twelve production rules:
q0 R q1 | P q2 | S q3,
q1 S q4 | R q5 | P q6,
q2 R q4 | P q5 | S q6,
q3 P q4 | S q5 | R q6.
To add the transitions to the three final states, we also include the following transitions:
q4 λ,
q5 λ,
q6 λ.
T = {R, P, S}
S = q0,
Answer: Recall that a regular grammar is either right-‐ or left-‐linear and since the resulting
grammar is right-‐linear, the grammar is a regular grammar.
4. Many of you may have seen the episodes of the television series “Big Bang Theory” where they
mention an extension to the Rock-‐Paper-‐Scissors game called Rock-‐Paper-‐Scissors-‐Lizard-‐Spock
(http://en.wikipedia.org/wiki/Rock -‐paper-‐scissors-‐lizard-‐Spock). Create a DFA for this expanded
game. Convert the DFA to a regular grammar as we did in this module.
Try it! Open the file RockPaperScissors_DFA.jflap if it is not loaded in JFLAP. Convert the DFA to a
grammar by selecting Convert > Convert to Grammar. You may perform the conversion one step at a
time. Click Step and the first grammar rule is generated on the right-‐hand pane, (q3) -‐> R(q6). Repeat the
process a few more times paying close attention to each new rule generated. Finish the conversion
by clicking Step to Completion. The resulting grammar should be similar to the one that was developed
earlier with each variable surrounded by ( ).
1. Can all DFAs be converted to a regular grammar (right-‐linear) with the given algorithm?
Answer: Yes. Using the algorithm, any DFA may be converted to a regular grammar. Every DFA
has exactly one start state; this translates to the start variable for the grammar. Each transition
in the DFA becomes one production rule in the grammar. A DFA must have at least one final
state which allows for the derivation to terminate.
Reference:
Peter Linz, “An Introduction to Formal Languages and Automata” 5th edition, Jones and Bartlett, 2011.