CH 3 - Regular Languages Amd Regular Grammars
CH 3 - Regular Languages Amd Regular Grammars
1
Outline
Operations on strings and languages
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
4
Operations on languages
The concatenation of languages L1 and L2 is
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.
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
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))*.
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}.
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)*
(1*01)*1* + (1*01)*1*0
16
Examples
The set of strings over {0,1} that end in 3 consecutive 1's.
(0 + 1)* 111
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
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.
Æ 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:
25
Converting FA to RE
Given a DFA F, we construct a regular expression R such
that L(F) == L(R).
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.
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
predicate verb 36
article a
article the
noun boy
noun dog
verb runs
verb walks
37
A derivation of “the boy walks”:
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
S aSb S
43
Other derivations:
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:
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 SA
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
Ba
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
Ba
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 | ε
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.
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.
57
Examples
Solution:
a c
S b Α ε f
58
Examples
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.
• 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)
This
results in an εNFA for the Left Regular
grammar.
62
Note:
63
Exercise
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