Lm-Flat
Lm-Flat
GUDLAVALLERUENGINEERINGCOLLEGE
(AnAutonomousInstitutewithPermanentAffiliationtoJNTUK,Kakinada)
SeshadriRaoKnowledgeVillage,Gudlavalleru–521356.
DepartmentofComputerScienceandEngineering
HANDOUT
on
FORMALLANGUAGESANDAUTOMATATHEORY
Vision
To be a Centre of Excellence in computer science and
engineering education and training to meet the challenging
needs of the industry and society.
Mission
➢ Tofosterindustry-academiarelationshipformutualbenefitandgrowth.
ProgramEducationalObjectives
HANDOUTONFORMAL LANGUAGESANDAUTOMATATHEORY
withapplicationtootherareas, suchaslogic.
The notion of a context-free grammar and the corresponding
pushdown automaton (Units 4 and 5) has aided immensely the
specification of programminglanguagesandinthedesignofparsers-
anotherkeyportionof
acompiler.Formalspecificationsofprogramminglanguageshaverepl
aced extensive and often incomplete or ambiguous descriptions
of languages. Understanding the capabilities of the pushdown
automaton has greatly simplified parsing. In early compilers,
parser design is a difficult problem, and many of the early
parsers were quite inefficient and unnecessarily restrictive. Based
on context-free-grammar-based techniques, parser design
isnolongeraproblem,andparsingoccupiesonlyafewpercentofthetim
e spent in typical compilation.
In Unit 6, we deal with Turing machines and one of the
fundamental problems of computer science; there are
algorithms for computing
functions.Therearefunctionsthataresimplynotcomputable;thatis,t
here isnocomputerprogramthatcaneverbewritten.
2. Pre-Requisites
• MathematicalFoundationofComputerScience
3. CourseObjectives:
• Tointroducetheclassificationofmachinesbytheirpowertorecogn
ize languagesandtosolveproblemsincomputing.
• To familiarizehow to employ deterministic and non-
deterministic machines.
CourseOutcomes:
CO1:comparetheautomatabasedontheirrecognizingpowe
r. CO2: design finite automata for regular languages.
CO3:reduceDFAbyapplyingminimizationalgorithm.
CO4: write regular expressions for regular languages or for DFA by
applying Arden’s theorem.
CO5:generategrammarforCFL’s.
CO6: use algorithm to simplify
grammar.CO7:designPDA‘sforcontextfreel
IIYear–II-Semester 2018-19 CSE
FormalLanguagesandAutomataTheory 6
anguages.
CO8:designTuringMachineforthephrase-structuredlanguages.
4. ProgramOutcomes:
EngineeringGraduateswillbeableto:
1. Engineering knowledge:Apply the knowledge of mathematics,
science, engineering fundamentals, and an engineering
specialization to the solution ofcomplexengineeringproblems.
2. Problem analysis:Identify, formulate, review research literature,
and analyze complex engineering problems reaching substantiated
conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
3. Design/development of solutions:Design solutions for complex
engineering problems and design system components or
processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural,
societal, and environmental considerations.
4. Conduct investigations of complex problems:Use research-based
knowledge and research methods including design of experiments,
analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
5. Modern tool usage:Create, select, and apply appropriate
techniques, resources, and modern engineering and IT tools
including prediction and modeling to complex engineering
activities with an understanding of the limitations.
6. The engineer and society:Apply reasoning informed by the
contextual knowledgetoassesssocietal,health,safety,legal
andculturalissuesand the consequent responsibilities relevant to
the professional engineering practice.
7. Environment and sustainability:Understand the impact of the
professionalengineeringsolutionsinsocietalandenvironmentalcon
texts, and demonstrate the knowledge of, and need for sustainable
development.
5. MappingofCourseOutcomeswithProgramOutcomes:
1 2 3 4 5 6 7 8 9 10 11 12
CO1 H H H
CO2 H L H
CO3 H L H
CO4 M H
CO5 H M M
CO6 M
CO7 M H L M
CO8 M H L M
6. PrescribedTextBooks
1. John E.Hopcroft, Rajeev Motwani& Jeffrey D.Ullman J.D.,
“Introduction to Automata Theory Languages and Computation”,
3rd edition, Pearson Education.
2. LewisH.R.,Papdimitriou,“ElementsofTheoryofComputation”,2 ndedi
tion, PHI.
7. ReferenceBooks
1. DanielI.A.Cohen,JohnWiley,“IntroductiontolanguagesandtheTheoryof
Computation”.
2. Sipser,Thomson,“IntroductiontoTheoryofComputation”,2 ndedition.
3. Mishra and Chandrashekaran, “Theory of computer science - Automata,
Languages, and Computation”, 2ndedition, PHI.
4. K.KrithivasanandR.Rama;IntroductiontoFormalLanguages,AutomataTheorya
nd Computation; Pearson Education, 2009.
8. URLsandOtherE-LearningResources
1. BasisforaMathematicalTOC:http://www-formal.stanford.edu/jmc/basis1.pdf
2. Finite Automta:
http://www.cs.odu.edu/~toida/nerzic/390teched/regular/fa/intr_2_fa.html
3. PDA:https://brilliant.org/wiki/pushdown- automata/
4. TuringMachine:http://plato.stanford.edu/entries/turing-machine
9. DigitalLearningMaterials:
• http://nptel.ac.in/courses/106104028/
• http://nptel.ac.in/courses/106104148/
• http://nptel.ac.in/courses/106106049/
10. LectureSchedule/LessonPlan
No.ofPeriods
Topic
TheoryTutorial
UNIT–1:Fundamentals
Strings,Alphabet,Language,Operationsonstrings 1
Operationsonlanguages,FiniteStateSystem 1 1
FiniteAutomatonModel 1
Acceptanceofstringsandlanguages 1
Deterministicfiniteautomaton 2
2
Nondeterministicfiniteautomaton 2
Transitiondiagrams,languagerecognizersandapplicationsofFiniteAutomata 2
Total 10+3(T)
UNIT–2:Finite Automata
NFAwithεtransitions–significance,acceptance ofalanguagebyaε–NFA 1
EquivalencebetweenNFAwithandwithoutεtransitions 2
Minimizationof FSM 2 1
NFAtoDFA conversion 1
equivalencebetweentwoFSM‘s 1
Finiteautomatawithoutputs-Mooremachine,Mealymachines 1
MooretoMealyCoversion-examples 1 1
MealytoMooreconversion-examples 1
Total 10+2(T)
UNIT– 3:RegularLanguages
RegularSets,IdentityRules 1
1
Regularexpressions 2
ConstructionofregularexpressionforagivenfiniteAutomata 1
1
Pumpinglemmaofregularsets 1
Closurepropertiesofregularsets,applicationsofregularlanguages. 1
Total 7+2(T)
UNIT–4:Grammar Formalism
Chomskyhierarchyoflanguages 1
Regulargrammars-rightlinearandleftlineargrammars-examples 1
1
EquivalencebetweenregularlineargrammarandFA 1
EquivalencebetweenFAandregulargrammar 1
Contextfreegrammar-examples 2
Derivation-Rightmostandleftmostderivationofstrings,sententialforms,
2 1
Derivation trees
Total 8+2(T)
UNIT– 5:ContextFreeGrammars
Ambiguityincontext freegrammars 1
MinimizationofContextFreeGrammars 1
Chomskynormal form 1
Greibachnormalform 2
1
PumpingLemmaforContextFreeLanguages 1
EnumerationofPropertiesofCFL(proofsnotrequired),applicationsof CFLs
1
Pushdownautomata,modelofPDA 1
DesignofPDA 2 1
ApplicationsofPDA 1
Total 11+2(T)
UNIT– 6:TuringMachine
TuringMachine,model 1
1
DesignofTM 2
TypesofTuringMachines 1
Computablefunctions 1
1
Recursivelyenumerablelanguages,Recursivelanguages 1
Decidabilityofproblems 1
Undecidabilityofpostscorrespondenceproblem 1
Total 8+2(T)
TotalNo.ofPeriods: 54 13(T)
FORMALLANGUAGESANDAUTOMATATHEORY
UNIT-I
Objective:
• Tointroducetheclassificationofmachinesbytheirpowertorecogn
ize languagesandtosolveproblemsincomputing.
• Tofamiliarizehowtoemploy deterministicandnon-deterministic
finite automata.
Syllabus:
Strings,alphabet,language,operations,finitestatemachine,finiteauto
maton model, acceptance of strings and languages, deterministic
finite automaton and non deterministic finite automaton, transition
diagrams and language recognizers.
LearningOutcomes:
Studentswillbeableto:
• Understand the basic definitions like alphabet, string, language
and their operations.
• Understand themodelof FA.
• DesignDFAandNFAforthegivenregularlanguage.
• TestthedesignedDFAandNFAforthesetofstringsthatbelongstoL
and for the set of strings that doesn’t belongs to L.
LearningMaterial
Alphabet:
Analphabetisafinite,nonemptysetofsymbols.Itisdenotedby∑.
Example:
∑={0,1} is binary alphabet consistingof the symbols 0 and1.
∑={a,b,c...z}islowercaseEnglishalphabet.
PowersofanAlphabet
If Σ is an alphabet, we can express the set of all strings of a certain length from
thatalphabetbyusingtheexponentialnotation.ItisdenotedbyΣk-
thesetof strings of length k.
Example:
Σ0 = {ε},regardlessofwhatalphabetΣis. εistheonlystringoflength0. If Σ = {0,
1} then,
Σ1={0,1}
Σ2={00, 01,10, 11}
Σ3={000,001,010,011,100,101,110,111}
ThesetofallstringsoveranalphabetΣisdenoted by Σ*. Σ*=Σ0∪Σ1∪Σ2∪.. For
example, {0, 1}* = {ε, 0, 1, 00, 01, 10, 11, 000,.................}
Thesymbol∗iscalledKleenestarandisnamedafterthemathematicianand
logicianStephenColeKleene.
Thesymbol+iscalledPositiveclosurei.e.Σ+=Σ1∪Σ2∪...
Σ∗=Σ+∪{ε
}
String:
Astring(orword)isafinitesequenceofsymbolschosenfromsomealphabet.
The letters u, v, w, x, y and z are used to denote string.
Example:
IfΣ={a,b,c}thenabcbisastringformedfromthatalphabet.
• Thelengthofastringw,denoted|
w|,isthenumberofsymbolscomposing the string.
Example:
Thestringabcb has length 4.
• Theemptystringdenotedbyε,isthestring consistingofzero symbols.
Thus|ε|=0.
Operationsonstrings:
• Concatenationofstrings
Theconcatenationoftwostringsisthestringformedbywritingthefi
rst, followed by the second, with no intervening space.
Concatenation of stringsisdenotedby◦.
Thatis,ifwandxarestrings,thenwxistheconcatenationofthesetw
o strings.
Example:
Theconcatenationofdogandhouseisdoghouse.
Letx=0100101andy=1111thenx◦y=01001011111
• StringReversal
Reversing a string means writing the string
backwards. It is denoted by wR
Example:
Reverseofthestringabcdisdcba.
Note:Ifw=wR,thenthatstringiscalledpalindrome.
• Substring
Asubstringisapartofastring.
Example:
If abcd is string then possible substrings
areε,a,b,c,d,ab,bc,cd,abc,bcd are proper substrings for the given
string
Aprefixofastringisanynumberofleadingsymbolsofthatstring
. A suffix of a string is any number of trailing symbols.
IIYear–II-Semester 2018-19 CSE
FormalLanguagesandAutomataTheory 15
Example:
• Concatenation
Concatenation of two languages L1 and L2 is the language L1 o
L2 ,each element of which is a string formed by combining one
string of L1 with another string of L2.
Example:
L1={bc,bcc,cc}and L2={cc,ccc},then L1oL2 =
{bccc,bcccc,bcccccc,cccc,ccccc}
• Reversal
If L is language, then L Ris obtained by reversing the
corresponding string in L.This operation is similar to the reversal
of astring.
LR ={wR|w∈L}
Example:
IfL={0,011,0111},thenLR ={0,110,1110}
• KleeneClosure
TheKleeneclosure(orjustclosure)ofL,denotedL*,istheset
∞
L*=U
Li
i=0
• PositiveClosure
ThepositiveclosureofL,denotedL+,istheset
∞
L+=U
Li
i=1
Thatis,L*denoteswordsconstructedbyconcatenatinganynumberof
words from L.
L+isthesame,whereε,isexcluded.
Note:L+containsεifandonlyifLdoes.
Example:
LetL1={10,1}
L*=L0UL1UL2..................={ε,1,10,11,111,1111,......................}
L+=L1UL2UL3...................={1,10,11,111,1111.............}
FiniteAutomaton:
• A finite automaton (FA) consists of a finite set of states and a set
of transitions from state to statethat occuron input symbols
chosenfrom an alphabet ∑.
• For each input symbol there is exactly one transition out of each
state (possibly back to the state itself).
• One state, usually denoted q0is the initial state, in which the
automaton starts. Some states are designated as final or accepting
states.
Formally,afiniteautomatonisdenotedbya5-tuple(Q,∑,δ,qo,F),where
• Qisafiniteset ofstates.
• ∑isafiniteinputalphabet.
• δisthetransitionfunctionmappingQx∑toQi.e.,δ(q,a)isastate for
each state q and input symbol a.
• qo∈Qistheinitialstate.
• F⊆Qis the set of final states. It is assumedhere that there maybe
• morethanonefinalstate.
TransitionDiagram:
• AtransitiondiagramisadirectedgraphassociatedwithanFAinwhicht
he verticesofthegraphcorrespondtothestatesoftheFA.
• Ifthereisatransitionfromstateqtostateponinputa,thenthereisanarc
labelledafromstateqtostatepinthetransitiondiagram.
Stateisdenotedby
IIYear–II-Semester 2018-19 CSE
FormalLanguagesandAutomataTheory 19
Transition is denoted by
Initialstateisdenotedby Fi
TransitionTable:
A tabular representation in which rows
correspond to states,
columnscorrespondtoinputsandentriescorrespondtonextstates.
FiniteAutomataModel:
Blockdiagramofafiniteautomaton
Thevariouscomponentsareexplainedasfollows:
(i) Inputtape:
• Theinput tapeisdividedinto squares,
eachsquarecontainingasingle symbol from the input alphabet
∑.
• Theendsquaresofthetapecontaintheendmarker
¢attheleftendand the endmarker $ at the right end.
• The absence of endmarkers indicates that the tape is of infinite
length. The left-to-right sequence of symbols between the two
endmarkers is the input string to be processed.
(ii) Readinghead:
• The head examines only one square at a time and can move one
• Forfurtheranalysis,werestrictthemovementoftheR-
headonlytothe right side.
(iii) Finite control:The input to the finite control will usually be the
symbol undertheR-
head,saya,andthepresentstateofthemachine,sayq,togive the
following outputs:
• A motion of R-head along the tape to the next square (in
some a null move,i.e.theR-
headremainingtothesamesquareispermitted)
• Thenextstateofthe finitestatemachinegivenbyδ(q,a).
AcceptanceofStringbyaFiniteAutomaton:
The FA accepts a string x if the sequence of transitions corresponding to
the symbols of x leads from the start state to an accepting state and
the
entirestringhastobeconsumed,i.e.,astringxisacceptedbyafiniteautomat
onM
=(Q, ∑,δ,qo,F)
ifδ(q0,x)=qforsomeq ∈
F.
Thisisbasicallytheacceptabilityofastringbythefinalstate.
Note:Afinalstateisalsocalledanacceptingstate.
Transitionfunctionδandforanytwoinputstringsxandy,
δ(q,xy)=δ(δ(q,x), y)
Example:
Consider the finite state machine whose transition functionδis given in
theformofatransition table.HereQ = {q0,q1,q2,q3},∑={0,1},
Input
State
0 1
q2 q3 q1 q0
q0
q1
q2 q0 q3
q3 q1 q2
δ(q0,110101)=δ(q1,10101)
=δ(q0,0101)
=δ(q2,101)
=δ(q3,01)
=δ(q1,1)=q0
q0isfinalstate,thereforegivenstringisacceptedby finite
automata.
Deterministicfiniteautomaton:
Formally,adeterministicfiniteautomatoncanberepresentedbya5-tupleM=
(Q,∑,δ,qo,F),
where
• Qisafiniteset ofstates.
• ∑isafiniteinputalphabet.
• δisthetransitionfunctionmappingQx∑toQi.e.,δ(q,a)isastatefor each
state q and input symbol a.
• qo∈Qistheinitialstate.
• F ⊆Q isthesetoffinalstates.Itisassumed herethattheremay
be more than one final state.
StepstodesignaDFA:
1. Understand the languagefor whichthe DFA has to bedesigned
andwrite the language forthe setof stringsstarting with
minimumstring thatare accepted by FA.
2. Drawtransitiondiagramfortheminimumlengthstring.
3. Obtainthepossibletransitionstobemadeforeachstateoneachinp
ut symbol.
4. Drawthetransitiontable.
5. TestDFAwithfewstringsthatareacceptedandfewstringsthatare
rejected by the given language.
6. RepresentDFAwithtuples.
Examples
1. DesignDFAthatacceptsallstringswhichstartswith‘1’overthe alphabet
{0,1}
Step1:UnderstandthelanguageforwhichtheDFAhastobedesigned
and write the language for the set of strings starting with
minimum string that is accepted by FA.
L={1,10,11,100,110,101,111,..................................................}
Step2:Drawtransitiondiagramfortheminimumlengthstring.
Step4:Drawthetransitiontable.
Input
State
0 1
q0 q2 q1
q1
q1 q1 q1
q2 q2 q2
Step 5: Test DFA with few strings that are accepted and few
strings that are rejected by the given language.
Casei)Let w=1001∈L
δ(q0,1001)=δ(q1,010)
=δ(q1,10)=δ(q1,0)=q1
q1is final state and the entire string has been consumed i.e.,
given string is accepted by DFA.
Caseii)Letw=0001∉L
δ(q0,0001)=δ(q2,001)
=δ(q2,10)
=δ(q2,0)
=q2
q2is not final state and the entire string has been consumed i.e.,
given string is rejected by DFA.
Step6:RepresentDFAwithtuples.
DFA,M=(Q,∑,δ,qo,F)
whereQ={q0,q1,q2}
∑={0,1}
δ:δ(q0,0)=q2
δ(q0,1)=q1
δ(q1,0)=q1
δ(q1,1)=q1
δ(q2,0)=q2
δ(q2,1)=q2
q0–initialstate
F–finalstate={q1}
L={00,100,000,001,1100,1000,0100,1001,0001,11000,11100,........}
Step2:Drawtransitiondiagramfortheminimumlengthstring.
Step4:Drawthetransitiontable.
Input
State
0 1
q0 q1 q0
q1 q2 q0
q2 q2 q2
Step 5: Test DFA with few strings that are accepted and few
strings that are rejected by the given language.
Casei)Letw=1001∈L
δ(q0,1001)=δ(q0,001)
=δ(q1,01)
=δ(q2,1)
=q2
q2 is final state and the entire string has been consumed
i.e., given string is accepted by DFA.
Caseii)Letw=1011∉L
δ(q0,1011)=δ(q0,011)
=δ(q1,11)
=δ(q0,1)
=q0
q0is not final state and the entire string has been consumed i.e.,
given string is rejected by DFA.
Step6:RepresentDFAwithtuples.
DFA,M=(Q,∑,δ,qo,F)
whereQ={q0,q1,q2}
∑={0,1}
δ:δ(q0,0)=q1
δ(q0,1)=q0
δ(q1,0)=
q2
δ(q1,1)=
q0
δ(q2,0)=
q2
δ(q2,1)=
q2
q0–initialstate
F–finalstate={q2}
Nondeterministicfiniteautomaton(NDFA/NFA):
Anondeterministicfiniteautomatonisa5-tuple(Q,∑,δ,qo,F),where
• Qisafinitenonemptysetofstates;
• ∑isafinitenonemptysetofinputs;
• δ isthetransitionfunctionmappingfromQx∑into2Qwhichisthe
power set of Q, the set of all subsets of Q;
• qo∈Qistheinitialstate;and
• F⊆Qisthesetoffinalstates
StepstodesignaNFA:
1. Understand the language for which theNFA has to be designed
and write the language for the set of strings starting with
minimum string that is accepted by FA.
2. Drawtransitiondiagramfortheminimumlengthstring.
3. Obtainthepossibletransitionstobemadeforeachstateoneachinp
ut symbol.
4. Drawthetransitiontable.
5. Test NFA with few strings that are accepted and few strings that
are rejected by the given language.
6. RepresentNFAwithtuples.
Examples:
1. Design NFA that accepts all strings which contains ‘00’ as substring
over the alphabet {0,1}
Step2:Drawtransitiondiagramfortheminimumlengthstring.
Step
3:Obtainthepossibletransitionstobemadeforeachstateoneach
input symbol. 0, 1
Step4:Drawthetransitiontable.
Input
State
0 1
q0 {q0,q1} q0
qq11 q2 -
q2 q2 q2
Step 5: Test NFA with few strings that are accepted and few
strings that are rejected by the given language.
Casei)Let w=0100∈L
δ(q0,0100)=δ({q0,q1},100)
=δ(q0,00)
=δ({q0,q1},0)
={q0,q1,q2}
IIYear–II-Semester 2018-19 CSE
FormalLanguagesandAutomataTheory 31
q2is final state and the entire string has been consumed i.e.,
given string isacceptedbyNFA.
Caseii)Letw=1011∉L
δ(q0,1011)=δ(q0,011)
=δ({q0,q1},11)
=δ(q0,1)
=q0
q0 is not final state and the entire string has been consumed i.e.,
given string is rejected by NFA.
Step6:RepresentNFAwithtuples.
NFA,M=(Q,∑,δ,qo,F)
whereQ={q0,q1,q2}
∑={0,1}
δ:δ(q0,0)={q0,q
1} δ(q0,1) =
q0 δ(q1,0) = q2
δ(q1,1) =Ø
δ(q2,0) = q2
δ(q2,1) = q2
q0–initialstate
F–finalstate={q2}
Step4:Drawthetransitiontable.
Input
State
0 1
q0 {q0,q3} {q0,q1}
q1 - q2
q2
q2 q2
q3 q4 -
q4 q4
q4
Step 5: Test NFA with few strings that are accepted and few
strings that are rejected by the given language.
Casei)Lettheinput,w=01001 ∈L
δ(q0,0)={q0,q3}
δ(q0,01)=δ(δ(q0,0),1)=δ({q0,q3},1)=δ(q0,1)∪δ(q3,1)={q0,q1}
Similarly,wecompute
δ(q0,010)={q0,q3}, δ(q0,0100)={q0,q3,q4}
and
δ(q0,01001)={q0,q1,q4}
final state
Aftertheentirestringisconsumed,theFAisinthestateq4
. Asq4isthefinalstate,thestringisaacceptedbyFA
Caseii)Letw=010∉L
δ(q0,010)=δ({q0,q3},10)
=δ({q0,q1},0)
={q0,q3}
Thereisnopathtothefinalstateaftertheentirestringisconsumed.
So the string is rejected by FA.
Step6:RepresentNFAwithtuples.
NFA,M=(Q,∑,δ,qo,F)
whereQ={q0,q1,q2,q3,q4}
∑={0,1}
δ:δ(q0,0)={q0,q3}
δ(q0,1)={q0,q1}
δ(q1,0)=Ø
δ(q1,1)=
q2
δ(q2,0)=
q2
δ(q2,1)=
q2
δ(q3,0)=
q4
δ(q3,1)=
Ø
δ(q4,0)=q4
δ(q4,1)=q4
q0–initialstate
F–finalstate={q2,q4}
Note: The minimal state DFA, accepting all strings over the alphabet
{0, 1} where the nth symbol in every string from the right end is a 1,
has 2nstates.
Languagerecognizers:
A languagerecognizeris a device thatacceptsvalid stringsproduced in
a given language. Finite state automata are formalized types of
language recognizers.
ThelanguageacceptedbyFiniteAutomataMdesignatedL(M)istheset{x|
δ(q0,x)isinF}.
ApplicationsofFA:
• UsedinLexicalanalysisphaseofacompilertorecognizetokens.
• Usedintexteditorsforstringmatching.
• Softwarefordesigningandcheckingthebehaviorofdigital circuits.
LimitationsofFA:
• FA’swillhavefiniteamountofmemory.
IIYear–II-Semester 2018-19 CSE
FormalLanguagesandAutomataTheory 36
UNIT-I
Assignment-Cum-TutorialQuestions
SECTION-A
ObjectiveQuestions
1. Theprefixofabcis [ ]
A) c B)bc C)b D)ε
2. ∑*=∑ U ε
+ [True|False]
3. Alphabet is . [ ]
A) Finitecollectionofstrings. B) Finite collectionof symbols.
C)Finitecollectionoflanguages. D)Alltheabove.
4. A ofastringisanynumberofleadingsymbolsofthatstring.
5. is a directedgraph associated with an FAin which
thevertices of the graph correspond to the states of the FA.
6. The transition function for NFA is a mapping function given as
.
7. The transition function for DFA is a mapping function given as
.
8. A = {a, b, c}. Power set of A= .
9. FAhas [ ]
A) Unlimitedmemory B)nomemoryat all
C)Limited memory D)noneoftheabove.
10. Numberofstatesrequirestoacceptstringendswith10. [ ]
A) 3 B)2 C)1 D)can’tberepresented.
11. Considerthefiniteautomatoninthefollowingfigure
Whatisthesetofreachablestatesfortheinputstring0011? [ ]
A) {q0,q1,q2} B){q0,q1} C){q0,q1,q2,q3} (D){q3}
12. GiventhelanguageL={ab,aa,baa},whichofthefollowingstringsareinL*?
1) abaabaaabaa 2) aaaabaaaa 3)baaaaabaaaab4)baaaaabaa[ ]
A)1,2and3 B)2,3and4 C)1,2and4 D)1, 3and4
13. Intheautomatonbelow,sisthestartstateandtistheonlyfinalstate.
A)Setofallstringsthatdonotendwithab [
B) Setofallstringsthatbeginwitheitheranaorab
C) Setofallstringsthatdonotcontainthesubstringab,
D) Alltheabove
15. WhatistheminimumnumberofstatesintheNFAacceptingthelanguage
{a,ab}?
A) 3 B) 2 C)1 D)4 [ ]
16. Thesmallestfiniteautomationwhichacceptsthelanguage{x|
lengthofx isdivisible by 3} has [ ]
A) 2states B)3states C)4statesD)5states
17. ThebelowDFAacceptsthesetofallstringsover{0,1}that [ ]
a) begineitherwith0or1 b)endwith0
c)endwith00 d)containthesubstring00
18. Consider a DFA over ∑={a,b} accepting allstrings which have number of a’s
divisible by 6 and number of b’s divisible by 8. What is the number
of states that the DFA will have?
[ ]
A) 8 B) 14 C) 15 D) 48
SECTION-B
SUBJECTIVEQUESTIONS
1. Definestringandalphabet.
2. Explainoperationsonstringsandlanguages.
3. DefinePositiveClosureandKleeneClosure.
4. Define(i)FiniteAutomaton(FA) (ii)Transitiondiagram
5. ExplainthemodelofFA.
6. WritethedifferencesbetweenNFAandDFA.
7. Whatisthedifferencebetweenemptylanguageandnullstring?
8. WhichofthefollowingFiniteAutomatonishavingambiguityandwhy?
i) NFAii)DFA
9. DrawtheFinitestatemachinefor acceptingthelanguages□andØ.
10. Fromthegiventransitiontable.Checkwhetherthefollowingstringsa
re accepted or not.
i) 101101
ii) 000000
Q/∑ 0 1
q0
q2 q1
q1 q3 q0
q2 q0 q3
q3 q1 q2
11. ConstructDFAacceptingthesetofallstringsbeginningwith101.
12. Design a DFA for a language which contains strings of a’s & b’s
and each string ends with aab.
13. DescribethewordswinthelanguageLacceptedbytheautomatonin
14. Design DFA accepting the set of all strings that begin with 01 and
end with 11.
15. a)DesignaDFAtoacceptthefollowinglanguage.L={w:|w|mod3=0} on
Σ={a}
b) DesignDFAacceptingthelanguagewhosebinaryinterpretationis
divisible by 5 over the alphabet {0, 1}.
16. DesignaDFAtoacceptstringsofa’sandb’shavingevennumberofa’sand
b’s.
17. DesignaDFAthatacceptsallstringsoverΣ={0,1}thatdonotcontain101
asasubstring.
18. DesignNFAthatacceptsthelanguageofstringsoverΣ={0,1}suchthat some
two 0’s are separated by a string whose length is 4i, for some i ≥0.
19. DesignaNFAtoacceptstringsof0’s&1’ssuchthateachstringendswi
th 00.
20. FortheNFAgivenbelow;
i. Checkwhetherthestringaxxaxxaisacceptedornot
ii. Giveatleasttwotransitionpaths
SECTION-C
1. ConsiderthefollowingDeterministicFiniteAutomata
Whichofthefollowingistrue? [ ]
A) Itonlyacceptsstringswithprefixas“aababb”
B) Itonlyacceptsstringswithsubstringas“aababb”
C) Itonlyacceptsstringswithsuffixas“aababb”
D) Noneoftheabove
2. The possiblenumberofstatesofadeterministicfiniteautomatonthat
accepts a regular language
L={w1aw2|w1,w2∈{a,b}*,|w1|= 2,w2>=3}is
[GATE2017set-2]
3. Letwbeanystringoflengthnin{0,1}*.LetLbethesetofallsubstringsof
w.Whatisthenumberofstatesinanon-deterministicfiniteautomaton
that accepts L? [ ]
A)n-1 B)n C)n+1 D)2n-1
[GATE2010
]
4. ConsiderthemachineM:
[GATE2005]
ThelanguagerecognizedbyMis: [ ]
a) {wЄ{a,b}*|everyainwisfollowedbyexactlytwob’s}
b) {wЄ{a,b}*|everyainwisfollowedbyatleasttwob’s}
c) {wЄ{a,b}*|wcontainsthesubstring‘abb’}
d) {wЄ{a,b}*|wdoesnotcontain‘aa’asasubstring}
5. Thefollowingfinitestatemachineacceptsallthosebinarystringsinwhich
the number of 1’s and 0’s are respectively [ ]
[GATE2004]
a) divisibleby3and2 b)oddandeven
c)evenandodd d)divisibleby2and3
6. ConsiderthefollowingdeterministicfinitestateautomatonM.
[GATE2003]
LetSdenotethesetofsevenbitbinarystringsinwhichthefirst,thefourt
h,
andthelastbitsare1.ThenumberofstringsinSthatareacceptedbyMis
[ ]
(A)1 (B)5 (C)7 (D)8
7. ConsidertheNFAMshownbelow.
LetthelanguageacceptedbyMbeL.LetL1bethelanguageacceptedbyth
e NFA M1, obtained by changing the accepting state of M to a non-
accepting state and by changing the non-accepting state of M to
accepting states. Which of the following Statements is true? []
A) L1={0,1}*–L B)L1={0,1}*
C)L1⊆L D)L1=L
8. Constructafinitestatemachinethatacceptsthelanguage,over{0,1}ofall
stringsthatcontainneitherthesubstring00northesubstring11.
[Gate2000]
9. What can be said about a regular language L over {a} whose
minimal finitestate automaton has two states? [ ]
[Gate2000]
A) Lmustbe{an|nisodd}
B) Lmustbe{an|niseven}
C) Lmustbe{an|>=0}
D) EitherLmustbe{an|nisodd},orLmustbe{an|niseven}
UNIT–II
Objective:
Tofamiliarizehowtoemploynon-deterministicfiniteautomatawithεtransitions
andfiniteautomatawithoutputs.
Syllabus:
FiniteAutomata:
NFAwithεtransitions-significance,acceptanceoflanguages,equivalencebetween
NFA with and without ε transitions, NFA to DFA conversion, minimization of
FSM, equivalence between two FSM‘s, finite automata with output-Moore
and Mealy machines, applications of FA.
LearningOutcomes:
Studentswillbeableto:
• ConvertNFAtoDFAandNFAwithepsilontransitionstoNFAwithout
Epsilon transitions.
• MinimizethegivenDFA.
• TestwhetherthetwoDFA’sareequivalentornot.
• DesignMooreandMealyMachines
NFA withεtransitions:
An ε-NFAisatuple(Q,Σ,δ,qo,F)
where
• Qisasetofstates,
• Σisthealphabet,
• δisthetransitionfunctionthatmapseachpairconsistingofastateanda symbolin Σ
∪ { ε } to a subset of Q,
• q0istheinitialstate,
• F⊂Qisthesetoffinal(oraccepting)states.
Significanceofε-NFA:
Itbecomesverydifficultormanytimesitseemstobeimpossibletodrawdirectly
NFA or DFA.
Example:
Stringacceptancebyε–NFA
Fig:1
TransitionTable:
Q/∑ a b ε
q0 - - {q1,q2}
q1 q3 - -
q2 - q4 -
q3 q1 - -
q4
- q2 -
Example:
Checkwhetherthestring‘bbb’isacceptedornotfortheaboveautomaton.
ε b b b
q0 q2 q4 q2 q4q1
ε
ε–NFAtoNFA Conversion:
Step1:Findtheε-closureforallstatesinthegivenε-NFA.
ε-closure (q) denotes the set of all states p such that there is a path from
q to p labelled ε.
Step2:Findtheextendedtransitionfunctionforallstatesonallinputsymbolsfor
thegivenε-NFA.
δ'(q,a)=ε-closure(δ(δ'(q, ε),a))
Step3:Drawthetransitiontableordiagramfromtheextendedtransitionfunctio
n (NFA)
Step4:FisthesetoffinalstatesofNFA,whoseε-closurecontainsthefinalstate
of ε -NFA.
Example:
1.ConvertNFAwithε-movesintoanequivalentNFAwithoutε-moves.
Step2:Findtheextendedtransitionfunctionforallstatesonallinputsymbolsfor
thegivenε-NFA.
δ'(q0,0)=ε-closure(δ(δ'(q0,ε),0))
=ε-closure(δ{q0,q1,q2},0)
=ε-closure(δ(q0,0)Uδ(q1,0)Uδ(q2,0))
=ε-closure(q0UØUØ)
={q0,q1,q2}
δ'(q0,1)=ε-closure(δ(δ'(q0,ε),1))
=ε-closure(δ{q0,q1,q2},1)
=ε-closure(δ(q0,1) Uδ(q1,1) Uδ(q2,1))
=ε-closure(ØUq1UØ)
={q1,q2}
δ'(q0,2)=ε-closure(δ(δ'(q0,ε),2))
=ε-closure(δ{q0,q1,q2},2)
=ε-closure(δ(q0,2)Uδ(q1,2)Uδ(q2,2))
=ε-closure(q2UØ)
={q2}
δ'(q1,0)=ε-closure(δ(δ'(q1,ε),0))
=ε-closure(δ{q1,q2},0)
=ε-closure(δ(q1,0)Uδ(q2,0))
=ε-closure(Ø)
={Ø }
δ'(q1,1)=ε-closure(δ(δ'(q1,ε),1))
=ε-closure(δ{q1,q2},1)
=ε-closure(δ(q1,1)Uδ(q2,1))
=ε-closure(q1)
={q1,q2}
δ(q1,2)=ε-closure(δ(δ'(q1,ε),2))
=ε-closure(δ{q1,q2},2)
=ε-closure(δ(q1,2)Uδ(q2,2))
=ε-closure(q2)
={q2}
δ(q2,0)=ε-closure(δ(δ'(q2,ε),0))
=ε-closure(δ(q2,2))
=ε-closure(Ø)
={Ø}
δ(q2,1)=ε-closure(δ(δ'(q2,ε),1))
=ε-closure(δ(q2,1))
=ε-closure(Ø)
={Ø}
δ(q2,2)=ε-closure(δ(δ'(q2,ε),2))
=ε-closure(δ(q2,2))
=ε-closure(q2)
={q2}
Step3:Drawthetransitiontableordiagramfromtheextendedtransitionfunctio
n (NFA)
Inputs
State
0 1 2
q0 {q0,q1,q2} {q1,q2} q2
q1 Ø {q1,q2} q2
*q2 Ø Ø q2
Step4:FisthesetoffinalstatesofNFA,whoseε-closurecontainsthefinalstate
ofε-NFA.
Inputs
State
0 1 2
q0 {q0,q1,q2} {q1,q2} q2
q1 Ø {q1,q2} q2
q2 Ø Ø q2
Stringacceptancebyε-NFA:
Letw=001
0 0 ε 1 ε
q0 q0 q0 q1 q1 q2
Asq2is thefinalstate,thestringisacceptedbythegivenε-NFA.
StringacceptancebyNFA:
Ifw=001
0 0 1
q0 q0 q 0q q1
0 0 1 1 q2
0 q10q2
q2
Asq1 andq2arefinalstates,thestringisacceptedbytheNFA.
NFAtoDFAConversion:
Step1:FirsttakethestartingstateofNFAasthestartingstateofDFA.
Step 2: Apply the inputs on initial state and represent the corresponding
states in the transitiontable.
Step3:Foreachnewlygeneratedstate,applytheinputsandrepresentthe
corresponding states in the transition table.
Step4:Repeatstep3untilnomorenewstatesaregenerated.
Step 5:The states which contain any of the final statesof the NFA are the
final statesoftheequivalentDFA.
Step6: Representthetransitiondiagramfromtheconstructedtable.
Step7:TochecktheequivalenceofNFAandDFA,thestringacceptedbyNFA should
be accepted by DFA.
Step8: Write thetuplerepresentation forthe obtainedDFA.
Example:
1.ConstructDFAequivalenttothe NFAM=({q0,q1},{0,1},δ,q0,{q1})
whereδ(q0,0)={q0,q1} δ(q0,1)={q1} δ(q1,0)=Ø δ(q1,1)={q0,q1}
Step1:FirsttakethestartingstateofNFAasthestartingstateofDFA
Q/∑ 0 1
[q0]
Step 2: Apply the inputs on initial state and represent the corresponding
states in the transitiontable.
Q/∑ 0 1
[q0] [q0,q1] [q1]
Step3:Foreachnewlygeneratedstate,applytheinputsandrepresentthe
corresponding states in the transition table.
Q/∑ 0 1
[q0] [q0,q1] [q1]
[q0,q1] [q0,q1] [q0,q1]
[q1] Ø [q0,q1]
Step4:Stoptheprocedureastherearenomorenewstatesbeinggenerated.
Step 5:The stateswhich contain any of the final statesof the NFA are the
final statesoftheequivalentDFA.
q1isthe final state in NFA. q1is included in the state [q0,q1] and [q1]. So
[q0,q1]and [q1] are the final states of the DFA.
Q/∑ 0 1
[q0] [q0,q1] [q1]
[q1] Ø [q0,q1]
Step6:Representthe transitiondiagramfromtheconstructedtable.
Step7:TochecktheequivalenceofNFAandDFA,thestringacceptedbyNFA should
be accepted by DFA.
Letw=1110bethestringacceptedbyNFA.
AcceptabilitybyNFA:
1 q1 0 Ø
1 q 0 q0
q0 1 q1 1 q0
1 1 0 q1
1 q1
q 0
0
Ø
AcceptabilitybyDFA:
1 1 1 0
δ([q0],1110)=δ([q1],110) [q0] [q1] [q0,q1] [q0,q1] [q0,q1]
=δ([q0,q1],10)
=δ([q0,q1],0)
=[q0,q1]□F
∑={0,1}
δ-transitionfunction
MinimizationofFiniteAutomata:
Twostatesqlandq2areequivalent(denotedbyq1≡q2)ifbothδ(q1,x)andδ(q2,x)
arefinalstates. orbothof themarenonfinalstatesforall x□∑*.
Twostatesq1andq2arek-equivalent(k≥0)ifbothδ(q1,x)andδ(q2,x)arefinal
statesorbothnonfinalstatesforallstringsxoflengthkorless.Inparticular,an
y two final states are 0-equivalent and anytwononfinal states are also 0-
equivalent.
ConstructionofMinimumAutomaton:
Step 1:(Construction ofπ0)· By definition of 0-equivalence,π0={Q10, Q20}
where Q10is the set of all final states and Q20=Q - Q10.
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 10
Step2:(Constructionofπk+1fromπk).
• LetQikbeanysubsetinπk.Ifq1andq2areinQik,theyare(k+1)-
equivalent provided δ(q1,a)andδ(q2,a)arek-equivalent.
• Findoutwhetherδ(q1,a)andδ(q2,a)areinthesameequivalence classin
πkforeverya□∑.Ifsoq1andq2are(k+1)-equivalent.
• Inthisway,Qikisfurtherdividedinto(k+1)-equivalenceclasses.Repeat
this for every Qik in πkto get all the elements of πk+1.
Step3:Constructπnforn=1,2,.......untilπn=πn+1.
Example:
Constructaminimumstateautomatonequivalenttothefiniteautomaton.
Solution:
Itwillbeeasierifweconstructthetransitiontable.
Step 1: Constructionof
whereQ10=F={q2} Q20=Q-Q10
∴π0={{q2},{q0,q1,q3,q4,q5,q6,q7}}
Step2:The{q2}inπ0cannotbefurtherpartitioned.So,Q11={q2}.
Compare q0with q1, q3,q4,q5,q6and q7.
Considerqoandq1□Q20.
• The entries under the 0- column corresponding to qo and q1 are q1
and q6; they lie in Q20.
• Theentriesunderthe1-columnareq5andq2.q2□Q10andq5□Q20.
Thereforeqoandq1arenot1-equivalent.
Q/∑ 0 1
q0 q1 q5
q1 q6 q2
Considerq0andq3
Q/∑ 0 1
q0 q1 q5
q3 q2 q6
Q/∑ 0 1
q0 q1 q5
q4 q7 q5
{qo.q4,q6}isasubsetinπ1.
So,Q21={q0,q4,q6}
Step3:Constructπnforn=1,2,.......untilπn=πn+1.
Calculate2-equivalent,π2.
π2={{q2},{qo,q4},{q6},{q1,q7},{q3,q5}}
Similarlycalculate3-equivalent,π3.
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 14
π3={{q2},{qo,q4},{q6},{q1,q7},{q3,q5}}
Asπ2=π3,π2givesustheequivalenceclasses.
Step4: Constructionofminimumautomaton.
M'=(Q',{0,1},δ',q0',F')
where Q' = {[q2]. [q0, q4],[q6], [q1,q7],
EquivalencebetweentwoFSM’s:
Let M and M’ be two FSM’s over∑.We construct a comparison table
consisting of n+1 columnswhere n is the number of input symbols.
Step1:1stcolumnconsistingofa pairofstatesofform (q, q’) whereq belongs toM
and q’ belongs M’.
Step2:If(q,q’)appearsinthesamerowof1stcolumnthenthecorresponding entryin
a column (a belongs to ∑)is(r,r’) where (r,r’) are pairfromq and q’ ona.
Example:
Checkwhetherthegiventwofiniteautomata’sareequivalentornot.
Solution:
q1isinitial state ofM1and q4isinitialstateofM2,makethema
pairandplaceit in 1strow of the transition table.
Comparisontable
Q/∑ c d
(q2,q5) (q3,q4)
Hereq3isnon-finalstateandq4isfinalstate.
MooreMachine
AMooremachineisasixtuple(Q,∑,∆,δ,q0,λ)
where
• Qisasetofstates,
• Σisthealphabet,
• δis thetransitionfunctionthat mapseachpairconsisting ofastateanda symbol
in Σ to Qi.e. .Q XΣ-> Q
• q0istheinitial state,
• ∆isoutputalphabet
• λisamappingfromQto∆givingtheoutputassociatedwitheachstate
Note:For a Moore machine if the input string is of length n, the output string
is of length n + 1. The first output is λ (qo)for alloutput strings.
MealyMachine
AMealymachineisasixtuple(Q,∑,∆,δ,q0,λ)
where
• Qisasetofstates,
• Σisthealphabet,
• δis thetransitionfunctionthat mapseachpairconsisting ofastateanda symbol
in Σ to Q i.e. .Q XΣ-> Q
• ∆isoutputalphabet
• q0istheinitial state,
• λmapsQx∑to∆i.e.,λ(q,a)givestheoutputassociatedwiththetransition
fromstateqon inputa
Example:
• Thegiventransitiondiagramismooremachinebecauseeachstateis
associated with output.
w=011theoutputis0010
q0 0 q0 1 q1 1 q0Transitions
0 0 1 0 Outputs
Example:
• The given transition diagram is mealy machine because output
depends on present state and present input.
• Inthebelowdiagram
λ(q0,0)=0 λ(q1,0)=2 λ(q2,0)=0
λ(q0,1)=1 λ(q1,1)=0 λ(q2,1)= 2
w=011theoutputis010
q0 0 q0 1 q1 1 q0Transitions
0 1 0 Outputs
Example:
1. DesignMooremachinetodeterminetheresiduemod3foreachbinary string
treated as a binary integer.
Mooremachinecalculatingresidue mod 3
MooreTable
Present NextState
Output
State 0 1
q0 q0 q1 0
q1 q2 q0 1
q2 q1 q2 2
TupleRepresentation:
q0={q0}
Example:
1. DesignMealymachinetodeterminetheresiduemod3foreachbinary string
treated as a binary integer.
MealyTable:
q1 q2 2 q0 0
q2 q1 1 q2 2
TupleRepresentation:
Q={q0,q1,q2} ∆={0,1,2} ∑={0,1}
q0={q0}
λ(q1,1)=0
λ(q2,0)=1
λ(q2,1)=2
MooretoMealyConversion:
IfM1=(Q,∑,∆,δ,q0,λ)isaMooremachine,thenthereisaMealymachineM2 equivalent to
Ml.
Procedure:
• ThenMlandM2enterthesamesequenceofstatesonthesameinput,an
d with each transition M2emits the output that Mlassociates with
the state entered.
Example:
Construct a Mealy Machinewhich is equivalent to theMoore machine given
by table below.
Present NextState
Output
State 0 1
q0 q3 q1 0
q1 q1 q2 1
q2 q2 q3 0
q3 q3 q0 0
Solution:
λ'(q,a)tobeλ(δ(q,a))
λ'(q0,0)=λ(δ(q0,0)) λ'(q0,1)=λ(δ(q0,1))
=λ(q3) =λ(q1)
=0 =1
λ'(q1,0)=λ(δ(q1,0)) λ'(q1,1)=λ(δ(q1,1))
=λ(q1) =λ(q2)
=1 =0
λ'(q2,0)=λ(δ(q2,0)) λ'(q1,1)=λ(δ(q2,1))
=λ(q2) =λ(q3)
=0 =0
λ'(q3,0)=λ(δ(q3,0)) λ'(q3,1)=λ(δ(q3,1))
=λ(q3) =λ(q0)
=0 =0
MealyTable:
q1 q1 1 q2 0
q2 q2 0 q3 0
q3 q3 0 q0 0
MealytoMooreConversion:
If M1= (Q,Σ,Δ,δ, λ, q0) is a Mealy machine, then there is a Moore machine M2
equivalent to Ml.
Procedure:
• Determine the number of different output associated with qi in the
next state column.
• Wesplitqiintodifferentstatesaccordingtodifferentoutputassociate
dwith it
For example: q2is associated with two different outputs 0 and 1, so
we split q2into q20 and q21.
Example:
ConstructMooremachineforthegivenmealymachine.
Solution:
• We get two states (q1 and q2) that are associated with different
outputs (0 and 1). so we split both states into q 10, q11and q20, q21.
UNIT-II
Assignment-Cum-TutorialQuestions
SECTION-A
ObjectiveQuestions
1. WhatisthecomplementofthelanguageacceptedbytheNFAshownbelow?
[ ]
2. NFAwithεcanincreasetheprocessingtimeofNFA [True/False]
3. ofastateisthesetofstatesthatcanbereachedbyε- transitions.
4. Thenumberofstates inDFAis thenumberofstatesinNFAforthe
samelanguage. [ ]
(A)greaterthan (B)lessthan (C)equalto (D)none
5. Given a Non-deterministic Finite Automaton (NFA) with states p and r
as initial states and final states respectively and transition table as
given below:
TheminimumnumberofstatesrequiredinDeterministicFiniteAutomaton
(DFA) equivalent to NFA is [ ]
(A) 5 (B) 4 (C)3 (D)2
6. Theoutputin machineisassociatedwithtransition. [ ]
(A)Moore (B) Mealy (C)both (D)DFA
7. The twostates q 1 andq 2 are saidtobe ifboth δ (q 1,a)and
(q2,a) reach final states or both of them reach non final states for all
a∈ ∑.
8. For a Moore machine if the input string is of length n, the output
string is of length n + 1. [True/False]
9. InaMealymachineiftheinputstringisoflengthn,theoutputstringisof
length .
(A)n (B)n+1 (C)2n (D)n+2
10. Chooseincorrectstatement. [ ]
(A) MooreandMealymachinesareFSM’swithoutputcapability.
(B) AnygivenMooremachinehasanequivalentMealyMachine.
(C) AnygivenMealymachinehasanequivalentMooreMachine.
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 24
NextState
Present
State
state output state output
q0 q0 0 q1 1
q1 q2 2 q0 0
q2 q1 1 q2 2
Iftheinitialstateisq0.Whatistheoutputsequenceforthestring101?
(A)0012 (B)122 (C)112 (D)0122
SECTION-B
SUBJECTIVEQUESTIONS
1. Considerthefollowingfiniteautomatonwith□-
movesObtainequivalent automaton without □-moves.
2. ConstructNFAforthesetofstringsin(0+1)*suchthatsometwo0’sare
separated by a string whose length is 4i, for some i≥0.
3. ConstructaNFAwithout ∈forthefollowingNFAwith∈.
4. Defineε-closure.Findtheε-closuresoftheeachstateinthefollowingε-NFA.
5. ConstructanequivalentDFAforaNDFAM=({q1,q2,q3},q1,q3)whereδis
givenby
δ(q1,0)={q2,q3}, δ(q1,1)={q1},
δ(q2,0)={q1,q2}, δ(q2,1)=∅
δ(q3,0)={q2}, δ(q3,1)={q1,q2}
6. ConstructanequivalentDFAforthefollowingNFA
7. VerifywhetherthefollowingFAisequivalent?
8. FindtheequivalencebetweenM1&M2
9. Constructtheminimumstateautomatonequivalenttothetransitiondiagram
10. ConstructaminimumstateautomatonequivalenttoagivenautomatonM
whosetransitiontableisdefinedbytable
11. Explainaboutthefiniteautomatawithoutputsindetail.
12. ConstructaMealymachinewhichisequivalenttotheMooremachine
defined by table
13. ConstructaMooremachineequivalenttotheMealymachineMdefinedby
SECTION-C
QUESTIONS AT THE LEVEL OF GATE
1. Let denotethetransitionfunctionand
denotetheextendedtransition functionoftheε-
NFAwhosetransitiontableisgivenbelow:
[GATE2017(Set2)]
Then (q2,aba)is [ ]
(A)∅ (B){q0,q1,q3} (C) {q0,q1,q2} (D){q0,q2,q3}
2. Adeterministicfiniteautomation(DFA)DwithalphabetΣ={a,b}isgivenbelow
[GATE2011]
WhichofthefollowingfinitestatemachinesisavalidminimalDFAwhich
accepts the same language as D? [ ]
3. Considerthefollowingfinitestateautomaton [ ]
The minimum state automaton equivalent to the above FSA has the
followingnumber of states [GATE 2007 ]
(A)1 (B)2 (C)3 (D)4
4. The followingdiagramrepresentsa finite state machinewhich
takesasinput a binary number from the least significant bit.
[GATE 2005]
Whichoneofthefollowingistrue? [ ]
(A)Itcomputes1’scomplementoftheinputnumber
(B)Itcomputes2’scomplementoftheinputnumber
(C)Itincrementstheinputnumber
(D)Itdecrementstheinputnumber
(A) Outputsthesumofthepresentandthepreviousbitsoftheinput
(B) Outputs01whenevertheinputsequencecontains11
(C) Outputs00whenevertheinputsequencecontains10
(D) Noneoftheabove
6. Givenanarbitrarynon-
deterministicfiniteautomaton(NFA)withNstates,the
maximumnumberofstatesinanequivalentminimizedDFAisatleast
(A)N2 (B) 2N (C)2N (D)N! [ ] [GATE2001]
UNIT-III
Objective:
Tofamiliarizehowtoemployregularexpressions.
Syllabus:
Regular sets, regular expressions, identity rules, construction of
finite Automata for a given regularexpressions and its inter
conversion, pumping lemma of regular sets, closure properties of
regular sets (proofs not required),applications of regular
languages.
LearningOutcomes:
Studentswillbeableto:
• understandtheregularsetsandhowtorepresenttheregularexpr
essions.
• constructfiniteAutomataforagivenregularexpressionandviceversa.
• listclosurepropertiesofregularlanguages.
• understandthedifferentapplicationsofregularlanguages.
LearningMaterialRegul
ar set:
A language is a regular set (or just regular) if it is the set accepted
by some finite automaton.
Example:
L= {0, 1, 10, 00, 01, 11, 000, 101,.................} is a regular set
representing any no of 0’s and any no of 1’s.
Regularexpression:
Thelanguages acceptedby finiteautomata are easily described by
simple expressions called regular expressions.
1) Øisaregularexpressionanddenotestheemptyset.
2) εisaregularexpressionanddenotestheset{ε}.
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 2
3) ForeachainΣ,aisaregularexpressionanddenotestheset{a}.
4) IfrandsareregularexpressionsdenotingthelanguagesRandS,
respectively, then
(r+s),(rs),and(r*)areregularexpressionsthatdenotethesetsRUS,RS,
and R*, respectively.
SomeExampleson Regularexpressions
1. Writeregularexpressionsforeachofthefollowinglanguagesover
∑={0,1}.
a) Thesetrepresenting{00}.
00
b) Thesetrepresentingallstringsof0'sand1's.
(0+1)*
c) Thesetofallstringsrepresentingwithatleasttwoconsecutive0’s.
(0+1)*00(0+1)*
d) Thesetofallstringsendingin011.
(0+1)*011
e) The set of all strings representing any number of 0's followed
by any number of 1’s followed by any number of 2's.
0*1*2*
f) Theset of all stringsstartingwith011.
011(0+1)*
2. Writeregularexpressionsforeachofthefollowinglanguagesover
∑={a, b}.
a) Thesetofallstringsendingwitheitheraorbb.
(a+b)*(a+bb)
b) Theset of strings consisting of even no. of a’s followed by
odd no. of b’s.
(aa)*(bb)*b
c) Thesetofstringsrepresentingevennumberofa’s.
(b*ab*ab*)*+b*
IdentityRulesRelatedtoRegularExpressions
Givenr,sandtareregularexpressions,thefollowingidentitieshold:
• ∅*=ε
• ε*=ε
• r+=rr*=r*r
• r*r*=r*
• (r*)*=r*
• r+s=s+r
• (r+s) +t=r+(s+ t)
• (rs)t=r(st)
• r(s+t)=rs+rt
• (r+ s)t=rt+st
• (ε+r)*=r*
• r+∅=∅+r=r
• rε=εr=r
• ∅L=L∅=∅
• r+r=r
• ε+rr*=ε+r*r=r*
ConstructionofFiniteautomataforagivenregularexpression
EquivalenceofFiniteAutomataandRegularExpressions
• Thelanguagesacceptedbyfiniteautomataarepreciselythe
languages denotedby regularexpressions.
• Foreveryregular expressionthereisanequivalentNFAwithε-
transitions.
• ForeveryDFAthereisaregularexpressiondenotingitslanguage.
Oneormoreoperators:
Letrhaveioperators.Therearethreecasesdependingontheformofr.
Case1:Union(r=rl+r2.)
ThereareNFA’sM1=(Q1,∑1,δ1,q1,{f1})andM2=(Q2,∑2,δ2,q2,{f2})
withL(M1)=L(r1)andL(M2)=L(r2).
Construct
M=(Q1∪Q2∪{q0,f0},∑1∪∑2,δ,q0,{f0})whereδisdefinedby
i) δ(q0,ε)={q1,q2}
iv) δ(f1,ε)=δ1(f2,ε)={f0}
L(M)=L(M1)∪L(M2)
Case2:Concatenation(r=r1r2).
whereδisdefinedby
ii) δ(f1,ε)={q2}
iii) δ(q,a)=δ2(q,a)forqinQ2andain∑2∪{ε}
L(M)={xy|xisinL(M1)andyisinL(M2)}andL(M)=L(M1)L(M2)
Case3:Closure(r= r1*)
LetM1=(Q1,∑1,δ1,q1,{f1})andL(M1)=r1.
ConstructM=(Q1∪{q0,f0},∑1,δ,q0,{f0}),whereδisdefinedby
i) δ(q0,ε)=δ(f1,ε)={q1,f0}
Example:
1.ConstructanNFAfortheregularexpression01*+1
Regularexpressionisoftheformr1+r2,wherer1=01*andr2=1.
The automaton for r2is
Expressr1asr3andr4,wherer3=0andr4=1*
r4isr5*wherer5=1 The
ToconstructanNFAforr4=r5* usetheconstructionofclosure.The
resulting NFAfor r4 is
Then,for r1=r3r4usetheconstructionofconcatenation.
ConstructionofregularexpressionsforthegivenfiniteAutomata:Arde
n’s Theorem
LetPandQbetworegularexpressionsoverΣ,andifPdoesnotcontain
epsilon,thenR=Q+RPhasauniquesolutionR=QP*.
Procedure:
Assumethegivenfiniteautomatashouldnotcontainanyepsilons.
Step1:FindthereachabilityforeachandeverystateingivenFinite
automata.
Reachabilityofastateisthesetofstateswhoseedgesenterintothatstate.
Step2:Fortheinitialstateoffiniteautomata,addepsilontothe
reachability equation.
Step3:SolvetheequationsbyusingArden’sTheorem.
Step4:Substitutetheresultsofeachstateequationintothefinalstate
equation,to get the regularexpression forthe given DFA.
Example:
1.Constructregularexpressionforthegivenfiniteautomaton.
ThegivenFiniteAutomataisnothavingany□’s(epsilons).
Step1:FindthereachabilityforeachandeverystateingivenFinite
automata.
Reachabilityofastateisthesetofstateswhoseedgesenterintothatstate.
q0=q00 1
q1=q01+q10+q21 2
q2=q11+q20 3
Step2:Fortheinitialstateoffiniteautomata,addepsilontothe
reachability equation.
q0=q00+□
Step3:SolvetheequationsbyusingArden’sTheorem.
Afterapplyingarden’stheoremforequation3
q2=q110* 4
Substituteequation4inequation2
q1=q01+q10+q110*
q1=q01+q1(0+10*) 5
Applyarden’stheoremonequation5
q1=q01(0+10*)*___________6
Applyarden’stheoremonequation1
q0=q00+□
q0=□0* 7
Substituteequation7inequation6
q1=□0*1(0+10*)*_____________8
Step4:Substitutetheresultsofeachstateequationintothefinalstate
equation, to get the regularexpression for the given DFA.
q2=□0*1(0+10*)*10*
Therefore,theregularexpressionforthegivenDFAis0*1(0+10*)*10*.
PumpingLemmaforRegularSets:
• Pumping lemma, which is a powerful tool for proving certain
languages non-regular.
• It is also useful in the development of algorithms to answer
certain questions concerning finite automata, such as
whether the language accepted by a given FA is finite or
infinite.
Lemma
LetLbearegularset.Thenthereisaconstantnsuchthatifzisanyword
inL, and|z|> n, we maywrite z=uvwinsuch awaythat|uv|≤ n, v ≥
1,
andforalli>0,uviwisinL.Furthermore,nisnogreaterthanthenumber
ofstatesofthesmallestFAacceptingL.
Example:
The set L = {0i2|i is an integer, i≥1],which consists of all strings of 0’s
whoselength isaperfectsquare,isnotregular.
AssumeLisregularandletnbethe
integerinthepumpinglemma. Let z = 0n2.
Bythe pumpinglemma,0n2maybewrittenasuvw,where 1≤|v|≤nand
uviwisinLforalli.Leti=2,n2<|uv2w|<n2+n<(n+1)2.
That is, the length of uv2w lies properly between n2and (n +1)2,
and is thus not a perfect square.
Thus uv2w is not in L, a
contradiction.
WeconcludethatLisnotregular.
ClosurePropertiesof RegularSets:
• Theregular sets areclosed under
union,concatenation,andKleene closure.
• Theclassofregularsetsisclosedundercomplementation.Thatis,i
UNIT-III
Assignment-Cum-TutorialQuestions
SECTION-A
ObjectiveQuestions
1. Thelanguagesacceptedbyfiniteautomataareeasilydescribedbysi
mple expressions called .
2. A language is a ifit is theset accepted by
somefinite automaton.
3. WhatisthesolutionforequationR=Q+RP(ifPandQareREandPdoes
notcontainε)? [
(a)R=QP* (b)R=QP (c)R=PQ* (d)R=P*Q*
4. ∅+R= .
5. ∅*= .
6. ε*= .
7. ε+rr*=r* [True/False]
8. Pumpinglemmaisgenerallyusedforproving [ ]
(a) agivengrammarisregular
(b) agivengrammarisnotregular
(c) whethertwogivenregularexpressionsareequivalent
(d) noneoftheabove
9. Regularsetsareclosedunder [ ]
(a) Union (b)concatenation
(c)Kleeneclosure (d)Alloftheabove
10. a+bdenotestheset . [ ]
(a){a,b} (b){ab} (c){a} (d){b}
11. Thesetofallstringsof{0,1}havingexactlytwo0’sis [ ]
(a)1*01*01* (b){(0+1)*} (c){11+0}* (d){00+11}*
12. Theregularexpressiontorepresentallstrings withlengthatmost2over
{a,b}is .
(a)ε (b) ε+(a+b)+(a+b).(a+b) (c)(a+b) (d)(a+b).(a+b)
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 13
13. Whichoneofthefollowinglanguagesoverthealphabet{0,1}is
described by the regular expression: (0+1)*0(0+1)*0(0+1)*? [
]
(a) Thesetofall stringscontainingthesubstring00.
(b) Thesetofallstringscontainingatmosttwo 0’s.
(c) Thesetofallstringscontainingatleasttwo0’s.
(d) Thesetofallstringsthatbeginandendwitheither0or1.
14. Consider the languages L1 = □and L2 = {0}. Which one of the
following represents L1 L2 * + L1* [ ]
(A){□} (B)Ø (C)0* (D){□,0}
15. WhatistheregularexpressionforthegivenDFA?
16. Whichofthefollowinglanguagesarenotregular? [ ]
(a)L=an|n>=1(b)L=anbm|n,m>=1 (c)anbn|n>=1 (d)a2n|n>=0
17. WhatistheregularexpressionforthegivenDFA? [ ]
SECTION-B
SUBJECTIVEQUESTIONS
1. Defineregularsetandregularexpression.
2. StateArden’sTheorem.
3. ListtheclosurepropertiesofRegularLanguages.
4. Explainpumpinglemmaforregularlanguageswithanexample.
5. Writetheregularexpressionforallstringsendingin1101overthe
alphabet {0, 1}.
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 14
6. Designaε-NFAfortheregularexpressiona*bc|ab*|c*.
7. ConstructNFAwithε-movesfortheregularexpression10+(0+11)0*1
8. ConstructFiniteautomatafortheregularexpression1(01+10)*00.
9. Whatispumpinglemmaforregularsets?Showthatthelanguage
L={anbncn|n>=1} isnotregular.
10. Constructfiniteautomationtoaccepttheregularexpression(0+1)*
(00+11)(0+ 1)*.
11. Usingpumpinglemma,showthefollowinglanguageisnotregu
lar: L= {w □{0,1}*|the number of 0’s in w is a perfect
square}
12. ConstructtheregularexpressionforthefollowingDFA.
13. ConstructregularexpressionforthefollowingDFA.
14. ConstructregularexpressionforthegivenDFA.
SECTION-C
QUESTIONS AT THE LEVEL OF GATE
1. ThenumberofstatesintheminimumsizedDFAthatacceptsthe
language defined by the regular expression (0+1)*(0+1)(0+1)* is
.
[GATE2016Set-B]
2. Whichoftheregularexpressionsgivenbelowrepresentthefollowing
DFA? [ ] [GATE 2014 Set-1]
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 16
I) 0*1(1+00*1)*
II) 0*1*1+11*0*1
III) (0+1)*1
(a)IandIIonly (b)IandIIIonly
6. ConsiderthelanguageL=(111+11111)*.Theminimumnumberofst
atesin any DFA accepting this language is: [][GATE 2006]
(a)3 (b)5 (c)8 (d)9
UNIT-IV
Objective:
Tounderstandregulargrammarsandcontextfreegrammars.
Syllabus:
Chomsky hierarchy of languages, Regular grammars- right linear
and left linear grammars, Equivalence between regular linear
grammar and FA and its inter conversion, Context free grammar,
derivation trees, Sentential forms, right most and left most
derivation of strings
LearningOutcomes:
Studentswillbeableto:
• understandChomskyhierarchyoflanguages.
• understand and construct the regular grammarfor the given
regular language or regular expression.
• convertRegularGrammarintoequivalentDFAandviceversa.
• constructContextfreegrammarforthegivenlanguage.
• constructrightmost,leftmostderivationandderivationtreesfo
rthe given string and grammar.
LearningMaterialChomsky
hierarchyoflanguages:
ThefourclassesoflanguagesareoftencalledtheChomskyhierarchy,aft
er Noam Chomsky, who defined these classes as potential models
of natural languages.
Chomskyclassifiesthegrammarintofourtypes:
Production
Grammar Languages Automaton
rules
Recursively
enumerable Turing
Type0 αβ
/ Phrase machines
Structured
Context- Linear- αβ
Type1
sensitive bound |α|<=|
automata β|
Push-down
Type2 Context-free Aα
automata
A w
Finite-state
Type3 Regular automata AwB
ABw
RegularGrammar:
A right-orleft-lineargrammariscalledaregulargrammar.
Right-LinearGrammar:
If all productions of a grammar are of the form AwB or Aw, where
A and B arevariables and w is a (possiblyempty) string of
terminals, then we say the grammar is right-linear.
Example:
Representthelanguage0(10)*bytheright-lineargrammar.
The language generated by the given Regular
Expression is L = {0, 010, 01010, 0101010,
......................................................}
Right-LinearGrammar:
S0A
A10A|ε
Left-LinearGrammar:
IfallproductionsareoftheformABworAw,wecallitleft-linear.
Example:
Representthelanguage0(10)*bytheleft-lineargrammar.
The language generated by the given Regular
Expression is L = {0, 010, 01010, 0101010,
......................................................}
Left-
LinearGramm
ar: SS10 |0
Equivalenceofregulargrammarsandfiniteautomata:
Alanguageisregularif andonlyifithasaleft-lineargrammarandifand only
if it has a right-linear grammar.
ConstructionofaRegularGrammarforagivenDFA:
LetM=({q0,q1...qn},∑,δ,qo,F).WeconstructGasG=({A0,A1,...............,
An},∑,P,A0)
wherePisdefinedbythefollowingrules:
(i) AiaAjisincludedinPifδ(qi,a)=qj∉F.
(ii) AiaAjandAiaareincludedinPifδ(qi,a)=qj∈F.
Note:WecanconstructonlyrightlineargrammarforthegivenDFA.
If we want to constructleft linear grammarfor the given DFA, reverse
the edges of the given DFA and interchange initial and final states.
Example:
1. Construct regular grammar (right linear grammar) for the given
DFA.
GivenM=({q0,q1},{a,b},δ,qo,{q1})
ConstructG=({A0,A1},{a,b},P,A0)wherePisgiven by
(i) AiaAjisincludedinPifδ(qi,a)=qj∉F.
δ(q0,a)=q0∉F⇒A0aA0
(ii) AiaAjandAiaareincludedinPifδ(qi,a)=qj∈F.
δ(q0, b) = q1∈ F ⇒A0bA1 and A0b
δ(q1, a) = q1∈ F ⇒A1aA1 and A1a
δ(q1,b)=q1∈F⇒A1bA1andA1b
∴Pisgivenby
A0aA0, A0bA1, A0b
A1aA1, A1a, A1bA1, A1b
StepstoconvertFiniteAutomatatoLeftLinearGrammar:
Step 1: Reverse all the edges of the givenautomata and interchange
initial state and final states.
Step2:RepresenttheproductionsusingLeftLinearGrammar.
Example:
2. ConstructleftlineargrammarforthegivenDFA.
Step2:RepresenttheproductionsusingLeftLinearGrammar.
BBa BAa
BBb Ba
ConstructionofaDFAforagivenRegularGrammar:
LetG=({A0, A1,........,An},∑,P,A0).WeconstructaDFAMwhose
(i) statescorrespondtovariables.
(ii) initialstatecorrespondstoA0.
(iii) transitionsinMcorrespondtoproductionsinP.Asthelast
productionappliedinanyderivationisoftheformAia,the
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 7
correspondingtransitionterminatesatanewstate,andthisisthe unique
final state.
WedefineMas({q0,q1...qn,qf},∑,δ,qo,{qf})whereδisdefinedas follows:
(i) EachproductionAiaAjinducesatransitionfromqitoqjwith
label a,
(ii) EachproductionAkainduces atransitionfromqktoqfwith
label a.
Example:
1. G=({A0,A1},{a,b},P,A0)wherePconsistsofA0aA1,A1bA1, A1a,
A1bA0. Construct a DFA M accepting L(G).
Context-FreeGrammar:
Acontext-freegrammar(CFGorjustgrammar)isdenotedG=(V,T,P,S), where
• VandTarefinitesetsofvariablesandterminals,respectively.
• P is a finite set of productions; each production is of the form
A,αwhere A is a variable and α is a string ofsymbols from (V ∪
T)*.
• Sisaspecialvariablecalledthestartsymbol.
2. WriteCFGforthelanguageL={anbm|n,m>=1}.
L= {a,b,ab,aab,.........abb,aabb, aaabbb,
aaaabbbb, aaaaabbbbb, }
G = ({S, A, B}, {a, b},
P, S) P: S -> AB
A->aA|a
B->bB|b
3. WriteCFGforthelanguageL={aa,ab,ba,b
b} G = ({S, A}, {a, b}, P, S)
P: S->AA
A->a|b
4. WriteCFGforthelanguageL={an|n>=0}.
L={ε,a,aa,aaa,aaaa,aaaaa,aaaaaa,...............................}
G=({A},{a},P,A)
P: A->aA|ε
5. WriteCFGfortheregularexpression(a+b)*.
L={ε,a,b,aa,ab,ba,bb,aaa,abb,aba,................................}
G=({S},{a,b},P,S)
P: S->aS|bS|ε
6. Write CFG to generate all strings of {a, b} whose length is
atleast 2. L= { aa, ab , ba, bb, aaa,abb,aba,....}
G =({S,A,B},{a,b},P,S)
P: S -> AAB
A->a|b
B->aB|bB|ε
7. Write CFG to generate all strings of {a, b} whose length is
atmost 2. L= { ε, a,b, aa, ab , ba, bb}
G=({S,A},{a,b},P,
S) P: S -> AA
A->a|b|ε
8. WriteCFGto generatepalindromes over {a,b}.
L={ε,a,b,aa,bb,aba,bab,aaaa,abba,.....................}
G=({S},{a,b},P,S)
P: S->aSa|bSb
S -> a |b |ε
9. WriteCFGtogenerateequalnumberofa’sandb’s.
L={ab,ba,aabb,abab,bbaa,baba,.............................}
G=(V,T,P,S),where V={S,A,B},T={a,b},SandP. P:
S -> aB A ->bAA
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 10
S->bA B->b
A->a B->bS
A->aS B->aBB
Sentential Form:
Astringofterminalsandvariablesαiscalledasententialformif
Derivation:
Derivation isthe processof
applyingproductionsrepeatedlytoexpand non- terminals in terms
of terminals or non-terminals, until there are no more non-
terminals.
A derivation can be either Leftmost derivation or Right
mostderivation.
Leftmostderivation:
If at each step in a derivation a production is applied to the
leftmost variable,thenthederivationissaidtobeleftmost.
Example:
ConsiderthegrammarG=({S,A},{a,b},P, S),wherePconsistsof
S aAS |a
ASbA|SS|ba
Thecorrespondingleftmostderivationis
S=>aAS=>aSbAS=>aabAS=>aabbaS=>aabbaa.
Rightmostderivation:
Aderivationinwhichtherightmostvariableisreplacedateachstepis
said to be rightmost.
Example:
ConsiderthegrammarG=({S,A},{a,b},P, S),wherePconsistsof
S aAS |a
ASbA|SS|ba
Thecorrespondingrightmostderivationis
S=>aAS=>aAa=>aSbAa=>aSbbaa=>aabbaa.
Note:“If w is in L(G) for CFG G, then w has at least one parse tree,
and corresponding to a particular parse tree, w has a unique
leftmost and a unique rightmost derivation.”
DerivationTrees(or)Parsetree:
The derivations in a CFG can be represented using trees.Such
trees representing derivations are called derivation trees.
LetG=(V,T,P,S)beaCFG.Atreeisaderivation(orparse)treeforGif:
1) Everyvertexhasalabel,whichisasymbolofV∪T∪{ε}.
2) ThelabeloftherootisS.
3) IfavertexisinteriorandhaslabelA,thenAmustbe inV.
4) IfnhaslabelAandverticesn1,n2,n3,...,nkarethesonsofvertex
n, in order from the left, with labels X1, X2, ......., Xk,
respectively, then AX1X2 .......Xkmust be a production in P.
5) Ifvertex n has labelε, then n is a leaf and is the only son
of its father.
Example:
Consider the grammar G =({S, A}, {a, b], P, S), where P consists of
S aAS |a
ASbA|SS|ba
Constructaderivationtreeforthestring“aabbaa”
A derivation tree is a natural description of the derivation of a
particular
sententialformofthegrammarG.Ifwereadthelabelsoftheleavesfroml
eft to right, we have a sentential form. We call this string the
yield of the derivation tree.
S=>aAS=>aSbAS=>aabAS=>aabbaS=>aabbaa.
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 12
Note:Someleavescouldbelabelledbyε.
UNIT-IV
Assignment-Cum-TutorialQuestions
SECTION-A
ObjectiveQuestions
1. TheClanguageis [ ]
a) Acontextfreelanguage b)Acontextsensitivelanguage
c)Aregularlanguage d)None
2. Everyregulargrammariscontextfreegrammar. (True|False)
3. Thefiniteautomataacceptsthefollowinglanguage: [ ]
a) Contextfreelanguage b)regularlanguage
c)Contextsensitivelanguage d)alloftheabove
4. Context-freegrammar canberecognizedby [ ]
a) FiniteAutomata b)LinearboundedAutomata
c)PushdownAutomata d)both(b)and(c)
5. ThelanguageacceptedbyaTuringMachine: [ ]
a) Type0 b)Type1 c)Type2 d)Type3
6. Matchthefollowing
1. Context FreeLanguage a.TuringMachine [ ]
2. RecursivelyEnumerable b.FiniteAutomata [ ]
3. RegularLanguage c.LinearBoundedAutomata [ ]
4. ContextSensitiveLanguage d.PushDownAutomata [ ]
7. Foreveryrightlineargrammar,therewillbe anequivalentFA.
[True/False]
8. RecursivelyEnumerablelanguageisalsocalledas .
9. Acontextfreegrammaris [ ]
a) Type0 b)Type1 c)Type2 d)Type3
10. WhichwordcanbegeneratedbyS->d|bA,A->d|ccA [ ]
a) bccccd b)aabccd c)ababccd d)abbbd
11. Whichofthefollowingstringsisinthelanguagedefinedbygrammar
S0A, A1A|0A|1 [ ]
a)01100 b)00101 c)10011 d)11111
c) stringscontainoddnumberofa'sandevennumberofb's.
d) stringscontainevennumberofa'sandevennumberofb's
13. Given the following productions of a grammar:
[ ] SaA| aBB AaaA |ε
B bB| bbC C B
Whichofthefollowingistrue?
a) Thelanguagecorrespondingtothegivengrammarisasetofeven
number of a’s.
b) Thelanguagecorrespondingtothegivengrammarisasetofodd
number of a’s.
c) Thelanguagecorrespondingtothegivengrammarisasetofeven
number of a’s followed by odd number of b’s.
d) Thelanguagecorrespondingtothegivengrammarisasetofodd
number of a’s followed by even number of b’s.
14. AregulargrammarforthelanguageL={anbm|nisevenandmis
even}is [
a)SaSb|X;XbXa|ε b)SaaS|X;XbSb|ε
c)SaSb|X;XXab|ε d)SaaS|X;XbbX|ε
15. Whichoftheregularexpressionscorrespondstothisgrammar?
SAB|AS Aa|aA Bb
a)(aa)*b b)aa*b c)(ab)* d) a(ab)*[
17. TheCFGSaS|bS|a|bisequivalenttotheregularexpression
a) (a*+b)*b)(a+b)* c) (a+b)(a+b)* d)(a+b)(a+b) [ ]
18. TheregulargrammarforthegivenFAis [ ]
a) AaA | bB | a c)AaA|bB|b
BbA|aB|b BbA|aB|a
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 15
b) AaA | bB | ε d)AbA|aB|a
BbA|aB|ε BaA|bB|b
SECTION-B
SUBJECTIVEQUESTIONS
1. ShowtheVenndiagramofChomskyhierarchylanguageandtheir
counterpart automata.
2. DefineRegulargrammarwithanexample.
3. DefineContextFreeGrammarwithanexample.
4. What issententialform?Explainwithan example.
5. Explainderivation treewith anexample.
6. DefineLMDandRMD.
7. Showthatid+id*idcan be generatedby twodistinct derivation
treesfor the grammar
EE+E|E*E|(E)|id
8. DesignCFGforoddpalindromes?
9. LetGbethegramma
r S aB |bA
Aa |aS |bAA Bb|
bS|aBB.
Forthestringaaabbabbbafinda
i. Leftmostderivation
ii. Rightmostderivation
iii. ParseTree
10. Obtaintheright lineargrammarforthefollowingFA.
12. Obtaintheleftlineargrammarfor(11+01)*101.
13. ConvertthefollowingDFAtoRegulargrammar
14. Isthefollowinggrammarambiguo
us? S AB|aaB
A>a|Aa Bb
Sa ⃒b ⃒Є}
16. DrawaderivationtreeforthestringabaabafortheCFGgivenbyG where
P = {SaSa SbSb
17. Obtainarightlineargrammarandleftlineargrammarforthefollo
wing FA.
SECTION-C
QUESTIONS AT THE LEVEL OF GATE
1. G1:SaS|B,Bb|bB [GATE2016]
G2:SaA|bB;AaA|B|□,BbB|□
a) {ambn|m>0orn>0}and{ambn|m>0andn>0}
b) {ambn|m>0andn>0}and{ambn|m>0orn>=0}
c) {ambn|m>=0orn>0}and{ambn|m>0andn>0}
d) {ambn|m>=0andn>0}and{ambn|m>0orn>0}
BaBB SbAA
Whichofthefollowingstringsisgeneratedbythegrammar? [ ]
a) aaaabb b)aabbbb c)aabbab d)abbbba
4. HowmanyderivationtreesarethereforthegrammarinQuestion3?
a) 1 b)2 c)3 d)4 [ ]
5. [GATE2006]
6. Considertheregulargrammar: [GATE2005]
S→Xa|Ya
X→Za
Z→Sa|□
Y→Wa
W→Sa
where Sis the starting symbol,the setofterminalsis {a}andthe
setof non- terminals is {S, W, X, Y,
Z}.
Wewishtoconstructadeterministicfiniteautomaton(DFA)torecognize
the same language. What is the minimum number of states
required for the DFA? []
a) 2 b)3 c)4 d)5
UNIT-V
Objective:
To understand and design push down automata’s for a given Context
free language.
Syllabus:
Ambiguity in context free grammars, minimization of Context Free
Grammars, Chomsky normal form, Greibach normal form,
pumping lemma
forContextFreeLanguages,closurepropertiesofCFL(proofsnotrequir
ed), applications of CFLs
Pushdownautomata:
Pushdownautomata,modelofPDA,designofPDA.
LearningOutcomes:
Studentswillbeableto:
• understandambiguityincontextfreegrammars.
• minimizethegivencontextfreegrammar.
• apply Chomsky and Greibach Normal Forms on context
free grammars.
• understandanddesignPDAforgivencontextfreelanguages.
LearningMaterial
Ambiguityincontextfreegrammars:
A context-free grammar G is said to be ambiguous if it has two parse
trees for some word.
(or)
Awordwhichhasmorethanoneleftmostderivationormorethanone
rightmost derivation is said to be ambiguous.
Example:
G=({S},{a,b,+,*},P.S),wherePconsistsofSS+S|S*S|a|b We
have two derivation trees for a+ a* b
Twoderivationtreesfora+a*b
1) Eliminationofuselesssymbols.
2) Eliminationofε–Productions.
3) EliminationofUnitProductions.
EliminationofUselessSymbols:
LetG=(V,T,P,S)beagrammar.AsymbolXisuselessifitisnotinvolvedin
derivation.
(or)
AsymbolXisuselessifthereisnowayofgettingaterminalstringfromit.
Example:
Considerthegramm
ar SAB |a
Aa
Aa
WefindthatonlySandaappearinsententialforms.Thus({S},{a},{Sa},
S) isan
equivalentgrammarwithnouselesssymbols.
Eliminationofε–Productions:
AproductionoftheformAε,whereAisavariable,iscalledanull production.
IfL=L(G)forsomeCFGG =(V,T,P,S),then L-{ ε} isL(G')foraCFG G' with
no useless symbols or ε-productions.
Example:
Considerthegramm
ar A0B1|1B1
B0B|1B|ε
Remove ε-
productionsfromthegrammar. Bεε is the
null production.
Thenewproductionsaftereliminationofεare
A0B1|1B1|01|11
B0B|1B|0|1
EliminationofUnitProductions:
Aproductionof theformABwhoseright-handsideconsists ofa
single variable is called a unit production.
All other productions, including those of the form A a and ε-
productions, are nonunit productions.
Example:
Considerthegramm
ar S0A|1B|C
A0S |00
B1|A
C01
Removeunitproductionfromthegrammar.
SCandBAaretheunitproductions
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 5
>B1B2B3...Bmbythesetofproductions
{A->B1D1,D1->B2D2,..........Dm-3->B m-2Dm-2,Dm-2->Bm-1Bm}
Example:
Considerthegrammar({S,A,B},
{a,b},P,S)thathastheproductions: S bA |aB
AbAA |aS |a
BaBB|bS|b
Findanequivalent grammarinCNF.
Step1: Simplifythegrammar.
a) Eliminateε–productions
b) Eliminateunitproductions
c) EliminateUselesssymbols.
The given grammar does not containε–productions, unit productions
and useless symbols.
Itisinoptimizedform.
Step2:TheonlyproductionsalreadyinproperformareAaandBb.
So we may begin by replacing terminalson the right by variables,
except in the case of the productionsA a and B b.
SbAisreplacedbySCbAandCbb.
Similarly, AaS is replaced by A CaS and Caa; AbAA is replaced by
ACbAA; S aB is replaced by SCaB;
BbSisreplacedbyBCbS,andBaBBisreplacedbyBCaBB.
Inthenextstage,theproductionACbAAisreplacedbyACbD1andD1
AA,andtheproductionBCaBBisreplacedbyBCaD2andD2BB.
Step3:TheproductionsforthegrammarinCNFare:
ACaS|CbD1|aD2BB B
CbS |CaD2|bCa a
Cbb
GreibachNormalForm:
Everycontext-freelanguageLwithoute canbe
generatedbyagrammarfor which every production is of the form A
aα, where A isa variable, a is a terminal, and α is a (possibly empty) string
of variables.
Lemma 1:Define anA-production tobe a production withvariable A
α α2be aproductioninPand
onthe left.LetG=(V,T,P,S)beaCFG.LetA1B
β |β2|……|βrbe the setofallB-productions.LetG1=(V,T,P1,S) be
B1
obtained from G by deleting the production A αα1Bα 2 from P and
addingtheproductionsAαβ 1α1α 2|α1β2α2|……|α1βrα2. Then L(G) =
L(G1).
α 1|Aα2|……|Aαr be the set
Lemma2:LetG=(V,T,P,S)beaCFG.LetAAα
of A-productions for which A is the leftmost symbol of the right-
hand β |β2|……|βsbethe
side.LetA1 remainingA-
productions.LetG1=(V U {B}, T, P1, S) be the CFG formed by
adding the variable B to V and replacingalltheA-
productionsbytheproductions:
ThenL(G1)=L(G).
Example:
ConverttoGreibachnormalformthegramm
ar G=i{A1,A2,A3},{a,b},PA1),
wherePconsistsofthefollowing:
WenowapplyLemma2totheproductions
PumpingLemmaforCFL's:
LetLbe anyCFL. Thenthere isaconstantn, dependingonlyon
L,such that if z is in L and |z|
≥n,thenwemaywritez=uvwxysuchthat
Example:
ConsiderthelanguageL={aibici|
≥1}.SupposeLwerecontextfreeandlet n be the constant.
Considerz=anbncn.Writez=uvwxysoastosatisfytheconditionsofthe
pumping lemma.
Since |vwx|≤n, it is not possible for vx to contain instances of a’s and
c’s, because the rightmost a is n + 1 positions away from the
leftmost c.
Ifvandx consistofa’s only, thenuwy(thestring uv iwxiy withi=0)has n
b’s and n c’s but fewer than n a’s since |vx|≥ 1.
Thus,uwyisnotoftheformaibici.ButbythepumpinglemmavwyisinL,a
contradiction.
Thecaseswherevandxconsistonlyofb’soronlyofc’saredisposedof similarly.
If vx has a’s and b’s, then uwy has more c’s than a’s or b’s, and
again it is not in L.
If vx containsb’sand c’s, a similarcontradiction
results. We conclude that L is not a context-free
language.
ClosurePropertiesofCFL’s:
• Context-
freelanguagesareclosedunderunion,concatenationand Kleene
closure.
• Thecontext-freelanguagesareclosedundersubstitution.
• TheCFL’s are closedunderhomomorphism.
• TheCFL’sarenotclosedunder intersection.
• TheCFL’sarenotclosedunder complementation.
Applicationsofthepumpinglemma:
The pumping lemma can be used to prove a variety of languages not to
be context free, using the same "adversary" argument as for the
regular set pumping lemma.
Pushdownautomata:
Moves:
ModelofPDA:
• Pushdown automaton has a read-only input tape, an input
alphabet a finite state control, a set of final states, and an
initial state as in the case of an FA.
IIYear–II-Semester 2018-19 CSE
Formal LanguagesandAutomata Theory 14
Instantaneousdescription:
Instantaneous description (ID) is the configuration of a PDA at a
given instant. We define an ID tobe atriple (q,w, γ), whereq isa state,
wa string of input symbols,andγastringofstacksymbols.
AcceptedLanguages:
Example:
DesignaPDAthataccepts{wwR|
win(0+1)*} L = { ε, 0, 1, 00, 11, 0110, 1001,
.............................................................................}
LetM=(Q,∑,Г,δ,q0,Z0,F)bethePDA
ConsiderM = ({q1,q2}, {0,1}, {0, 1,Z0},δ,q1, Z0,Ø)
δ(q1,0,Z0)={(q1,0Z0)}
δ(q1,1,Z0)={(q1,1Z0)}
δ(q1,0,0)={(q1,00),(q2, ε)}
δ(q1,1,0)={(q1,10)}
δ(q1,0,1)={(q1,01)}
δ(q1,1,1)={(q1,11),(q2, ε)}
δ(q2,0,0)={(q2, ε)}
δ(q2,1,1)={(q2,ε)}
δ(q1,ε,Z0)={(q2,ε}
δ(q2,ε,Z0)={(q2,ε)}
DeterministicPDA:
ThePDAisdeterministicinthesensethatatmostonemoveispossiblefro
m any ID.
FormallywesayaPDAMisdeterministicif:
EquivalenceofPDA'sandCFL's:
CFGtoPDA Conversion
IfLisacontext-freelanguage,thenthereexistsaPDAMsuchthatL=N(M).
Procedure:
LetL=L(G),whereG=(VN,Σ,P,S)isacontextfreegrammar.
WeconstructaPDA Mas
M=((q),Σ,VNUΣ,Z0,q,δ,Ø)
Whereδisdefinedbythefollowingrules:
R1:δ(q,ε,A)={(q,α)|A->αisinP}
R2: δ(q,a,a)={(q,ε)}foreveryainΣ.
Example:
ConstructapdaMequivalenttothefollowingcontextfreegramm
ar: S->0BB
B->0S|1S|0.
Testwhether0104isinN(M).
Solution:
A=(({q},{0,1},{S,B,0,1},δ,q,Z0,Ø)
δisdefinedbythefollowingrules:
R1:δ(q,ε,S)={(q,0BB)}
R2:δ(q,ε,B)={(q,0S),(q,0S),(q,0)}
R3:δ(q,0,0)={(q,ε)}
R4:δ(q,1,1)={(q,ε)}
String Checking
(q,0104,S)
⊢(q,0104,0BB) byRuleR1
⊢(q,104,BB) byRuleR3
⊢(q,104,1SB) byRuleR2since(q,1S)∈α(q,∧,B)
⊢(q,04,SB) byRuleR4
⊢(q,04,0BBB) byRuleR1
⊢(q,03,BBB) byRuleR3
⊢*(q,03,000) byRuleR2since(q,0)∈α(q,∧,B)
⊢*(q,ε,ε) byRuleR3
UNIT-V
Assignment-Cum-TutorialQuestions
A. ObjectiveQuestions
1. GrammarthatproducemorethanoneParsetreeforsamewordis:
a) Ambiguous b)Unambiguous [ ]
c)Complementation d)ConcatenationIntersection
2. ForeverygrammartherewillanequivalentgrammarinCNF.
[True/False]
3. Thederivationtreesofstringsgeneratedbyacontextfreegramma
rin Chomsky Normal Form are always binary trees [True |
False]
4. Whichofthefollowingconversionisnotpossible(algorithmically)?
a)RegulargrammartoContext-freegrammar [ ]
b)NondeterministicFSAtoDeterministicFSA
c) NondeterministicPDAtoDeterministicPDA
d) Alloftheabove
5. CFL’sarenotclosedintersectionandcomplementation.[True|False]
6. CFL’sareclosedunder [ ]
a) union b)concatenation c)closure d)All
7. ThegrammarGwiththeproductions [ ]
A→AA|(a)|ε isan
a) Ambiguousgrammar b)Unambiguousgrammar
c) Grammar d)None
8. Identifytheuselesssymbolinthegrammargivenbelow. [ ]
S->AB|C A- B->BC C->b
>a
d) C
a)S b)A c)B
9. Findanequivalentreducedgrammarforthegivengrammar.[ ]
S->0|1|ε S->0S0|1S1
a) S->0|1,S->0S0|1S1|0|1 b)S->0|1,S->SS|0S1|1S1
c)S->0|1,S->00|11 d)None
10. WhichoneofthefollowingisaChomskyNormalFormgrammar?
[ ]
(i)A->BC|a(ii)A->aA|a|b (iii)A->BCD|a,B->a,C->c,D->d
a)(i)only b)(i)and(iii) c)(ii)and(iii) d)(i),(ii)and(iii)
11. WhichoneofthefollowingisnotaGreibachNormalformgrammar?
[ ]
(i)S->a|bA|aA|bB (ii) S->a|aA|AB (iii)S->a|A|aA
A->a A->a A->a
B->b B->b
a) (i)and(ii) b)(i)and(iii) c)(ii)and(iii) d)(i),(ii) and(iii)
12. L={0n12n|n>=1}is [ ]
a) regular b)context-freebutnotregular
c)context-freebutregular d)None
13. RecognizethelanguageacceptedbythePDAwiththefollowing
moves [ ]
δ(q0,a,Z0)=(q0,aZ0) , δ(q0,a,a)=(q0,aa)
δ(q0,b,a)=(q1,ε) , δ(q1,b,a)=(q1,ε)
δ(q1,c,Z0)=(q2,Z0), δ(q2,c,Z0)=(q2,Z0)
a) L={anbncn|n,m>=1} b)L={anbncm|n,m>=1}
c)L={ambncn|n,m>=1} d)L={ambncm|n, m>=1}
14. ThegrammarsG1andG2are
G1:S->0S0|1S1|0|1|ε
G2:isS->as|asb|X,X->Xa|a.
Whichisthecorrectstatement? [ ]
a) G1isambiguous,G2isunambiguous
b) G1isunambiguous,G2isambiguous
c)BothG1andG2areambiguous
d)BothG1andG2areunambiguous
B. Descriptivequestions
1. Whatisanambiguousgrammar?Explainwith anexample.
2. DefineUselesssymbolandgiveexample.
3. WhatisanNullproductionandUnitproducation?Explainwithan
example.
4. ListtheapplicationsofCFG.
5. ListtheclosurepropertiesofCFL.
6. ExplainpumpinglemmaforCFL’swithanexample.
7. ExplainthemodelofPDA.
8. Showthatthegrammarisambiguou
s. S → 0A |1B
A→0AA|1S|1
B→ 1BB|0S|0
9. ConvertthefollowinggrammarintoG
NF S XA |BB
Bb|SB
X b
10. DesignPDAforL={wcwr|w?(0+1)*}
11. DesignPDAforthelanguageL={anbn+mcm|n,m>=1}
12. WhatisthelanguagegeneratedbythegrammarG=(V,T,P,S)where
P={S->aSb, S->ab}?
13. Forthefollowinggrammar:
S->ABC|BbB,A->aA|BaC|aaa,B->bBb|a|D,C->CA|AC,D->ε
i. Eliminateε-productions.
ii. Eliminateanyunitproductionsinthe resultinggrammar.
iii. Eliminateanyuselesssymbolsintheresultinggrammar.
iv. PuttheresultinggrammarinChomskyNormal Form
14. FindaCFG,withoutεproductions,unitproductionsanduseless
productionsequivalenttothegrammardefinedby
SABaC
ABC
Bb|ε
CD|ε
D d
15. ObtainthePDAforthegivenregularlanguage:L={wwr|wisin
(0+1)*}.
16. ConvertthefollowingGrammarintoC
NF. S → AbcD / abc
A→aASB/d
B→b/cb
D→d
17. Considerthegrammar({S,A,B},{a,b},P,S)thathasthe
productions:
SbA|aB
AbAA|aS|a
BaBB|bS|b
18. ShowthatL={anbncn⃒n≥0}isnotacontextfreelanguage.
Findanequivalent grammarinCNF.
C. GateQuestions
1. Identifythelanguagegeneratedbythefollowinggrammar,Where
Sis the start variable. [ ] [Gate 2017]
S->XY
X->aX|a
Y->aYb|epsilon
2. Considerthefollowingstatementsaboutthecontextfreegrammar
G={S→SS,S→ab,S→ba,S→Ε} [ ][Gate2006]
Gisambiguous
Gproducesallstringswithequalnumberofa’sandb’s
GcanbeacceptedbyadeterministicPDA.
WhichcombinationbelowexpressesallthetruestatementsaboutG?
a) Ionly b)IandIIIonly
c)I andII only d)I,IIandIII
3. Considerthelanguages: [ ][Gate2005]
L1={wwR|wbelongs{0,1}*}
L2={w#wR|
wbelongs{0,1}*},where#isaspecialsymbol L 3=
{ww|wbelongs {0,1}*}
WhichoneofthefollowingisTRUE?
a) L1isadeterministicCFL b)L2is adeterministic CFL
c)L3isaCFL,butnotadeterministicCFLd)L3isadeterministicCFL
4. IfL1iscontextfreelanguageandL2isaregularlanguagewhichofthe
following is/are false? [ ] [Gate 1999]
a) L1-L2is notcontextfree b)L1∩L2iscontextfree
c) ~L1 d)~L2
a) Union,Intersection b) Union,Kleeneclosure
c)Intersection,complement d)Complement,Kleeneclosure
UNITVI
Objective:
TounderstandanddesignTuringMachinesforthegivenrecursivelyenumerablelanguages.
Syllabus:
Turing Machine: Turing Machine, model, Design of TM, Types of Turing Machines, Computable
functions, Recursivelyenumerable languages, church‘s hypothesis.
Computability Theory: Decidability of problems, universal Turing Machine, Undecidability of posts
correspondenceproblem, Turingreducibility, definitionofPandNPproblems, NPcompleteandNP hard
problems.
LearningOutcomes:
Studentswillbeableto:
• understandturingmachineanditsmodel.
• designTuringMachine’sforRecursivelyEnumerablelanguages.
• definePandNPclassofproblems.
• definedecidabilityandundecidabilityofproblems.
LearningMaterial
TuringMachine:
ATuringmachine(TM) isdenotedby
TheTuringMachineModel:
• The basic model has a finite control, an input tape that is divided into cells, and a tape head
thatscans one cell of the tape at a time.
• The tape has a leftmost cell but is infinite to the right. Each cell of the tape may hold exactly one
of a finite number of tape symbols.
• Initially,thenleftmostcells,forsomefiniten ≥0,holdtheinput,whichis astringof symbols chosen from
a subset of the tape symbols called the input symbols.
• The remaining infinity of cells each hold the blank, which is a special tape symbol that is not an
input symbol.
MovesofTurningMachine
In one move the Turing machine, depending upon the symbol scanned by the tape head and the state ofthe
finite control,
1) changesstate,
2) printsasymbolonthetapecellscanned,replacingwhatwaswrittenthere,and
3) movesitsheadleftorrightonecell.
Note : The difference between a Turing machine and a two-way finite automaton lies in the former's ability
to change symbols on its tape.
Instantaneousdescription(ID):
• InstantaneousdescriptionoftheTuringmachineMisdenotedbyα1qα2.
• Hereq,thecurrent stateofM, is inQ;α1α2 isthe string inГ*that isthecontentsofthetapeupto the
rightmost nonblank symbol or the symbol to the left of the head, whichever is rightmost. (Observe
that the blank B may occur in α1α2.).
• The tape head is assumed to be scanning the leftmost symbol of α2, or if α2 = ε, the head is
scanning a blank.
AcceptancebyTurningMachine
The language accepted by M, denoted L(M), is the set ofthose words in ∑* that cause M to enter a final state
when placed,justified at the left, on the tape of M, , with M in state q0, and the tape head of M at the
leftmost cell.
Formally,thelanguageacceptedbyM=(Q,∑,Г,δ,q0,B,F)is
Example:
DesignaTMtoacceptthelanguageL={0n1n|n≥1}.
Initially,thetapeofMcontains0n1nfollowed byinfinityofblanks.
Repeatedly, M replaces the leftmost 0 byX, moves right to the leftmost 1, replacing it by Y,, moves left to
find the rightmost X, then moves one cellright to the leftmost 0 and repeats the cycle.
If,however,whensearchingfora1,Mfindsablankinstead,,thenMhaltswithoutaccepting.
If,afterchanginga 1toa Y,Mfindsnomore 0's,then Mchecks thatnomore 1's remain,acceptingif there are none.
Thefunctionδ
TransitionDiagram
StringVerficationbyTurningMachine
AcomputationofM
TypesofTuringMachines:
i) Two-wayinfinitetape:
A Turing machine with a two-way infinite tape is denoted by M = (Q,∑,Г,δ,q0,B,F). As its name
implies, the tape is infinite to the left as well as to the right. We denote an ID of such a device as for
the one-wayinfinite TM. We imagine, however, that there is an infinityof blank cells bothto the left
and right of the current nonblank portion of the tape.
ii) MultitapeTuringmachines:
A multitape Turing machine consists of a finite control with k tape heads and k tapes; each tape is
infinite in both directions.. On a single move, depending on the state of the finite control and the
symbol scanned by each of the tape heads,the machine can:
1) changestate;
2) printanewsymboloneachofthecellsscannedbyitstapeheads;
3) moveeachofitstapeheads,, independently,one celltotheleft orright,orkeepit stationary.
Initially, the input appears on the first tape, and the other tapes are blank.
iii) NondeterministicTuringmachines:
A nondeterministic Turing machine is a device with a finite control and a single, one-way infinite
tape. For a given state and tape symbol scanned by the tape head, the machine has a finite number of
choices for the next move. Eachchoice consists ofa new state, atape symbolto print, and a direction
ofhead motion. Notethat the nondeterministicTM is not permittedto makea move inwhichthe next
state is selected fromone choice, and the symbol printed and/or direction of head motion are selected
from other choices. The nondeterministic TM accepts its input if any sequence of choices of moves
leads to an accepting state.
iv) MultidimensionalTuringmachines:
The device has the usual finite control, but the tape consists of a k-dimensional array of cells infinite
inall2k directions, for some fixedk. Depending onthe stateand symbol scanned, thedevice changes
state, prints a new symbol, and moves its tape head in one of 2k directions, either positively or
negatively, alongoneofthekaxes. Initially, the input isalongone axis, andthehead isat the left end of the
input. At any time, only a finite number of rows in any dimension contain nonblank symbols, and
these rows each have only a finite number of nonblank symbols.
v) MultiheadTuringmachines:
Ak-head Turing machine has some fixed number,,k, of heads. The heads are numbered 1 throughk,,
and a move of the TM depends on the state and on the symbol scanned by each head.. In one move,
the heads may each move independently left,, right, or remain stationary.
vi) Off-lineTuringmachines:
Anoff-lineTuring machine is a multitapeTMwhoseinput tapeisread-only. Usuallywe surroundthe input
byendmarkers, ⊄ onthe left and $ontheright. The Turing machine is not allowed to move the input
tape head off the region between ⊄ and $.
Recursive function: a functionwhichcalls itself directlyor indirectlyand terminates after finite number of
steps.
Totalrecursivefunction
• Afunctioniscalledtotalrecursivefunctionifitisdefinedforallitsarguments.
• Let f(a1,a2.....,a) be a function and defined on function g(b1,b2,....,bm), then f is total function if
every element of f is assigned to some unique element of function g.
• Fromthedefinitionitisclearthattotalrecursivefunctionisthesubsetofpartialrecursive function.
• AllthosepartialfunctionsforwhichTMhaltsarecalledtotalrecursivefunctions.
Partialrecursivefunction
• Afunctioniscalledpartialrecursivefunctionifitisdefinedforsomeofitsarguments.
• Letf(a1,a2.....,a)beafunctionanddefinedonfunctiong(b1,b2,.............,bm),thenfispartialfunctionif
someelementsoffisassignedtoalmostoneelement offunctiong.
• Partialrecursivefunctionareturingcomputable.Itmeansthatthereexistaturingmachinefor every partial
recursive function.
Recursivelyenumerablelanguages
AlanguagethatisacceptedbyaTuringmachineissaidtoberecursivelyenumerable(r.e.).
• Recursivelyenumerablelanguagesareequivalenttotheclassofpartialrecursivefunctions.
RecursiveLanguage:
A subclass of the r.e.sets,calledthe recursive sets,which are those languages acceptedby atleastone Turing
machine that halts on all inputs.
Church'sHypothesis:
Theassumptionthattheintuitivenotionof"computablefunction"canbeidentifiedwiththeclassof partial recursive
functions is known as Church's hypothesis or the Church-Turing thesis.
Decidableandundecidableproblems:
• Aproblemwhoselanguageisrecursiveissaidtobedecidable.
• A problem is undecidableif thereisnoalgorithmthat takes asinputan instance of the problem and
determines whether the answer to that instance is "yes" or "no."
Post'sCorrespondenceProblem:
AninstanceofPost'sCorrespondenceProblem(PCP)consistsoftwolists,A=w1,,...,wkandB=x1,...
,xk,ofstringsoversomealphabet∑.ThisinstanceofPCPhasasolutionifthereisanysequenceof
integersi1,i2,...,im,withm≥1,suchthat,wi1,wi2,…,wim=xi1,xi2,… xim
.
Thesequence i1,…,imisasolutiontothisinstanceofPCP.
Example1:
Let∑={0,1}.LetAandBbe listsofthreestringseach,asdefined
Givenlistsarex=(b,bab3,ba)y=(b3,ba,a)
TheinstancesofPCPisasfollows
ListX ListY
i Xi Yi
1 a b3
2 bab3 ba
3 ba a
ListX ListY
i Xi Yi
1 01 0101
2 1 10
3 1 11
X2=1 Y2=10
X3=1 Y3=11
Wecannotgetsolutionsequence.ThereforethegivenPCPishavingnosolution.
TuringReducibility:
LanguageLlisreducedtoL2byfindinganalgorithmthatmappedstringsinL1tostringsinL2and strings not in L1 to
strings not in L2. This notion of reducibility is often called many-one reducibility.
AmoregeneraltechniqueiscalledTuringreducibility,andconsistssimplyofshowingthatL1is recursive in L2.
IfL1ismany-onereducibletoL2,thensurelyL1isTuring-reducibleto L2.
Pand NPproblems:
The languagesrecognizable indeterministicpolynomialtime forma naturaland important class, theclass
Ui≥1DTIME(ni), which we denote by P. It is an intuitively appealing notion that P is the class ofproblems
that can be solved efficiently.
There are a number of important problems that do not appear to be in P but have efficient
nondeterministic algorithms. These problems fall into the class Ui≥1NTIME(ni), which we denoteby NP.
NPcompleteandNPhardproblems:
Letlbeaclassoflanguages.
Alanguage L is complete for lwithrespect to polynomial-time reductions ifL is in l, andeverylanguage in l
is polynomial-time reducible to L.
LisNP-completeifLiscompleteforNPwithrespecttolog-spacereductions.
Lishardforlwithrespecttopolynomial-timereductionsifeverylanguageinlispolynomial-time reducible to L, but
L is not necessarily in l.
LisNP-hardifLishard forNPwithrespecttolog-spacereductions.
NP
NP
NC
HALTINGPROBLEM
The problem of determining whether a program halts on a given inputis undecidable.This is to say that no
program can correctly code halts.There is no algorithm for deciding halting problem
Haltingproblemissimplynotsolvable.
Aproblemthat canbesolvedbyanalgorithmiscalledsolvable.
Aproblemthat cannot besolved byanalgorithmcalledunsolvable. An
algorithm that solves a problem is called a decision procedure.
The most famous of the unsolvable problems is the problems described by Ko.It is generally called
halting problem for turing machine to determine for arbitrary given turing machine M and input
w,whether M will eventually halt on input W.
Closurepropertiesofrecursivelanguages
• Union:IfL1andIfL2aretworecursivelanguages,theirunionL1∪L2willalsoberecursive because if TM
halts for L1and halts for L2, it will also halt for L1∪L2.
• Concatenation:IfL1 andIf L2 aretworecursive languages, their concatenationL1.L2 will also be
recursive.
• KleeneClosure:IfL1isrecursive, itskleeneclosureL1*willalsoberecursive.
• Intersectionandcomplement:IfL1andIfL2aretworecursivelanguages,theirintersectionL1∩
L2willalsoberecursive.
Closurepropertiesofrecursivelyenumerablelanguages
• Recursivelyenumerablelanguagesarenotclosedundercomplementation
Assignment-Cum-TutorialQuestions
A. Questionstestingtheunderstanding/rememberinglevelofstudents
I) ObjectiveQuestions
1. The move functionofTuringMachine is .
2. The languageacceptedbyaTurning machine iscalled language.
3. Recursivelyenumerablelanguagesareequivalenttotheclassof functions.
4. Recursivelyenumerablelanguagesareclosedundercomplementation. [True|False]
5. Thesetofallrecursivelanguagesisasubsetofthesetofallrecursivelyenumerable languages.
[True|False]
6. PhrasestructuredlanguagesareacceptedbyTM. [True|False]
7. ThepowerofNon-deterministicTurningmachineanddeterministicTurningMachinearesame.
[True |False]
8. Aproblemwhose language isrecursive iscalled .
9. Recursivelanguagesare [ ]
a. a).ApropersubsetofCFL b).AlwaysrecognizablebyPDA
b. c).AlsocalledType0languages d).RecognizablebyTM
10. PhrasestructuredlanguagesarealsocalledasType0languages. [True|False]
II) Descriptivequestions
1. DefineTurningMachine.ExplainaboutmodelofTurningMachine
2. Explainabouttypesofturingmachines.
3. Writeshort notesonhaltingproblemofaTuringMachine.
4. DiscussChurch’sHypothesis?
5. WriteshortnotesonPandNPproblemsandgiveexamples.
6. WriteshortnotesonNPCompleteandNPhardproblemsandgiveexamples.
7. DiscussindetailsaboutTuringReducibility.
8. Listpropertiesofrecursiveandrecursivelyenumerablelanguages.
9. What ispostcorrespondenceproblem?Explainwithanexample
B. Questiontestingtheabilityofstudentsinapplyingtheconcepts.
I) Multiple Choice Questions:
1. WhichofthefollowinglanguagesareacceptedbyaTurningMachine? [ ]
(i) L={anbn|n>=0}
(ii) L={anb2nc2n|n>=0}
(iii) Thesetofpalindromesoveralphabet{a,b}
a)Only(i) b)Only(ii) c)(i)and(iii) d)(i),(ii)and(iii)
2. A single tape Turing Machine M has three states q0, q1 and q2, ofwhich q0 is the starting state. The
tapealphabet ofM is{0, 1, B}and its input alphabet is{0, 1}.ThesymbolB istheblanksymbolusedto
indicate end ofan input string. The transition function ofM is described in the following table
0 1 B
Whichofthe followingstatementsistrueaboutM? [ ]
a) Mhaltsaftercomputing1’scomplement ofabinarynumber
b) Mhaltsaftercomputing2’scomplement ofabinarynumber
c) Mhaltsafterreversingofabinarynumber
d) None
3. AsingletapeTuringMachineMhas four statesq0, q1, q2andq3,ofwhichq0 isthestartingstate.The
tapealphabet ofM is{0, 1, B}and its input alphabet is{0, 1}.ThesymbolB istheblank symbolusedto
indicate end ofan input string. The transition function ofM is described in the following table
0 1 B
q q q q1,B,L
q q q
WhichofthefollowingstatementsistrueaboutM? [ ]
a.Mhaltsaftercomputing1’scomplementofabinarynumber
b.Mhaltsaftercomputing2’scomplementofabinarynumber
c.Mhaltsafter reversingofabinarynumber
d.None
4.ThegiventablerepresentsaTuringmachinewhichaccepts [ ]
a) evennumberof1’s
b) oddnumberof1’s
c) evennumberof1’sandoddnumberof1’s
d) evennumberof1’soroddnumberof1’s
5. ThetransitionsofaTuringMachinearegivenbelow [ ]
δ (q0,1)=(q0,1,R)
δ(q0,B)=(q1,1,R)
δ(q1,B)=(q2,B,R)
Theinputonthetapeisq011Bthentheoutputonthetapeis [ ]
a) 111Bq2B b)1111Bq2B c) 111Bq1B d)1111Bq1B
II) Problems
1. DesignTMforthelanguageL={anbncn|n>=1}
2. DesignTMforthelanguageL={anbmcn+m|n,m>=1}
3. DesignaTuringmachinethatacceptsthelanguageL={WWR/W□.(0+1)*and
WRisreverseofW}
4. ConsidertheTMdescribedbythetransitiontablegivenbelow.Representtheprocessingof
a)011b)0011usingID’s.WhichofthestringsareacceptedbyTM?
5. DesignTMforsubtractionoftwonumbers.
6. Showthatthefollowingpostcorrespondenceproblemhasasolutionandgivethesolution.
i ListA ListB
1 11 11
2 100 001
3 111 11
C. GATE/NET/SLET
1. Whichofthefollowingstatementsis/areFALSE? GATECS2013 [ ]
1. Foreverynon-deterministicTuringmachine,thereexistsanequivalentdeterministicTuring
machine.
2. Turingrecognizablelanguagesareclosedunderunionandcomplementation.
3. Turingdecidablelanguagesareclosedunderintersectionandcomplementation.
4. Turingrecognizablelanguagesareclosedunderunionandintersection.
a) 1and4only b)1and3only c)2only d)3only
4. IfLandL'arerecursivelyenumerable,thenLis GATECS2008 [
a) regular b)context-free
c)Context-sensitive d)recursive
5. Let L1 be a recursive language, and let L2 be a recursively enumerable but not a recursive
language.Which one of the following is TRUE? GATE-CS-2005 [ ]
L1'-->ComplementofL1 L2'--
>ComplementofL2
a) L1'isrecursiveandL2'isrecursivelyenumerable
b) L1'isrecursiveandL2'isnotrecursivelyenumerable
c) L1'andL2'arerecursivelyenumerable
d) L1'isrecursivelyenumerableandL2'isrecursive
6. Considerthefollowingtypesoflanguages: GATE-CS-2016(Set2)
L1Regular, L2:Context-free,
L3:Recursive, L4:Recursivelyenumerable.
Whichofthefollowingis/areTRUE?
I.L3'UL4isrecursivelyenumerable II.L2UL3isrecursive
III.L1*UL2iscontext-free IV.L1UL2'iscontext-free
a)Ionly b)IandIIIonly c)IandIVonly d)I,IIandIIIonly
7. AsingletapeTuringMachineMhastwostatesq0andq1,ofwhichq0 isthestartingstate.
Thetapealphabet ofM is{0, 1, B}and its input alphabet is{0, 1}. ThesymbolB istheblank symbol
usedtoindicateendofaninput string.ThetransitionfunctionofMisdescribedinthefollowingtable
GATE-CS-2003 [ ]
0 1 B
q0 q1,1,R q1,1,R Halt
q1 q1,1,R q0,1,L q0,B, L
The table is interpreted as illustrated below. The entry(q1, 1, R) in row q0 and column 1 signifies that if M is
instateq0andreads1onthecurrenttapesquare,thenitwrites1onthesametapesquare,moves its
II.B.Tech-I-Semester A.Y.2018-19 CSE
FormalLanguagesandAutomataTheory 17
tape head one position to the rightand transitions tostate q1. Which of thefollowingstatements is true about
M?
a) Mdoesnothalt onanystringin(0+1)+
b) Mdoesnothalt onanystringin(00+1)+
c) Mhaltsonallstringendingina0
d) Mhaltsonallstringendingina1
8. Whichofthefollowingistrue? GATE-CS-2002 [ ]
a) Thecomplementofarecursivelanguageisrecursive.
b) Thecomplementofarecursivelyenumerablelanguageisrecursivelyenumerable.
c) Thecomplementofarecursivelanguageiseitherrecursiveorrecursivelyenumerable.
d) Thecomplementofacontext-freelanguageiscontext-free