Explore BrainMass

# Converting a regular expression to a DFA

Not what you're looking for? Search our solutions OR ask your own Custom question.

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

How can a regular expression be converted into a deterministic finite automaton (DFA)?

© BrainMass Inc. brainmass.com September 29, 2022, 12:53 pm ad1c9bdddf
https://brainmass.com/computer-science/programming-languages/converting-regular-expression-dfa-207858

## SOLUTION This solution is FREE courtesy of BrainMass!

The basic approach to convert a regular expression to a DFA is to first convert the regular expression an an NFA (non-deterministic finite automaton) and then convert the NFA to a DFA. Doing this breaks the problem down into a couple of manageable states.

An NFA differs from a DFA in that there are "epsilon" states which are transitions between states with no input. There are also multiple transitions from one state to another on the same input.

In order to convert the regular expression to an NFA we use the following five rules:

1) any literal becomes an NFA that goes from the start state to final state, transitioning on the input. That is, a literal "a" in the regular expression becomes an NFA like this:

<pre>
(start) ----[a]----> (final)
</pre>

2) any "epsilon" in the regular expression (that is a zero length string) becomes the NFA:

<pre>
(start) ----[e]----> (final)
</pre>

3) the regular expression AB becomes the NFA for the regular expression A followed by the NFA for B -- linked together with an "epsilon" arc:

<pre>
(start) ----[e]----> (A's NFA) ----[e]----> (B's NFA) ----[e]----> (final)
</pre>

4) the regular expression A|B becomes the NFA as follows:

<pre>
(start) ----[e]----> (A's NFA) ----[e]----> (final)
/
----[e]----> (B's NFA) ----[e]--->/
</pre>

5) the regular expression A* becomes:

<pre>
(start) ----[e]----> (A's NFA) ----[e]----> (final)
^ / /
<-------[e]------/ /
/
-----------------[e]---------------->/
</pre>

We can recursively apply these rules to any regular expression to end up with an NFA that represents that regular expression.

The next stage is to convert the NFA to a DFA.

We assign a number to each NFA state. We will generate DFA states that have sets of numbers for their names. For example, a DFA state may have been assigned the set {1,2,3,4}. This indicates that arriving to the state labeled {1,2,3,4} in the DFA is the same as arriving to the state 1, the state 2, the state 3, or the state 4 in the NFA. The trick is figuring out what all of those DFA states will be.

We define the closure of a NFA node as the set of all the nodes reachable by this node using epsilon transitions. That is, the list of all nodes that can be reached with no input.

Then we follow the following algorithm:
1) The start state of the new DFA is given the label that is the closure of the start state for the NFA.
2) For every state, labeled (s1, s2, s3..sn), in the DFA do the following:
a) For every character, c, do the following: (we have a nested loop here)

1) find all the states reachable by s1, s2, ..., or sn using c arrows and we union together the closures of these nodes to get a new set.
2) If this set is not the label of any other node in the DFA constructed so far, we create a new DFA node with this label and create an arc to it
3) If this set is already the label of a node in the DFA we create a "c" arc to that node.

The nested loop in 2 and a is repeated until the inner loop creates no new DFA nodes.

This process will result in converting the original regular expression into a DFA. There is a potential for the DFA to contain a very large number of nodes since each node represents all of the possible ways to get to that node. This is why it is often more efficient to represent a language using a regular expression instead of a DFA.

This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

© BrainMass Inc. brainmass.com September 29, 2022, 12:53 pm ad1c9bdddf>
https://brainmass.com/computer-science/programming-languages/converting-regular-expression-dfa-207858