语法分析方法小结自上而下分析自下而上分析其他语法分析方法比较自上而下分析自上而下分析一般过程LL(1)文法预测分析程序递归下降分析实现表驱动分析实现自上而下分析一般过程Inthetop-downparsing,webeginwiththestartsymbolandateachstep,expandoneoftheremainingnonterminalsbyreplacingitwiththerightsideofoneitsproductions.Werepeatuntilonlyterminalsremain.Thetop-downparsecreatealeftmostderivationofthesentence.LL(1)文法Thefirst“L”meanswescantheinputfromlefttoright;thesecond“L”meanswecreatealeftmostderivation;andthe1meansoneinputsymboloflookahead.AgrammarGisLL(1)iffwheneverA–>u|varetwodistinctproductionsofG,thefollowingconditionshold:-fornoterminaladobothuandvderivestringsbeginningwitha(i.e.firstsetsaredisjoint)-atmostoneofuandvcanderivetheemptystring-ifv=>*thenudoesnotderiveanystringbeginningwithaterminalinFollow(A)Thefirstsetofasequenceofsymbolsu,writtenasFirst(u)isthesetofterminalswhichstartallthesequencesofsymbolsderivablefromu.Abitmoreformally,considerallstringsderivablefromubyaleftmostderivation.Ifu=>*v,wherevbeginswithsometerminal,thatterminalisinFirst(u).Ifu=>*,thenisinFirst(u).First集Follow集ThefollowsetofanonterminalAisthesetofterminalsymbolsthatcanappearimmediatelytotherightofAinavalidsententialform.Abitmoreformally,foreveryvalidsententialformS=>*uAv,wherevbeginswithsometerminal,thatterminalisinFollow(A).计算First集TocalculateFirst(u)whereuhastheformX1X2...Xn,dothefollowing:1.IfX1isaterminal,thenaddX1toFirst(u),otherwiseaddFirst(X1)-toFirst(u).2.IfX1isanullablenonterminal,i.e.,X1=>*,addFirst(X2)-toFirst(u).Furthermore,ifX2canalsogoto,thenaddFirst(X3)-andsoon,throughallXnuntilthefirstnonnullableone.3.IfX1X2...Xn=>*,addtothefirstset.计算Follow集Foreachnonterminalinthegrammar,dothefollowing:1.Place#inFollow(S)whereSisthestartsymboland#istheinput'srightendmarker.Theendmarkermightbeendoffile,itmightbenewline,itmightbeaspecialsymbol,whateveristheexpectedendofinputindicationforthisgrammar.Wewilltypicallyuse#astheendmar...