简体   繁体   English

为由 01 或 010 组成的语言设计正则表达式或有限自动机?

[英]Design a regular expression or Finite Automata for a language that consists of 01 or 010?

I have tried below question for designing automata but didn't success:我尝试过以下问题来设计自动机,但没有成功:

The set of strings that consists of either 01 repeated one or more times or 010 repeated one or more times.01重复一次或多次或010重复一次或多次组成的字符串集。

where string contains binary characters only.其中 string 仅包含二进制字符。 Please read question carefully.请仔细阅读问题。 Question taken from Automata Theory Language and Computation by john E.Hopcroft chapter 2. john E.Hopcroft 第 2 章取自 Automata Theory Language and Computation 的问题。

You can use ENFA(Finite automata with Eplison-Transition).您可以使用 ENFA(带有 Eplison-Transition 的有限自动机)。 You can give regular expression too.你也可以给出正则表达式。

The set of strings that consists of either 01 repeated one or more times or 010 repeated one or more times.由 01 重复一次或多次或 010 重复一次或多次组成的字符串集。

I'm pretty sure I know how to parse this correctly, but will provide notes on how to get the answer for both interpretations I can see (the second interpretation might be a bit of a stretch linguistically, but bear with me).我很确定我知道如何正确解析它,但会提供有关如何为我能看到的两种解释获得答案的注释(第二种解释在语言上可能有点牵强,但请耐心等待)。 The first parsing I see is this:我看到的第一个解析是这样的:

The set of strings that consists of either (01 repeated one or more times) or (010 repeated one or more times).由 (01 重复一次或多次) 或 (010 重复一次或多次) 组成的字符串集。

This one's easy;这个很简单; regular expressions are closed under union, which is what the "or" above represents, so we just need regular expressions for the parenthesized parts and we can get the complete expression from those.正则表达式在联合下是封闭的,这就是上面的“或”所代表的意思,所以我们只需要括号部分的正则表达式,我们可以从中得到完整的表达式。 The subexpressions are easy: repetition is represented by either the Kleene closure (*) or the commonly defined "at least once" operator (+).子表达式很简单:重复由 Kleene 闭包 (*) 或通常定义的“至少一次”运算符 (+) 表示。 Following this line of thought, the subexpressions are simply (01)+ and (010)+ and the complete solution is (01)+ + (010)+ (where + has two meanings: used as a unary operator, it's the "at least once" repetition operator, and where used as a binary operator it means union).按照这种思路,子表达式就是 (01)+ 和 (010)+,完整的解决方案是 (01)++ (010)+ (其中 + 有两个含义:用作一元运算符,它是“at至少一次”重复运算符,在用作二元运算符时,它表示联合)。

The second parsing I see is this:我看到的第二个解析是这样的:

The set of strings that consist of either 01 (repeated one or more times) or 010 (repeated one or more times).由 01 (重复一次或多次)或 010(重复一次或多次)组成的字符串集。

In other words, we can accept any string formed by repeating either 01 or 010, but we need to use at least one of these.换句话说,我们可以接受由重复 01 或 010 形成的任何字符串,但我们至少需要使用其中一个。 Since we can choose either one to repeat, and since under this interpretation mixing is allowed, we can take (01 + 010)+ as the regular expression.由于我们可以选择其中一个重复,并且由于在这种解释下允许混合,我们可以将 (01 + 010)+ 作为正则表达式。

Again, the first interpretation is almost certainly what is intended, if you copied the question word-for-word.同样,如果您逐字逐句复制问题,则几乎可以肯定第一种解释是预期的。

EDIT: The answer is desired for a third, even less plausible, interpretation: any string, so long as it contains either 01 repeated more than once or 010 repeated more than once.编辑:答案是第三种甚至更不合理的解释:任何字符串,只要它包含 01 重复不止一次或 010 重复不止一次。

To get 01 more than once: a = (0+1)* (01) (0+1)* (01)+ (0+1)* To get 010 more than once: b = (0+1)* (010) (0+1)* (010)+ (0+1)* To get either of the above: a + b多次获得 01: a = (0+1)* (01) (0+1)* (01)+ (0+1)*多次获得 010: b = (0+1)* (010) (0+1)* (010)+ (0+1)*得到以上任何一个: a + b

To get 01 at least once: c = (0+1)* (01)+ (0+1)* To get 010 at least once: d = (0+1)* (010)+ (0+1)* To get either of the above: c + d至少获得一次 01: c = (0+1)* (01)+ (0+1)*至少获得一次 010: d = (0+1)* (010)+ (0+1)*要获得上述任何一项: c + d

Note: unless you're a native speaker of English and are pretty confident about this interpretation after having discussed it with whomever came up with this assignment, this interpretation is pretty far-fetched - not quite as much as assuming "string" means the stuff cats play with, but getting there.注意:除非你是英语为母语的人,并且在与提出这个任务的任何人讨论后对这个解释非常有信心,否则这种解释是相当牵强的——不像假设“字符串”意味着这些东西那么多猫玩,但到达那里。

Here I used epsilon NFA.在这里,我使用了 epsilon NFA。 To construct the finite automata to answer this question.构造有限自动机来回答这个问题。

Here we need to construct the FSM which consists of 01 or 010 repeated one or more times.这里我们需要构造一个由 01 或 010 重复一次或多次组成的 FSM。

The regular expression satisfying the given condition is: (01+010)(01+010)*满足给定条件的正则表达式为:(01+010)(01+010)*

The ENFA to this question is designed below:这个问题的ENFA设计如下:

这个问题的ENFA就是在这里设计的。

Regular Expression that accepts one or more 01 is:接受一个或多个01的正则表达式是:

(0+1)*(01)(0+1)*

And the Regular Expression that accepts one or more 010 is:而接受一个或多个010的正则表达式是:

(0+1)*(010)(0+1)*

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

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