Lec 04-Regular Expression
Lec 04-Regular Expression
Example
– The second term, b*a* describes all the words that do not contain the
substring ab (i.e., all a’s, all b’s, Λ, or some b’s followed by some a’s).
Theory Of Automata 2
Example
• Hence,
(Λ + a)b* = b* + ab*
Theory Of Automata 5
Example
(a + aa + aaa)(bb + bbb)
= abb + abbb + aabb + aabbb + aaabb + aaabbb
Theory Of Automata 6
Example
language(r1r2) = L1L2
Theory Of Automata 9
Definition contd.
• Rule 2 (cont.):
• Proof
• This algorithm only works for finite languages because an infinite language
would become a regular expression that is infinitely long, which is
forbidden.
Theory Of Automata 12
How Hard It Is To Understand A
Regular Expression
Let us examine some regular expressions and
see if we could understand something about
the languages they represent.
Example
(λ + b)(ab)*(λ + a)
• Note that
(a + b*)* = (a + b)*
since the internal * adds nothing to the
language. However,
• Since both the single letter a and the single letter b are words
of the form a*b*, this language contains all strings of a’s and
b’s. That is,
(a*b*)* = (a + b)*
• This expression represents all the words that are made up of syllables of
three types:
type1 = aa
type2 = bb
type3 = (ab + ba)(aa + bb)*(ab + ba)
• All strings with an even number of a’s and an even number of b’s belong
to the language defined by E.
Theory Of Automata 18
Algorithms for EVEN-EVEN
• We want to determine whether a long string of a’s and b’s has the
property that the number of a’s is even and the number of b’s is even.
• Algorithm 1: Keep two binary flags, the a-flag and the b-flag. Every time
an a is read, the a-flag is reversed (0 to 1, or 1 to 0); and every time a b is
read, the b-flag is reversed. We start both flags at 0 and check to be sure
they are both 0 at the end.
• Algorithm 2: Keep only one binary flag, called the type3-flag. We read
letter in two at a time. If they are the same, then we do not touch the
type3-flag, since we have a factor of type1 or type2. If, however, the two
letters do not match, we reverse the type3-flag. If the flag starts at 0 and if
it is also 0 at the end, then the input string contains an even number of a’s
and an even number of b’s.
Theory Of Automata 19
• If the input string is
(aa)(ab)(bb)(ba)(ab)(bb)(bb)(bb)(ab)(ab)(bb)(b
a)(aa) then, by Algorithm 2, the type3-flag is
reversed 6 times and ends at 0.