# Program to construct DFA for Regular Expression C( A + B)+

In this article, we will be discussing how to construct a Deterministic Finite Automaton (DFA) for the Regular Expression C(A + B)+ using C++. We'll start by understanding the problem and the theory behind it, then we'll dive into the implementation and conclude with a relevant example to demonstrate its use.

## Understanding the Problem Statement

A Deterministic Finite Automaton (DFA) is a theoretical model of computation used in automata theory, a branch of theoretical computer science. It's one of the simplest types of automata and an essential concept in the study of compilers and parsers.

The task here is to program a DFA for the Regular Expression C(A + B)+. This expression can be interpreted as 'C' followed by one or more occurrences of either 'A' or 'B'. Our goal is to create a C++ program that will check if a given input string matches this Regular Expression.

## Theoretical Background

A DFA consists of a set of states and transitions between those states on input symbols. It starts from an initial state and reads the input symbols. For each input symbol, it transitions to a new state until all input symbols have been read. The DFA accepts the input if and only if it ends in a final (or accepting) state.

In this case, the DFA for the Regular Expression C(A + B)+ can be visualized as follows −

• Start state: q0

• Accepting state: q2

• Transitions −

• q0 on input 'C' goes to q1

• q1 on input 'A' or 'B' goes to q2

• q2 on input 'A' or 'B' stays at q2

### Example

Now let's implement this DFA in C++. Note that this program will only work with uppercase 'A', 'B', and 'C'.

#include <iostream>
#include <string>
using namespace std;

enum State { q0, q1, q2, q3 };

State getNextState(State currentState, char input) {
switch (currentState) {
case q0: return (input == 'C') ? q1 : q3;
case q1: return (input == 'A' || input == 'B') ? q2 : q3;
case q2: return (input == 'A' || input == 'B') ? q2 : q3;
default: return q3;
}
}

bool matchesRE(string s) {
State currentState = q0;
for (char c : s) {
currentState = getNextState(currentState, c);
}
return currentState == q2;
}

int main() {
string test = "CABAB";
cout << (matchesRE(test) ? "Matches Regular Expression" : "Does not match Regular Expression") << endl;
return 0;
}


### Output

Matches Regular Expression


## Test Case

Let's use the string "CABAB" as an example. This string starts with 'C' and is followed by a sequence of 'A's and 'B's. Therefore, it matches the Regular Expression C(A + B)+, and the output of the program will be: "Matches Regular Expression".

## Conclusion

In this article, we have taken a closer look at the theoretical model of computation, DFA, and its application in validating Regular Expressions. We have focused on the Regular Expression C(A + B)+ and created a C++ program to check if an input string matches this Regular Expression. We hope this discussion has been informative and has helped you understand DFAs and their implementation in C++ better.

Updated on: 18-May-2023

262 Views