资源描述:
《5 context-free languages (1)》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、5Context-freeLanguages5.1IntroductionYouhavetriedtoconstructaregulargrammarforthelanguage{akbk:k∈N},butfailed.However,thestringsoftheformakbkcanbegeneratedbytwoproductionssuchasS→ε,S→aSb.Forexample,withsuchproductions,wecanhaveaderivationfora3b3asS⇒aSb
2、⇒aaSbb⇒aaaSbbb⇒aaabbb.TheproductionS→aSbviolatestheregularityconditionthatanonterminalontherighthandsideofaproductioncanappearasonlytherightmostsymbol.Toaccom-modatethesegeneraltypeofproductions,wewilldefinecontext-freegrammars.5.2Context-freeGrammarsAco
3、ntext-freegrammar(CFG)isaquadrupleG=(N,Σ,R,S),whereNisanalphabet,calledthesetofnonterminalsymbols,Σisanotheralphabet,calledthesetofterminalsymbols,N∩Σ=∅,S∈Nisthestartsymbol,andRisafinitesubsetofN×(N∪Σ)∗.ThefiniterelationRfromNto(N∪Σ)∗isthesetofproductions,
4、whereeachelement(A,u)isrewrittenintheformA→uasearlier.Anapplicationofaproductionruleisformalizedbytherelationofderivationinonestep,de-11notedby⇒.WewritexAy⇒xuy(readasxAygivesinonestepxuy)iffx,y∈(N∪Σ)∗,A∈N,u∈(N∪Σ∗),andA→uisaproductioninR.We∗1denoteby⇒,t
5、hetransitiveandreflexiveclosureoftheone-stepyield⇒.1∗Wesimplywrite⇒forboth⇒and⇒ifnoconfusionarises.Thatis,weoverusethissymbolforderivation:u⇒v(readasugivesv)iffthereisasequenceofA.Singh,ElementsofComputationTheory,TextsinComputerScience,125cSpringer-Verl
6、agLondonLimited20091265Context-freeLanguages11111derivationsu⇒u1⇒u2⇒···⇒un⇒vorwhenu=v.Suchasequenceisalsocalledaderivation.Thelengthofthisderivationiseithern+1or0,accordingly.Ifmanycontext-freegrammarsareinvolvedinaparticularcontext,thenwewillwrite⇒tosay
7、thatthederivationisdoneinthegrammarG.Astringw∈Σ∗isGgeneratedbythegrammarGiffS⇒winG.ThelanguagegeneratedbyGisL(G)={w∈Σ∗:S⇒winG}.Alanguageiscalledacontext-freelanguage(CFL)ifitisgeneratedbyacontext-freegrammar.Theword“context-free”comesfromthemodeofapplica
8、tionofaproduction.TheproductionA→uisappliedonthestringxAy,wherethecontextx(ontheleft)alongwithy(ontheright)remainsunchanged.Thatis,itcanbeappliedwithoutbotheringwhatxandyare.Whenthismodeofapplicationisviolated,agrammarisc