Conversion of Regular Expression to Finite Automata
Last Updated :
18 Jan, 2022
As the regular expressions can be constructed from Finite Automata using the State Elimination Method, the reverse method, state decomposition method can be used to construct Finite Automata from the given regular expressions.
Note: This method will construct NFA (with or without ε-transitions, depending on the expression) for the given regular expression, which can be further converted to DFA using NFA to DFA conversion.
State Decomposition Method
Theorem: Every language defined by a regular expression is also defined by a Finite Automata.
Proof: Let's assume L = L(R) for a regular expression R. We prove that L = L(M) for some ε-NFA M with:
1) Exactly one accepting state.
2) No incoming edges at the initial state.
3) No outgoing edges at the accepting state.
The proof is done by structural induction on R by following the steps below:
Step 1: Create a starting state, say q1, and a final state, say q2. Label the transition q1 to q2 as the given regular expression, R, as in Fig 1. But, if R is (Q)*, Kleene's closure of another regular expression Q, then create a single initial state, which will also be the final state, as in Fig 2.
Fig 1
Fig 2 Step 2: Repeat the following rules (state decomposition method) by considering the least precedency regular expression operator first until no operator is left in the expression. Precedence of operators in regular expressions is defined as Union < Concatenation < Kleene's Closure.
Union operator (+) can be eliminated by introducing parallel edges between the two states as follows.
Fig 3: Removal of Union OperatorThe concatenation operator ('.' or no operator at all) can be eliminated by introducing a new state between the states as follows.
Fig 4: Removal of Concatenation OperatorKleene's Closure (*) can be eliminated by introducing self-loops on states based on the following conditions:
1. If there is only one outgoing edge at the left-most state, i.e., A in transition A -> B, then introduce self-loop on state A and label edge A to B as an ε-transition, as shown in Fig 5.
Fig 52. Else if there is only one incoming edge at the right-most state, i.e., B in transition A -> B, then introduce self-loop on state B and label edge A to B as an ε-transition, as shown in Fig 6.
Fig 63. Else introduce a new state between two states having self-loop labeled as the expression. The new state will have ε-transitions with the previous states as follows, as shown in Fig 7.
Fig 7Example:
Construct Finite Automata for the regular expression, R = (ab + ba)*
Solution:
Step 1: As the given expression, R, is of the form (Q)*, so we will create a single initial state that will also be the final state, having self-loop labeled (ab + ba), as shown in Fig 8. (Refer Fig 2 above)
Fig 8 Step 2:
A. As the least precedency operator in the expression is a union(+). So we will introduce parallel edges (parallel self-loops here) for 'ab' and 'ba', as shown in Fig 9.
Fig 9B. Now we have two labels with concatenation operators (no operator mentioned between two variables is concatenation), so we remove them one by one by introducing new states, q1 and q2 as shown in Fig 10 and Fig 11. (Refer Fig 4 above)
Fig 10
Fig 11Step 3: As no operators are left, we can say that Fig 11 is the required finite automata (NFA).
Similar Reads
Design finite automata from regular expressions
Prerequisite - Finite automata, Regular expressions, grammar, and language. In this article, we will see some popular regular expressions and how we can convert them to finite automata (NFA and DFA). Let's discuss it one by one. Overview :Let a and b are input symbols and r is the regular expression
3 min read
Designing Finite Automata from Regular Expression (Set 6)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 5) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = a(a+b)* The languag
3 min read
Designing Finite Automata from Regular Expression (Set 7)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 6) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = b*aa(a+b)*+b*ab*aa(
3 min read
Designing Finite Automata from Regular Expression (Set 8)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 7) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = {an | n≥ 1} The
2 min read
Designing Finite Automata from Regular Expression (Set 4)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 3) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Regular language, L1 = (a+b)(a+b) The lang
3 min read
Designing Finite Automata from Regular Expression (Set 2)
Prerequisite: Finite automata, Regular expressions, grammar and language. Designing finite automata from Regular expression In the below article, we shall see some Designing of Finite Automata form the given Regular Expression- Regular Expression 1: Φ (Phi). The language of the given RE is L1 =
3 min read
Designing Finite Automata from Regular Expression (Set 1)
In this article, we will see some popular regular expressions and how we can convert them to finite automata. Even number of a's : The regular expression for even number of a's is (b|ab*ab*)*. We can construct a finite automata as shown in Figure 1. The above automata will accept all strings which h
3 min read
Designing Finite Automata from Regular Expression (Set 5)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 4) In the below article, we shall see some Designing of Non-deterministic Finite Automata form the given Regular Expression- As NFA can be changed to corresponding DFA. Re
3 min read
Designing Finite Automata from Regular Expression (Set 3)
Prerequisite: Finite automata, Regular expressions, grammar and language, Designing finite automata from Regular expression (Set 2) In the below article, we shall see some Designing of Finite Automata form the given Regular Expression. Regular Expression 1: 'ab*' ('a' followed by any number of 'b').
3 min read
Conversion of Epsilon-NFA to NFA
An NFA (Non-deterministic Finite Automaton) is a theoretical model used to recognize patterns and determine whether a string belongs to a specific language. An Epsilon-NFA (ε-NFA) is a special type of NFA where transitions can happen without reading any input symbol, using epsilon (ε) transitions.An
4 min read