简体   繁体   English

正则表达式和自动机

[英]Regular expressions and automata

I'm studying Regular Expressions by reading Aho's book. 我正在通过阅读Aho的书来学习正则表达式。 I don't understand two of the statements in the book: 我不理解本书中的两个陈述:

Question A: 问题A:

1(0+1)*1 + 1 : denotes the set of all strings beginning and ending with a 1.

My question why is +1 added at end of the regular expression? 我的问题为何在正则表达式的末尾加上+1 Shouldn't 1(0+1)*1 be sufficient? 1(0+1)*1是否足够?


I'm also having trouble with the following: 我在以下方面也遇到了麻烦:

Question B: 问题B:

The set of strings containing only 0's and 1's that have atmost one 1 as below 仅包含0和1且最多具有1的字符串集,如下所示

    0*+0*10* 

Can you explain how the solution 0*+0*10* is arrived at, step by step? 您能否一步一步解释解决方案0*+0*10*是如何到达的?

As to question a: 1(0+1)*1 does not match the one-character string 1, which begins and ends with 1. One needs a special case for it, which the example does. 关于问题a:1(0 + 1)* 1与以1开始和结束的单字符字符串1不匹配。示例需要这样做,它需要一个特殊的情况。

As to question b: I cannot speak for the author. 关于问题b:我不能代表作者发言。 However... Any string that contains at most one 1 is a string that either has no 1s or has exactly one 1. Assuming that the alphabet is {0,1}, the former means any string that contains zero or more 0s, that is, 0*. 但是...任何最多包含1的字符串是不包含1或正好为1的字符串。假定字母为{0,1},则前者表示包含0或多个0的任何字符串,即是0 *。 The latter, with the same assumption, means any string that contains zero or more 0s followed by one 1 followed by zero or mpre 0s, that is, 0*10*. 后者具有相同的假设,表示包含零个或多个0,后跟一个1,后跟零或mpre 0,即0 * 10 *的字符串。 Combining these yields the example. 将这些结合起来便产生了示例。

For Question a : 1(0+1)*1 denotes set of all strings beginning and ending with one but does not contain string 1 which has length one and starts and ends with one. 对于问题a1(0+1)*1表示所有以1开始和结束但不包含长度为1且以1开始和结束的字符串1(0+1)*1的所有字符串的集合。

For Question b : Set of strings containing atmost one 1 = A + B where A is set of all strings containing zero 1 s and B is the set of all strings containing exactly one 1 对于问题b :包含至少一个1的字符串的集合= A + B其中A是包含零个1 s的所有字符串的集合,而B是包含恰好一个1的所有字符串的集合。

So A is 0* and B is 0*10* Hence we get the answer as 0* + 0*10* 所以A是0*而B是0*10*因此我们得到的答案是0* + 0*10*

For the first example, the string that is accepted by the + 1 but not by the rest is 1 . 对于第一个示例,+ 1接受但其余部分不接受的字符串是1 The rest of the expressions can handle 11, but not a string where the first and last character are the same. 其余表达式可以处理11,但不能处理第一个和最后一个字符相同的字符串。

It's similar reasoning for the second string - 0* handles strings of all zeroes, 0*10* handles strings of 1 one. 第二个字符串的原因与此类似-0 *处理全零的字符串,0 * 10 *处理1的字符串。

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

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