CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-6-PDA.pdf ·...

22
CS1010: Theory of Computation Lorenzo De Stefani Fall 2019 Lecture 6: Pushdown Automata

Transcript of CS1010: Theory of Computationcs.brown.edu/courses/csci1010/files/doc/notes/Lecture-6-PDA.pdf ·...

CS1010:TheoryofComputation

LorenzoDeStefaniFall2019

Lecture6:PushdownAutomata

Outline• Pushdownautomata• FormalDefinition• ThelanguageofaPDA• ConstructingaPDA• EquivalenceofPDAsandCFGs

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 1

FromSipser Chapter2.2

PushdownAutomata(PDA)• SimilartoNFAsbuthaveanextracomponentcalledastack– Thestackprovidesextramemorythatisseparatefromthecontrol

• AllowsPDAtorecognizenon-regularlanguages• Equivalentinpower/expressivenesstoaCFG• Somelanguageseasilydescribedbygeneratorsothersbyrecognizers

• NondeterministicPDA’snot equivalenttodeterministiconesbutNPDA=CFG

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 2

ComponentsofaFA

• Thestatecontrolrepresentsthestatesandtransitionfunction

• Tape containstheinputstring• Arrowrepresentstheinputheadandpointstothenextsymboltobereadfromthetape

Statecontrol a a b b

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 3

ComponentsofaPDA

ThePDAaddsastack– Stackactslikealocalmemory

– Canwritetothestackandreadthembacklater

– AstackisaLIFO(LastInFirstOut)andsizeisnotbounded

– Writetothetop(push)andrest“pushdown”or

– Canremove(read)fromthetop(pop)andothersymbolsmoveup

Statecontrol a a b b

xy

z stack

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 4

PDAandLanguage0n1n

CanaPDArecognizethelanguage0n1n?– Yes,becausesizeofstackisnotbounded– DescribethePDAthatrecognizesthislanguage

• Readsymbolsfrominput.Pusheach0ontothestack.• Assoonasa1’sareseen,startingpoppingone0foreach1• Iffinishreadingtheinputandhaveno0’sonstack,thenaccepttheinputstring• Ifstackisemptyand1sremainorifstackbecomesemptyandstill1’sinstring,reject• Ifatanytimeseea0afterseeinga1,thenreject

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 5

DefinitionofaPDA

SimilartothatofaFAbutnowwehaveastack– Stackalphabetmaybedifferentfrominputalphabet• StackalphabetrepresentedbyΓ

– Transitionfunctionmustincludethestack• DomainoftransitionfunctionisQ× S ε× Γ ε

– Thecurrentstate,nextinputsymbolandtopstacksymboldeterminethenextmove

• ImageoftransitionfunctionisQ× Γ ε– Thenextstateandthenextsymbolontopofthestack

[<latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit>

[<latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit>

[<latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit><latexit sha1_base64="(null)">(null)</latexit>

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 6

FormalDefinitionofPDAApushdownautomataisa6-tuple(Q,S,Γ,δ,q0,F),whereQ,S,Γ,andFarefinitesets

1. Qisthesetofstates2. S istheinputalphabet3. Γ isthestackalphabet4. δ:Q× Sε× Γε® P(Q× Γε)istransitionfunction5. q0 Î Qisthestartstate,and6. FÍ Qisthesetofacceptstates

– NotethatatanystepthePDAmayenteranewstateandpossiblywriteasymbolontopofthestack• Thisdefinitionallowsfornondeterminism sinceδ can

returnaset

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 7

StringsrecognizedbyPDAsThefollowing3conditionsmustbesatisfiedforastringtobeaccepted:

1. Mmuststartinthestartstatewithanemptystack2. Mmustmoveaccordingtothetransitionfunction3. Attheendoftheinput,Mmustbeinanaccept

state– Tomakeiteasytotestforanemptystack,a$is

initiallypushedontothestack

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 8

Notation

Wewritetransitionsasa,b® ctomean:– Whenthemachineisreads“a”fromtheinputand“b”isonthetopofthestack,“b”isreplacedwith“c”

– Anyofa,b,orccanbeε!• Ifaisε thencanmakestackchangewithoutreadinganinputsymbol• Ifbisε thennoneedtopopasymbol(justpushc)• Ifcisε thennonewsymboliswritten(justpopb)

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 9

PDAfor0n1n

FormallydescribePDAthataccepts{0n1n|n≥0}LetM1be(Q,S,Γ,δ,q0,F),where

Q={q1,q2,q3,q4}S ={0,1}Γ ={0,$}F={q1,q4}

0, ε® 0

1, 0 ® ε

ε, ε® $

ε, $ ® ε

q1

q4

q2

q3

1, 0 ® ε

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 10

Example:PDAforaibjck,i=jori=k

DesignaPDAthatrecognizesthelanguage{aibjck|i,j,k≥0andi=jori=k}– Thinkofaninformaldescription– Canwedoitwithoutusingnon-determinism?• No

–Withnon-determinism?• Similarto0n1nexceptthatweneedtoguessnon-deterministicallywhethertomatcha’swithb’sorc’s

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 11

Example:PDAforaibjck,i=jori=k

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 12

Example2:PDAfor{wwR}DesignaPDAforthelanguage{wwR|wÎ {0,1}*}– wR isthereverseofwsothisisthelanguageofpalindromes

– CanyouinformallydescribethePDA?– Canyoucomeupwithanon-deterministicone?

• Yes,pushsymbolsthatarereadontothestackandatsomepointnondeterministically guessthatyouareinthemiddleofthestringandthenpopoffstackvalueastheymatchtheinput(ifnomatch,thenreject)

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 13

PDAfor{wwR}

0, ε® 01, ε® 1

0, 0 ® ε1, 1 ® ε

ε, ε® $

ε, $ ® ε

q1

q4

q2

q3

ε, ε® ε

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 14

EquivalenceofPDAsandCFGs

– Firstdirection:ifalanguageLiscontextfreethensomePDArecognizesitProofidea:WeshowhowtotakeaCFGthatgeneratesLandconvertitintoanequivalentPDAP• ThusPacceptsastringonlyiftheCFGcanderiveit• EachmainstepofthePDAinvolvesanapplicationofoneruleintheCFG• ThestackcontainstheintermediatestringsgeneratedbytheCFG• SincetheCFGmayhaveachoiceofrulestoapply,thePDAmustuseitsnon-determinism

• Oneissue:sincethePDAcanonlyaccessthetopofthestack,anyterminalsymbolspushedontothetopofthestackmustbecheckedagainsttheinputstringimmediately.– Iftheterminalsymbolmatchesthenextinputcharacter,thenadvanceinputstring– Iftheterminalsymboldoesnotmatch,thenterminatethatpath

Theorem:Alanguageiscontextfreeifandonlyifsomepushdownautomatonrecognizesit

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 15

InformalDescriptionofP• Placemarkersymbol$andthestartvariableonthestack

• Repeatforever– IfthetopofthestackisavariableA,nondeterministicallyselectoneoftherulesforAandsubstituteAbythestringontheright-handsideoftherule

– Ifthetopofstackisaterminalsymbola,readthenextsymbolfromtheinputandcompareittoa.Iftheymatch,repeat.Iftheydonotmatch,rejectthisbranch.

– Ifthetopofstackisthesymbol$,entertheacceptstate.Doingsoacceptstheinputifithasallbeenread.

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 16

Example2.25:constructaPDAP1fromaCFGG

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 17

Example

• NotethatthetoppathinqloopbranchestotherightandreplacesSwithaTb– Itfirstpushesb,thenT,thena(aisthenattopofstack)

• NotethepathbelowthatreplacesTwithTa– ItreplacesTwithathenpopsTontopofthat

• Yourtask:– ShowhowthisPDAacceptsthestringaab,whichhasthefollowingderivation:• S® aTb® aTab® aab

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 18

Examplecontinued

• Inthefollowing,theleftisthetopofstack–WestartwithS$• Wetakethetopbranchtotherightandwegetthefollowingaswegothrueachstate:– S$® b$® Tb$® aTb$

• Wereadaanduserulea,a® ε topopittogetTb$• Wenexttakethe2nd branchgoingtoright:

– Tb$® ab$® Tab$• Wenextuseruleε,T® ε topopTtogetab$• Thenwepopathenpopbatwhichpointwehave$• Everythingreadsoaccept

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 19

RelationshipofRegularLanguages&CFLs

• WeknowthatCFGsdefineCFLs• WenowknowthataPDArecognizesthesameclassoflanguagesandhencerecognizesCFLs

• WeknowthateveryPDAisaFAthatjustignoresthestack

• ThusPDAsrecognizeregularlanguages• ThustheclassofCFLscontainsregularlanguages• ButsinceweknowthataFAisnotaspowerfulasaPDA(e.g.,0n1n)wecansaythatCFLsandregularlanguagesarenotequivalent

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 20

RelationshipofRegularLanguages&CFLs

Regular languages

Context Free Languages

9/24/19 TheoryofComputation- Fall'19LorenzoDeStefani 21