Automata Theory: Lecture 3, 4

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

Automata Theory

Lecture 3, 4
Lecture’s Objective

Defining/Expressing languages
Regular Expression & Regular Languages
Expressing a language
• L = {x Ɛ {a,b}* | |X| <= 8}
• L = {x Ɛ {a,b}* | |X| is odd}
• L = {x Ɛ {a,b}* | na (x) >= nb (x)}
• L = {x Ɛ {a,b}* | |X| >= 2 and x begins and ends with b}

• A finite way to describe infinite languages


• L1 = {ab, bab}* U {b} {bb}*
• L2 = {byb | y Ɛ {a , b}*}
Recursive definitionof a language – Another
way
• It offers natural way of defining functions on sets.
• It also offer natural way of proving some property or condition
satisfied by every element of set.
• There are three steps
• Basis step
• Recursive step
• Enforcing step
RD of natural numbers
1. 0 ∈ N.
2. For every n ∈ N, n + 1 ∈ N.
3. Every element of N can be obtained by using statement 1 or
statement 2.

• N contains 0 and closed under successor operation


RD of few subsets of N
1. 15 ∈ A.
2. For every n ∈ A, n + 1 ∈ A.
3. Every element of N can be obtained by using statement 1 or
statement 2.

• N contains 15 and greater than 15 natural numbers.


More examples
RD of {a, b}*
1. ^ ∈ {a, b}∗.
2. For every x ∈ {a, b}∗, both xa and xb are in {a, b}∗.

AnBn language AnBn = {anbn | n ∈ N}


3. ^ ∈ AnBn.
4. For every x ∈ AnBn, axb ∈ AnBn.
More examples
Palindrome
1. ^ , a, and b are elements of Pal.
2. For every x ∈ Pal, axa and bxb are in Pal.

Algebraic expression and balanced strings of parenthesis


3. a ∈ Expr.
4. For every x and every y in Expr, x + y and x ∗ y are in Expr.
5. For every x ∈ Expr, (x) ∈ Expr.
Regular Expression – Another way to express
languages
All the languages expressible through
Regular Expressions are called Regular
Languages
Practice:
Writing and understanding RE
Regular Expressions – Understanding RegEx
Regular Expression Language
• ^ L = {^}
• a L = {a}
• b L = {b}
• a+b L = {a, b}
• ab L = {ab}
• a* L = {^, a, aa, aaa, aaaa, ….}
• a+ L = {a, aa, aaa, aaaa, ….}
• aa* L = {a, aa, aaa, aaaa, ….} equivalent to a+
• (a+b)* L = {^, a, b, aa, ab, ba, bb, ….}
• (ab)* L = {^, ab, abab, ababab, abababab, ….}
• a(a+b)* L = {a, aa, ab, aaa, aab, aba, abb, ….} All strings starting with a
• (a+b)*b All strings ending with b
• a(a+b)*b All strings staring with a and ending with b
• a(a+b)*b + b(a+b)*a All strings starting and ending in different letters
• (a+b)* a (a+b)* All strings having at least one a
Regular Expressions – Understanding RegEx
Regular Expression Language
• ^ L = {^}
• a L = {a}
• b L = {b}
• a+b L = {a, b}
• ab L = {ab}
• a* L = {^, a, aa, aaa, aaaa, ….}
• a+ L = {a, aa, aaa, aaaa, ….}
• aa* L = {a, aa, aaa, aaaa, ….} equivalent to a+
• (a+b)* L = {^, a, b, aa, ab, ba, bb, ….}
• (ab)* L = {^, ab, abab, ababab, abababab, ….}
• a(a+b)* L = {a, aa, ab, aaa, aab, aba, abb, ….} All strings starting with a
• (a+b)*b All strings ending with b
• a(a+b)*b All strings staring with a and ending with b
• a(a+b)*b + b(a+b)*a All strings starting and ending in different letters
• (a+b)* a (a+b)* All strings having at least one a
Writing RegEx for a language
• Few rules to remember
• Understand the use of (a+b)*
• First write compulsory part of expression and then fill missing areas in RegEx
• Examples:
• Having at least one a
• Having at least two a’s
• Having at least one double a
• Having exactly two a’s
• Starting and ending in same letter (both versions)
• Starting and ending with different letter
Writing RegEx for a language – cont.
• Few more examples
• All strings of even length
• All strings of odd length
• All strings with length at least one and at most three
• Number of 0s is a multiple of 3
• Starts with 0 and has odd length, or starts with 1 and has even length
• All strings having even number of a’s
• Even-Even Language
• Examples for larger alphabet set
• Language of odd numbers
• Language of even numbers
Compiler and RegEx
• Lexical Analyzer
• int count = 10;  int is a keyword
count is an identifier
= is an operator
10 is a constant
; is a symbol

• Regular expression for identifier


• Regular expression for integer number
Exercises
• RegEx for CNIC
• RegEx for Email address
• RegEx for Phone number
• RegEx for different scenarios and real life problem will be shared later

You might also like