Search code examples
finite-automataformal-languagesnfa

Is this correct NFA graph?


Task: Build NFA from a given regular expression.

I decided to push some of my old programs to GitHub. Specifically problems regarding Theory of formal languages. After testing code I had this result and I can't really tell if this a wrong or correct output. It is kindaaa looks right but not something Thompson's algo would output. Also those little loops look suspicious. They basically do nothing though. Input example and graph that I get


Solution

  • Definitely wrong.

    The epsilon-self-loops look to me like a bug in the handling of the union operator. There should be an epsilon transition from each end state in the union to a new end state, so my guess is that you have mixed up the epsilon links. I'm not sure how you end up with the correct epsilon transition on a in one case and b in the other, so perhaps the bug is more complicated.

    You're right that in this case, there is no harm in the epsilon self-loop. But it is quite possible that the absence of an epsilon link from the end of the union leg to the union's end state will cause a problem with (a*|b) or (a|b*). One of those might actually turn out to recognize (a|b)+.

    Also, your Kleene star implementation does not allow zero repetitions. What you have is (a|b)+, not (a|b)*, because there is no epsilon transition from the start state to the state of the star subconstruction.