You are on page 1of 46

REGULAR LANGUAGES AND

REGULAR GRAMMARS
Rao Wakeel Ahmad
2

REGULAR LANGUAGES
• A language is regular if there exists a finite accepter
for it. Therefore, every regular language can be
described by some dfa or some nfa.

• There are some other ways of representing regular


languages like Regular Expression. These
representations have important practical applications,
3

Regular Expression (RE)


• One way of describing regular languages is via the
notation of regular expressions.

• This notation involves a combination of strings of


symbols from some alphabet Σ, parentheses, and the
operators +, ., and *, remember following
1. Union denoted by +
2. Concatenation by .

3. Star-closure by *
4

Formal Definition of RE
• Let Σ be a given alphabet. Then

1. Ø, λ and a ∈ Σ are all regular expressions. These are


called primitive regular expressions.

2. If r1 and r2 are regular expressions, so are r1+r2,


r1.r2, and (r1).

3. A string is a regular expression if and only if it can


be derived from the primitive regular expressions
by a finite number of applications of the rules in (2).
5

Regular Expression: An Example


The expression (a + (b·c))* stands for

{λ, a, bc, aa, abc, bca, bcbc, aaa, aabc,…}


6

Languages Associated RE
• Regular expressions can be used to describe some
simple languages. If r is a regular expression, we will
let L(r) denote the language associated with r.
7

Languages Associated RE
The language L(r) denoted by any regular expression r is
defined by the following rules.
1. Ø is a regular expression denoting the empty set,
2. λ is a regular expression denoting {λ}.
3. For every a ∈ Σ, a is a regular expression denoting {a}.
4. L (r1 + r2) = L (r1)∪ L (r2)
5. L (r1 · r2) = L (r1) ∪ L (r2)
6. L ((r1)) = L (r1)
7. L ( ) = (L (r1))*
Regular Expression
Regular Expression
Building Regular Expressions
Assume that ∑ = {a, b, c}

1. Zero or more: a* means “zero or more a’s”.

Similarly to say “zero or more ab’s,” i.e., {^, ab,


abab,…………} you need to say (ab)*.
Building Regular Expressions
Assume that ∑ = {a, b, c}

2. One or more: Since a* means “zero or more a’s”,


you can use aa* (or equivalently a*a) to mean
“one or more a’s”.

Similarly to describe ‘one or more ab’s”, that is {ab,


abab, ababab,…………..}, you can use ab (ab)*.
Building Regular Expressions
Assume that ∑ = {a, b, c}

3. Zero or one: It can be described as an optional ‘a’


with (a + ^).
Building Regular Expressions
Assume that ∑ = {a, b, c}

4. Any string at all: To describe any string at all use


(a + b + c)*.

5. Any nonempty string: This is written any


character from S = {a, b, c} followed by any string at
all: (a + b + c) (a + b + c)*
Building Regular Expressions
Assume that ∑ = {a, b, c}

6. Any string not containing ..........: To describe any


string at all that does not contain an ‘a’ use (b + c)*.

7. Any string containing exactly one ........: To


describe any string that contains exactly one ‘a’ put
“any string not containing an a”, on either side of the
‘a’ like: (b + c)* a(b + c)* .
16

Languages Associated RE

L(r) is the set of all strings on {a, b}, terminated by


either an a or a bb.
17

Languages Associated RE
The expression r =(aa)* (bb)* b

L(r) is the set of all strings with an even number of a’s


followed by an odd number of b’s
18

Languages Associated RE
L(r) = {w ∈ Σ*: w has at least one pair of consecutive
zeros}.

r = (0 + 1)* 00(0 + 1)*


19

Home Practice
1. Find all strings in L((a + b) b (a + ab)*) of length less
than four.

2. Find the language of the expression


((0 + 1) (0 + 1)*)* 00 (0 + 1)*

3. Find the language of the expression


r = (1 + 01)* (0 + 1*)

4. Find a regular expression for the set


{anbm: n ≥ 3,m is even}.
22

Conversion of RE to DFA: Basic Rules


23

Conversion of RE to DFA: Examples


24

Conversion of RE to DFA: Examples


25

Conversion of RE to DFA: Examples


26

Conversion of RE to DFA: Examples


27

Conversion of RE to DFA: Examples


28

Conversion of RE to DFA: Examples


29

Regular Grammars
• A regular grammar is one that is either right-linear or
left linear.
1. A grammar G =(V, T, S, P) is said to be right-linear if
all productions are of the form
A → xB,
A→x
1. A grammar is said to be left-linear if all productions
are of the form
A → Bx,
A → x.
30

Linear Grammars
• The grammar G =({S, A, B}, {a, b}, S, P) with productions

S→A
A → aB|λ,
B → Ab

• The grammar itself is neither right-linear nor left-linear,


and therefore is not regular. The grammar is an example
of a linear grammar
31

Identifying Nonregular Languages


• Regular Languages are the languages that can be
designed using Finite State Machines.

• But due to their limitation there are some languages


that can not be designed using Finite State Machine,
and those languages are known as Not Regular
Languages.

• So how we can prove that a language is not Regular?


32

Identifying Nonregular Languages


• So how we can prove that a language is not Regular?
• Solution: We will be using Pumping Lemma.
33

Pumping Lemma
34

Pumping Lemma
35

Pumping Lemma
36

Pumping Lemma
37

Pumping Lemma
38

Pumping Lemma
39

Pumping Lemma
40

Pumping Lemma
41

Pumping Lemma
42

Pumping Lemma
43

Pumping Lemma
44

Pumping Lemma
45

Pumping Lemma
46

Pumping Lemma

You might also like