Chapter 3 Regular Languages and Regular Grammars
-
Upload
laurel-morales -
Category
Documents
-
view
48 -
download
7
description
Transcript of Chapter 3 Regular Languages and Regular Grammars
1
Chapter 3Chapter 3
Regular Languages andRegular Languages and
Regular GrammarsRegular Grammars
2
3.1: Regular Expressions (1)3.1: Regular Expressions (1)
Regular Expression (RE): E is a regular expression over if E is one of:
a, where a If r and s are regular expressions (REs), then
the following expressions also regular: r | s (r or s) rs (r followed by s) r* (r repeated zero or more times)
Each RE has an equivalent regular language (RL)
3
3.1: Regular Expressions (2)3.1: Regular Expressions (2)
Regular Language (RL): L is a regular language over if L is one of:
empty set {} a set that contains empty
string a} where a
If R and S are regular languages (RL), then the following languages also regular: R S = {w | w R or w S} RS = {rs | r R and s S} R* = R0 R1 R2 R3 …
4
Rules for Specifying Regular Expressions:Rules for Specifying Regular Expressions:
1. is a regular expression L =
2. If a is in , a is a regular expression L = {a}, the set containing the string a.
3. Let r and s be regular expressions with languages L(r) and L(s). Then
a. r | s is a RE L(r) L(s)
b. rs is a RE L(r) L(s)
c. r* is a RE (L(r))*
d. (r) is a RE L(r), extra parenthesis
precedence
3.1: Regular Expressions (3)3.1: Regular Expressions (3)
5
3.1: Regular Expressions (4)3.1: Regular Expressions (4)
Examples:Examples: {0, 1}{00, 11} = {000, 011, 100, 111} {0} = {, 0, 00, 000, 0000, …} 11}* = {, 10, 1010, 101010, …,
01, 0101, 010101, …, 1001, 100101, 10010101, …, 0110, 011010, 01101010,
…} Notational shorthand:Notational shorthand:
L0 = Li = LLi-1
L+ = LL*
6
3.1: Regular Expressions (5)3.1: Regular Expressions (5)
Let Let LL be a language over {a, b}, each string in be a language over {a, b}, each string in LL contains the substring contains the substring bbbb L = {a, b}*{bb}{a, b}*
L is regular language (RL). Why?L is regular language (RL). Why? {a} and {b} are RLs {a, b} is RL {a, b}* is RL {b}{b} = {bb} is also RL Then L = {a, b}*{bb}{a, b}* is RL
7
3.1: Regular Expressions (6)3.1: Regular Expressions (6)
Let L be a language over {a, b}, each string in L begins and ends with an a contains at least one b L = {a}{a, b}*{b}{a, b}*{a} L = a*b*a
L is regular language (RL). Why? {a} and {b} are RLs {a, b}* is RL Then L = {a}{a, b}*{b}{a, b}*{a} is RL
8
3.1: Regular Expressions (7)3.1: Regular Expressions (7)
L = {a, b}*{bb}{a, b}* RE = (a|b)*bb(a|b)*
L = {a}{a, b}*{b}{a, b}*{a} RE = a(a|b)*b(a|b)*a
This RE = (a)|((b)*(c)) is equivalent to a|b*c
We say REs r and s are equivalent (r=s), iff r and s represent the same language Example: r = a|b, s = b|a r = s Why? Since L(r) = L(s) = {a, b}
9
3.1: Regular Expressions (8)3.1: Regular Expressions (8)
Let Let = {a, b}= {a, b}
RE a|b L = {a, b} RE (a|b)(a|b) L = {aa, ab, ba, bb} RE aa|ab|ba|bb same as above RE a* L = {, a , aa, aaa, …} RE (a|b)* L = set of all strings of
a’s and b’s including
RE (a*b*)* same as above RE a|a*b L = {a,b,ab,aab,aaab, …}
10
3.1: Regular Expressions (9)3.1: Regular Expressions (9)
Algebraic Properties of regular ExpressionsAlgebraic Properties of regular Expressions
AXIOM
r | s = s | r
r | (s | t) = (r | s) | t
(r s) t = r (s t)
r = r = r
r* = (r*)* = ( r | )+ = r+ |
r ( s | t ) = r s | r t( s | t ) r = s r | t r
r** = r*
r+ = r r*
11
3.1: Regular Expressions (10)3.1: Regular Expressions (10)
abcabc concatenation (“followed by”)
a | b | ca | b | c alternation (“or”)
** zero or more occurrences
++ one or more occurrences
12
3.1: Regular Expressions (11)3.1: Regular Expressions (11)
All strings of 1s and 0sAll strings of 1s and 0s(0 | 1)*
All strings of 1s and 0s beginning with a 1All strings of 1s and 0s beginning with a 11 (0 | 1)*
13
3.1: Regular Expressions (12)3.1: Regular Expressions (12)
All strings containing two or more All strings containing two or more 00s s (1|0)*0(1|0)*0(1|0)*
All strings containing an even number of All strings containing an even number of 00s s (1*01*01*)* | 1*
14
3.1: Regular Expressions (13)3.1: Regular Expressions (13)
All strings containing an All strings containing an eveneven number of number of 00s and s and even even number of number of 11ss( 0 0 | 1 1 )*(( 0 1 | 1 0 )( 0 0 | 1 1 )*( 0 1 | 1 0 )( 0 0 | 1 1 )*)*
All strings of alternating All strings of alternating 00s and s and 11ss( | 1 ) ( 0 1 )* ( | 0 )
15
3.1: Regular Expressions (14)3.1: Regular Expressions (14) Strings over the alphabet {0, 1} with Strings over the alphabet {0, 1} with nono
consecutive 0'sconsecutive 0's (1 | 01 )* (0 | ) 1*(01+)* (0 | ) 1*(011*)* (0 | )
Strings over the alphabet {a, b} with Strings over the alphabet {a, b} with exactly three exactly three b'sb's a*ba*ba*ba*
Strings over the alphabet {a, b, c} containing (Strings over the alphabet {a, b, c} containing (at at least onceleast once) ) bcbc (a|b|c)*bc(a|b|c)*
16
3.1: Regular Expressions (15)3.1: Regular Expressions (15)
Strings over the alphabet {a, b} in which Strings over the alphabet {a, b} in which substrings substrings abab and and baba occur an unequal number of occur an unequal number of timestimes (a+b+)+ | (b+a+)+
17
3.1: Regular Expressions (16)3.1: Regular Expressions (16) Describe the following in English:Describe the following in English:
(0|1)* all strings over {0, 1}
b*ab*ab*ab* all strings over {a, b} with exactly 3 a’s
18
3.1: Regular Expressions (17)3.1: Regular Expressions (17)
Examples of RE:Examples of RE:
01* {0, 01, 011, 0111, …..}
(01*)(01) {001, 0101, 01101, 011101, …..}
(0 | 1)* = {0, 1, 00, 01, 10, 11, …..} i.e., all strings of 0 and 1
(0 | 1)* 00 (0 | 1)* = {00, 1001, …..} i.e., all 0 and 1 strings containing a “00”
19
3.1: Regular Expressions (18)3.1: Regular Expressions (18)
More Examples of RE:More Examples of RE:
(1 | 10)* all strings starting with “1” and containing no “00”
(0 | 1)*011 all strings ending with “011”
0*1* all strings with no “0” after “1”
00*11* all strings with at least one “0” and one “1”, and no “0” after
“1”
20
3.1: Regular Expressions (19)3.1: Regular Expressions (19)
What languages do the following RE represent?What languages do the following RE represent?
(1 | 01 | 001)*( | 0 | 00)
((0 | 1)(0 | 1))* | ((0 | 1)(0 | 1)(0 | 1))*
21
3.1: Regular Expressions (20)3.1: Regular Expressions (20)
Home Study:Home Study:
Construct a RE over ={0,1} such that It does not contain any string with two consecutive “0”s It has no prefix with two or more “0”s than “1” nor two or
more “1”s than “0” The set of all strings ending with “00” The set of all strings with 3 consecutive 0’s The set of all strings beginning with “1”, which when
interpreted as a binary no., is divisible by 5 The set of all strings with a “1” at the 5th position from the
right The set of all strings not containing 101 as a sub-string
22
3.1: Regular Expressions (21)3.1: Regular Expressions (21)
Strings over the alphabet {Strings over the alphabet {((, , ))} where the } where the parentheses are balancedparentheses are balanced Not regular
L = {aL = {annbbnn | | nn 0} 0} L = ab, aabb, aaabbb, aaaabbbb, …} Not regular
23
3.2:3.2: Connection Between RE & RL (1)Connection Between RE & RL (1)
A language A language LL is called is called regularregular if and only if there if and only if there exists some DFA M such that L = L(M).exists some DFA M such that L = L(M).
Since a DFA has an equivalent NFA, then Since a DFA has an equivalent NFA, then A language L is called regular if and only if
there exists some NFA N such that L = L(N).
If we have a RE If we have a RE rr, we can construct an NFA that , we can construct an NFA that accept L(accept L(rr).).
24
3.2:3.2: Connection Between RE & RL (2)Connection Between RE & RL (2)
2. For a in the regular expression, construct NFA
astartL = {a}
1. For in the regular expression, construct NFA
startL = {}
0. For in the regular expression, construct NFA
start L = { } =
25
3.2:3.2: Connection Between RE & RL (3)Connection Between RE & RL (3)
where i and f are new start / final states, and -moves are introduced from i to the old start states of Ms and Mt as well as from all of their final states to f.
3.(a) If s and t are regular expressions, Ms and Mt are their NFAs. s|t has NFA:
start i f
Ms
Mt
L = {L(Ms) L(Mt)}
26
3.2:3.2: Connection Between RE & RL (4)Connection Between RE & RL (4)
3.(b) If s and t are regular expressions, Ms, Mt their NFAs. st (concatenation) has NFA:
starti fMs Mt
Alternative:
overlap
Msstart i fMt
where i is the start state of Ms (or new under the alternative) and f is the final state of Mt (or new). Overlap maps final states of Ms to start state of Mt
L = {L(Ms)L(Mt)}
27
3.2:3.2: Connection Between RE & RL (5)Connection Between RE & RL (5)
fMsstart i
where : i is new start state and f is new final state
-move i to f (to accept null string)
-moves i to old start, old final(s) to f
-move old final to old start (WHY?)
3.(c) If s is a regular expressions and Ms its NFA,
s* (Kleene star) has NFA:L = {L(Ms)*}
28
3.2:3.2: Connection Between RE & RL (6)Connection Between RE & RL (6)
Build an Build an NFA-NFA- that accepts that accepts (a|b)(a|b)**babaaa
bb
baba
a|ba|b
astart
abstart q1
bstart
a
b
start
29
3.2:3.2: Connection Between RE & RL (7)Connection Between RE & RL (7)
Build an Build an NFA-NFA- that accepts that accepts (a|b)(a|b)**baba
(a|b)(a|b)**a
b
30
3.2:3.2: Connection Between RE & RL (8)Connection Between RE & RL (8)
Build an Build an NFA-NFA- that accepts that accepts (a|b)(a|b)**baba
ab
a
b
31
3.2:3.2: Connection Between RE & RL (9)Connection Between RE & RL (9)
r13
r12r5
r3 r11r4
r9
r10
r8r7
r6
r0
r1 r2
b
*c
a a
|
( )
b
|
*
c
(ab*c) | (a(b|c*))Decomposition for this regular expression:
What is the NFA? Let’s construct it !
32
3.2:3.2: Connection Between RE & RL (10)Connection Between RE & RL (10)
r3: a
r0: b
r2: c
b
r1:
r4 : r1 r2b
c
r5 : r3 r4
b
a c
33
3.2:3.2: Connection Between RE & RL (11)Connection Between RE & RL (11)
r11: a
r7: b
r6: c
c
r9 : r7 | r8
b
r10 : r9
c
r8:
c
r12 : r11 r10
b
a
34
3.2:3.2: Connection Between RE & RL (12)Connection Between RE & RL (12)
r13 : r5 | r12
b
a c
c
b
a
1
6543
8
2
10
9 12 13 14
11
15
7
16
17
35
3.2:3.2: Connection Between RE & RL (13)Connection Between RE & RL (13)
Let’s try Let’s try aa ( ( bb | | cc ) )**
1. 1. aa, , bb, & , & cc
2. 2. bb | | cc
3. ( 3. ( bb | | cc ) )**
S0 S1 a
S0 S1 b
S0 S1 c
S1 S2 b
S3 S4 c
S0 S5
S2 S3 b
S4 S5 c
S1 S6 S0 S7
36
3.2:3.2: Connection Between RE & RL (14)Connection Between RE & RL (14)
4. 4. aa ( ( bb | | cc ) )**
S0 S1 a
S4 S5 b
S6 S7 c
S3 S8 S2 S9
S0 S1 a
b | c
37
3.2:3.2: Connection Between RE & RL (15)Connection Between RE & RL (15)
Let : a
abb a*b+
3 patterns
NFA’s :
start
start
start
1
b
b
bb
a
a
a
2
3 4 5
87
6
38
3.2:3.2: Connection Between RE & RL (16)Connection Between RE & RL (16)
NFA for : a | abb | a*b+
0
b
b
bb
a
a
a
2
3 4 5
87
6
1
start
39
Regular Expression to NFA-Regular Expression to NFA-
(a | ba)*a
40
First Parsing StepFirst Parsing Step
concatenate
(a|ba)* a
41
Second Parsing StepSecond Parsing Step
concatenate
* a
a|ba
42
Third Parsing StepThird Parsing Step
concatenate
* a
|
a ba
43
Fourth Parsing StepFourth Parsing Step
concatenate
* a
|
a concatenate
b a
44
Identify Leaf NodesIdentify Leaf Nodes
concatenate
+ a
|
a concatenate
b a
45
Convert Leaf NodesConvert Leaf Nodes
concatenate
*
|
concatenate
a
a
a
b
46
Identify Convertible Node(s)Identify Convertible Node(s)
concatenate
*
|
concatenate
a
a
a
b
47
Convert NodeConvert Node
concatenate
*
|
a
a ab
48
Identify Convertible NodeIdentify Convertible Node
concatenate
*
|
a
a ab
49
Convert NodeConvert Node
concatenate
* a
a
ab
50
Identify Convertible NodeIdentify Convertible Node
concatenate
* a
a
ab
51
Convert NodeConvert Node
concatenate
a
a
ab
52
Identify Convertible NodeIdentify Convertible Node
concatenate
a
a
ab
53
Convert Final NodeConvert Final Node
a
a
ab
54
3.2:3.2: Expression Graphs (1)Expression Graphs (1)
NFA to RENFA to RE
If L is accepted by some NFA-, then L is represented by some regular expression
An expression graph is like a state diagram but it can have regular expressions as labels on arcs
An NFA- is an expression graph An expression graph can be reduced to one with
just two states If we reduce an NFA- in this way, the arc label
then corresponds to the regular expression representing it
55
3.2:3.2: Expression Graphs (2)Expression Graphs (2)
wik kwjij i
wji wik kj
wik kwjij i
wji (wii)* wik
kjwii
w
w*start
w1
(w1)*w2(w3 w4(w1)*w2)*start w2
w3
w4
56
3.2:3.2: Expression Graphs (3)Expression Graphs (3)
Merge Edges :
c
b
a
a | b | c
p
ac*b
ba c
Replace state by Edges
q0 q1
q0q1
57
3.2:3.2: Expression Graphs (4)Expression Graphs (4)
Let Let GG be the state diagram of a finite automata be the state diagram of a finite automata Let Let mm be the number of final states of be the number of final states of GG Make Make mm copies of copies of GG, each of which has one final , each of which has one final
state.state. Call these graphsCall these graphs G G11, , GG22, …, , …, GGmm For each For each GGtt
Repeat Do the steps in the previous slide
Until the only states in Gt are the start state and the single final state
Determine the RE of Gt The RE of The RE of GG is obtained by joining RE’s of each is obtained by joining RE’s of each
GGtt by by oror
58
3.2:3.2: Expression Graphs (5)Expression Graphs (5)
c3
c1 2
bG: start
b
c3
c1 2
bG1: start
b
c3
c1 2
bG2: start
b
59
3.2:3.2: Expression Graphs (6)Expression Graphs (6)
c3
c1 2
bG1: start
b
3cc
1
b b
1
b
b*
60
3.2:3.2: Expression Graphs (7)Expression Graphs (7)
c3
c1 2
bG2: start
b
3cc
1
b b
b*ccb*
RE for G b* | b*ccb*
61
3.3 Regular Grammars (1)
A grammar is said to be right-linear if all production rules are of the form: A xB A x Where A, B NT, and x T*
A grammar is said to be left-linear if all production rules are of the form: A Bx A x Where A, B NT, and x T*
A regular grammar is one that either right-linear or left-linear, but not both
62
3.3 Regular Grammars (2)
The grammars G1 is right-linear G1=({S}, {a, b}, S, {S abS | a} L(G1) = {{ab}*a} r = (ab)*a
The grammars G2 is left-linear G2=({S, A, B}, {a, b}, S, { S Aab
A Aab |
B a } L(G2) = {a{ab}+} r = a(ab)+
Both grammars G1 and G2 are regular
63
3.3 Regular Grammars (3)
The grammars G is not regular G=({S, A, B}, {a, b}, S, { S A
A aB |
B Ab }
Every production rule is either in right-linear or left-linear from
The grammar itself is neither right-linear nor left-linear
64
3.3 Regular Grammars (4)
A language is said to be regular if it is generated by some regular grammar.
A regular language may be generated by both regular and non-regular grammars.
Derivations in regular grammars (right-linear) have a nice form There is at most one non-terminal present in a
sentential form and that non-terminal, if present, is the rightmost symbol in the string.
ab...cD ab...cdE D dE
65
3.3 Regular Grammars (5)
The grammars G1 & G2 generate the language a+b*
The grammar G1 is not a regular, because of the rule S AB does not have the specified form
G2 is a regular grammar (right-linear)
G1=({S,A,B}, {a,b}, S, {S AB A aA | aB bB | }
G2=({S,B}, {a,b}, S, {S aS | aB B bB | }
66
3.3 Regular Grammars (6)
The following grammars are regular grammars (right-linear)
G=({S,O}, {a,b}, S, {S aO | bO | O aS | bS }
G=({S,B}, {a,b}, S, {S aS | bB | B aB | bS}
G=({S,B,C}, {a,b,c}, S, {S bS | cS | aB | B aB | cS |
bC | C aB | bS | }
67
3.3 Regular Grammars (7)
Non-regular grammar S abSA |
A Aa |
Its equivalent regular grammar S aB |
B bS | bA A aA |
Both grammars generate the language | (ab)+a*
68
3.3 Regular Grammars (8)
The language a+b+ is generated by the grammar G G: S aS | aA
A bA | b And also accepted by NFA M
Derivation of aabb using CFG G S aS aaA aabA aabb
Accepting aabb using NFA M [S, aabb] ├ [S, abb] ├ [A, bb] ├ [A, b] ├ [Z, ]
S A Zba
a b
69
3.3 Regular Grammars (9)
The NFA M = (Q, , , S, F) can be constructed from the rules of a grammar G = (NT, , P, S) as follows: Q = NT {Z}, where Z NT if A a P
Q = NT otherwise
(A, a) = B whenever A aB P(A, a) = Z whenever A a P
F = {A | A P} {Z} if Z QF = {A | A P} otherwise
70
3.3 Regular Grammars (10)
The the grammar G generates and the NFA M accepts the language a*(a|b+) G: S aS | bB | a
B bB |
M
S B
Za
b
a b
71
3.3 Regular Grammars (11)
Given the following grammar G that generates the language over {a, b, c} that do not contain the substring abc G: S bS | cS | aB |
B aB | cS | bC | C aB | bS |
What is NFA M that accepts L(G)?
S B Cba
b, c a
ac
b
72
3.3 Regular Grammars (12)
The DFA M that accepts the language over {a, b} with even number of a’s and odd number of b’s is given
Give grammar G for M G: S aA | bB
A aS | bCB bS | aC | C aB | bA
S B
a
A Cb
a aa
b
b
b