I am trying to construct a regular expression that the total number of a's is divisible by 3 no matter how they are distributed. aabaabbaba. This is What i came up with:
b*ab*ab*
Now, someone told me i could do it this way
(b*ab*ab*)*
Why would i need to enclose it and why is the outside kleene star needed?
Wouldnt the outside kleene distribute among all the a's and b's inside the parenthesis? if thats the case then what would a double kleene mean?
For the number of 'a's to be divisible by three, you'll need three 'a's in your expression. So the correct expression is:
(b*ab*ab*ab*)*
This expression is saying 'a' three times, with possible 'b's in the middle. The last star says repeat (the whole parenthesized expression) as necessary.
The outer * repeats the entire sequence zero or more times.
In other words, zero or more substrings that match b*ab*ab*.
Related
I need to write regular expression for language {0,1} with no more than one zero in a row.
The regex expression would be:-
0?(1+0)*1*
Since, there can be a single zero at the start, so we have 0?, meaning it either present singly or absent.
The next (1+0) means, we see a series of (at least one) 1's followed by a single zero. This whole group of a zero following a series of 1's can be repeated multiple times.
And, we also add 1*, at the end to avoid our pattern to always detect the required strings but always ending in zero.
I have this following questing in regular expression and I just can't get my head around these kind of problems.
L1 = { 0n1m | n≥3 ∧ m is odd }
How would I write a regular expression for this sort of problem when the alphabet is {0,1}.
What's the answer?
The regular expression for your example is:
000+1(11)*1
So what does this do?
The first two characters, 00, are literal zeros. This is going to be important for the next point
The second two characters, 0+, mean "at least one zero, no upper bound". These first four characters satisfy the first condition, which is that we have at least three zeros.
The next character, 1, is a literal one. Since we need to have an odd number of ones, this is the smallest number we're allowed to have
The last-but-one characters, (11), represent a logical grouping of two literal ones, and the ending * says to match this grouping zero or more times. Since we always have at least one 1, we'll always match an odd number. So we're done.
How'd I get that?
The key is knowing regular expression syntax. I happen to have quite a bit of experience in it, but this website helped me to verify.
Once you know the basic building blocks of regex, you need to break down your problem into what you can represent.
For example, regex allows us to specify a lower AND upper bound for matching (the {x,y} syntax), but doesn't allow to specify just a lower bound ({x} will match exactly x times). So I knew I would have to use either + or * to specify the zeros, as those are the only specifiers that permit an infinite number of matches. I also knew that it didn't make sense to apply those modifiers to a group; the restriction that we must have at least 3 zeroes doesn't imply that we must have a multiple of three, for example, so (000)+ was out. I had to apply the modifier to only one character, which meant I had to match a few literals first. 000 guarantees matching exactly three 0s, and 0* (Final expression 0000*) does exactly what I want, and then I condensed that to the equivalent 000+.
For the second condition, I had to think about what an odd number is. By definition, an odd number can be expressed by 2*k + 1, where k is an integer. So I had to match one 1 (Hence the literal 1), and some number of the substring 11. That led me to the group, and then the *. On a slightly different problem, you could write 1(11)+ to match any odd number of ones, and at least 3.
1 A colleague of mine pointed out to me that the + operator isn't technically part of the formal definition of regular expressions. If this is an academic question rather than a programming one, you might find the 0000* version more helpful. In that case, the final string would be 0000*1(11)*
I am trying to describe regular expression in English here,
and let's say we have for (b(bb)*)*
you would say: zero or more b's
or we can have (a(aa)*b(bb)*)*
you would say: odd number of a's that end in odd number of b's
now my question is about ((a+b)a)*
you would say: words of even length where every even letter is an 'a'
where did the even length come from ??? how did they get every even letter is an 'a' ? is it from the zero a's because zero is an even number ?
((a+b)a)*
"you would say: words of even length where every even letter is an 'a'"
This is not a correct description. More accurate would be "words that have at least one a, followed by exactly one b, followed by exactly one a, zero or more times"
(+ means "one or more", * means "zero or more".)
It's more about the back and forth of as and bs--there could be million as between the bs, but there's never two bs next to each other.
And note that the inner parenthesis are not needed. In other words, this is equivalent:
(a+ba)*
Free spaced:
(a+ //"a", one or more times
b //followed by exactly one "b"
a //followed by exactly one "a"
)* //zero or more times
The only way your description is accurate is if you interpret the expression a+b as a or b. Most regular expression tools write this using a vertical bar, as in a|b. The other commenters and answerers have interpreted the + as a postfix operator meaning "one or more".
Using that reading, the reason every string in this set must be of even length is because the repetition comes outside a string of length 2. It means "zero or more copies of either aa or ba". Clearly every word matching that description is of even length. 0 is even by definition, and every second letter has to be an a.
{ ¢, aa, ba, aaaa, aaba, baaa, baba, ... }
OP, your question is based on curriculum from lecture and text where + is intended as an OR operation. The text and lecture material makes very clear what the notation means...
(b(bb)*)* = 0-n b's followed by 0-n bb's. in other words, zero or more b's.
(a(aa)*b(bb)*)* follows the pattern 2n + 1 a's following with 2n + 1 b's, if
not an empty string.
((a+b)a)* is more ambiguous i.e. more things could be said about it than the
answer, but the answer cannot be said to be wrong either. It is all
words of even length composed of all a's, or a's and b's. My guess
is that this answer would have gotten partial credit, and full
credit for including the part about a's being the even letter.
I was told that the language generated by the regular expression:
(a*b*)*
is regular.
However, my thinking goes against this, as follows. Can anyone please provide an explanation whether I'm thinking right or wrong?
My Thoughts
(a*b*) refers to a single sequence of any amount of a, followed by any amount of b (can be empty). And this single sequence (which can't be changed) can be repeated 0 or more time. For example:
a* = a
b* = bbbb
-> (a*b*) = abbbb
-> (a*b*)* = abbbbabbbbabbbb, ...
On the other hand, since aba is not an exact repetition of the sequence ab, it is not included in the language.
aaabaaabaaab => is included in the language
aba => is not included in the language
Thus, the language consists of sequences that are an arbitrary-time repetition of a subsequence that is any amount of a followed by any amount of b. Therefore, the language is not regular since it requires a stack.
It's a zero or more times, followed by b zero or more times, repeated zero or more times.
""
"a"
"b"
"ab"
"ba"
"aab"
"bbabb"
"aba"
all pass.
* is not +.
aba is in that language; it's just an overly-complicated way to say "the set of all strings consisting of as and bs".
EDIT: The repeating group doesn't mean that the contents of the group must be repeated exactly; that would require a backreference. ((a*b*)?\1*)
Rather, it means that the group itself should be repeated, matching any string that it can match.
Technically /(a*b*)*/ will match everything and nothing.
Because all the operators are *'s it means zero or more. So since zero is an option, it will pretty much match anything.
It's wrong, you don't need a stack. Your DFA just thinks "can I add just another a (or not)?" or "can I add just another b (or not)?" in an endless loop until the word is consumed.
It is a regular expression, yes.
The * say something like "can repeat 0 or more times". The + is basically similar, different only that it need one repeatition on minimal (or be 1 or more times).
This regular expressions says, somethink like:
Repeat "below group" zero or more times;
Repeat a zero or more times;
Repeat b zero or more times;
Can works fine with all of your examples.
Edit/Note: the aba is validated too.
I hope to help :p
Basically, it'll match any string thats empty or made by a bunch of a and b. It reads:
(('a' zero or + times)('b' zero or + times) zero of plus times
That's why it matches aba:
(('a' one time)('b' one time)) one time ((a one time)(b zero time)) one time
You're wrong. :)
0 is also an amount, so aba is in this language. It wouldn't be if the regex was (a+b+)+, because + would mean '1 or more' where * means '0 or more'.
I am really stuck with these 2 questions for over 2 days now. I'm trying to figure out what the question means. My tutor is out of town too.
Question 1: Write a regular expression for the only strings that are not generated over {a,b} by the expression: (a+b)****a(a+b)****. Explain your reasoning.
And I tried the second question. Do you think is there any better answer than this one?
What is a regular expression of a set of strings that contain an odd number of as or exactly two bs (a((a|b)(a|b))****|bb) I know to represent any odd length of a's, the RE is a((a|b)(a|b))****
Here's a start for the first question. First consider the strings that this regular expression generates:
(a+b)*a(a+b)*
It must begin with a AND
Every b must have at least one an a immediately before it AND
There must either be an aab or else the string must end in a.
The inverse of this is:
It must not begin with a OR
There is at least one b not after an a OR
The string consists only of repetitions of ab.
For the second question you should check that you have understood the question correctly. Your interpretation seems to be:
What is the regular expression for the set of strings that contain either (an odd number of a's and any number of b's) or (exactly two b's and no a's).
But another interpretation is this:
What is the regular expression for the set of strings that contain either (an odd number of a's and any number of b's) or (exactly two b's and any number of a's).
To match two a's you would use something like aa right?
Now we know that the + is a quantifier for 1 or more and the * is a quantifier for 0 or more. So if we want to repeat that entire pattern, we can put it in a group and repeat the entire pattern like so: (aa)+.
That would match:
aa
aaaa
But not:
a (because aa requires at least 2 items)`
aaa (because aa will match the first two, but you'll have an extra a)
And if we want to make that odd an even, we can simply add one extra a outside of the group like so: a(aa)+. However, since we wanted an odd amount without a specific minimum we shouldn't use + since that will require atleast 3 a's.
So the entire answer would be: (bb|a(aa)*)
It sounds like the first question is asking you to write a regular expression for the set of strings that do not match the provided regular expression.
For instance, suppose the question was asking for a regular expression for the set of strings not matched by aa+ over {a}. Well, here are a few strings that do match:
'aa'
'aaaa'
'aaaaa'
What are some strings that do not match? Here are the only two:
''
'a'
A regex for the latter set is a?.
Regarding the second question, I would suggest drumming up some positive and negative test cases. Run some strings like this through your regex and see what happens:
'a' (should pass)
'aaa' (should pass)
'bb' (should pass)
'' (should fail)
'aa' (should fail)
'aba' (should fail)
Good luck!
The expression (a+b)*a(a+b)* just means: there has to be an a inside the string. The only strings that cant be generated by this expression are those: b*
This expression means that RE must contain Atleast 1 'A' in the expression.
this expression doest not accept
'b'
'b'*
or
Empty set