This is my automata, and it Regular Expression for this language is (aaa*b|ba)a*
I want to make a C++ program for check input string is accepted by this language or not.
This program get the string and print Accepted
or Rejected
For example:
input: aaaba - output: Accepted
input: baa - output: Accepted
input: aaa - output: Rejected
#include <string>
#include <iostream>
bool check_string(const std::string& s) {
static constexpr int INV = -1;
static constexpr int INITIAL_STATE = 0;
static constexpr int ACCEPTING_STATE = 3;
static const int transition_table[5][2] = {
// a b
{ 1, 2 }, // 0
{ 4, INV }, // 1
{ 3, INV }, // 2
{ 3, INV }, // 3
{ 4, 3 } // 4
};
int state = INITIAL_STATE;
for (char c : s) {
if (c != 'a' && c != 'b') return false;
state = transition_table[state][c - 'a'];
if (state == INV) return false;
}
return state == ACCEPTING_STATE;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: check str\n";
return 1;
}
if (check_string(argv[1]))
std::cout << "Accepted\n";
else
std::cout << "Rejected\n";
return 0;
}