Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NFA -> DFA transformation + optimizations in this domain #8

Open
MaxGraey opened this issue Jan 24, 2021 · 6 comments
Open

Add NFA -> DFA transformation + optimizations in this domain #8

MaxGraey opened this issue Jan 24, 2021 · 6 comments

Comments

@MaxGraey
Copy link
Contributor

Great article about this:
https://jasonhpriestley.com/regex-dfa

@ColinEberhardt
Copy link
Owner

That's a really interesting article - thanks.

@ColinEberhardt
Copy link
Owner

Just FYI - I've been exploring a multi-state NFA algorithm, which is conceptually similar to a DFA (it is in some senses an emulated DFA):

https://github.com/ColinEberhardt/assemblyscript-regex/tree/multi-state-NFA

However, it doesn't appear to be any faster in my benchmark tests!

@MaxGraey
Copy link
Contributor Author

As I understand NFA -> DFA not helps in some scenarios.

@ColinEberhardt
Copy link
Owner

I believe the principal advantage is that DFAs do not result in back-tracking. However, many simple regex's do not result in any significant backtracking. This is why I'm keen to add some more complex expressions to the benchmark test!

@MaxGraey
Copy link
Contributor Author

Also It seems this part could be a more effecient:

function addNextState(
  state: State,
  nextStates: State[],
  visited: State[]
): void {
  if (state.epsilonTransitions.length > 0) {
    for (let i = 0; i < state.epsilonTransitions.length; i++) {
      const st = state.epsilonTransitions[i];
      if (!visited.includes(st)) {
        visited.push(st);
        addNextState(st, nextStates, visited);
      }
    }
  } else {
    nextStates.push(state);
  }
}

with avoiding recursion and use stack / queue instead. Also visited better replace to Set instead array

@MaxGraey
Copy link
Contributor Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants