0 ratings0% found this document useful (0 votes) 7 views18 pagesFlat Unit
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
EXCEL ENGINEERING COLLEGE
(Autonomous)
b.e Computer Sclonce and Enginwenng
IV Semester
20C8401- Formal Languages and Automata Theory
Regulations 2020
INIT - jI(Regular Expressions and Langua
PART. A
Questions and Answers
QNo |
Define a reguiar exp
© Aregular expression is a string thal describes the whole set of strings according lo verta
tules. These expressions are used by many text editors and utilities to search bodies of text for
lon.
syitan
certain patterns etc.
Let £ be an alphabet. The regular expression over £ and the sets they denote are:
1
i. Dis ar.e and denotes emply set. ii. € is are and denotes the set {C}
il, For each ‘a’ in Eat is a re and denotes the set {a}.
iv. If and’‘s’ are r.e denoting the languages R and S respectively then (r+s),
| (1s) and (r*) are re that denote the sels RUS, RS and R° respectively.
{ one :
Write regular expression for the set of strings over (0,4) that have atleast one and atmost one
2} RE is 0°1(0| 1)"
| RE is 0*/ 0°10"
dl ee as asin
| State the Pumping lemma for regular languages.
| Let L be a regular language, accepted by some finite automaton D with a string w in L wt
wexyz, then
| ~ bylsn,
|v [lz then
Y xyz € L for alliz 0.
|
t Construct a re for the language which accepts all strings with atleast two c’s over the set E=(c.b}.
4 | RE=(b4c)* c (b+0)*c (bec)
| List the closure properties of regular language:
|v Union
‘ ¥ intersection
¥ Set complement
¥ String reversalConcatenation
‘Star
Homomorphism
<< <<
Give language of regular expression a?(blc)*
a? moans either a ore
(bc)* means the set ofall symbols no other than b and c including the empty string = {¢, b,c, bb
6¢, cb, be, bbb, c¢¢,...)
Show whether a language L= {On2n | n>0} Is regular or not using pumping lemma.
‘© To prove that L is not a regular language, we will use a proof by contradiction. Assume that
Lis regular.
© Let L be a regular langu
written as wexyz, then
accepted by some finite automaton D with a string w in L
by Isr,
lyl21 then
xyz € L for alli2 0.
But, this is clearly not in [Link] is a contradiction with the pumping lemma. Therefore our
| assumption that L is regular is incorrect, and L is not a regular language
Differentiate L* and L+
* L* denotes Kleene closure and is given by L* =U LI
i=0 example : 0* =(€ ,0,00,000.....
Language includes empty words also.
| © Lédenotes Positive closure and is given by L4=ULi
example:0#=(0,00,000,
~ | Write a r.e to denote a language L which accepts all the strings which begin or end with either 00 or
11.
The r.e consists of two parts: L1=(00+11) (any no of 0's and 1's)
=(00#11)(0#1)*
L2=(any no of 0’s and 1's)(00+11)
10 |
|
=(0+4)*(00+11)
Hence r.e R=L14L2
=[(00#14)(0+4)"] + (0+4)* (004411) ‘
' Construct are for the language over the set Z=(a,b) in which total number of a's are dv's "1c Sy
RE is (b*a b*ab*ab*)*igontity : (I)(0¥4) (I)(O#4)+
(0+1)= 0 or 1,No other possibilities,
(0+1)#= {0,1,01,10,1000,0101,,
}
ue Reg exp denoting a language over £=(1) having (even length of string
string
(iijodd longth of a
12. | () Even length of string RE=(11)*
| (i) Odd length ofthe sting RE=1(11)*
~TGive Reg expfor ro
|
| {)Set ofall strings over {a,b}with 3 consecutive b's.
13. | jet ofall strings that end with ‘t’and has no substring ‘00"
i) (a+b)* bbb (a+b)*
ii) (4408) (10+01)"1
| List the applications of Regular expressions and Finite automata
ed using regula ©
ing language can be express
3g identifier can be express
id separates the tokens. For e¢
| Lexical analyzers: The tokens of the programmi
The lexical analyzer scans the input program an
| as a regular expression as:
|
| (etten(ettereaigit)
| if anything in the source language matches with this reg exp then it is recognized as an identifier
| The letter is(A Zabg....2)and digits (0,1...9)
Thus reg exp identifies token in a language.
14
| Text editors: These are programs u
UNIX text editors uses the reg exp for substituting the strings su
| | ives the substtue a single blank fr the fist string of wo or more blanks ina given ine in
| editors any re exp is converted to an NFA with € transitions, this NFA can be then simulated cirectly
| Give the Reg exp for the language that accepts all strings In which ‘a appears tripled over the
| 7 set“)
| | RE=(aaa)*
sed for processing the text. For example
sch as: Sihhh*h
UNIX tex!
* Mention the applications of pumping lemma.
| |
| Pumping lemma is used to check if a language is regular or not.
| (Assure that the language(L) is regular.
16 | (i) Select a constant 'n’
(ii) Selec a string(2) in L, such that zn
(iv) Split the word z into u.v and w such that uvi<=n and v=".
png lemma thal there exists 2
(v) You achieve a contradiction to| () You achive a contradiction to pumping lemma that there exists an
Such that uvw is not in [Link] L is not a regular language.
List the closure property of regular sets.
| The regular: sate te dood Sa union, concatenation and Kleene closure.
| rtura= rt +12
Tha2= 12 (r pore
The class of regular sets is closed under complementation, substitution, homomorphism and
inverse homomorphism, !
Write the Reg exp for the language such that every string will have atleast one ‘a’ followed by
18 atleast one
RE=atb+
* | Write the exp for the language starting with and has no consecutive b's
RE=(atab)*
20 | Define ei
PART. B e
[ QNo Questions pases
Construct a NFA that accepts all strings that end in [Link] its transition table and extended
transition function for the input string [Link] construct a DFA for the above NFA using
‘subset construction method
| Solution: |
0,1 1
@) ‘
| Transition function for the String w= 00101
| 5(a,.€)= (a)
| 5(q..0) = {4,01}
| 8 (q,,00) = 5(6(q.,0),0) |
| = 5((9..01),0) |
| = 8(q,,0) U 5(q,0)
5(q.,001) = 5(6(4.,41))
= 6(q..1) U (Qt) |
= (4%)
5(B(q_,0010) =5 (5 (q1.42).0)
= 8(q.,0) U (q.0)
= {a} Ud
= {9.0}ot) U (Quit)
1.02)
8(6(q,,00101)) = {[Link]}
qz in find state the input string 00101 in accepted
NEA to DFA
Step 1: Consider initial state q, and process the inpul symibul U
4n(q..0) = (a..01)
then,
8D(q.,0) = {4.09} ~ New State
For olp symbol 1
5y(q..0) = {4.}
then,
a0(q.,.1) = {4}
Step 2:
New State process the I/P symbol 0
Bn({q.,4:,0) = 5N(G,,0) u Sv(q.0)
= (gg Ud
={a..0)
then,
BDf{a..41.01 = [9..48]
for ip Symbol 1
Bu({a..anh1) = Sy(Q.1) Unt)
= {9,) U (aa)
= (9,.42}
then,
BD(la,.01)1) = 42.92) New State
Step 3:
New State (a, dal process l/P Symbol 0
Bu({9.,42},0) = 5N(g,,0) U BN(g2.0)
09} U >
= {a}
8D((9..4:3.0) = [4..41]
For iP Symbol,
Bn ({q..qh).1) = BN(a..1) U BN(ga,t)
= {9} Ud
={a)
then,SD((q.,42)
Transition Table
STATE uP
{9.01
) {aa
faa) | {a} ae
~{b, d}) where 6 is defined as
{4.0
Construct the DFA equivalent to the NFA. M={{a, b, ¢ 0} (0, 1), 5,3,
5 0 4
a {bd} {b}
b c { bc} |
c d a
d a
— |
Consider initial state a (start state) and process the input symbol 0 '
Bn(a,0) = {b,d}
then,
&D(a,0) = [b,d] ~~ ---New State
When process the input symbol 1
Bn(a,1) = {b} |
then, 5D(a.1) = [b] wwmsNew State ;
Step 2:
New state process the input symbol 0
Bw({b,d},0) = Sn(b,0) UV Bn(d,0) |
={c} U {}
8D Ilb,d],0] = {c}
When process I/P symbol 4
3n({b.d],1) = S(b,1)USn(d,1)
= {b,c} U {a}
= {a,b,c}
then, SD[{b,d},1] = {a,b,c} ----
New State
Step 3:n(b,0) « (c)
then, 80(b,0) « {c)
When process symbol 1
By (b.1) © (b,c)
then, 80(b,1) = (b,c)
Ston 4;
Now State
New stato {c] when process symbol 0
BN(C.0) = (a) :
then, 30(¢,0) = {4
When process input symbol 1
n(,1) = (0)
then, 80(c,1) = [a]
Step 5;
~-New State
New state [a,b,c] when process /P symbol
5n({a,b,c},0) = 6n((a,0) U n(b,0) U5n(¢,0))
= {b,d) U {c} U {d}
= (bod)
then, 8D({a,b,c},0) = [b,c,d) ~
When process /P symbol 1
Sy ({a,b,c},1) = 5n((a,1) U Bn(b,1) U Bn(c,1))
= {b} U {b,c} U {a}
= {a,b,c}
voce New State
tep 6:
New state [b,c] when process I/P symbol 0
4y{{b,c},0] = (5n(b,0) U Sn(c,0))
= {c} U {a}
= {c,d}
then, &D{fb,c},0] = {c,d}
When process I/P symbol 1
4y{{b,c},1] = (Sy(b,1) U Sy(c,1))
= {0} U {a}
= {a,b,c}
‘New State
Step 7:
New state [d] when it process I/P 0
5n({d},0) = [5n(d,0))
=o
then, dD[d,0] = 6
When process I/P symbol 1
5 ({d},1) = 5m (d,1)
= {a}
then,
5D(d,1) = [a]
Step 8:
New state [b,c,d] when it process input symbol 0Syi{b,¢,4),0) * (Bw(b,0) U Bn(c,0) U n(d,0))
#{c)U()Ud
*{o.d)
then, 8((b,c,d),0) * [c,d]
When process /P symbol 1
Sra({b,c,d),1) = (Sr(b,1) U Bn(c,1) U Bn(d,1))
= (bic) U (a) U (a)
= (a,b,c)
then, 8D({a,b,c),1) = a,b,c)
Step 9:
New state {c,d} when it process l/P symbol 0
Sn({c,d),0) = (5n(c,0) U Sn(d,0))
={d}Ud
={d)
| then, 8D({c,d},0) = [d)
{c,d} when it process VP symbol 1
Sn({c,d},1) = (5n(c,1) U Bn(d,1))
[ ‘§ a New state [b,cd) when it process Input symbol
|
|
= {a} U {a}
= {a}
then, 8D({c,d},1) = [a]
| Step 10:
| Transition Table for DFA
|
| STATE uP
0 1
a {b,d} {b}
b {c} ___ {bc} |
c {d} {a}
d > a |
{bd} ( {abc}
{b,c} {c,d} {abc}
{c,d} {a} {a}
{a,b,c} {b,c,d} {a,b,c}
{b,c,d) {e.d) {abc}
3 _ | Construct regular expression for the given finite automata -Solution:
‘Solution:
The formula for finding the regular expression is
Ry)=
APO Rul (RRR? YF > Rilo
Ru =Ral” + Ry (Ra? Yt > Ras”
Rat= Rea + Ru? = (Ru? YP» Rue
=0+(6+1)'0
=0 (€+1)"
Rn’ = 01"
Rn'=Ra® + Rx® (Rx® yt Ru®
(E+0+1) + > (erty 0
= E4041
Ra’ = (€+0+1)
Sub Rix’, Rea’ to finding Ria?
Rut= Rua’ + Rut © (Rea' )* Rn’
= 170+ 1°0(E+0+1)* (€+0+1)
= 1°0 + 1°0 (E+0+1)"
= 10+ 1"0(0+1)"
= 1°Q(E+(0+1)")
Rua? = 1°0(0+1)*
Construct the folowing i NFA for regular expression #(0+4)"(00+11) in(e*+b*)
| Solution:
| NFA with epsilon for (0+1) is given below
on SeSolution:
NFA with epsilon for (0+1) is given below
5 (7 (p >
O-O~*
NFA with epsilon for (0+1)* is given below
JID |
NFA with epsilon for (0+1)*(00+11) is given below
_—_ roo| lijateb*)
| NFA with epsilon for a* is given belowsf
t
Determine the DFA from the given NFA. M= ((qo,qi), {@.b}, 5, do, q1}) with the state diagram table
3 given below. (Dec 16)
STAT] a b €
—a. | a) | |] ta)
“oh o {ar} &
€.Closure(q,) = {q.,4h)
€.Closure(q:) = {qu}
oh)
5({q..9:},6) = €.Closure(5(q,,b) U 5(q.b))
Closure( U qi)
= €.Closure{q;}
=o
Step 3:
B({aih.a) = €.Closure(6(q.,2)
.Closure()
=o
Step 4:
({qi},b) = €.Closure(5(q1,b)
= €.Closure( they both have two corresponding regular expressions, R and $ respectively
> (LUM) can be represented using the regular expression R+S
> Therefore, (L UM) Is also regular
4 and L2, which are represented using !111!2
The union of two regular languages, L
f strings that are either in L1 or L2 or both.
regular and which represents the set o
Example
L1 = (140).(1+0) = (00, 10, 11, 04} and
L2={e, 100)
then L1 UL2={E, 00, 10, 11, 01, 100}.
Complementation
im IfLisanRL over 5, then Leyte
> ToshowL is also regular, make the following constructionExample -
L1= (0,1 }and L2=
Kleene closure -
if L1 is a regular languagi
the set of those strings wl
string can be repeated any numbé
L1=(0,1}={€, 0, 1, 00, 04, 10, 11...
including a null string.
Inte
mt A quick, indirect way to prove:
By DeMorgan’s law:
= LaM=(LUM)
im Since we know RLs are closed
closed under intersection
im Amore direct way would be construct
Difference:
im We observe:
me L-M=L OM
Since LMM" is closed under intersection al
Therefore, L- Mis also regular
Construct NFA with Epsilon for the RE = (alb)*
minimized DFA.
Solution:
, then the Kleene closure Le. Lt
hich are formed by taking a
er of times and concal
Example -
uj} then L* is all
consent q0is a non-final state. If not, do the opposite.
The concatenation of two regular languages,
also regular and which represents the set of strings
conecatenating it with any string in L2.
L2, which are represented using L1.L2's
LU and
that are formed by taking any string 1) ©
{ 00, 11} then L1.L2 = {000, 044, 100, 111}.
+ of L1 is also regular and represents
umber of strings from L1 and the Same
tenating those strings.
strings possible with symbols 0 auc :
under union and complementation, they are also
a finite automaton for LM
ind complementation
‘ab and convert into DFA and further find the—
RE = (a/b)*abb-
| warns Ao
| “QC
| eClosure(0)=(0,1,2,47) > A
| MoviAal3.8)
eClosure(Mov[A.a))=(3,6,7,1,2,4,8) — B
Mov[A,b]={5}
| e Closure(Mov[A,b})={5,6,7,1,2,4,8} — C
Step3:
| MovIB,21=(3.8}
| e Closure(Mov[B,a))={ 3,6,7,1,2,4,8} > B
Mov[B,b]={9,5}
eClosure(Mov[B,a])={ 9,5,6,1,2,4,7 } > D
Step 4:
MoviC,a}={8,3)
={3,6,7,1,2,4,8} B
Mov{C,b]={5}
e Closure(Mov{C,b])={5,6,7,1,2,4,8} — C
Step5:
Mov([D,a]={3,8}
e Closure(Mov[D,a])={ 3,6,7,1,2,4,8} > B
Mov[D,b]={5,10}
e Closure(Mov[D,b])={ 5,6,1,2,4,7,10 } > E
Step6:
Mov[E,a]={3,8}
e Closure(Mov[E,a])={ 3,6,7,1,2,4,8} > B
Mov(E,b]={5}
e Closure(Mov[E,b]) ={5,6,7,1,2,4,8} — C
State a b
=A
B
oe|o a
c
D
*E B c
Transition Block Method: _|
is final state
-D for b input transition to E state
1
a
o
| E
€ B ¢
imple.
Explain three applications of Regular expression with an ex
{Regular expression in Unix
The first enhancement to the regular-expression notation concerns the fact
‘that most real applications ‘deal with the ASCII character set. Our examples
have typically used a small alphabet, such as {0, 1). The existence of only two
symbols allowed us to wri uccinet expressions like 0+. for “any character.”
cymbals atpere were 128 characters sa he same expression SOU 1 iv
listing ther all, and ‘would be highly inconvenient to w ite. Thus, UNIX re:
ting her: 2 Sow v0 wit charter elses repre eS i
SIaT eters as auceinctly ms possible. The rules for charactes
(dot) stands for “any character.”
‘¢ The symbol «
regular expression
“e The seqruence [ayaa **-ae) stands for the
ay tage +a
out half the characters, ince we don"t have 0 wits
ie, we could express the four characters used I= c
by [o>=!]
‘This notation saves abs
the +-signs. For exam]
comparison operators
vethere are speriel notations for several of te most couton classes of
characters. For instance:
a) Ceaigiht) is the ser of tem digits, the same a8 fo-914
rabetic character, ws ees LR-Za~2)
b) Cratpha:] stands for any alphi
¢) Cadnun:) stands forthe digits ax letters (alphabetic aut mumetic
characters), as does [A-Za-20-9).
ave used in UNIX regular es
None of these openers
In addition, there are several operators
fons that we have not encin tered prestosty
eetand what languages can be expressed, but thy somel
express what we want,
1. ‘The operator | used in place of + to denove unten
2. ‘The operator ? moans “zero oF one of." Thus, RY in UNIX Is the samme
ac ¢-+ Rin this book's regular-expression notation.
3. The operator + means “one or more uf.” Thus, R+ ix UNIX is shorthand
for RR" in our notation.
4. The operator {n} means “n copies of." Thus, ONIX
To operator {n) cans *n copies of.” Thus, 2{5) in UNIX is shorthandii)Lexical Analyzer
One of the oldest: applications of regular expressions was in sperifving the com
ponent of a compiler called a “lexienl analyzer.” This component scans the we
source program and recognizes ail tokens, those substrings uf cousecutive hiat-
acters that belong together logically. Keywords and identifiers are
examples of tokens, but there are many others.
common
iii)Finding Pattern in Text
A regular expression is a pattern that the regular expression en
; i ;
text. A pattern consists of one or more character literals, opera Ore oe eePts to match in input
tors, or constructs, |
Mrbiestie oBaees im =