0% found this document useful (0 votes)
214 views67 pages

CH 3 - Regular Languages Amd Regular Grammars

The chapter covers regular languages and regular expressions. It defines regular expressions and how they are used to represent regular languages. It also discusses operations on strings and languages, and how to construct finite automata from regular expressions.

Uploaded by

halal.army07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
214 views67 pages

CH 3 - Regular Languages Amd Regular Grammars

The chapter covers regular languages and regular expressions. It defines regular expressions and how they are used to represent regular languages. It also discusses operations on strings and languages, and how to construct finite automata from regular expressions.

Uploaded by

halal.army07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

Formal Languages and Automata Theory --- Chapter 3

Chapter 3 – Regular Languages


And
Regular Grammar

1
Outline
Operations on strings and languages

Regular Expression and Equivalence to FA

Regular Grammar

Language of Regular Grammar

2
Regular Languages
 A language is regular if there exists a finite automata for it.
 The languages which are not accepted by a finite automata are
called non-regular languages.
 All languages are either regular or non-regular, not both.
 Examples
 The languages whose words are in arithmetic progression and need no
comparison are regular: L={a2n, n>=1} words are aa, aaaa, aaaaaa, …
 The languages whose words need some sort of comparison are not
regular: L={anbn,n>=0} here number of a’s must be equal to number of
b’s. for each a we check existence of b; which we can’t using FA.

3
Operations on strings
 Given two strings s
= a1…an and t = b1…bm, we define
their concatenation st = a1…anb1…bm
s = abb, t = cba st = abbcba

 We define sn as the concatenation ss…s n times


s = 011 s3 = 011011011

4
Operations on languages
 The concatenation of languages L1 and L2 is

L1L2 = {st: s  L1, t  L2}

 Similarly, we write Ln for LL…L (n times)


 The union of languages L1  L2 is the set of all strings
that are in L1 or in L2

 Example: L1 = {01, 0}, L2 = {e, 1, 11, 111, …}.


What is L1L2 and L1  L2?
5
Operations on languages
 The star (Kleene closure) of L are all strings made up of
zero or more chunks from L:
L* = L0  L1  L2  …

 This is always infinite, and always contains e

 Example: L1 = {01, 0}, L2 = {e, 1, 11, 111, …}.


What is L1* and L2*?

6
Constructing languages with operations
 Let’s fix an alphabet, say S = {0, 1}
 We can construct languages by starting with simple
ones, like {0}, {1} and combining them

{0}({0}{1})*
all strings that start with 0
0(0+1)*

({0}{1}*)({1}{0}*) 01*+10*

7
Regular expressions
 Regular expressions are an algebraic way to describe
languages.
 They describe exactly the regular languages.
 If E is a regular expression, then L(E) is the language it
defines.
 We’ll describe RE’s and their languages recursively.

A language is regular if it is represented by a regular


expression

8
RE’s: Definition
 Basis 1: If a is any symbol, then a is a RE, and L(a) = {a}.
 Note: {a} is the language containing one string, and that
string is of length 1.
 Basis 2: ε is a RE, and L(ε) = {ε} => The language that
contains just the empty string

 Basis 3: ∅ is a RE, and L(∅) = ∅ => The language that


contains no strings

9
RE’s: Definition – (2)
 Induction 1: If E1 and E2 are regular expressions, then
E1+E2 is a regular expression, and
L(E1+E2) = L(E1)L(E2).
 Induction 2: If E1 and E2 are regular expressions, then
E1E2 is a regular expression, and L(E1E2) = L(E1)L(E2).
Concatenation : the set of strings wx such that w is in L(E1)
and x is in L(E2).

10
RE’s: Definition – (3)
 Induction 3: If E is a RE, then E* is a RE, and
L(E*) = (L(E))*.

Closure, or “Kleene closure” = set of strings


w1w2…wn, for some n > 0, where each wi is in L(E).
Note: when n=0, the string is ε.

11
Precedence of Operators
 Parentheses may be used wherever needed to
influence the grouping of operators.
 Order of precedence is:
 * (Kleene closure)….[highest priority],
 then concatenation,
 then + (Union) ….[lowest].

12
Example of RE and corresponding RL
 ( a + b )2 corresponds to the language {aa, ab, ba, bb}, that is the set of
strings of length 2 over the alphabet {a, b}.

 In general ( a + b )k corresponds to the set of strings of length k over the


alphabet {a, b}.

 ( a + b )* corresponds to the set of all strings over the alphabet


{a, b}.

 a*b* corresponds to the set of strings consisting of zero or more a's


followed by zero or more b's.

 a*b+a* corresponds to the set of strings consisting of zero or more a's


followed by one or more b's followed by zero or more a's.
13
Example of RE and corresponding RL
 ( ab )+ corresponds to the language {ab, abab,
ababab, ... }, that is, the set of strings of repeated ab's.

 Note: A regular expression is not unique for a


language. That is, a regular language, in general,
corresponds to more than one regular expressions.

 For example ( a + b )* and ( a*b* )* correspond to the set


of all strings over the alphabet {a, b}.(Reading: CHECK!)

14
Examples: RE’s
 (01) = {01}.
 (01+0) = {01, 0}.
 (0(1+0)) = {01, 00}.
 Note order of precedence of operators.
 (0*) = {ε, 0, 00, 000,… }.
 ((0+10)*1) = all strings of 0’s and 1’s without two
consecutive 1’s.
 01* = {0, 01, 011, 0111, …..}
 (01*)(01) = {001, 0101, 01101, 011101, …..}

15
Examples
 Construct a RE over  = {0,1} that represents
 All strings that have two consecutive 0s.

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

 All strings except those with two consecutive 0s.

(1*01)*1* + (1*01)*1*0

 All strings with an even number of 0s.


(1*01*01*)*

16
Examples
 The set of strings over {0,1} that end in 3 consecutive 1's.
(0 + 1)* 111

 The set of strings over {0,1} that have at least one 1.


0* 1 (0 + 1)*

 The set of strings over {0,1} that have at most one 1.


0* + 0* 1 0*

 All strings over {0,1} that start and end with the same digit
0(0+1)*0 + 1(0+1)*1 + 0 + 1
17
RE and DFA
 For every regular expression, we have to give a DFA for
the same language

regular eNFA NFA DFA


expression

 For every DFA, we give a regular expression for the


same language

18
Constructing a FA from a RE
 If the operand is a character c, then our FA has two
states, q0 (the start state) and qf (the final, accepting
state), and a transition from q0 to qf with label c.

 If the operand is epsilon, then our FA has two states, q0


(the start state) and qf (the final state), and an epsilon
transition from q0 to qf.

 If the operand is null, then our FA has two states, q0 (the


start state) and qf (the final state), and no transitions.
19
General method
regular expr eNFA

Æ q0 qf

e q0
e qf

symbol c q0 c qf

20
Constructing a FA from a RE
 Given FA for R1 and R2, we now see how to build a FA for
R1R2, R1+R2, and R1*.
 Let A (with start state a0 and final state aF) be the machine accepting L(R1)
and
 B (with start state b0 and final state bF) be the machine accepting L(R2).
 The machine C accepting L(R1R2) includes A and B, with start state
a0, final state bF, and an epsilon transition from aF to b0.
 The machine C accepting L(R1+R2) includes A and B, with a new start
state c0, a new final state cF, and epsilon transitions from c0 to a0
and b0, and from aF and bF to cF.
 The machine C accepting L(R1*) includes A, with a new start state
c0, a new final state cF, and epsilon transitions from c0 to a0 and cF,
and from aF to a0, and from aF to cF.
21
General method continued
regular expr eNFA

R1R2 q0  MR1   qf
MR2

MR1 

q0 qf
R1 + R2
 
MR2



R1* q0   qf
MR1
22
Examples: regular expression → eNFA
 R1 =0 R2 = 1 1
q0 0 qf q0 qf

q2 0 q3
 R3 =0 + 1  
q0 qf
=R1+R2
 q4 1 q5 

23
Examples: regular expression → eNFA

q2 0 q3 M3
 
 R3 =0+1 q0 q1
 q4 1 q5 

 R4 = (0 + 1)*
=R3* 

q’0  M3  q’1

24
Convention
 When we draw a box around an eNFA:

 The arrow going in points to the start state


 The arrow going out represents all transitions going out of
accepting states
 None of the states inside the box are accepting states.
 The labels of the states inside the box are distinct from all
other states in the diagram

25
Converting FA to RE
 Given a DFA F, we construct a regular expression R such
that L(F) == L(R).

 We preprocess the FA, turning the labels on transitions


into regular expressions.

 Ifthere is a transition with label {a,b}, then we replace


the label with the regular expression a + b.

 Foreach accepting state qf in F, eliminate all states in F


except the start state q0 and qf. 26
Converting FA to RE
 To eliminate a state qE, consider all pairs of states qA
and qB such that there is a transition from qA to qE with
label R1, a transition from qE to qE with label R2
(possibly null, meaning no transition), and a transition
from qE to qB with label R3. Introduce a transition from
qA to qB with label R1R2*R3.

 Ifthere is already a transition from qA to qB with label


R4, then replace that label with R4+R1R2*R3.

27
Converting FA to RE
 After eliminating all states except q0 and qf:

 If
q0 == qf, then the resulting regular expression is R1*,
where R1 is the label on the transition from q0 to q0.

 Ifq0 != qf, then assume the transition from q0 to q0 is


labeled R1, the transition from q0 to qf is labeled R2, the
transition from qf to qf is labeled R3, and the transition
from qf to q0 is labeled R4. The resulting regular
expression is R1*R2(R3 + R4R1*R2)*
28
Converting FA to RE
 Let RFi be the regular expression produced by
eliminating all the states except q0 and qfi, and

 Ifthere are n final states in the DFA, then the regular


expression that generates the strings accepted by the
original DFA is RF1 + RF2 + ... RFn.

29
Example
 Construct regular expression for these FA:
0 1 1 0,1
0
1
q1 q2 1 q1 0 q2
q0
0

e+0*+(0*11*00*) * 0*11*

30
Example
 Construct regular expression for these FA:
1 0 0 0,1

q0 0 q1 1 1 q2
q0 q1
e
1
q2

01*+1 0*10*1(0+1) *

31
Example
 Construct FA for these regular expressions :
0*1*2 * 1*0(1*+01*0) *

0 1 2 1 1

e e 0
q0 q1 q2 q0 q1
0

32
Homework
Construct RE for the following Finite Automata

33
Regular Grammars

34
Grammars
For natural languages, each has its own grammar.
An Amharic sentence follows Amharic language
grammar.
For programming languages, A C++ program has to
obey the C++ programming grammar.
A regular language can be accepted by a finite state
automaton and denoted by a regular expression.
In this section, we shall show that a regular language
can be generated by a regular grammar.

35
Grammars
 Grammars express languages

 Example: The English language


sentence  noun _ phrase predicate

noun _ phrase  article noun

predicate  verb 36
 article  a
article  the

noun  boy
noun  dog

verb  runs
verb  walks
37
 A derivation of “the boy walks”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 the noun verb
 the boy verb
 the boy walks
38
 A derivation of “a dog runs”:

sentence  noun _ phrase predicate


 noun _ phrase verb
 article noun verb
 a noun verb
 a dog verb
 a dog runs
39
 Language of the grammar:
L = { “a boy runs”,
“a boy walks”,
“the boy runs”,
“the boy walks”,
“a dog runs”,
“a dog walks”,
“the dog runs”,
“the dog walks”
} 40
Notation

noun  boy
noun  dog

Variable Terminal
or Production
Non-terminal rule

41
Another Example
 Grammar: S  aSb
S 

 Derivation of sentence : ab
S  aSb  ab

S  aSb S 
42
 Grammar: S  aSb
S 

 Derivation of sentence : aabb


S  aSb  aaSbb  aabb

S  aSb S 
43
 Other derivations:

S  aSb  aaSbb  aaaSbbb  aaabbb

S  aSb  aaSbb  aaaSbbb


 aaaaSbbbb  aaaabbbb

44
Grammars
Definition: A grammar G = (V, T, P, S) is defined
as follows.
(1) V is a finite set of variables. S is the start
symbol in V.
(2) T is a finite set of terminals, and T  V =
.
(3) P is a finite set of productions or rewriting
rules. Each production is of the form:

A  aB, where A, B  V and a  T, or


A  a, where A  V and a  T.

45
A Non-Linear Grammar
Has the production rules like:
S  SS
S 
S  aSb
S  bSa

46
Linear Grammars
Grammars with at most one variable at the right side of a
production

S  Ab
Examples: S  aSb
A  aAb
S 
A

47
Another Linear Grammar

Grammar:
G SA
A  aB | 
B  Ab

48
Right-Linear Grammars (RLG)
All productions have the form:

A  xB or A x

Example: string of
terminals
S  abS
S a

49
Left-Linear Grammars (LLG)
All productions have the form:

A  Bx or A x
Example:
S  Aab string of
terminals
A  Aab | B
Ba
50
Regular Grammars
A regular grammar is any
right-linear or left-linear grammar

Examples:
G1 G2
S  abS S  Aab
S a A  Aab | B
Ba
51
Language of a Regular Grammar
The language generated by a regular grammar
G = (V, T, P, S) is denoted by L(G).
Example: Find L(G) for G = (V, T, P, S), where V = {S, A,
B}, T = {0, 1} and P contains the following productions:
S  0A | 1B | 1
A  0S | 1B | 1
B  0B | 1A | 0

52
Language of a Regular Grammar
Solution: The set generated by a regular grammar G
is Produced by applying the production rules until all
the variables are reduced to terminals.
P: S  0A | 1B | 1, A  0S | 1B | 1, B  0B | 1A | 0

S  0A S  1B S  0A
 01B  10B  01
 010B  100
 0101A
 01011
{  {0, 1}* |  has odd number of 1’s}.
53
Example
S → aS | bA
A → cA | ε

This grammar produces the language produced by the


regular expression a*bc*

S → aS → aaS → … → a…aS →a…abA →a…abcA


→ a…abccA → … → a…abc…c

54
Reg. Grammars Vs Reg. Languages
Right Linear Grammars are equivalent to εNFA
Two directions:
1. Given a Right Linear grammar, we can construct an
εNFA that recognizes the same language with the
Right Linear grammar.

2. Given an εNFA, we can construct a Right Linear


grammar that describes the same language with the
εNFA.

55
1. RLG→ εNFA
Suppose that we have a right linear grammar (V, Σ, P,
S). construct an εNFA (Q, Σ, δ, S, {f}).
 The set of states Q will be the set VU{f}, where f is a
new symbol denoting the final state
 Productions in P have three possible forms:
 A → ε : add the transition δ(Α,ε) = f
 A → a : add the transition δ(A,a) = f
 A → aB : add the transition δ(Α,a) = B

56
2. εNFA → RLG
Suppose that We have an εNFA (Q, Σ, δ, q0, F,). construct
a right linear grammar (Q, Σ, P, q0).
 For each transition δ(qi ,a) = qj, construct the rule qi →
aqj in P.

 Furthermore, for every state qi in F, add the rule qi → ε


in P.

57
Examples

1) Transform the following Right Linear


grammar into an equivalent εNFA.
S → aS |bA
A → cA | ε

Solution:

a c

S b Α ε f

58
Examples

2) Transform the following DFA to a right linear


grammar
b

q0 a q1

b a
Solution:
Q0 → aQ1 | bQ0
Q1 → aQ1 |bQ0 | ε

59
Left Linear Grammars
• Left Linear Grammars also produce the Regular
Languages but not straightforward.

• Actually, a Left Linear grammar produces the reverse


of the language produced by the Right Linear grammar
in which we reversed the rules:

• A → Ba to A →aB.

60
Example

C → Bc
B → Ab
A→a
The derivation of abc is:
C → Bc → Abc → abc
The Right Regular grammar with the rules of the form A
→ Ba reversed is
C → cB
B → bA
A→a
and it produces the reverse language.
61
Example (continued)

 So, just create the εNFA for the language


produced by the Right Regular grammar and
then compute the reverse (change start with
final state and reverse the arrows).

 This
results in an εNFA for the Left Regular
grammar.

62
Note:

(1)A DFA can recognize a regular language.


(2)A regular expression can represent a regular
language.
(3)A regular grammar can generate a regular
language.

63
Exercise

Given the following English grammar.


S →NP VP Answer:
VP → V NP S ⇒NP VP
NP → NAME ⇒ NAME VP
NP → DET N ⇒ Abebe VP
⇒ Abebe V NP
NAME → Abebe
⇒ Abebe killed NP
V → killed ⇒ Abebe killed DET N
DET → the ⇒ Abebe killed the N
N → lion ⇒ Abebe killed the lion

Show the generaiton of the sentence


Abebe killed the lion
64
Home work
1. Create a regular grammar for the language
of strings over {a, b} which contains the
substring aa.

2. Create NFA’s for the following languages and


construct the corresponding regular
grammars. Use S as the first non-terminal.
(a) (1+0)∗01∗

(b) {w| w ∈ {0,1} ∗ and w doesn’t end in 01}


65
Home work
3. Construct the equivalent DFA for the grammar having the
following productions:
A →0A|1B
B →0C|1B
C →0B|1D| ε
D →0D|1D

4. Construct the RLG and LLG for the regular expression:


a(a+b)*ab.

Hint: for LLG, reverse the regular expression, find its RLG, then
change the terminals with variables in the right side of P.
66
Home work
 State and Prove the Pumping Lemma?
 Why we use it for?
 How do we use it?

67

You might also like