0% found this document useful (0 votes)
28 views37 pages

TOA Lecture01

Theory of automata lecture slide

Uploaded by

Hamza Anjum
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)
28 views37 pages

TOA Lecture01

Theory of automata lecture slide

Uploaded by

Hamza Anjum
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/ 37

Language Theory and Finite

Automata

By

Dr. Nazir Ahmad Zafar


1
• M. Sc. Mathematics:
• Quaid-i-Azam University, Islamabad, 1991
• M. Phil Mathematics:
• Quaid-i-Azam University, Islamabad, 1993,
• M. Sc. Nuclear Engineering:
• Quaid-i-Azam University, Islamabad,1994,
• PhD Computer Science:
• Kyushu University, Japan, 2004.

Dr. Nazir Ahmad Zafar 2


Evaluation Procedure
Activity %
Quizzes 20
Mid-term Examination 30
Final Exam 50
Total 100
Note: Unannounced quizzes may come
Dr. Nazir Ahmad Zafar 3
Summary
• Introduction
• Formal and In-formal languages
• Alphabets, Strings, Null string, Words
• Valid and In-valid alphabets
• Length of a string
• Reverse of a string
• Defining languages
• Descriptive definition of languages
• Theorem
Dr. Nazir Ahmad Zafar 4
Some Basic Languages
• EQUAL, EVEN-EVEN, INTEGER, EVEN
• { an bn}
• {an bn an }
• Factorial
• FACTORIAL
• DOUBLE FACTORIAL
• SQUARE
• DOUBLE SQUARE
• PRIME
• PALINDROME
Dr. Nazir Ahmad Zafar 5
Introduction
What does automata mean?
• It is the plural of automaton, and it means
“something that works automatically”

Introduction to languages
• There are two types of languages
– Formal Languages (Syntactic languages)
– Informal Languages (Semantic languages)
Dr. Nazir Ahmad Zafar 6
Alphabets
Definition (alphabet)
A finite non-empty set of symbols (called
letters), is called an alphabet.
It is denoted by Σ (Greek letter sigma).
Example 1
1. Σ = {a, b}
2. Σ = {0,1} (important as this is the language
which the computer understands)
3. Σ = {i, j , k} etc.
Dr. Nazir Ahmad Zafar 7
Strings
• Σ (alphabet) includes letters, digits and a
variety of operators including sequential
operators such as GOTO and IF
Definition (Strings)
• Concatenation of finite letters from the
alphabet is called a string.
Example 2
If Σ = {a,b} then
a, abab, aaabb, ababababababababab
Dr. Nazir Ahmad Zafar 8
Strings
Empty string or null string
Sometimes a string with no symbol at all is
used
It is denoted by small Greek letter Lambda λ
OR capital Greek letter Lambda Λ, called an
empty string or null string.
The capital lambda will mostly be used to
denote the empty string, in further discussion.

Dr. Nazir Ahmad Zafar 9


Words
Definition (Words)
Words are strings belonging to some language.
Example 3
If Σ = {x} then language L can be defined as
L = {xn : n =1, 2, 3, . . . } or L = {x, xx, xxx, . . . }
Here x, xx, … are the words of L

Note:
All words are strings, but not all strings are words.
Dr. Nazir Ahmad Zafar 10
Valid/In-valid alphabets
While defining an alphabet, It may contain letters
consisting of group of symbols
Example 4
Σ1 = {B, aB, bab, d}.
Example 5
If Σ2 = {B, Ba, bab, d} then a string BababB can
be tokenized in two ways
– (Ba), (bab), (B) is identified as a string
– (B), (abab), (B) is not identified as a string
Ambiguous alphabets
Dr. Nazir Ahmad Zafar 11
Words
• Which shows that the second group cannot be
identified as a string defined over Σ
• As when this string is scanned by the compiler
(Lexical Analyzer),
• First symbol B is identified as a letter belonging to Σ,
while for the second letter the lexical analyzer would
not be able to identify it
• Hence while defining an alphabet it should be kept in
mind that ambiguity should not be created

Dr. Nazir Ahmad Zafar 12


Remarks
• While defining an alphabet of letters
consisting of more than one symbols, no letter
should be started with the letter of the same
alphabet, i.e., one letter should not be the
prefix of another.
• However, a letter may be ended in a letter of
same alphabet.

Dr. Nazir Ahmad Zafar 13


Valid and In-Valid Alphabets
Example 6
Σ1 = {B, aB, bab, d}
Σ1 is a valid alphabet
Example 7
Σ2 = {B, Ba, bab, d}
Σ2 is an in-valid alphabet.

Dr. Nazir Ahmad Zafar 14


Length of Strings
Definition (Length of Strings)
The length of string s, denoted by |s|, is the
number of letters in the string.
Example 8
If Σ = {a, b} and s = ababa, then |s| = 5
Example 9
Σ = {B, aB, bab, d} and s = BaBbabBd then
Tokenizing = (B), (aB), (bab), (B), (d)
And |s| = 5
Dr. Nazir Ahmad Zafar 15
Reverse of a String
Definition
Reverse of a string s denoted by Rev(s) is obtained by
writing the letters of s in reverse order.
Example 10
If s = abc is defined over Σ = {a, b, c}
then Rev(s) = cba
Example 11
Σ = {B, aB, bab, d} and s = BaBbabBd then
Tokenizing = (B), (aB), (bab), (B), (d)
Rev(s) = dBbabaBB
Dr. Nazir Ahmad Zafar 16
Defining Languages

The languages can be defined in different


ways , such as
– Descriptive definition,
– Recursive definition,
– Using Regular Expressions (RE) and
– Using Finite Automaton (FA) etc.

Dr. Nazir Ahmad Zafar 17


Descriptive definition of language
The language is defined, describing the
conditions imposed on its words.
Example 12
The language L of strings of odd length, defined
over Σ = {a}, can be written as
L = {a, aaa, aaaaa, . . . }
Example 13
Language L, defined over Σ = {a, b, c}, of
strings that does not start with a, can be written
as L = {Λ, b, c, ba, bb, bc, ca, . . . }
Dr. Nazir Ahmad Zafar 18
Descriptive Definition of Language

The language L of strings of length 2,


defined over Σ = {0, 1, 2}, can be written as
L = {00, 01, 02,10, 11,12,20,21,22}

Mathematical Description
L = {s : S | Length(s) = 2 }

Dr. Nazir Ahmad Zafar 19


More Examples
Example 14
The language L of strings ending in 0,
defined over Σ = {0, 1}, can be written as
L= {0, 00, 10, 000, 010, 100, 110, . . .}
Example 15
The language EQUAL, of strings with
number of a’s equal to number of b’s,
defined over Σ = {a, b}, can be written as
{Λ, ab, aabb, abab, baba,abba, . . . }
Dr. Nazir Ahmad Zafar 20
More Examples
Example 16
The language EVEN-EVEN, of strings with
even number of a’s and even number of b’s,
defined over Σ = {a, b}, can be written as
{Λ, aa, bb, aaaa, aabb, abab, abba, baab, baba,
bbaa, bbbb, . . .}
Example 17
The language INTEGER, of strings defined
over Σ = {-, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, can be
written as INTEGER = {. . ., -2, -1, 0, 1, 2, . . }
Dr. Nazir Ahmad Zafar 21
More Examples
Example 18
The language EVEN, of stings defined over
Σ = {-, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9} is written as
EVEN = {. . ., -4, -2, 0, 2, 4, . . .}
Example 19
The language {anbn }, of strings defined over Σ
= {a, b}, is {anbn | n = 1, 2, 3, . . . } can be
written as
{ab, aabb, aaabbb, aaaabbbb, . . . }
Dr. Nazir Ahmad Zafar 22
More Examples
Example 20
The language {anbnan}, of strings defined over Σ
= {a, b}, is {anbnan| n = 1, 2, 3,…}, can be
written as : {aba, aabbaa, aaabbbaaa,
aaaabbbbaaaa, . . .}
Example 21a
The language factorial, of strings defined over Σ
= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} is {1, 2, 6, 24, 120,
...}
Dr. Nazir Ahmad Zafar 23
More Examples
Example 21b
The language FACTORIAL, of strings defined
over Σ = {a}, is
{an! | n = 1, 2, 3, . . . }, which can be written as
{a, aa, aaaaaa, . . .}
Note:
It is to be noted that the language
FACTORIAL can be defined over any single
letter alphabet.
Dr. Nazir Ahmad Zafar 24
More Examples
Example 22
The language DOUBLEFACTORIAL, of
strings defined over Σ = {a, b}, is
{an!bn! | n = 1, 2, 3, . . .}, which can be written
as {ab, aabb, aaaaaabbbbbb,…}
Example 23
The language SQUARE, of strings defined
over Σ = {a}, is {an2 n = 1, 2, 3, . . .}, and can
be written as: {a, aaaa, aaaaaaaaa,. . .}
Dr. Nazir Ahmad Zafar 25
More Examples
Example 24
The language DOUBLESQUARE, of strings
defined over Σ = {a, b}, is
{an2 bn2 | n = 1, 2, 3, . . .}, which is written as
{ab, aaaabbbb, aaaaaaaaabbbbbbbbb, . . .}
Example 25
The language PRIME, of strings defined over
Σ = {a}, is {ap | p is prime}, can be written as
{aa, aaa, aaaaa, aaaaaaa, . . .}
Dr. Nazir Ahmad Zafar 26
More Examples
An Important language PALINDROME
The language consisting of Λ and the strings s
defined over Σ such that Rev(s) = s.
It is to be denoted that the words of
PALINDROME are called palindromes.
Example 26
For Σ = {a, b}
PALINDROME = {Λ , a, b, aa, bb, aaa, aba, bab,
bbb, . . .}
Dr. Nazir Ahmad Zafar 27
Theorem
Theorem
There are as many palindromes of length 2n as
there are of length 2n-1.
Note
Before proving the above theorem, the
following is to be noted
Analysis
Note that number of strings of length ‘m’
defined over alphabet of ‘n’ letters is nm.
Dr. Nazir Ahmad Zafar 28
Examples: Theorem
Example 27
The language of strings of length 1, defined
over Σ = {a, b} is
L = Σ = {a, b}
number of strings = 21 = 2
Example 28
The language of strings of length 2, defined over
Σ = {a, b} is
L = Σ x Σ = {aa, ab, ba, bb} i.e.
number of strings = 22 = 4
Dr. Nazir Ahmad Zafar 29
Examples: Theorem
Example 29
The language of strings of length 3, defined
over Σ = {a, b} is
L=ΣxΣxΣ
= {aa, ab, ba, bb} x {a, b}
= {aaa, aab, aba, abb, baa, bab, bba, bbb}

Number of strings = 23 = 8

Dr. Nazir Ahmad Zafar 30


Example 4: Theorem

Example 30 If Σ = {a, b},


Then all possible strings of length 4.

L = (Σ x Σ) x (Σ x Σ)
= {aa, ab, ba, bb} x {aa, ab, ba, bb}
= {aaaa, aaab, aaba, aabb, abaa, abab, abba,
abbb, baaa, baab, baba, babb, bbaa, bbab,
bbba, bbbb}
Dr. Nazir Ahmad Zafar 31
Example 31: Palindromes of length 2n, 2n-1
Procedure: Palindromes of length 2n
• Compute all strings of length n
• Find reverse of every string
• Palindrome of length 2n = {sRev(s): s is string}

Procedure: Palindromes of length 2n-1


• Compute all strings of length n-1
• Find reverse of every string
• Palindrome of length 2n-1 =
{saRev(s): s is string}  {sbRev(s): s is string}
Dr. Nazir Ahmad Zafar 32
Example 32: All palindromes of length 2, 4, 6
Strings S, Length n Rev (S) Palindromes Length 2n
n=1 a a aa
b b bb

n=2 aa aa aaaa
bb bb bbbb
ab ba abba
ba ab baab

n=3 aaa aaa aaaaaa


aab baa aabbaa
aba aba abaaba
baa aab baaaab
abb bba abbbba
bab bab babbab
bba abb bbaabb
bbb bbb bbbbbb
Dr. Nazir Ahmad Zafar 33
Example 33: All possible palindromes of length 8
Strings S, Length n Rev (S) Palindromes Length 2n
n=4 aaaa aaaa aaaaaaaa
aaab baaa aaabbaaa
aaba abaa aabaabaa
aabb bbaa aabbbbaa
abaa aaba abaaaaba
abab baba ababbaba
abba abba abbaabba
abbb bbba abbbbbba
baaa aaab baaaaaab
baab baab baabbaab
baba abab babaabab
babb bbab babbbbab
bbaa aabb bbaaaabb
bbab babb bbabbabb
bbba abbb bbbaabbb
bbbb bbbb bbbbbbbb
Dr. Nazir Ahmad Zafar 34
Proof
• To calculate the number of palindromes of
length(2n), consider the following diagram,

Dr. Nazir Ahmad Zafar 35


Proof
• It shows that there are as many palindromes of length
2n as there are strings of length n i.e. the required
number of palindromes are 2n.
• To calculate number of palindromes of length (2n-1)
with ‘a’ as the middle letter, consider following diagram

Dr. Nazir Ahmad Zafar 36


Proof
• The above diagram shows that there are as
many palindromes of length 2n-1 as there
are strings of length n-1 i.e. the required
number of palindromes are 2n-1.
• Similarly the number of palindromes of
length 2n-1, with ‘b’ as middle letter, will
be 2n-1 as well.
• Hence the total number of palindromes of
length 2n-1 will be =
2n-1 + 2n-1 = 2 (2n-1) = 2n, Proof is Complete
37
Dr. Nazir Ahmad Zafar

You might also like