I'm currently working with a DFA that requires a specific regular expression to validate a given string, which is (bab | bbb) (a* b*) (a* | b*) (ba)* (aba) (bab | aba)* bb (a | b)* (bab | aba) (a | b*)
My approach involves defining transitions to determine the validity of an inputted string:
class DFA_Exp1 {
constructor() {
// Transitions are defined within an object
this.transitions = {
0: { a: "invalid", b: 1 },
1: { a: 2, b: 2 },
2: { a: "invalid", b: 3 },
...
};
this.acceptingState = 17;
}
validateInput(input) {
let currentState = 0; // Start from initial state
for (let i = 0; i < input.length; i++) {
const symbol = input[i];
if (!this.transitions[currentState]) {
return "invalid";
}
currentState = this.transitions[currentState][symbol];
if (currentState === "invalid" || currentState === undefined) {
return "invalid";
}
}
if (currentState === this.acceptingState) {
return "valid";
}
return "invalid";
}
}
I've noticed that my current implementation does not seem to work as expected, as the currentState always remains at 0 and strings are constantly flagged as invalid.
I'm wondering if there might be a more effective way to validate strings or if there's an issue with my validateInput method?
(I'm still quite new to JavaScript and automata theory)