I am trying to convert a regular expression into NFA and I am having trouble in it. If you are unaware of the subject then this is a link to what I am saying here.
The problem here is that the author explains that given a string of characters you first convert it into postfix. He mentions that in real time it would be better to draw the NFA while parsing the R.E but has given no such method to do so…..
I am having problems on starting this up. Can anyone please guide me on what should be the algorithm to create NFA while parsing the string because the parenthesis are a big problem as they are supposed to be done first……
PS:- I am actually not sure what other tags should be placed in this….Also this is NOT homework
Here’s a combined parser, NFA builder, and NFA interpreter in one page of Python. I hope I’m not spoiling the fun of figuring it out for yourself — you might prefer to wait and keep hacking before following the link.
It’s sort of like deinst’s suggestion, but ‘backwards’. As deinst says, you can have the parser build an NFA for each subexpression of the regular expression and then hook them up as you go. For instance, for
(a|b)*cyou’d first parse(a|b)*to get NFA #1, then parsecto get NFA #2, then clobber NFA #1’s final state, changing it to #2’s start state. And so on recursively. This is the conventional answer.My code instead first creates a trivial NFA with just an accepting state and nothing more. Then it parses
c, extending the NFA: now we have an NFA that checks forcand then accepts. Next, it recursively parses(a|b)*, still extending the NFA. The contract of the parser, given a stringreand an NFAk, is to parse the string to produce a result NFA that ends up at the start ofkwhenrematches. This approach avoids the need to clobber bits of the partial NFAs to hook them together.