eliben / code-for-blog

Code samples from my blog
The Unlicense
1.57k stars 744 forks source link

issue in regex_fsm #23

Closed gdshukla closed 4 years ago

gdshukla commented 4 years ago

I was trying code from regex_fsm and it failes to match if my pattern is "12ab*c" and input string is "12abbc1". To check if we have reached at the end of pattern, if modified DFA::simulate function as

int DFA::simulate(std::string input)
{
    uint32_t currState = start;
    int count = 0;
    for (auto i : input)
    {
        count++;        // count number of chars matched
        transition t = std::make_pair(currState, i);
        if (transTable.find(t) == transTable.end())
        {
            // added this if block
            if (final.find(currState) != final.end())
            {
                return count - 1;       // found
            }
            return -1;      // failure
        }
        currState = transTable[t];
    }
    if (final.find(currState) != final.end())
    {
        return count;       // found 
    }
    else
    {
        return -1;      // failure
    }
}

but it still fails as the transition for the last character in input string ie "1" is present in DFA::trans_table. Any suggestions? gdshukla@gmail.com

eliben commented 4 years ago

I don't think you're taking this code snippet from my repository -- where is it from?

In general, the code is for exact matches, not partial matches. I can't offer much support now - it's been a really, really long time since I worked on this.

gdshukla commented 4 years ago

I'm sorry. I modified some variable names to match my convention style. Anyways, I fixed the issue. Sorry to disturb you. And thanks for your code and ideas. Very helpful