Professional Documents
Culture Documents
Lecture#02, Recursive Definitions, Regular Expressions
Lecture#02, Recursive Definitions, Regular Expressions
Lecture#02
Recursive Definition of a Language, Regular Expressions
Lecture Contents
Recursive definition of languages
Languages defined through recursive definition
RE
Recursive definition of RE
defining languages by RE
{ x}*, { x}+, {a+b}*
Recursive Definition
It is a way of defining a set (a language) recursively.
Process:
1. Basic elements are identified and listed in rule 1. These elements are the ones
that cannot be defined recursively.
2. Rule 2 defines the way new elements are built based on existing elements
Recursive Definition of a Language
The following three steps are used in recursive definition
1. Some basic words are specified in the language
2. Rules for constructing more words are defined in the language
3. No strings except those constructed in above, are allowed to be in the language
Example
Defining language of INTEGER
Step 1: 1 is in INTEGER.
Step 2: If x is in INTEGER then x+1 and x-1 are also in INTEGER.
Step 3: No strings except those constructed in above, are allowed to be in
INTEGER.
Recursive Definition of a Language …
Example
Defining language of EVEN
Step 1: 2 is in EVEN.
Step 2: If x is in EVEN then x+2 and x-2 are also in EVEN.
Step 3: No values except those constructed in above, are allowed to be in EVEN.
Example
Defining the language factorial
Step 1: As 0!=1, so 1 is in factorial.
Step 2: if x! is in factorial then (x+1)*x! is also in factorial.
Step 3: No values except those constructed in above, are allowed to be in
factorial.
Recursive Definition of a Language …
Example
Defining the language PALINDROME, defined over Σ = {a, b}
Step 1: ^, a and b are in PALINDROME
Step 2: if x is in PALINDROME, so are axa, bxb
Step 3: No strings except those constructed in above, are allowed to be in
palindrome
Example
Defining the language {anbn }, n=1,2,3,… , of strings defined over Σ={a,b}
Step 1: ab is in {anbn}
Step 2: if x is in {anbn}, then axb is in {anbn}
Step 3: No strings except those constructed in above, are allowed to be in {anbn}.
Recursive Definition of a Language …
Example
Defining the language L, of strings ending in a , defined over Σ={a,b}
Step 1: a is in L
Step 2: if x is in L then s(x) is also in L, where s belongs to Σ*
Step 3: No strings except those constructed in above, are allowed to be in L
OR
Step 1: a is in L
Step 2: if x is in L, so are ax, bx
Step 3: No strings except those constructed in above, are allowed to be in L
Recursive Definition of a Language …
Example
Defining the language L, of strings beginning and ending in same letters , defined
over Σ={a, b}
Step 1: a and b are in L
Step 2: (a)s(a) and (b)s(b) are also in L, where s belongs to Σ*
Step 3: No strings except those constructed in above, are allowed to be in L
Recursive Definition of a Language …
Example
Defining the language L, of strings containing aa or bb , defined over Σ={a, b}
Step 1: aa and bb are in L
Step 2: s(aa)s and s(bb)s are also in L, where s belongs to Σ*
Step 3: No strings except those constructed in above, are allowed to be in L
Example
Defining the language L, of strings containing exactly one a, defined over Σ={a, b}
Step 1: a is in L
Step 2: s(a)s is also in L, where s belongs to b*
Step 3: No strings except those constructed in above, are allowed to be in L
Regular Expression (RE)
As discussed earlier that a* generates Λ, a, aa, aaa, … and a+ generates a, aa, aaa,
aaaa, …, so the language L1 = {Λ, a, aa, aaa, …} and L2 = {a, aa, aaa, aaaa, …} can
simply be expressed by a* and a+, respectively.
a* and a+ are called the regular expressions (RE) for L1 and L2 respectively.
a+b+ab+ba+bba+abb
((a+b)(a+b))*
3. Now consider another language L3, of odd length, defined over Σ = {a, b},
then it’s regular expression may be:
(a+b)((a+b)(a+b))* or ((a+b)
(a+b))*(a+b)
Regular Expression Examples
Consider the language, defined over Σ = {a , b} of words having at least one
a, may be expressed by a regular expression RE where RE=?
(a+b)*a(a+b)*
Regular Expression Examples
Consider the language, defined over Σ = {a, b} of words having at least one a
and one b, may be expressed by a regular expression
(a+b)*a(a+b)*b(a+b)*+ (a+b)*b(a+b)*a(a+b)*
Regular Expression Examples
Consider the language, defined over Σ ={a, b}, of words starting with double
a and ending in double b then its regular expression may be aa(a+b)*bb
Regular Expression Examples
Consider the language, defined over Σ ={a, b} of words starting with a and
ending in b OR starting with b and ending in a, then its regular expression
may be a(a+b)*b+b(a+b)*a
Languages Defined Through RE
All languages below are defined over Σ = {a,b}
Language containing no word at all
Languages containing only words aa, aba, bab, baba, baa
Language of all words containing any combination a’s only
Language of all words or length two or less
Language of all words of length four
Language of all words with count of a’s three or more
Language of all words of even length
Language of all words ending on b
Languages Defined Through RE
All languages below are defined over Σ = {a, b}
Language containing no words starting and ending on a
Languages containing only words starting and ending on same letter
Language of all words with a appears in pairs only
Language of all words not ending on b
Language of all words with count of a as multiple of three
EVEN-EVEN
23
Regular Expressions
Regular
expressions
= Finite Automata
(DFA, NFA, -NFA)
Syntactical
expressions Automata/machines
Regular
Languages
Formal language
classes
24
Precedence of Operators
Highest to lowest
* operator (star)
. (concatenation)
+ operator
Example:
01* + 1 = ( 0 . ((1)*) ) + 1
25
Algebraic Laws…
Distributive:
E(F+G) = EF + EG
(F+G)E = FE+GE
Idempotent: E + E = E
Involving Kleene closures:
(E*)* = E*
Φ* =
* =
E+ =EE*
27
True or False?
Let R and S be two regular expressions. Then:
1. ((R*)*)* = R* ?
2. (R+S)* = R* + S* ?
3. (RS + R)* RS = (RR*S)* ?
Difference between a*+b* AND (a+b)*
Discuss the difference between two
Equivalent REs
Two regular expressions are said to be equivalent if they generate the
same language