You are on page 1of 35

THEORY OF COMPUTATION

Mr. Zaheed Ahmed

Theory of Computation

Chapter No 2

LANGUAGES

Theory of Computation

Mr. Zaheed Ahmed

2

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)

Theory of Computation

Mr. Zaheed Ahmed

3

Zaheed Ahmed 4 . Σ = {i. Example 1.Alphabets Definition A finite non-empty set of symbols (called letters). j . is called an alphabet. b} 2. It is denoted by Σ (Greek letter sigma).1} (important as this is the language which the computer understands) 3. k} etc. Σ = {0. Σ = {a. Theory of Computation Mr.

Example If Σ= {a. abab.b} then a. 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. ababababababababab  Theory of Computation Mr. aaabb.Strings Σ (alphabet) includes letters. Zaheed Ahmed 5 .

Strings Empty string or null string  Sometimes a string with no symbol at all is used.  The Λ will mostly be used to denote the empty string.  It is denoted by small Greek letter Lambda λ OR capital Greek letter Lambda Λ. Theory of Computation Mr. in further discussion. Zaheed Ahmed 6 . called an empty string or null string.

. Theory of Computation Mr. xx. Zaheed Ahmed 7 . . . .Words Definition Words are strings belonging to some language. 3. but not all strings are words. } or L = {x. . } Here x. … are the words of L Note: All words are strings. 2. . Example If Σ = {x} then language L can be defined as L = {xn : n =1. xxx. xx.

aB. bab. (B)  (B). (bab). d} then a string BababB can be tokenized in two ways  (Ba). It may contain letters consisting of group of symbols Example1 Σ1 = {B. Zaheed Ahmed 8 . bab. (abab). Example 2 If Σ2 = {B.Valid/In-valid Alphabets While defining an alphabet. (B) Theory of Computation Mr. Ba. d}.

Zaheed Ahmed 9 . Theory of Computation Mr. 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. First symbol B is identified as a letter belonging to Σ.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).

a letter may be ended in a letter of same alphabet. One letter should not be the prefix of another. Zaheed Ahmed 10 . no letter should be started with the letter of the same alphabet i.e. Theory of Computation Mr.Remarks    While defining an alphabet of letters consisting of more than one symbols. However.

bab. bab. Zaheed Ahmed 11 .Examples of Valid and In-Valid Alphabets Example 1 Σ1 = {B. Theory of Computation Mr. aB. d} Σ2 is an in-valid alphabet. d} Σ1 is a valid alphabet Example 2 Σ2 = {B. Ba.

Zaheed Ahmed 12 . denoted by |s|. d} and s = BaBbabBd then Tokenizing = (B). Example 1 If Σ = {a. b} and s = ababa. bab. (d) And |s| = 5 Theory of Computation Mr. aB. (bab). (aB). then|s| = 5 Example 2 Σ = {B. is the number of letters in the string. (B).Length of Strings Definition (Length of Strings) The length of string s.

Zaheed Ahmed 13 . d} and s = BaBbabBd then Rev(s) = dBbabaBB Theory of Computation Mr. bab. c} then Rev(s) = cba Example 2 Σ = {B. Example 1 If s = abc is defined over Σ = {a. aB.Reverse of a String Definition The reverse of a string s denoted by Rev(s) is obtained by writing the letters of s in reverse order. b.

 Recursive definition. such as  Descriptive definition.Defining Languages The languages can be defined in different ways .  Using Regular Expressions (RE) and  Using Finite Automaton (FA) etc. Zaheed Ahmed 14 . Theory of Computation Mr.

Descriptive Definition of Language The language is defined. c}. defined over Σ = {a}. . . bc. b. bb. can be written as L = {b. } Theory of Computation Mr. . aaa. ba. . aaaaa. . c. Zaheed Ahmed 15 . defined over Σ = {a. ca. Example 1 The language L of strings of odd length. can be written as L = {a. } Example 2 Language L. of strings that does not start with a. . describing the conditions imposed on its words.

11. 2}. 1.12.10.20. 02.Descriptive Definition of Language The language L of strings of length 2.21. Zaheed Ahmed 16 . 01. can be written as L = {00.22} Mathematical Description L = {s : S | Length(s) = 2 } Theory of Computation Mr. defined over Σ = {0.

defined over Σ = {0. . Zaheed Ahmed 17 . can be written as {Λ. ab. abab. 110. 010. 100. 10. } Theory of Computation Mr. 00.abba. . b}. . aabb. of strings with number of a’s equal to number of b’s. can be written as L= {0. baba.} Example 5 The language EQUAL.More Examples Example 4 The language L of strings ending in 0. . 000. . . 1}. defined over Σ = {a.

. . b}. } Theory of Computation Mr. bbaa. 4. can be written as {Λ. Zaheed Ahmed 18 . 2. . 1. bb. baba. can be written as INTEGER = {. -2. 3. . abba. 6. -1. 0. 0. . . 9}. . 7. of strings defined over Σ = {-. 8. 1. aa. aabb. 2. bbbb. baab. abab. of strings with even number of a’s and even number of b’s. .} Example 7 The language INTEGER. 5.More Examples Example 6 The language EVEN-EVEN. aaaa. defined over Σ = {a.

. . 7. . of stings defined over Σ = {-. . 4. 0. 9} is written as EVEN = {. 2. . .. . 4. 5. 3. 2.More Examples Example 8 The language EVEN. } Theory of Computation Mr. . of strings defined over Σ = {a. Zaheed Ahmed 19 . . 1. b}. aaabbb. 3. . 6. 0.} Example 9 The language {anbn }. aabb. aaaabbbb. 2. is {anbn | n = 1. -2. } can be written as {ab. 8. -4. .

2. ..} Example 11 The language factorial. 2. of strings defined over Σ = {0. 9} is {1. aabbaa. b}.More Examples Example 10 The language {anbnan}. 24. . 2. Zaheed Ahmed 20 . . 3. 3. 5. 6. aaaabbbbaaaa. .} Theory of Computation Mr. can be written as : {aba. 120. 7. 1. is {anbnan| n = 1. . of strings defined over Σ = {a. 1. 6.…}. aaabbbaaa. 4. 8.

. . . is {an! | n = 1. }. . . aa. of strings defined over Σ = {a}. 2. 3. Theory of Computation Mr. Zaheed Ahmed 21 . . aaaaaa.More Examples Example 12 The language FACTORIAL.} Note: It is to be noted that the language FACTORIAL can be defined over any single letter alphabet. which can be written as {a.

. 2. . aaaaaabbbbbb.More Examples Example 13 The language DOUBLEFACTORIAL.}. . . Zaheed Ahmed 22 .…} Example 14 The language SQUARE. b}. 3. which can be written as {ab. . and can be written as: {a. 3.} Theory of Computation Mr. aabb. aaaaaaaaa. of strings defined over Σ = {a. . 2. is {an2 n = 1. aaaa. of strings defined over Σ = {a}. . is {an!bn! | n = 1. .}. .

aaaabbbb. . is {an2 bn2 | n = 1. is {ap | p is prime}. . b}. aaaaa.} Example 16 The language PRIME. . 3.More Examples Example 15 The language DOUBLESQUARE. aaa. aaaaaaaaabbbbbbbbb. . .}. . . . can be written as {aa. Zaheed Ahmed 23 .} Theory of Computation Mr. . aaaaaaa. of strings defined over Σ = {a}. of strings defined over Σ = {a. which can be written as {ab. 2.

aa.} Theory of Computation Mr. aba. b. Example 17 For Σ = {a. aaa. .More Examples An Important language PALINDROME The language consisting of Λ and the strings s defined over Σ such that Rev(s) = s. . Zaheed Ahmed 24 . a. . It is to be denoted that the words of PALINDROME are called palindromes. b} PALINDROME = {Λ . bbb. bb. bab.

Lexicographical Order Assume that the symbols in Σ are themselves ordered. Theory of Computation Mr.  Definition: A set of strings is in lexicographical order if  - - The strings are grouped first according to their length. within each group. Then. Zaheed Ahmed 25 . the strings are ordered “alphabetically” according to the ordering of the symbols.

…. bbb. } Theory of Computation Mr. a. bbbb. bb. ba. aaa. b. …. ab. aa.Lexicographical Order  Example: Let the alphabet be Σ = {a. b}  The set of all strings in Lexicographical order is  { . aaaa.. …. Zaheed Ahmed 26 .

the following is to be noted Analysis Note that number of strings of length ‘m’ defined over alphabet of ‘n’ letters is nm.Theorem Theorem There are as many palindromes of length 2n as there are of length 2n-1. Note Before proving the above theorem. Zaheed Ahmed 27 . Theory of Computation Mr.

defined over Σ = {a. bb} i. number of strings = 8 = 23 Theory of Computation Mr.Theorem Example 1 The language of strings of length 2.e. bba. aab. b} is L = {aa.e. abb. b} is L = {aaa. ba. ab. baa. bbb} i. bab. aba. defined over Σ = {a. Zaheed Ahmed 28 . number of strings = 4 = 22 Example 2 The language of strings of length 3.

Proof  To calculate the number of palindromes of length(2n). consider the following diagram. Zaheed Ahmed 29 . Theory of Computation Mr.

Zaheed Ahmed 30 . the required number of palindromes are 2n.e. To calculate the number of palindromes of length (2n-1) with ‘a’ as the middle letter. consider the following diagram Theory of Computation Mr.Proof   It shows that there are as many palindromes of length 2n as there are strings of length n i.

Proof Theory of Computation Mr. Zaheed Ahmed 31 .

Theory of Computation Mr. with ‘ b ’ as middle letter. the required number of palindromes are 2n-1. will be 2n-1 as well. Zaheed Ahmed 32 . Hence the total number of palindromes of length 2n-1 will be = 2n-1 + 2n-1 = 2n-1(1 + 1) = 2n-1(2) = 2n-1+1 =2n.Proof    The above diagram shows that there are as many palindromes of length 2n-1 as there are strings of length n-1 i. Similarly the number of palindromes of length 2n1.e.

Theory of Computation Mr. This is the most important tool in our whole study.Constructive Algorithm   The method of proving that something exists by showing how to create it is called proof by constructive algorithm. Zaheed Ahmed 33 .

Summary         Introduction to the course title Formal and In-formal languages Alphabets. Words Valid and In-valid alphabets Length of a string Reverse of a string Defining languages Descriptive definition of languages Theory of Computation Mr. Strings. Zaheed Ahmed 34 . Null string.

INTEGER. EVEN-EVEN. EVEN { an bn} {an bn an } Factorial FACTORIAL DOUBLE FACTORIAL SQUARE DOUBLE SQUARE PRIME PALINDROME Theory of Computation Mr.Summary           EQUAL. Zaheed Ahmed 35 .