#include <iostream>
#include <string>
using namespace std;
// Function to check if character is a letter
bool isLetter(char c) {
return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
// Function to check if character is a digit
bool isDigit(char c) {
return (c >= '0' && c <= '9');
// Transition function implementation
int trans(int q, char c) {
// State q1 (state 1 - start state)
if (q == 1) {
if (isLetter(c)) return 2; // δ(q1, L) = q2
if (isDigit(c)) return 3; // δ(q1, D) = q3
// State q2 (state 2 - accept state)
else if (q == 2) {
if (isLetter(c)) return 2; // δ(q2, L) = q2
if (isDigit(c)) return 2; // δ(q2, D) = q2
// State q3 (state 3 - reject state)
else if (q == 3) {
return 3; // δ(q3, L) = q3, δ(q3, D) = q3
return 3; // default reject state
// DFA simulation algorithm
string simulateDFA(string w) {
int q = 1; // q = q0 (start state)
// Process each character in the string
for (int i = 0; i < [Link](); i++) {
char c = w[i];
q = trans(q, c); // q = trans(q, c)
// Check if final state is in F (accept states)
if (q == 2) return "yes"; // q is in F
else return "no";
int main() {
string identifier;
cout <<
"===========================================\n";
cout << " C/C++ Identifier Validator using DFA\n";
cout <<
"===========================================\n";
cout << "Pattern: L(L* U D*)\n";
cout << "L = {a-z, A-Z}, D = {0-9}\n";
cout <<
"===========================================\n\
n";
// Test cases
string testCases[] = {
"aa", "aba", "abba", "abbba", // from pattern ab*a
"variable", "var1", "var123", "count2",
"1variable", "9abc", "123",
"x", "y2", "test_var", "_var",
"myVar", "Var1", "a1b2c3"
};
int numTests = sizeof(testCases) / sizeof(testCases[0]);
cout << "Testing specimen identifiers:\n";
cout << "-------------------------------------------\n";
for (int i = 0; i < numTests; i++) {
string result = simulateDFA(testCases[i]);
cout << "Input: \"" << testCases[i] << "\"";
cout << " \t--> " << result;
if (result == "yes") {
cout << " (Valid C/C++ identifier)";
} else {
cout << " (Invalid C/C++ identifier)";
cout << endl;
cout << "\n-------------------------------------------\n";
cout << "Enter identifier to validate (or 'quit' to exit):\n";
while (true) {
cout << "\nIdentifier: ";
cin >> identifier;
if (identifier == "quit" || identifier == "exit") {
cout << "Program terminated.\n";
break;
string result = simulateDFA(identifier);
cout << "Result: " << result;
if (result == "yes") {
cout << " - Valid C/C++ identifier" << endl;
} else {
cout << " - Invalid C/C++ identifier" << endl;
}
return 0;
```
### Specimen Results:
```
===========================================
C/C++ Identifier Validator using DFA
===========================================
Pattern: L(L* U D*)
L = {a-z, A-Z}, D = {0-9}
===========================================
Testing specimen identifiers:
-------------------------------------------
Input: "aa" --> yes (Valid C/C++ identifier)
Input: "aba" --> yes (Valid C/C++ identifier)
Input: "abba" --> yes (Valid C/C++ identifier)
Input: "abbba" --> yes (Valid C/C++ identifier)
Input: "variable" --> yes (Valid C/C++ identifier)
Input: "var1" --> yes (Valid C/C++ identifier)
Input: "var123" --> yes (Valid C/C++ identifier)
Input: "count2" --> yes (Valid C/C++ identifier)
Input: "1variable" --> no (Invalid C/C++ identifier)
Input: "9abc" --> no (Invalid C/C++ identifier)
Input: "123" --> no (Invalid C/C++ identifier)
Input: "x" --> yes (Valid C/C++ identifier)
Input: "y2" --> yes (Valid C/C++ identifier)
Input: "test_var" --> no (Invalid C/C++ identifier)
Input: "_var" --> no (Invalid C/C++ identifier)
Input: "myVar" --> yes (Valid C/C++ identifier)
Input: "Var1" --> yes (Valid C/C++ identifier)
Input: "a1b2c3" --> yes (Valid C/C++ identifier)
-------------------------------------------
Enter identifier to validate (or 'quit' to exit):
Identifier: sum
Result: yes - Valid C/C++ identifier
Identifier: 2fast
Result: no - Invalid C/C++ identifier
Identifier: total99
Result: yes - Valid C/C++ identifier
Identifier: quit
Program terminated.