How can I build a regular expression that, using only the concatenate, union and star operations, over the alphabet {0,1}, describes the language "Every three consecutive characters contain at least two 1, and the input has length at least 3"? For instance 110011, 0101 and 11 should be refused. I was thinking on using the logic from this (incomplete) DFA, but I can't figure out how to get a regular expression that follows the rule. Thanks!
Asked
Active
Viewed 1,690 times
3

user1354784
- 309
- 2
- 13
-
1You can use this to transform a finite automaton to a regular expression. – Sep 25 '16 at 08:43
-
That automaton can't be correct since it accepts all strings of length one. – Raphael Sep 25 '16 at 11:59
-
That's why I stated it was incomplete, I ommited the successive transitions from the initial state that lead to one of the four states above. I just wanted to show the behaviour the regulard expression should have once it reaches that point. – user1354784 Sep 25 '16 at 15:18
-
@Camil all the examples I have seen using this method seem to be limited to a single accept state. – user1354784 Sep 25 '16 at 15:24
-
The first answer to that question says: "If you have several final states (or even initial states) then there is no simple way of merging these ones, other than applying the transitive closure method." One of the other answers to that question describes this method. – Sep 25 '16 at 15:38
-
1@Evil Sorry I forgot to specify "at least" two 1s, I will edit this right now – user1354784 Sep 25 '16 at 18:28
1 Answers
1
Assume that string $s$ is in $L$. We will look at the last two characters of the string:
00
, impossible, because this string could not be in $L$.01
, next character must be 1, new last two characters is11
.10
, next character must be 1, new last two characters is01
.11
, next character may be 1 or 0, new last two characters is10
or11
.
Note that no matter your current state, you will always pass through the 11
state within two steps. Assuming that $s$ ended with 11
, we get the following loop:
(1|(011))*
This will be the middle section of any string in $L$. All we need to do now is handle possible prefixes, making sure not to allow any with size < 3:
(111|011|1011|11011)
And finally, possible suffixes (note the empty union to express that the suffix is optional):
(|0|01)
Now all that's left is to concatenate them:
(111|011|1011|11011)(1|(011))*(|0|01)

orlp
- 13,386
- 1
- 24
- 40