0% found this document useful (0 votes)
16 views23 pages

TOC-L02-Regular Expressions-S25

The document provides an overview of regular expressions as a method to define languages in the context of computing. It outlines the rules for constructing regular expressions, examples of language definitions, and operations involving languages such as union and concatenation. Additionally, it discusses the equivalence of regular expressions and the application of the Kleene star in defining languages.

Uploaded by

Omar Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views23 pages

TOC-L02-Regular Expressions-S25

The document provides an overview of regular expressions as a method to define languages in the context of computing. It outlines the rules for constructing regular expressions, examples of language definitions, and operations involving languages such as union and concatenation. Additionally, it discusses the equivalence of regular expressions and the application of the Kleene star in defining languages.

Uploaded by

Omar Mohamed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

CS416 Theory of Computing

Assoc. Prof. Osama Fathy

2/25/2025 1
Lecture 2

Regular Expressions

2/25/2025 2
Overview

• How can a language be defined?


• Regular Expression Definition
• Rules on Regular Expressions
• Examples

2/25/2025 3
How can a language be defined?
A language can be defined by five methods:

(1) State all words of the language, e.g


L1 = {a, ab, abb, ……..}

(2) State English phrase of the language, e.g


The language of all the strings that contain an
even number of a’s and an even number of b’s.

(3) Regular Expression


(4) Graphs (FA, NFA, TG, GTG, PDA, TM, …)
(5) Grammer
2/25/2025 4
Regular Expressions
A new method to define languages
• alphabet  language
∑ = {x} ∑* = {Λ, x, xx, xxx, …}
or directly {x}* = {Λ, x, xx, xxx, …}
• language  language
R = {xx, xxx} R* = {Λ, xx, xxx, xxxx, …}
or directly {xx, xxx}* = {Λ, xx, xxx, xxxx, …}
• “letter”  language
x* (written in bold)
language(x*) = {Λ, x, xx, xxx, …}
or informally x* = {Λ, x, xx, xxx, …}

2/25/2025 5
Regular Expressions
• L1 = {a, ab, abb, abbb, …} or simply (ab*)
• L2 = {Λ, ab, abab, ababab, …} or simply (ab)*

Several ways to express the same language


• {x, xx, xxx, xxxx, …}
xx* x+ xx*x* x*xx* (x+)x* x*(x+) x*x*xx*

• L3= {Λ, a, b, aa, ab, bb, aaa, aab, abb, bbb, aaaa, …} or
simply (a*b*)
(a’s before b’s)

Remark: language(a*b*)  language((ab)*)


2/25/2025 6
Regular Expressions
• A useful symbol to simplify the writing:
– x+y choose either x or y

• 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)

• (a+b)* all words formed from alphabet {a,b}


• a(a+b)* = ?
• a(a+b)*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)

 Words with no a’s? b*

 All words formed from {a,b}?


(a+b)*a(a+b)* + b*

Thus: (a+b)* = (a+b)*a(a+b)* + b*


2/25/2025 10
Regular Expressions
• Example: The language of all words that have at least two a’s.
(a+b)*a(a+b)*a(a+b)*
= b*ab*a(a+b)*
= (a+b)*ab*ab*
= b*a(a+b)*ab*

• 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

• Example: (a+c)b* = ab*+cb*

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 ST
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.

• Example: S = {a, bb} T = {a, ab}


ST = {aa, aab, bba, bbab}

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.

• Let L be a finite language. There is a regular expression that


defines it.

• Algorithm (and proof)


Write each letter in L in bold, and write a (+) between the
regular expressions

• Example: L = {baa, abbba, bababa}


baa + abbba + bababa
2/25/2025 18
Rules define the language associated
with any regular expression
• The regular expression that is defined by this algorithm is
not necessarily unique.

Example: L = {aa, ab, ba, bb}


aa + ab + ba + bb or (a+b)(a+b)

Remark: This algorithm does not work for infinite languages.


Regular expressions must be finite, even if the
language defined is infinite.

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)*

• Is it possible to determine if two regular expressions are


equivalent?
– With a set of algebraic rules? Unknown.
– With an algorithm? Yes.
2/25/2025 20
Examples:
 Words with a double letter: (a+b)*(aa+bb)(a+b)*

 Words without a double letter: (ab)*


 Words that begin with b or end with a:
(Λ+b) (ab)* (Λ+a)

 (a+b)*(aa+bb)(a+b)* + (Λ+b)(ab)*(Λ +a)

2/25/2025 21
Examples:
• Language EVEN-EVEN defined by the expression:

(aa + bb + (ab + ba)(aa+bb)*(ab + ba))*

Every word in EVEN-EVEN has an even number of a’s and b’s.

Every word that contains an even number of a’s and b’s is a


member of EVEN-EVEN.

2/25/2025 22
Thanks

2/25/2025 23

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy