SlideShare une entreprise Scribd logo
1  sur  42
Dr. Hussien Sharaf
Computer Science Department
dr.sharaf@from-masr.com
Dr.Hussien Sharaf 2
Regular Expressions
A RE is a language for describing simple languages
and patterns.
Algorithm for using RE
1. Define a pattern: S1StudentnameS2
2. Loop
Find next pattern
Store StudentName into DB or encrypt StudentName
Until no match
REs are used in applications that involve file parsing
and text matching.
Many Implementations have been made for RE.
Dr.Hussien Sharaf 3
RE by C++ Boost library
//Pattern matching in a String
// Created by Flavio Castelli <flavio.castelli@gmail.com>
// distrubuted under GPL v2 license
#include <boost/regex.hpp>
#include <string>
int main() {
boost::regex pattern
("bg|olug",boost::regex_constants::icase|
boost::regex_constants::perl);
std::string stringa ("Searching for BsLug");
if (boost::regex_search (stringa, pattern,
boost::regex_constants::format_perl))
printf ("foundn");
else printf("not foundn");
return 0; }
Dr.Hussien Sharaf 4
Please check
[http://flavio.castelli.name/regexp-with-boost]
RE by C++ Boost library//Substitution
// Created by Flavio Castelli <flavio.castelli@gmail.com>
// distrubuted under GPL v2 license
#include <boost/regex.hpp>
#include <string> int main()
{boost::regex pattern
("bok",boost::regex_constants::icase|
boost::regex_constants::perl);
std::string stringa ("Searching for bok");
std::string replace (“book");
std::string newString;
newString = boost::regex_replace (stringa,
pattern, replace);
printf("The new string is:
|%s|n",newString.c_str());
return 0; }
Dr.Hussien Sharaf 5
Please check
[http://flavio.castelli.name/regexp-with-boost]
RE by C++ STL needs extra feature pack
#include <regex>
#include <iostream>
#include <string>
bool is_email_valid(const std::string& email) {
// define a regular expression
const std::tr1::regex
pattern("(w+)(.|_)?(w*)@(w+)(.(w+))+");
// try to match the string with the regular expression
return std::tr1::regex_match(email, pattern); }
Dr.Hussien Sharaf 6
Please check
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339
RE by C++ STL
#include <regex>
#include <iostream>
#include <string>
bool is_email_valid(const std::string& email) {
// define a regular expression
const std::tr1::regex
pattern("(w+)(.|_)?(w*)@(w+)(.(w+))+");
// try to match the string with the regular expression
return std::tr1::regex_match(email, pattern);
}
Dr.Hussien Sharaf 7
Please check
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339
RE by C++ STL
std::string str("abc + (inside brackets)dfsd");
std::smatch m;
std::regex_search(str, m, std::regex(“b"));
if (m[0].matched)
std::cout << "Found " << m.str(0) << 'n';
else
std::cout << "Not foundn“;
Dr.Hussien Sharaf 8
Please check
http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339
Other code samples
Other samples can be check at:
Boost Library
1. http://stackoverflow.com/questions/5804453/c-regular-expressions-
with-boost-regex
2. http://www.codeproject.com/KB/string/regex__.aspx
3. http://www.boost.org/doc/libs/1_43_0/more/getting_started/window
s.html#build-from-the-visual-studio-ide
STL Library
1. http://www.codeproject.com/KB/recipes/rexsearch.aspx
2. http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339
[recommended]
3. http://www.microsoft.com/download/en/confirmation.aspx?id=6922
[feature pack VS2008 Pack]
4. http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures-
Stephan-T-Lavavej-Standard-Template-Library-STL-8-of-n [Video]
Dr.Hussien Sharaf 9
RE Rules -1
1. Let ∑ ={x}, Then L = ∑*
In RE, we write x*
2. Let ∑ ={x, y}, Then L = ∑*
In RE, we write (x+y)*
3. Kleene’s star “*” means any combination of letters
of length zero or more.
Dr.Hussien Sharaf 10
RE Rules -2
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*
NOTE: r1
+
is not a RE
3. Nothing else is a regular expression.
Dr.Hussien Sharaf 11
RE-1
Example 1
∑ ={a, b}
- Formally describe all words with a followed by any
number of b’s
L = a b*
= ab*
- Give examples for words in L
{a ab abb abbb …..}
Dr.Hussien Sharaf 12
RE-2
Example 2
∑ ={a, b}
- Formally describe the language that contains nothing
or words starts with a where any a must be followed
by one or more b’s
L = (abb*)*
OR (b+ab)*
- Give examples for words in L
{Λ ab abb abababb …..}
Dr.Hussien Sharaf 13
RE-3
Example
∑ ={a, b}
- Formally describe all words with a followed by one or
more b’s
L = a b+
= abb*
- Give examples for words in L
{ab abb abbb …..}
Dr.Hussien Sharaf 14
RE-4
Example
∑ ={a, b, c}
- Formally describe all words that start with an a
followed by any number of b’s and then end with c.
L = a b*
c
- Give examples for words in L
{ac abc abbc abbbc …..}
Dr.Hussien Sharaf 15
RE-5
Example
∑ ={a, b}
- Formally describe all words where a’s if any come
before b’s if any.
L = a*
b*
- Give examples for words in L
{Λ a b aa ab bb aaa abb abbb bbb…..}
NOTE: a*
b*
≠ (ab)*
because first language does not contain abab but second language
has.
Once single b is detected then no a‘s can be added
Dr.Hussien Sharaf 16
RE-6
Example
∑ ={a}
- Formally describe all words where count of a is odd.
L = a(aa)*
OR (aa)*
a
- Give examples for words in L
{a aaa aaaaa …..}
Dr.Hussien Sharaf 17
RE-7.1
Example
∑ ={a, b, c}
- Formally describe all words where single a or c
comes in the start then odd number of b’s.
L = (a+c)b(bb)*
- Give examples for words in L
{ab cb abbb cbbb …..}
Dr.Hussien Sharaf 18
RE-7.2
Example
∑ ={a, b, c}
- Formally describe all words where single a or c
comes in the start then odd number of b’s in case of
a and zero or even number of b’s in case of c .
L = ab(bb)*
+c(bb)*
- Give examples for words in L
{ab c abbb cbb abbbbb …..}
Dr.Hussien Sharaf 19
RE-8
Example
∑ ={a, b, c}
- Formally describe all words where one or more a or
one or more c comes in the start then one or more
b’s.
L = (a+c) +
b+
= (aa*+cc*) bb*
- Give examples for words in L
{ab cb aabb cbbb …..}
Dr.Hussien Sharaf 20
RE-9
Example
∑ ={a, b}
- Formally describe all words with length three.
L = (a+b) 3
=(a+b) (a+b) (a+b)
- List all words in L
{aaa aab aba baa abb bab bba bbb}
- What is the count of words of length 4?
16 = 24
- What is the count of words of length 44?
244
Dr.Hussien Sharaf 21
Dr.Hussien Sharaf 22
RE-10.1
Example
∑ ={a, b}, What does L describe?
- L = (a+b)*
a(a+b) *
Dr.Hussien Sharaf 23
Any string of a's and b's
Single
a
Any string of a's and b's
- Give examples for words in L
{a ab aab bab abb …..}
RE-10.2
abbaab can parsed in 3 ways
Dr.Hussien Sharaf 24
Λ a bbaab
a
bbaab
abb a
ab
abb a
ab abba a b
abba a
b
RE-11
Example Page 38 Cohen
∑ ={a, b}
- Formally describe all words with at least two a's.
1) L = b*ab*a(a + b)*
Start with a jungle of b's (or no b's) until we find
the first a, then more b's (or no b's), then the
second a, then we finish up with anything.
- Give examples for words in L
{abbbabb aaaaa bbbabbbbabab…..}
Dr.Hussien Sharaf 25
RE-12
Example
∑ ={a, b}
- Formally describe all words with exactly two a's.
1) L = b*ab*ab*
- Give examples for words in L
{aab, baba, and bbbabbbab …..}
To make the word aab, we let the first and
second b* become Λ and the last becomes b
Dr.Hussien Sharaf 26
RE-13.1
Example
∑ ={a, b}
- Formally describe all words with least one a and least
one b.
1) L = (a + b)*a(a + b)* b(a + b)*
= (anything) a(anything) b(anything)
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*
Dr.Hussien Sharaf 27
RE-13.2
2) L =(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*
Notice that it is necessary to write bb*aa* because
b*a* will admit words we do not want, such as aaa.
Does this imply that
(a+b)*b(a+b)*a(a+b)*= bb*aa*??
False Left side includes the word aba, which the
expression on the right side does not.
Dr.Hussien Sharaf 28
Dr.Hussien Sharaf 29
Regular Languages
A language that can be defined by a RE is called a
regular language.
1. If L1 and L2 are regular languages then
L1 + L2 , L1 L2 and L1
* are also regular languages.
2. If L is a regular language then L’ (L complement)
is also a regular language.
L’ : is the language that is not accepted by r where r
is the RE that accepts language L.
Note: (L’)’ =L and (r’)’ =r
Dr.Hussien Sharaf 30
Regular Languages (cont.)
3. If L1 and L2 are regular languages then
L1 ∩ L2 is also a regular language.
L1 =words starting with a
r1 =a(a+b)*
L2 =words ending with a
r2 = (a+b)*a
L3 =L1 ∩ L2 = a(a+b)*a
L3 = words that start and end with a.
Dr.Hussien Sharaf 31
Assignment 3
1. Proof that: If L1 and L2 are regular languages then
L1 + L2 , L1 L2 and L1
* are also regular languages.
2. State which type of proofs you have used.
Dr.Hussien Sharaf 32
How powerful is RE?
RE can not express some languages such as
plaindrome strings an
bn
.
How can we prove that a language L is not regular?
Prove that there is no FA or RE that accepts L
Solution: use a Lemma called pumping Lemma.
It depends on pumping a string into a template. If the
string could not fit into the template then the string is
not an RE.
Dr.Hussien Sharaf 33
Formal Definition ”The pumping lemma for regular languages is a lemma
that makes use of a property shared by all regular languages RL. The
property specifies that any string w that belongs to a Regular Language L
can be broken into xyz ”
Formal Statement” Let L be a RL, then there exists some constant
integer n≥ 1 (that depends on L) such that any string w in L with |w|≥ n
(where n is a “pumping length”) can be written as
w = xyz with substrings x, y and z, such that
1. y ≠ε; OR |y| ≥ 1; OR y is not empty
2. |xy|≤ n,
3. For every i ≥ 0, any xyiz ∈ L
What is Pumping Lemma?
Example 1
Use pumping Lemma to prove that L|a|(odd) is a RL. Any
word w of L|a|(odd) have count of a is odd.
L = {a, aaa, aaaaa, …}
Solution
w = xyz, where
1. y = aa
2. |xy|<= n where x is a , z is ε, where n=3
3. For every i ≥ 0, any xyiz ∈ L
Therefore a(aa)i ∈ L
Note that Point#3 covers the word with length 1 ”a”
Example 1 (cont.)
Use pumping Lemma to prove that L|a|(odd) is a RL. Any
word w of L|a|(odd) have count of a is odd.
L = {a, aaa, aaaaa, …}
Can you find another solution?
How to prove that a given
language L in not regular ?
Assume the opposite: L is regular
The pumping lemma should hold for L
Use the pumping lemma to obtain a contradiction
Therefore, L is not regular
Example 2
prove that the language anbn is not regular
From the pumping lemma we can write
w = am bm in form of xyz that satisfies the three
conditions of the pumping Lemma
xyz = am
bm
= a...aa...aa...ab...b
m
w
y=ak
, 1£k £nThus:
m
yk zx
Example 2
Solution
Is there any valid n?
w = xyz, where
1. y = a
2. |xy|<= n where x is a j, y is ak for any j, k
3. For every i ≥ 0, any x yi z ∈ L
Therefore a..a(a)k bm ∈ L
xyz = am
bm
= a...aa...aa...ab...b
m
w
m
yk zx
Example 2
From the Pumping Lemma:
1. Since i could be zero then xz ∈ L
[Rule3]
2. y ≠ ε [Rule1]
3. In case i =0, x must have same the length
as z which is indicated by m
4. In cases i=k where k >=1, |xy| must be
equal m but since |x| is m then the
formula becomes m + k = m
Example 2
CONTRADICTION!!!
m + k ≠ m
Therefore: Our assumption that L is a regular
language is not true
Conclusion: L is not a regular language
Lbabaaaaaaazxyk
 ...............
x y zy
Thus:
…….
Is there such k integer
where m+k=m ?
m+k m
Dr.Hussien Sharaf 42

Contenu connexe

Tendances

Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4shah zeb
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4shah zeb
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryTsegazeab Asgedom
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
Formal language
Formal languageFormal language
Formal languageRajendran
 
Regular Expression Examples.pptx
Regular Expression Examples.pptxRegular Expression Examples.pptx
Regular Expression Examples.pptxGhulamRabani9
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of AutomataFarooq Mian
 
Chapt 01 Assembly Language
Chapt 01 Assembly LanguageChapt 01 Assembly Language
Chapt 01 Assembly LanguageHamza Akram
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expressionAnimesh Chaturvedi
 
Computer Architecture - Program Execution
Computer Architecture - Program ExecutionComputer Architecture - Program Execution
Computer Architecture - Program ExecutionVarun Bhargava
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statementsrajshreemuthiah
 

Tendances (20)

Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 
Chomsky Hierarchy.ppt
Chomsky Hierarchy.pptChomsky Hierarchy.ppt
Chomsky Hierarchy.ppt
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Finite automata
Finite automataFinite automata
Finite automata
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
Formal language
Formal languageFormal language
Formal language
 
Regular Expression Examples.pptx
Regular Expression Examples.pptxRegular Expression Examples.pptx
Regular Expression Examples.pptx
 
Theory of Automata
Theory of AutomataTheory of Automata
Theory of Automata
 
Chapt 01 Assembly Language
Chapt 01 Assembly LanguageChapt 01 Assembly Language
Chapt 01 Assembly Language
 
TOC 5 | Regular Expressions
TOC 5 | Regular ExpressionsTOC 5 | Regular Expressions
TOC 5 | Regular Expressions
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Regular language and Regular expression
Regular language and Regular expressionRegular language and Regular expression
Regular language and Regular expression
 
Computer Architecture - Program Execution
Computer Architecture - Program ExecutionComputer Architecture - Program Execution
Computer Architecture - Program Execution
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 

En vedette

Introduction to the theory of computation
Introduction to the theory of computationIntroduction to the theory of computation
Introduction to the theory of computationprasadmvreddy
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automatadeepinderbedi
 
Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Dr. Maamoun Ahmed
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMRajendran
 
Applications of Automata in Electronic Machines and Android Games (Finite Aut...
Applications of Automata in Electronic Machines and Android Games (Finite Aut...Applications of Automata in Electronic Machines and Android Games (Finite Aut...
Applications of Automata in Electronic Machines and Android Games (Finite Aut...ijcoa
 
Theory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemTheory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemKaran Thakkar
 
Decidability
DecidabilityDecidability
Decidabilityandrejko
 
Introduction to Automata Theory, Languages and Computation
Introduction to Automata Theory, Languages and ComputationIntroduction to Automata Theory, Languages and Computation
Introduction to Automata Theory, Languages and ComputationHarisree Sudarsan
 
Deciability (automata presentation)
Deciability (automata presentation)Deciability (automata presentation)
Deciability (automata presentation)Sagar Kumar
 
Automata languages and computation
Automata languages and computationAutomata languages and computation
Automata languages and computationKarthik Velou
 
Pumping Lemma
Pumping LemmaPumping Lemma
Pumping Lemmaeburhan
 
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...Farwa Ansari
 

En vedette (19)

Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Fafl notes [2010] (sjbit)
Fafl notes [2010] (sjbit)Fafl notes [2010] (sjbit)
Fafl notes [2010] (sjbit)
 
Introduction to the theory of computation
Introduction to the theory of computationIntroduction to the theory of computation
Introduction to the theory of computation
 
Deterministic Finite Automata
Deterministic Finite AutomataDeterministic Finite Automata
Deterministic Finite Automata
 
Theory of computation Lec6
Theory of computation Lec6Theory of computation Lec6
Theory of computation Lec6
 
Theory of computation / Post’s Correspondence Problems (PCP)
Theory of computation / Post’s Correspondence Problems (PCP)Theory of computation / Post’s Correspondence Problems (PCP)
Theory of computation / Post’s Correspondence Problems (PCP)
 
NFA or Non deterministic finite automata
NFA or Non deterministic finite automataNFA or Non deterministic finite automata
NFA or Non deterministic finite automata
 
Turing machines
Turing machinesTuring machines
Turing machines
 
Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEM
 
Applications of Automata in Electronic Machines and Android Games (Finite Aut...
Applications of Automata in Electronic Machines and Android Games (Finite Aut...Applications of Automata in Electronic Machines and Android Games (Finite Aut...
Applications of Automata in Electronic Machines and Android Games (Finite Aut...
 
Theory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemTheory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence Problem
 
Decidability
DecidabilityDecidability
Decidability
 
Introduction to Automata Theory, Languages and Computation
Introduction to Automata Theory, Languages and ComputationIntroduction to Automata Theory, Languages and Computation
Introduction to Automata Theory, Languages and Computation
 
Deciability (automata presentation)
Deciability (automata presentation)Deciability (automata presentation)
Deciability (automata presentation)
 
Automata languages and computation
Automata languages and computationAutomata languages and computation
Automata languages and computation
 
Pumping Lemma
Pumping LemmaPumping Lemma
Pumping Lemma
 
Forouzan atm
Forouzan atmForouzan atm
Forouzan atm
 
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
Introduction to Computer theory (Automata Theory) 2nd Edition By Denial I.A. ...
 

Similaire à Here are the key steps to prove that the language L of strings with an odd number of a's is a regular language using the pumping lemma:1. Let the pumping length p be an arbitrary positive integer. 2. Consider any string w in L with length |w| ≥ p. 3. w can be divided into xyz where: - y ≠ ε - |xy| ≤ p4. We pump y by repeating it zero or more times. This gives us the strings xy^iz for i ≥ 0.5. No matter how we pump y, the number of a's in xy^iz will remain odd since y only contains a's. 6

Similaire à Here are the key steps to prove that the language L of strings with an odd number of a's is a regular language using the pumping lemma:1. Let the pumping length p be an arbitrary positive integer. 2. Consider any string w in L with length |w| ≥ p. 3. w can be divided into xyz where: - y ≠ ε - |xy| ≤ p4. We pump y by repeating it zero or more times. This gives us the strings xy^iz for i ≥ 0.5. No matter how we pump y, the number of a's in xy^iz will remain odd since y only contains a's. 6 (20)

Cs419 lec3 lexical analysis using re
Cs419 lec3   lexical analysis using reCs419 lec3   lexical analysis using re
Cs419 lec3 lexical analysis using re
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptx
 
Cs419 lec4 lexical analysis using re
Cs419 lec4   lexical analysis using reCs419 lec4   lexical analysis using re
Cs419 lec4 lexical analysis using re
 
L_2_apl.pptx
L_2_apl.pptxL_2_apl.pptx
L_2_apl.pptx
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Lesson 02
Lesson 02Lesson 02
Lesson 02
 
Lesson 02
Lesson 02Lesson 02
Lesson 02
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02
 
Handout Regular expression with examples and lecture
Handout Regular expression with examples  and lecture Handout Regular expression with examples  and lecture
Handout Regular expression with examples and lecture
 
Theory of Automata ___ Basis ...........
Theory of Automata ___ Basis ...........Theory of Automata ___ Basis ...........
Theory of Automata ___ Basis ...........
 
regular expressions (Regex)
regular expressions (Regex)regular expressions (Regex)
regular expressions (Regex)
 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
 
Regular expressions h1
Regular expressions h1Regular expressions h1
Regular expressions h1
 
Language
LanguageLanguage
Language
 
theory of computation lecture 02
theory of computation lecture 02theory of computation lecture 02
theory of computation lecture 02
 
UNIT_-_II.docx
UNIT_-_II.docxUNIT_-_II.docx
UNIT_-_II.docx
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
Lesson 03
Lesson 03Lesson 03
Lesson 03
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
DBMS 11 | Design Theory [Normalization 1]
DBMS 11 | Design Theory [Normalization 1]DBMS 11 | Design Theory [Normalization 1]
DBMS 11 | Design Theory [Normalization 1]
 

Plus de Arab Open University and Cairo University

Plus de Arab Open University and Cairo University (20)

Infos2014
Infos2014Infos2014
Infos2014
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Lec4
Lec4Lec4
Lec4
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Cs419 lec12 semantic analyzer
Cs419 lec12  semantic analyzerCs419 lec12  semantic analyzer
Cs419 lec12 semantic analyzer
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Cs419 lec7 cfg
Cs419 lec7   cfgCs419 lec7   cfg
Cs419 lec7 cfg
 
Cs419 lec6 lexical analysis using nfa
Cs419 lec6   lexical analysis using nfaCs419 lec6   lexical analysis using nfa
Cs419 lec6 lexical analysis using nfa
 
Cs419 lec5 lexical analysis using dfa
Cs419 lec5   lexical analysis using dfaCs419 lec5   lexical analysis using dfa
Cs419 lec5 lexical analysis using dfa
 
Cs419 Compiler lec1&2 introduction
Cs419 Compiler lec1&2  introductionCs419 Compiler lec1&2  introduction
Cs419 Compiler lec1&2 introduction
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 
CS215 - Lec 6 record index
CS215 - Lec 6  record indexCS215 - Lec 6  record index
CS215 - Lec 6 record index
 

Dernier

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 

Dernier (20)

Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 

Here are the key steps to prove that the language L of strings with an odd number of a's is a regular language using the pumping lemma:1. Let the pumping length p be an arbitrary positive integer. 2. Consider any string w in L with length |w| ≥ p. 3. w can be divided into xyz where: - y ≠ ε - |xy| ≤ p4. We pump y by repeating it zero or more times. This gives us the strings xy^iz for i ≥ 0.5. No matter how we pump y, the number of a's in xy^iz will remain odd since y only contains a's. 6

  • 1. Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 3. Regular Expressions A RE is a language for describing simple languages and patterns. Algorithm for using RE 1. Define a pattern: S1StudentnameS2 2. Loop Find next pattern Store StudentName into DB or encrypt StudentName Until no match REs are used in applications that involve file parsing and text matching. Many Implementations have been made for RE. Dr.Hussien Sharaf 3
  • 4. RE by C++ Boost library //Pattern matching in a String // Created by Flavio Castelli <flavio.castelli@gmail.com> // distrubuted under GPL v2 license #include <boost/regex.hpp> #include <string> int main() { boost::regex pattern ("bg|olug",boost::regex_constants::icase| boost::regex_constants::perl); std::string stringa ("Searching for BsLug"); if (boost::regex_search (stringa, pattern, boost::regex_constants::format_perl)) printf ("foundn"); else printf("not foundn"); return 0; } Dr.Hussien Sharaf 4 Please check [http://flavio.castelli.name/regexp-with-boost]
  • 5. RE by C++ Boost library//Substitution // Created by Flavio Castelli <flavio.castelli@gmail.com> // distrubuted under GPL v2 license #include <boost/regex.hpp> #include <string> int main() {boost::regex pattern ("bok",boost::regex_constants::icase| boost::regex_constants::perl); std::string stringa ("Searching for bok"); std::string replace (“book"); std::string newString; newString = boost::regex_replace (stringa, pattern, replace); printf("The new string is: |%s|n",newString.c_str()); return 0; } Dr.Hussien Sharaf 5 Please check [http://flavio.castelli.name/regexp-with-boost]
  • 6. RE by C++ STL needs extra feature pack #include <regex> #include <iostream> #include <string> bool is_email_valid(const std::string& email) { // define a regular expression const std::tr1::regex pattern("(w+)(.|_)?(w*)@(w+)(.(w+))+"); // try to match the string with the regular expression return std::tr1::regex_match(email, pattern); } Dr.Hussien Sharaf 6 Please check http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339
  • 7. RE by C++ STL #include <regex> #include <iostream> #include <string> bool is_email_valid(const std::string& email) { // define a regular expression const std::tr1::regex pattern("(w+)(.|_)?(w*)@(w+)(.(w+))+"); // try to match the string with the regular expression return std::tr1::regex_match(email, pattern); } Dr.Hussien Sharaf 7 Please check http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339
  • 8. RE by C++ STL std::string str("abc + (inside brackets)dfsd"); std::smatch m; std::regex_search(str, m, std::regex(“b")); if (m[0].matched) std::cout << "Found " << m.str(0) << 'n'; else std::cout << "Not foundn“; Dr.Hussien Sharaf 8 Please check http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339
  • 9. Other code samples Other samples can be check at: Boost Library 1. http://stackoverflow.com/questions/5804453/c-regular-expressions- with-boost-regex 2. http://www.codeproject.com/KB/string/regex__.aspx 3. http://www.boost.org/doc/libs/1_43_0/more/getting_started/window s.html#build-from-the-visual-studio-ide STL Library 1. http://www.codeproject.com/KB/recipes/rexsearch.aspx 2. http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15339 [recommended] 3. http://www.microsoft.com/download/en/confirmation.aspx?id=6922 [feature pack VS2008 Pack] 4. http://channel9.msdn.com/Shows/Going+Deep/C9-Lectures- Stephan-T-Lavavej-Standard-Template-Library-STL-8-of-n [Video] Dr.Hussien Sharaf 9
  • 10. RE Rules -1 1. Let ∑ ={x}, Then L = ∑* In RE, we write x* 2. Let ∑ ={x, y}, Then L = ∑* In RE, we write (x+y)* 3. Kleene’s star “*” means any combination of letters of length zero or more. Dr.Hussien Sharaf 10
  • 11. RE Rules -2 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* NOTE: r1 + is not a RE 3. Nothing else is a regular expression. Dr.Hussien Sharaf 11
  • 12. RE-1 Example 1 ∑ ={a, b} - Formally describe all words with a followed by any number of b’s L = a b* = ab* - Give examples for words in L {a ab abb abbb …..} Dr.Hussien Sharaf 12
  • 13. RE-2 Example 2 ∑ ={a, b} - Formally describe the language that contains nothing or words starts with a where any a must be followed by one or more b’s L = (abb*)* OR (b+ab)* - Give examples for words in L {Λ ab abb abababb …..} Dr.Hussien Sharaf 13
  • 14. RE-3 Example ∑ ={a, b} - Formally describe all words with a followed by one or more b’s L = a b+ = abb* - Give examples for words in L {ab abb abbb …..} Dr.Hussien Sharaf 14
  • 15. RE-4 Example ∑ ={a, b, c} - Formally describe all words that start with an a followed by any number of b’s and then end with c. L = a b* c - Give examples for words in L {ac abc abbc abbbc …..} Dr.Hussien Sharaf 15
  • 16. RE-5 Example ∑ ={a, b} - Formally describe all words where a’s if any come before b’s if any. L = a* b* - Give examples for words in L {Λ a b aa ab bb aaa abb abbb bbb…..} NOTE: a* b* ≠ (ab)* because first language does not contain abab but second language has. Once single b is detected then no a‘s can be added Dr.Hussien Sharaf 16
  • 17. RE-6 Example ∑ ={a} - Formally describe all words where count of a is odd. L = a(aa)* OR (aa)* a - Give examples for words in L {a aaa aaaaa …..} Dr.Hussien Sharaf 17
  • 18. RE-7.1 Example ∑ ={a, b, c} - Formally describe all words where single a or c comes in the start then odd number of b’s. L = (a+c)b(bb)* - Give examples for words in L {ab cb abbb cbbb …..} Dr.Hussien Sharaf 18
  • 19. RE-7.2 Example ∑ ={a, b, c} - Formally describe all words where single a or c comes in the start then odd number of b’s in case of a and zero or even number of b’s in case of c . L = ab(bb)* +c(bb)* - Give examples for words in L {ab c abbb cbb abbbbb …..} Dr.Hussien Sharaf 19
  • 20. RE-8 Example ∑ ={a, b, c} - Formally describe all words where one or more a or one or more c comes in the start then one or more b’s. L = (a+c) + b+ = (aa*+cc*) bb* - Give examples for words in L {ab cb aabb cbbb …..} Dr.Hussien Sharaf 20
  • 21. RE-9 Example ∑ ={a, b} - Formally describe all words with length three. L = (a+b) 3 =(a+b) (a+b) (a+b) - List all words in L {aaa aab aba baa abb bab bba bbb} - What is the count of words of length 4? 16 = 24 - What is the count of words of length 44? 244 Dr.Hussien Sharaf 21
  • 23. RE-10.1 Example ∑ ={a, b}, What does L describe? - L = (a+b)* a(a+b) * Dr.Hussien Sharaf 23 Any string of a's and b's Single a Any string of a's and b's - Give examples for words in L {a ab aab bab abb …..}
  • 24. RE-10.2 abbaab can parsed in 3 ways Dr.Hussien Sharaf 24 Λ a bbaab a bbaab abb a ab abb a ab abba a b abba a b
  • 25. RE-11 Example Page 38 Cohen ∑ ={a, b} - Formally describe all words with at least two a's. 1) L = b*ab*a(a + b)* Start with a jungle of b's (or no b's) until we find the first a, then more b's (or no b's), then the second a, then we finish up with anything. - Give examples for words in L {abbbabb aaaaa bbbabbbbabab…..} Dr.Hussien Sharaf 25
  • 26. RE-12 Example ∑ ={a, b} - Formally describe all words with exactly two a's. 1) L = b*ab*ab* - Give examples for words in L {aab, baba, and bbbabbbab …..} To make the word aab, we let the first and second b* become Λ and the last becomes b Dr.Hussien Sharaf 26
  • 27. RE-13.1 Example ∑ ={a, b} - Formally describe all words with least one a and least one b. 1) L = (a + b)*a(a + b)* b(a + b)* = (anything) a(anything) b(anything) 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* Dr.Hussien Sharaf 27
  • 28. RE-13.2 2) L =(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* Notice that it is necessary to write bb*aa* because b*a* will admit words we do not want, such as aaa. Does this imply that (a+b)*b(a+b)*a(a+b)*= bb*aa*?? False Left side includes the word aba, which the expression on the right side does not. Dr.Hussien Sharaf 28
  • 30. Regular Languages A language that can be defined by a RE is called a regular language. 1. If L1 and L2 are regular languages then L1 + L2 , L1 L2 and L1 * are also regular languages. 2. If L is a regular language then L’ (L complement) is also a regular language. L’ : is the language that is not accepted by r where r is the RE that accepts language L. Note: (L’)’ =L and (r’)’ =r Dr.Hussien Sharaf 30
  • 31. Regular Languages (cont.) 3. If L1 and L2 are regular languages then L1 ∩ L2 is also a regular language. L1 =words starting with a r1 =a(a+b)* L2 =words ending with a r2 = (a+b)*a L3 =L1 ∩ L2 = a(a+b)*a L3 = words that start and end with a. Dr.Hussien Sharaf 31
  • 32. Assignment 3 1. Proof that: If L1 and L2 are regular languages then L1 + L2 , L1 L2 and L1 * are also regular languages. 2. State which type of proofs you have used. Dr.Hussien Sharaf 32
  • 33. How powerful is RE? RE can not express some languages such as plaindrome strings an bn . How can we prove that a language L is not regular? Prove that there is no FA or RE that accepts L Solution: use a Lemma called pumping Lemma. It depends on pumping a string into a template. If the string could not fit into the template then the string is not an RE. Dr.Hussien Sharaf 33
  • 34. Formal Definition ”The pumping lemma for regular languages is a lemma that makes use of a property shared by all regular languages RL. The property specifies that any string w that belongs to a Regular Language L can be broken into xyz ” Formal Statement” Let L be a RL, then there exists some constant integer n≥ 1 (that depends on L) such that any string w in L with |w|≥ n (where n is a “pumping length”) can be written as w = xyz with substrings x, y and z, such that 1. y ≠ε; OR |y| ≥ 1; OR y is not empty 2. |xy|≤ n, 3. For every i ≥ 0, any xyiz ∈ L What is Pumping Lemma?
  • 35. Example 1 Use pumping Lemma to prove that L|a|(odd) is a RL. Any word w of L|a|(odd) have count of a is odd. L = {a, aaa, aaaaa, …} Solution w = xyz, where 1. y = aa 2. |xy|<= n where x is a , z is ε, where n=3 3. For every i ≥ 0, any xyiz ∈ L Therefore a(aa)i ∈ L Note that Point#3 covers the word with length 1 ”a”
  • 36. Example 1 (cont.) Use pumping Lemma to prove that L|a|(odd) is a RL. Any word w of L|a|(odd) have count of a is odd. L = {a, aaa, aaaaa, …} Can you find another solution?
  • 37. How to prove that a given language L in not regular ? Assume the opposite: L is regular The pumping lemma should hold for L Use the pumping lemma to obtain a contradiction Therefore, L is not regular
  • 38. Example 2 prove that the language anbn is not regular From the pumping lemma we can write w = am bm in form of xyz that satisfies the three conditions of the pumping Lemma xyz = am bm = a...aa...aa...ab...b m w y=ak , 1£k £nThus: m yk zx
  • 39. Example 2 Solution Is there any valid n? w = xyz, where 1. y = a 2. |xy|<= n where x is a j, y is ak for any j, k 3. For every i ≥ 0, any x yi z ∈ L Therefore a..a(a)k bm ∈ L xyz = am bm = a...aa...aa...ab...b m w m yk zx
  • 40. Example 2 From the Pumping Lemma: 1. Since i could be zero then xz ∈ L [Rule3] 2. y ≠ ε [Rule1] 3. In case i =0, x must have same the length as z which is indicated by m 4. In cases i=k where k >=1, |xy| must be equal m but since |x| is m then the formula becomes m + k = m
  • 41. Example 2 CONTRADICTION!!! m + k ≠ m Therefore: Our assumption that L is a regular language is not true Conclusion: L is not a regular language Lbabaaaaaaazxyk  ............... x y zy Thus: ……. Is there such k integer where m+k=m ? m+k m