测试两种常规语言的交集

发布于 2024-08-23 17:34:04 字数 634 浏览 8 评论 0原文

我想测试两种语言是否有共同的字符串。这两种语言都来自下面描述的常规语言的子集,我只需要知道两种语言中是否存在字符串,而不是生成示例字符串。

语言由类似 glob 的字符串指定

<代码>/foo/**/bar/*.baz

其中 ** 匹配 0 个或多个字符,* 匹配零个或多个字符不是 /,所有其他字符都是文字。

有什么想法吗?

谢谢, 迈克

编辑:

我实现了一些似乎表现良好的东西,但尚未尝试正确性证明。您可以看到 单元测试

I want to test whether two languages have a string in common. Both of these languages are from a subset of regular languages described below and I only need to know whether there exists a string in both languages, not produce an example string.

The language is specified by a glob-like string like

/foo/**/bar/*.baz

where ** matches 0 or more characters, and * matches zero or more characters that are not /, and all other characters are literal.

Any ideas?

thanks,
mike

EDIT:

I implemented something that seems to perform well, but have yet to try a correctness proof. You can see the source and unit tests

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

落花随流水 2024-08-30 17:34:04

为两种语言构建 FA AB,并构造“交集 FA”AnB。如果 AnB 至少有一个可从开始状态访问的接受状态,则存在一个同时使用两种语言的单词。

构造 AnB 可能很棘手,但我确信 FA 教科书会介绍它。我采取的方法是:

  • AnB 的状态分别是 AB 状态的笛卡尔积。 AnB 中的状态写作 (a, b),其中 aA中的状态bB 中的状态。
  • 转换 (a, b) ->r (c, d) (意思是,存在从 (a, b)(c, d) 符号 r 上的 存在,当且仅当 a ->r cA 中的转换,并且 b - >r dB 中的转换。
  • (a, b)AnB 中的开始状态,当且仅当 ab中的开始状态分别为 AB
  • (a, b)AnB 中的接受状态,当且仅当每个都是其各自 FA 中的接受状态。

这都是我的想法,因此完全未经证实!

Build FAs A and B for both languages, and construct the "intersection FA" AnB. If AnB has at least one accepting state accessible from the start state, then there is a word that is in both languages.

Constructing AnB could be tricky, but I'm sure there are FA textbooks that cover it. The approach I would take is:

  • The states of AnB is the cartesian product of the states of A and B respectively. A state in AnB is written (a, b) where a is a state in A and b is a state in B.
  • A transition (a, b) ->r (c, d) (meaning, there is a transition from (a, b) to (c, d) on symbol r) exists iff a ->r c is a transition in A, and b ->r d is a transition in B.
  • (a, b) is a start state in AnB iff a and b are start states in A and B respectively.
  • (a, b) is an accepting state in AnB iff each is an accepting state in its respective FA.

This is all off the top of my head, and hence completely unproven!

面犯桃花 2024-08-30 17:34:04

我刚刚进行了快速搜索,这个问题是可判定的(又名可以完成),但我不知道有什么好的算法可以做到这一点。一种解决方案是:

  1. 将两个正则表达式转换为 NFA A 和 B
  2. 创建一个 NFA C,它表示 A 和 B 的交集。
  3. 现在尝试从 0 到 C 中状态数的每个字符串,看看 C 是否接受它(因为如果字符串较长,则必须在某一点重复状态)。

我知道这可能有点难以理解,但这是我知道的唯一方法。

I just did a quick search and this problem is decidable (aka can be done), but I don't know of any good algorithms to do it. One is solution is:

  1. Convert both regular expressions to NFAs A and B
  2. Create a NFA, C, that represents the intersection of A and B.
  3. Now try every string from 0 to the number of states in C and see if C accepts it (since if the string is longer it must repeat states at one point).

I know this might be a little hard to follow but this is only way I know how.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文