简体   繁体   English

如果 NFA 和 DFA 是等价的,为什么我们会有两个用于正则表达式的引擎?

[英]If NFA and DFA were equivalent, why would we have two engines for regex?

From Mastering Regular Expressions 3e :掌握正则表达式 3e

As a result, broadly speaking, there are three types of regex engines:因此,从广义上讲,正则表达式引擎分为三种类型:

  • DFA (POSIX or not—similar either way) DFA(POSIX 与否 - 类似)
  • Traditional NFA (most common: Perl, .NET, PHP, Java, Python, . . . )传统 NFA(最常见:Perl、.NET、PHP、Java、Python、..)
  • POSIX NFA POSIX NFA

From the Theory of Computation: Formal Languages, Automata, and Complexity :计算理论:形式语言、自动机和复杂性

For each NFA, there is a DFA that accepts exactly the same language.对于每个 NFA,都有一个 DFA 接受完全相同的语言。

Can I argue that NFA and DFA are the same thing?我可以说 NFA 和 DFA 是一回事吗? Or even though their ability to recognize patterns are equivalent, but they are still different in some way?或者即使它们识别模式的能力是相同的,但它们在某些方面仍然不同?

There are two things you're missing:你缺少两件事:

  1. The "traditional NFA" implementations actually include abilities beyond the strict computer science definition of an NFA. “传统 NFA”实现实际上包括超出 NFA 的严格计算机科学定义的能力。

  2. Performance characteristics are a thing to care about, even given two implementations that cover the same answer.性能特征是一件需要关心的事情,即使给出了涵盖相同答案的两个实现。

The net effect is that the backtracking implementations (I like that name better than "traditional NFA") are slightly more expressive than DFA implementations because they can match regexes like (\\w{3,})\\1 , which matches three or more word characters repeated twice (something that can't be matched by a DFA).最终效果是回溯实现(我更喜欢这个名字而不是“传统 NFA”)比 DFA 实现更具表现力,因为它们可以匹配像(\\w{3,})\\1这样的正则表达式,它匹配三个或更多单词重复两次的字符(DFA 无法匹配的内容)。 At the same time, DFA implementations are guaranteed O(n) in input length, but it is very easy to write regexes that have O(n^2) or worse behavior when presented with a string they don't match.同时,DFA 实现的输入长度保证为 O(n),但很容易编写具有 O(n^2) 或更差行为的正则表达式,当呈现出与它们不匹配的字符串时。 (See https://swtch.com/~rsc/regexp/regexp1.html ) (见https://swtch.com/~rsc/regexp/regexp1.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM