Lecture 2

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

Software Construction

Lec 02
Recursive Definitions
Previous Lecture
• Introduction to defining computer languages
• Alphabets
• Valid/In-valid alphabets
• Length of string
• Reverse of String
• Definition of different languages defined over
alphabets
Kleene Star Closure(*)
• Given Σ, then the Kleene Star Closure of the alphabet Σ, denoted by
Σ*, is the collection of all strings defined over Σ, including Λ.

• It is to be noted that Kleene Star Closure can be defined over any set
of strings.
• Examples
 If Σ = {a}
• Then Σ* = {Λ, a, aa, aaa, aaaa, ….}
 If Σ = {0,1}
• Then Σ* = {Λ, 0, 1, 00, 01, 10, 11, ….}
 If Σ = {aaB, c}
• Then Σ* = {Λ, aaB, c, aaBaaB, aaBc, caaB, cc, ….}
• Note
• Languages generated by Kleene Star Closure of set of strings, are
infinite languages. (except for the two cases coming ahead) (By
infinite language, it is supposed that the language contains infinite
many words, each of finite length).
Kleene Star Closure(*)
 If Σ = ¢
• Then Σ* = {Λ}
 If S = {Λ}
• Then Σ* = {Λ}
• Because Λ. Λ = Λ
• The Kleene closure of two sets can end up being the
same language.
• Example:
 S ={a b ab}, T={a b bb}
• S*= ………………. (language of all strings of a’s and b’s)
• T*= ………………. (language of all strings of a’s and b’s)
• Also (S*)* = S**
PLUS Operation (+)

• Plus Operation is same as Kleene Star Closure except


that it does not generate Λ (null string),
automatically.
• Examples
 If Σ = {0,1}
• Then Σ+ = {0, 1, 00, 01, 10, 11, ….}
 If Σ = {aab, c}
• Then Σ+ = {aab, c, aabaab, aabc, caab, cc, ….}
Recursive definition of languages
• The following three Rules are used in recursive definition
Rule 1: Some basic words are specified in the language.
Rule 2: Rules for constructing more words are defined in
the language.
Rule 3: No strings except those constructed in above, are
allowed to be in the language.
• Examples
• Defining language of INTEGER
Rule 1: 1 is in INTEGER.
Rule 2: If x is in INTEGER then x+1 and x-1 are also in
INTEGER.
Rule 3: No strings except those constructed in above, are
allowed to be in INTEGER.
Example Recursive

• Defining language of EVEN


Rule 1: 2 is in EVEN.
Rule 2: If x is in EVEN then x+2 is also in EVEN
Rule 3: No strings except those constructed in
above, are allowed to be in EVEN.
Prove 14 is in EVEN…..
Example Recursive

• Defining language of EVEN


Rule 1: 2 is in EVEN.
Rule 2: If x is in EVEN then x+2 and x-2 are also in EVEN.
Rule 3: No strings except those constructed in
above, are allowed to be in EVEN.
• Prove 14 is in EVEN….
• Note: Whether or not we want a recursive definition
depends on:
i. How easy the other possible definitions are to understand
ii. What types of theorems we wish to prove about the set
Example Recursive
• Defining the language factorial
Rule 1: As 0!=1, so 1 is in factorial.
Rule 2: n!=n*(n-1)! is in factorial.
Rule 3: No strings except those constructed in
above, are allowed to be in factorial.
Example Recursive
Example Recursive
• Defining the language L, of strings ending in a ,
defined over Σ={a,b}
Rule 1: a is in L
Rule 2: if x is in L then s(x) is also in L, where s
belongs to Σ*
Rule 3: No strings except those constructed in
above, are allowed to be in L
Example Recursive
• Defining the language L, of strings beginning and
ending in same letters , defined over Σ={a, b}
Rule 1: a and b are in L
Rule 2: (a)s(a) and (b)s(b) are also in L, where s
belongs to Σ*
Rule 3: No strings except those constructed in
above, are allowed to be in L
Example Recursive
• Defining the language L, of strings containing aa or
bb , defined over Σ={a, b}
Rule 1: aa and bb are in L
Rule 2: s(aa)s and s(bb)s are also in L, where s
belongs to Σ*
Rule 3: No strings except those constructed in
above, are allowed to be in L.
Example Recursive
• Defining the language PALINDROME, defined over Σ
= {a,b}
Rule 1: a and b are in PALINDROME
Rule 2: if x is palindrome, then s(x)Rev(s) and
xx will also be palindrome, where s belongs to Σ*
Rule 3: No strings except those constructed in
above, are allowed to be in palindrome
Example Recursive
Example Recursive
• Defining the language ARITHMETIC EXPRESSIONS
Σ = {0 1 2 3 4 5 6 7 8 9 + - * / ()}
Rule 1: Any number(positive, negative or zero is
in AE)
Rule 2: If x is in AE, then so are
i. (x)
ii. -x(provided x does not already start with a minus sign)
Rule 3: If x and y are in AE, then so are:
i. X+Y
ii. X-Y
iii. X*Y
iv. X/Y
v. X**Y(our notation for exponentiation)
• Solve exercise at the end of chapter 3
Source/s of lecture

• Introduction to Computer theory by Daniel I.A


Cohen (chapter 3, Recursive Definitions)

You might also like