SlideShare a Scribd company logo
1 of 17
Download to read offline
Compilers (CPL5316)
Software Engineering
Koya University
2017-2018
e-mail: rebaz.najeeb@koyauniversity.org
LECTURE 2
Regular Expressions
1
Compilers (CPL5316) # 1 Lectured by : Rebaz Najeeb
OUTLINE
⌐ Specification of tokens
⌐ Regular expressions and regular descriptions
⌐ Regular expressions examples
Compilers (CPL5316) # 2 Lectured by : Rebaz Najeeb
⌐
Compilers (CPL5316) # 3 Lectured by : Rebaz Najeeb
Which word you see first?
SPECIFICATION OF TOKEN
⌐ Regular expressions are important notations for specifying lexeme patterns. While they
cannot express all possible patterns, they are very effective in specifying those types of
patterns that we actually need for tokens.
⌐ For lexical analysis we care about regular languages, which can be described using
regular expressions.
⌐ Q: How are tokens defined and recognized?
Ans: By using regular expressions to define a token as a formal regular language.
Compilers (CPL5316) # 4 Lectured by : Rebaz Najeeb
STRINGS AND LANGUAGES
⌐ An alphabet: is any finite set of symbols such as letters, digits, and punctuation. It denoted by
the symbol ∑
i. The set {0,1} is the binary alphabet.
⌐ A string:
⌐ Is a finite sequence of symbols over an alphabet or drown from alphabet.
⌐ The terms "sentence" and "word" are often used as synonyms for "string."
⌐ The empty string is the string of length zero. It denoted by є .
⌐ |s| represents the length of a string s. Ex: banana is a string of length 6 such as |s|=6
⌐Language :
⌐Is any countable set of strings over some fixed alphabet L = {A, . . . , Z}, then{“A”,”B”,”C”,
“BF”…,”ABZ”,…} is considering the language
Compilers (CPL5316) # 5 Lectured by : Rebaz Najeeb
OPERATIONS ON LANGUAGES
⌐ Union: is the set of letters and digits. If A and B are the two sets then it denoted as :
A U B .
⌐ Concatenation: is the set of strings of length two. If A and B are the two sets, then it
denoted as : A B
⌐ Exponentiation: A4 is the set of all 4-letter strings.
⌐ Kleene Closure: A* is the set of all strings of letters, including the empty string .
⌐ Positive Closure : A+ is the set of all strings of one or more digits.
⌐ A(A U B)* is the set of all strings of letters and digits beginning with a letter.
Compilers (CPL5316) # 6 Lectured by : Rebaz Najeeb
EXAMPLES OF OPERATIONS
⌐ let A= {a,b,c} B= {1,2}
⌐ AB = {a1,a2,b1,b2,c1,c2}
⌐ A U B = {a,b,c,1,2}
⌐ A3 = all strings with length three (using a,b,c}
⌐ A* = all strings using letters a,b,c and empty string
⌐ A+ = doesn’t include the empty string
Compilers (CPL5316) # 7 Lectured by : Rebaz Najeeb
Regular expressions rules
⌐ We may remove parentheses by using precedence rules.
⌐ * highest
⌐ concatenation next
⌐ | lowest
a∣(b(c*)) = a ∣ bc*
⌐ Let ∑ = {a, b}
⌐a ∣ b => stands for the set {a, b}
⌐ab => stands for the set {ab}
⌐ (a ∣ b) (a ∣ b) => {aa, ab, ba, bb}
⌐a* => {ԑ, a, aa, aaa, ... }
⌐(a ∣ b)* => all strings containing zero or more instances of a's and b's {ԑ, a, b, aa, ab, ba, bb, aaa, …. }
⌐a ∣ a * b => { a, b, ab, aab, aaab, ... }
Compilers (CPL5316) # 8 Lectured by : Rebaz Najeeb
REGULAR DEFINITION
⌐ Regular definitions are multiline regular expressions Each line can refer to any of the previous lines but not to
itself or to subsequent lines.
⌐ To write regular expression for some languages can be difficult or can be quite complex. In those cases, we may
use regular definitions (names).
⌐ Ex: define Identifiers using RegEx
Letter → A | B | ... | Z | a | b | ... | z
digit → 0 | 1 | ... | 9
id → letter (letter | digit ) * ==
⌐Ex: Unsigned numbers
digit → 0 | 1 | ... | 9
digits → digit digit*
opt-fraction → . digits | ԑ Opt(optional)
** Regular definitions are not recursive: digits  digit digitsdigit wrong!
Compilers (CPL5316) # 9 Lectured by : Rebaz Najeeb
(A|...|Z|a|...|z) ( (A|...|Z|a|...|z) | (0|...|9) ) *
Regular expression examples
⌐ consider ∑={a,b } then generate all strings that has length exactly 2
L1= { aa, ab , ba , bb }
RE= aa + ab + ba + bb  a (a+b) + b(a+b)  (a+b) (a+b)
--------------------------------------------
String Length at least two : length can be 2,3,4,….
(a+b) (a+b) (a+b)*
Compilers (CPL5316) # 10 Lectured by : Rebaz Najeeb
Regular expression examples
⌐ consider ∑={a,b } , find RE that covers all strings that have at most length 2
Ans: (ε+a+b) (ε+a+b)
-----------------------------------------------------------
String length= even numbers
L1 {ε , aa , ab , ba , bb , aaaa, bbba , bbaa ,… }
Ans: ((a+b) (a+b) )*
((a+b) (a+b) )n  ((a+b)2)n  ((a+b))2n whiele N>=0
Compilers (CPL5316) # 11 Lectured by : Rebaz Najeeb
Regular expression examples
String length= odd numbers
L1 {a , abb , baaaa , bbb , aaa, babba , abbaa ,… }
((a+b))2n+1 whiele N>=0  ((a+b))2n (a+b)  ((a+b)2)n (a+b)  ((a+b)2)* (a+b)
((a+b) (a+b) )* (a+b)
Ans : ((a+b) (a+b) )* (a+b)
-----------------------------------------------------
String length= divisible by 3
Ans : ((a+b) (a+b) (a+b) )*
Compilers (CPL5316) # 12 Lectured by : Rebaz Najeeb
Regular expression examples
consider ∑={a,b } , find RE that covers all strings that have exactly two a ‘s
Ans: b* a b* a b*
-----------------------------------------
At least two a
b* a b* a (a+b)*
---------------------------------------------------------
At most two a L1= {ε,a,b,bb,ab , aab , bbabba , }
Ans: b* (a+ ε) b* (a+ ε) b*
Compilers (CPL5316) # 13 Lectured by : Rebaz Najeeb
Regular expression examples
consider ∑={a,b } , find RE that covers all strings in which number of (a)s are even
Ans: (b* a b* a b* )* b*
-----------------------------
Start with (a)
Ans: a ( a + b )*
----------------------------------------
Start and end with different alphabet
Ans: a (a+b)* b + b (a+b)* a
Compilers (CPL5316) # 14 Lectured by : Rebaz Najeeb
Catch patterns
consider ∑={0-9} , find RE that catches all the numbers that match the result of (9)+ by any
number.
(9)n * 2 = 1 (9)n-1 8
(9)n * 3 = 2 (9)n-1 7
Compilers (CPL5316) # 15 Lectured by : Rebaz Najeeb
Homework
consider ∑={letter or digits} , find RE that covers valid email addresses.
Rebaz.Najeeb@gmail.com
Compilers (CPL5316) # 16 Lectured by : Rebaz Najeeb
17

More Related Content

What's hot

Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysisDattatray Gandhmal
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Codesanchi29
 
Programming languages
Programming languagesProgramming languages
Programming languagesEelco Visser
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxIffat Anjum
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesGuido Wachsmuth
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)bolovv
 

What's hot (20)

Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Back patching
Back patchingBack patching
Back patching
 
Lecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptxLecture 13 intermediate code generation 2.pptx
Lecture 13 intermediate code generation 2.pptx
 
Compiler Design QA
Compiler Design QACompiler Design QA
Compiler Design QA
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Ch04
Ch04Ch04
Ch04
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Chapter Two(1)
Chapter Two(1)Chapter Two(1)
Chapter Two(1)
 
Ch03
Ch03Ch03
Ch03
 

Similar to regular expressions (Regex)

Similar to regular expressions (Regex) (20)

Lex analysis
Lex analysisLex analysis
Lex analysis
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
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
 
Mod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptxMod 2_RegularExpressions.pptx
Mod 2_RegularExpressions.pptx
 
Theory of Automata ___ Basis ...........
Theory of Automata ___ Basis ...........Theory of Automata ___ Basis ...........
Theory of Automata ___ Basis ...........
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
UNIT_-_II.docx
UNIT_-_II.docxUNIT_-_II.docx
UNIT_-_II.docx
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02
 
Chapter2CDpdf__2021_11_26_09_19_08.pdf
Chapter2CDpdf__2021_11_26_09_19_08.pdfChapter2CDpdf__2021_11_26_09_19_08.pdf
Chapter2CDpdf__2021_11_26_09_19_08.pdf
 
Lesson 02
Lesson 02Lesson 02
Lesson 02
 
Lesson 02
Lesson 02Lesson 02
Lesson 02
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Python data handling
Python data handlingPython data handling
Python data handling
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Lexicalanalyzer
LexicalanalyzerLexicalanalyzer
Lexicalanalyzer
 
Certified bit coded regular expression parsing
Certified bit coded regular expression parsingCertified bit coded regular expression parsing
Certified bit coded regular expression parsing
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 

Recently uploaded

Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )aarthirajkumar25
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxUmerFayaz5
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)PraveenaKalaiselvan1
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhousejana861314
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsSérgio Sacani
 
Cultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxCultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxpradhanghanshyam7136
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptxanandsmhk
 
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfAnalytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfSwapnil Therkar
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzohaibmir069
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRDelhi Call girls
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...Sérgio Sacani
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 

Recently uploaded (20)

Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Munirka Delhi 💯Call Us 🔝8264348440🔝
 
Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )Recombination DNA Technology (Nucleic Acid Hybridization )
Recombination DNA Technology (Nucleic Acid Hybridization )
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptx
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhouse
 
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroidsHubble Asteroid Hunter III. Physical properties of newly found asteroids
Hubble Asteroid Hunter III. Physical properties of newly found asteroids
 
Cultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptxCultivation of KODO MILLET . made by Ghanshyam pptx
Cultivation of KODO MILLET . made by Ghanshyam pptx
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptxUnlocking  the Potential: Deep dive into ocean of Ceramic Magnets.pptx
Unlocking the Potential: Deep dive into ocean of Ceramic Magnets.pptx
 
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdfAnalytical Profile of Coleus Forskohlii | Forskolin .pdf
Analytical Profile of Coleus Forskohlii | Forskolin .pdf
 
zoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistanzoogeography of pakistan.pptx fauna of Pakistan
zoogeography of pakistan.pptx fauna of Pakistan
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
 
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
PossibleEoarcheanRecordsoftheGeomagneticFieldPreservedintheIsuaSupracrustalBe...
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 

regular expressions (Regex)

  • 1. Compilers (CPL5316) Software Engineering Koya University 2017-2018 e-mail: rebaz.najeeb@koyauniversity.org LECTURE 2 Regular Expressions 1 Compilers (CPL5316) # 1 Lectured by : Rebaz Najeeb
  • 2. OUTLINE ⌐ Specification of tokens ⌐ Regular expressions and regular descriptions ⌐ Regular expressions examples Compilers (CPL5316) # 2 Lectured by : Rebaz Najeeb
  • 3. ⌐ Compilers (CPL5316) # 3 Lectured by : Rebaz Najeeb Which word you see first?
  • 4. SPECIFICATION OF TOKEN ⌐ Regular expressions are important notations for specifying lexeme patterns. While they cannot express all possible patterns, they are very effective in specifying those types of patterns that we actually need for tokens. ⌐ For lexical analysis we care about regular languages, which can be described using regular expressions. ⌐ Q: How are tokens defined and recognized? Ans: By using regular expressions to define a token as a formal regular language. Compilers (CPL5316) # 4 Lectured by : Rebaz Najeeb
  • 5. STRINGS AND LANGUAGES ⌐ An alphabet: is any finite set of symbols such as letters, digits, and punctuation. It denoted by the symbol ∑ i. The set {0,1} is the binary alphabet. ⌐ A string: ⌐ Is a finite sequence of symbols over an alphabet or drown from alphabet. ⌐ The terms "sentence" and "word" are often used as synonyms for "string." ⌐ The empty string is the string of length zero. It denoted by є . ⌐ |s| represents the length of a string s. Ex: banana is a string of length 6 such as |s|=6 ⌐Language : ⌐Is any countable set of strings over some fixed alphabet L = {A, . . . , Z}, then{“A”,”B”,”C”, “BF”…,”ABZ”,…} is considering the language Compilers (CPL5316) # 5 Lectured by : Rebaz Najeeb
  • 6. OPERATIONS ON LANGUAGES ⌐ Union: is the set of letters and digits. If A and B are the two sets then it denoted as : A U B . ⌐ Concatenation: is the set of strings of length two. If A and B are the two sets, then it denoted as : A B ⌐ Exponentiation: A4 is the set of all 4-letter strings. ⌐ Kleene Closure: A* is the set of all strings of letters, including the empty string . ⌐ Positive Closure : A+ is the set of all strings of one or more digits. ⌐ A(A U B)* is the set of all strings of letters and digits beginning with a letter. Compilers (CPL5316) # 6 Lectured by : Rebaz Najeeb
  • 7. EXAMPLES OF OPERATIONS ⌐ let A= {a,b,c} B= {1,2} ⌐ AB = {a1,a2,b1,b2,c1,c2} ⌐ A U B = {a,b,c,1,2} ⌐ A3 = all strings with length three (using a,b,c} ⌐ A* = all strings using letters a,b,c and empty string ⌐ A+ = doesn’t include the empty string Compilers (CPL5316) # 7 Lectured by : Rebaz Najeeb
  • 8. Regular expressions rules ⌐ We may remove parentheses by using precedence rules. ⌐ * highest ⌐ concatenation next ⌐ | lowest a∣(b(c*)) = a ∣ bc* ⌐ Let ∑ = {a, b} ⌐a ∣ b => stands for the set {a, b} ⌐ab => stands for the set {ab} ⌐ (a ∣ b) (a ∣ b) => {aa, ab, ba, bb} ⌐a* => {ԑ, a, aa, aaa, ... } ⌐(a ∣ b)* => all strings containing zero or more instances of a's and b's {ԑ, a, b, aa, ab, ba, bb, aaa, …. } ⌐a ∣ a * b => { a, b, ab, aab, aaab, ... } Compilers (CPL5316) # 8 Lectured by : Rebaz Najeeb
  • 9. REGULAR DEFINITION ⌐ Regular definitions are multiline regular expressions Each line can refer to any of the previous lines but not to itself or to subsequent lines. ⌐ To write regular expression for some languages can be difficult or can be quite complex. In those cases, we may use regular definitions (names). ⌐ Ex: define Identifiers using RegEx Letter → A | B | ... | Z | a | b | ... | z digit → 0 | 1 | ... | 9 id → letter (letter | digit ) * == ⌐Ex: Unsigned numbers digit → 0 | 1 | ... | 9 digits → digit digit* opt-fraction → . digits | ԑ Opt(optional) ** Regular definitions are not recursive: digits  digit digitsdigit wrong! Compilers (CPL5316) # 9 Lectured by : Rebaz Najeeb (A|...|Z|a|...|z) ( (A|...|Z|a|...|z) | (0|...|9) ) *
  • 10. Regular expression examples ⌐ consider ∑={a,b } then generate all strings that has length exactly 2 L1= { aa, ab , ba , bb } RE= aa + ab + ba + bb  a (a+b) + b(a+b)  (a+b) (a+b) -------------------------------------------- String Length at least two : length can be 2,3,4,…. (a+b) (a+b) (a+b)* Compilers (CPL5316) # 10 Lectured by : Rebaz Najeeb
  • 11. Regular expression examples ⌐ consider ∑={a,b } , find RE that covers all strings that have at most length 2 Ans: (ε+a+b) (ε+a+b) ----------------------------------------------------------- String length= even numbers L1 {ε , aa , ab , ba , bb , aaaa, bbba , bbaa ,… } Ans: ((a+b) (a+b) )* ((a+b) (a+b) )n  ((a+b)2)n  ((a+b))2n whiele N>=0 Compilers (CPL5316) # 11 Lectured by : Rebaz Najeeb
  • 12. Regular expression examples String length= odd numbers L1 {a , abb , baaaa , bbb , aaa, babba , abbaa ,… } ((a+b))2n+1 whiele N>=0  ((a+b))2n (a+b)  ((a+b)2)n (a+b)  ((a+b)2)* (a+b) ((a+b) (a+b) )* (a+b) Ans : ((a+b) (a+b) )* (a+b) ----------------------------------------------------- String length= divisible by 3 Ans : ((a+b) (a+b) (a+b) )* Compilers (CPL5316) # 12 Lectured by : Rebaz Najeeb
  • 13. Regular expression examples consider ∑={a,b } , find RE that covers all strings that have exactly two a ‘s Ans: b* a b* a b* ----------------------------------------- At least two a b* a b* a (a+b)* --------------------------------------------------------- At most two a L1= {ε,a,b,bb,ab , aab , bbabba , } Ans: b* (a+ ε) b* (a+ ε) b* Compilers (CPL5316) # 13 Lectured by : Rebaz Najeeb
  • 14. Regular expression examples consider ∑={a,b } , find RE that covers all strings in which number of (a)s are even Ans: (b* a b* a b* )* b* ----------------------------- Start with (a) Ans: a ( a + b )* ---------------------------------------- Start and end with different alphabet Ans: a (a+b)* b + b (a+b)* a Compilers (CPL5316) # 14 Lectured by : Rebaz Najeeb
  • 15. Catch patterns consider ∑={0-9} , find RE that catches all the numbers that match the result of (9)+ by any number. (9)n * 2 = 1 (9)n-1 8 (9)n * 3 = 2 (9)n-1 7 Compilers (CPL5316) # 15 Lectured by : Rebaz Najeeb
  • 16. Homework consider ∑={letter or digits} , find RE that covers valid email addresses. Rebaz.Najeeb@gmail.com Compilers (CPL5316) # 16 Lectured by : Rebaz Najeeb
  • 17. 17