CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We...
Transcript of CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-3-RegEx.pdf–We...
CS1010:TheoryofComputation
LorenzoDeStefaniFall2019
Lecture3:RegularExpression
Outline• WhatareRegularExpression• OperatorsinRegularExpressions• EquivalencebetweenRegularExpressionandFiniteStatesAutomata– RegularExpressionà NFA– RegularLanguageà RegularExpression
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 1
FromSipser Chapter1.3
RegularExpressions
• Meansofcharacterizinglanguagesbasedontheregularoperators
• Examples:– (0È 1)0*• A0or1followedbyanynumberof0’s• Concatenationoperatorimplied
–Whatdoes(0È 1)*mean?• Allpossiblestringsof0and1orε• IfS ={0,1},thenequivalenttoS*
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 2
DefinitionofRegularExpressionRisaregularexpressionifRis
1. a,forsomeainalphabetå2. ε3. Æ4. (R1È R2),whereR1andR2areregularexpressions5. (R1× R2),whereR1andR2areregularexpressions6. (R1*),whereR1isaregularexpression
Note:Thisisarecursivedefinition,commonincomputerscience• R1andR2alwayssmallerthanR,sonoissueofinfinite
recursion• Æmeanslanguagedoesnotincludeanystringsandε meansit
includestheemptystring
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 3
Operatorprecedence• *hasprecedenceoverconcatenationandunion
• Concatenationhasprecedenceoverunion• Parenthesesmaychangetheprecedence• Example:(0(0È1)0)*È0
• R1=(0(0È1)0)*;R2=0• R1ÈR2
• R1=(R3)*• R4=0;R5=0È1;R6=0
• R3=R4×R5×R6• R7=0;R8=1;
• R5=R7ÈR8
Isthisdifferentfrom(0(0È1)0)*È(0) ?
Isthisdifferentfrom(00È10)*È0 ?
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 4
Additionalnotationfor*• R+ =R*R=RR*– ConcatenationofatleastonestringfromR
• Rk– ShorhandnotationforconcatenationofkstringsfromR
• R+Èε= R*
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 5
SomeExamples
• 0*10*=– {w|wcontainsasingle1}
• å*1å*=– {w|whasatleastone1}
• 01È 10=– {01,10}
• (0È ε)(1È ε)=– {ε,0,1,01}
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 6
Testingyourunderstanding
• RÈ ⌀ =R• Rε=R• RÈε =Rifε inRor{R, ε}otherwise• R⌀ =⌀
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 7
EquivalenceofRegularExpressionsandFA
Theorem:Alanguageisregularifandonlyifsomeregularexpressiondescribesit
Twodirectionssoweneedtoprove:– Ifalanguageisdescribedbyaregularexpressionthenitisregular
– Ifalanguageisregularthenthereexistsaregularexpressionthatthatdescribesit
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 8
Proof:RegularExpressionè RegularLanguage
• Proofidea:GivenaregularexpressionRdescribingalanguageL,wewill….– ShowthatsomeFArecognizesit– UseNFAsincemaybeeasierandequivalenttoDFA
• Howdowedothis?–WewillusedefinitionofaregularexpressionandshowthatwecanbuildaFAcoveringeachstep.• Steps1,2and3ofdefinition(handlealphabetsymbols,ε,andÆ )• Steps4,5and6(handleunion,concatenation,andstar)
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 9
Proof:RegularExpressionè RegularLanguage
Forsteps1-3weconstructtheFAbelow.Asareminder:
1. a,forsomeainalphabetå2. ε3. Æ
aε Æ
a
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 10
ProofContinued
• Forsteps4-6(union,concatenationandstar)weusetheresultwepreviouslyobtainedshowingthatFAareclosedunderunion,concatenation,andstar
• WehaveshownhowtoconvertaRegularExpressionintoaFAwhichrecognizesthesamelanguage
• Bycorollary,saidlanguageisregular.
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 11
Example:RegularExpressionè NFA
Convert(abÈ a)*toanNFA(example1.56page68)– Outlineofrequiredsteps:• Handlea• Handleb• Handleab• HandleabÈ a• Handle(abÈ a)*
– Sometimesε-transitionsmayappearunnecessaryofconfusing• Besystematic!Alwaysstartincludingε-transitions!
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 12
EquivalenceofRegularExpressionsandFA
Theorem:Alanguageisregularifandonlyifsomeregularexpressiondescribesit
Twodirectionsweneedtoprove:– Ifalanguageisdescribedbyaregularexpressionthenitisregular
– Ifalanguageisregular,thenthereexistsaregulatexpressionthatdescribesit
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 13
Proof:RegularLanguageè RegularExpression
• Proofstrategy:– AregularlanguageisacceptedbyaDFA–WeneedtoshowthatcanconvertanyDFAtoaregularexpression
• Twosteps:–WeconstructaGeneralizedNon-deterministicFiniteStateAutomaton(GNFA)fromaDFA
–WeconvertaGFAintoaRegularExpression
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 14
GNFAinSpecialForm
• GFAsareNFAswheretransitionmaybelabeledwithregularexpressions ratherthanjustsymbolsfromå
• GFAsinspecialformhavethefollowingproperties– Onestartstatewithoutgoingarrowsgoingtoallotherstatesbutnoincomingarrows
– Onesingleacceptstateswithnooutgoingarrowsandarrowsincomingfromanyotherstate
– Allotherstateshavearrowsincomingandoutgoingtoeverystate,includingthemselves
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 15
DFAà GNFA
1. Addanewstartstatewithonearrowlabeledwithε tooldstartstate
2. Addnewacceptstatewitharrowslabeledwithεfromalloldacceptstates
3. Ifanyarrowfromremainingstateshasmultiplelabels,replacethemwithequivalentRegularExpressionE.g.,a,b ->aÈ b
4. AddremainingarrowsmarkedasÆ
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 16
DFAà GNFAexample
1a
2a, b
b
1a
2 a È b
b
Sε
ε
EdgesmarkedwithÆ areredundantandmaybeconfusing!
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 17
A
GNFAà RegularExpression
• WeproceedinaseriesofstepsreducingthenumberofstatesoftheGFAto2(startandaccept)• TheRegularExpressionleftontheonlyremainingarrowisequivalenttotheGFAand,hence,theDFA
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 18
GFAà RegularExpression
• WhileGNFAhasstatesotherthan“Start”and“Accept”– Pickastateqandremove itfromtheGNFA– Repairthetransitionsbycombiningtheregularexpressionsbyconcatenation
1a
2 a È b
b
Sε
ε
S
2 a È b
a*b
ε
S
a*b(a È b)*
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 19
A A
A
Formalizationoftheproof
Thetextbookprovidesarigorousproofbyinduction:– CONVERT:proceduretotransformGNFAGinto
RegularExpression– Statement:L(G)=CONVERT(G)• Base:Ghasonly2states• Inductivehypothesis:StatementholdsifGhasi>=2states• Inductivestep:weshowitholdsifGhasi+1states
1. LetG’denotetheversionofGwithi statesobtainedafteroneapplicationofCONVERT(G)
2. WearguethatifastringisacceptedbyGitwillalsobeacceptedbyG’andviceversa
3. Applyinductivehypothesis
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 20
EquivalenceofRegularExpressionsandFA
Theorem:Alanguageisregularifandonlyifsomeregularexpressiondescribesit
Twodirectionsweneedtoprove:– Ifalanguageisdescribedbyaregularexpressionthenitisregular
– Ifalanguageisregularthenthereexisits aregularexpressionthatdescribesit
9/17/19 TheoryofComputation- Fall'19LorenzoDeStefani 21