TOC-L02-Regular Expressions-S25
TOC-L02-Regular Expressions-S25
2/25/2025 1
Lecture 2
Regular Expressions
2/25/2025 2
Overview
2/25/2025 3
How can a language be defined?
A language can be defined by five methods:
2/25/2025 5
Regular Expressions
• L1 = {a, ab, abb, abbb, …} or simply (ab*)
• L2 = {Λ, ab, abab, ababab, …} or simply (ab)*
• L3= {Λ, a, b, aa, ab, bb, aaa, aab, abb, bbb, aaaa, …} or
simply (a*b*)
(a’s before b’s)
• Example:
= {a, b, c}
T = {a, c, ab, cb, abb, cbb, abbb, cbbb, …}
T = language((a+c)b*)
(defines the language whose words are constructed from
either a or c followed by some b’s)
2/25/2025 7
Regular Expressions
• L = {aaa, aab, aba, abb, baa, bab, bba, bbb}
all words of exactly three letters from the alphabet {a, b}
L = (a+b)(a+b)(a+b)
2/25/2025 8
Regular Expressions
• Definition: Given an alphabet Σ, the set of regular
expressions is defined by the following rules.
1. For every letter in Σ, the letter written in bold is a
regular expression. Λ is a regular expression.
2. If r1 and r2 are regular expressions, then so are:
1. (r1)
2. r1 r2
3. r1+r2
4. r1*
3. Nothing else is a regular expression.
2/25/2025 9
Regular Expressions
Remark: Notice that r1+ = r1r1*
r1=r2 if and only if language(r1) = language(r2)
Examples:
All words that have at least one a: (a+b)*a(a+b)*
abbaab: (Λ)a(bbaab) (abb)a(ab) (abba)a(b)
• Example: The language of all words that have exactly two a’s.
= b*ab*ab*
2/25/2025 11
Regular Expressions
Example: At least one a and at least one b?
• First solution:
(a+b)*a(a+b)*b(a+b)* + (a+b)*b(a+b)*a(a+b)*
• But (a+b)*a(a+b)*b(a+b)* expresses all words except
words of the form some b’s (at least one) followed by
some a’s (at least one). bb*aa*
• Second solution:
(a+b)*a(a+b)*b(a+b)* + bb*aa*
• Thus: (a+b)*a(a+b)*b(a+b)* + (a+b)*b(a+b)*a(a+b)*
= (a+b)*a(a+b)*b(a+b)* + bb*aa*
2/25/2025 12
Regular Expressions
• Example: The only words that do not contain both an a
and b in them are the words formed from all a’s or all b’s:
a*+b*
• Thus:
(a+b)* = (a+b)*a(a+b)*b(a+b)* + bb*aa* + a* + b*
2/25/2025 13
Regular Expressions
• Example: The language of all words formed from some
b’s (possibly 0) and all words where an a is followed by
some b’s (possibly 0):
{Λ, a, b, ab, bb, abb, bbb, abbb, bbbb, …}
b* + ab* (Λ + a)b*
• In general: concatenation is distributive over the +
operation.
r1(r2+ r3) = r1r2 + r1r3
(r1+ r2) r3 = r1r3 + r2r3
2/25/2025 14
Regular Expressions
• Two operations: language(s) language
If S and T are two languages from the same alphabet S,
1. S+T: the union of languages S and T defined as ST
2. ST: the product set is the set of words x written vw
with v a word in S and w a word in T.
2/25/2025 15
Rules define the language associated
with any regular expression
Rule 1: The language associated with a regular expression
that is just a single letter is that one-letter word
alone and the language associated with Λ is {Λ}.
L = {a} RE = a
L = {Λ} RE = Λ
2/25/2025 16
Rules define the language associated
with any regular expression
Rule 2: If L1 is the language associated with the regular
expression r1 and L2 is the language associated with
the regular expression r2, then:
(i) The language associated with the regular expression r1
times r2, that is:
language(r1r2) = L1L2
(ii) The regular expression r1+r2 is associated with the
language formed by the union of the sets L1 and L2.
language(r1 +r2) = L1+L2
(iii) The language associated with the regular expression
r1* is L1*, is the Kleene closure of the set L1:
language(r 1*) = L1*
2/25/2025 17
Rules define the language associated
with any regular expression
• Remark: For all regular expressions, there is some language
associated with it.
2/25/2025 19
Rules define the language associated
with any regular expression
• Kleene star applied to a subexpression with a star (*)
(a+b*)* (aa+ab*)*
(a+b*)* = (a+b)* (aa+ab*)* (aa+ab)* abb|abb
• (a*b*)*
The letter a and the letter b are in language(a*b*).
(a*b*)* = (a+b)*
2/25/2025 21
Examples:
• Language EVEN-EVEN defined by the expression:
2/25/2025 22
Thanks
2/25/2025 23