Chapt. 4 Syntax Analysis - Part1
Chapt. 4 Syntax Analysis - Part1
» FT To Tele bere pom (ED lid gn the care where we howe productions % the yorm A> Aol | Ady |---| Aam | fo | Ba) o> +] Bn whers. no Bi begins with an A, then we yeplace A procuch' ou by Acs piAilipea leo Tea! Alenw oy Al | wa Ale fetal | PAE Cpvicl the cthronrGgranrreer, : Sey mee\% VY NNEAMDNE. Scanned with CamScannerAlgorithm: Eliminating (egt recursion Anpuk > Grom mor G with no cyules or €-produchong ivalenk grammar. with no dest recursion outpub! An. eq method: 1 App Arrange dhe nondesminals in dome oe ne A a. for tz} tn do begin for j=l w ivt do begin replace each prod” a the 407" Ay AjY by the productions Ar= &¥ | Sarle + loca. wher Aj —r~3i|bal--° {Sn ore aul curren Ay produ cons / ear rmmediore Lept recursion anr0ng the Ai- produ ch ons end egy Ebiminote dope recursion the grommar ss pale A—> he IGAE Soli» Order, NTS | as o3A immediate, lest recursion for ici , No fer ica, Substitute & produch om in & eael a VE oz) AS Ac | Aaa|bad le , Ad, APs, Bi @2 : climi nate tegt recursion among A product ons A= baa'|a play eh jada’ le & Find Grammar S—> Aalk A> bd A'|A' At—> catfadat lé Scanned with CamScannerfest reuarsion from the grammar A— Ba jAale G-> Bvlabld 2 > order the NTs as 4,8 potursion prem & A-prod’s. tay there ‘o immediate. Uf Eliminating its A= BaA'|ca Aweal\halc : @ ia Als aalée a Replace A in B— Bb| ALIA ge Bb| Bavtblca'b|4 Nove grammor vs pu 4 > Baal|cat Al waa'le p > Bbl earth |calbla ston of B, ' p> cAlb BAB Hp] cA'bl a Brerleaatl ans pi» bp! lanl BE wa Now eliminating Legt retary Final grammar , A— Baa ica! Ala eal le \ p> cAlbe'| dB! Bi —> be! | aA'pe' |e Scanned with CamScanner+ hept fochoring af A> epi | eR are (wo A- produch on, and the input, begins with a noo emphg String dewved from a, we donot d.” egy know whether, to expand A oP oy bp: +p we may deper the le cision by expanding AtpaAl , cihen Apter deomng the inpuk derived 41om &, we expand Atdp Bio ove pa A—> aa! Al—> pi |Pe Algevil hm : Snput : Grammer G ie. legt factored, She ovginal produchions become Lege gattering. @ grammar Output: an equivalent left factored grammar Method : For eath nonterminal A pnd the ton gest pregix a common tw 2 or moreigii alternabkives | Lh ote, replace all A produch ens Aap] pal la Boly where p represent all alteznatves that do not begin with % by Aa> mar |p A'—> Bi |p2to> > [Ba A" id a New nonterminal. two alternahve for a w Si Ets| (Eases Eb => iy => Fy Nonlerminal Shad bongesk, progr *» S—»itts sN\a si-> esje Eomb “a lets Scanned with CamScanner Repeat edly apply dh transpormakon untl no ee non-beimincd have commen pPregin_ Basic Parsing Techniques prfecs. A : parser grom grammer G@ iw a program that takes as input as oubpub either o parse tree in error messog & a sting w and produces fence. a & OF ® sentence o &: w,ig we ow ven indicating thot ww nek a bo) odfable cing a sting % dokens from Lexi cad analyzer dnoe the tong OP be generated by the. “ap alo reports 4ynlax pource LAngurye yecve fom commonly oceuming A power oht and ert gies yam mor jor, the errors: At bhowd alec errors 20 that it con conhinue proce ng the yemainder a top ut | yee gramme on - | arsers Aor context - bottom parse trees from 2 2 base type % @ Bottorn 4p. parses ushich build Cleaves) athe ‘gop (4009 _>shigt- reduce poses _> eperater precedence parse: arses <— simp LR aan nical LR LALR. ah starts ie ceith aot and worle @ Top-down _pawiest down @ the Leaves —> Recursive descent > predichve parser parses Scanned with CamScanner# Top down Passing . € ' oy Gr can be viewed a an attempt 2 pind a teptmast ‘ derivation fey 07 in pur biiag . at con be adéo viewed as &/. attempk fp condtruc o& parse tree gor the. inpur Atarting from peoof and orecding dhe nodes % parse tree in preordes . general form 4 top-down parsing thar involves backtracking tee repeat{ie- making. repeated deat inpab) 2 thewn ¢ Consider. the grammar 5 S —chd A —> abla and input shing w= ead 40 cwonshuck a parse tree for thin Sentences 6: the inpub we rete a tree consishng gy avdingle node, Labeled pointes, points +o c, the first Aymbol 4 wo Then we wre. ist prot?” ors So expand the tree. Ss AI™ cA a hoptmest Leas jc matches Ish symbol | w, 40 We advance. inpak pointe, to a, 2rd aymbol gw and Conside, the next Leap Labeled A, Then & we expand A ining SF QL tosmate $ ead as ob : or “and inpuk symbol. Now consider d, 3rd input symbol and neat heagsb b dow not match d, we repork gorbune and go back tp A te Aee for another alternate eohich might match, we have a math To ge back ww A, inpwr pointer must be reset te positian2. 2 veg and altenare Ss a “Aa 4 Scanned with CamScanner~ Mow there ia a match ancl 1 parting is complete- parser » we creak a procedure for &S40 * sap lement thi F each NT Procedure 5 pre cedure St); begia , 7 if tnpuk symbol 2c! then fee Oe ADVANCE OO; if ACD then iy inpuk dymbol = tq! then begin ADVANCEUS return true end. ends 4 return yalae end pre cedure ACs hegia 7 isave se inpub— pointer s if inpwaymee | ='a’ then Pegi? povancel &y inpuddymbol = 'b' then an : ADVANCE Os AetuAN true end end ‘inpuk pointer := ave, /x failune t tind ab */ ig inpwt-aymbol ='a' then | beg, ADVANCE (9; ebe end Aabaro usd Aetuan false end Scanned with CamScannerDifpivation:- A bepe vecursive grammar can cause a top down spouses infinite loop. when we by % ehpand A, wes fo go in on moy eventualiy ind owuelves agarn tying t expand & withs We.” proving consumed any I/p- -4 we make a sere seqene % error neous expanse! ons and subseqently, discover @ mismatch , we may have & unde semankc eggects - - when ot lute. wu reporled , the errr actually occumed: we Have a very aitle idea wht # Recursive Descent pawing, Gk eliminater the need) for ba dn order that no backtracking aymbol a and NT A —b be expan cetrodeing over the tnpub- be repired , we must know, gimen dhe curren clp ded whith ome @ the alternate 4 pred?” A 7 di] a2 | -- = Jan a unique abfernate thak clerives a Abang beginning with a@- A parser thak used a rer oH recuse ve proceduned te th op with no backtracking @ called yeruyine pecognixe descent porsr- 8 consider the grammar a: ——>TE! E' -pgre_'|¢ Ter Ti aHeT' le F—(e)/ id Scanned with CamScannerae @ amyhon- bark tracking rrecursiwe detent parser Us- z " procedure, Boo; begin TOD EPRIMEC? 2 procedure EPRIME Os ' inpukaymbol = ‘+! then begio ADVANCE; TOs EPRIMED POs TPRIMEWD ends procedure, TPRIMECI, i4 inpur dymbel = '%*% then begin ‘a ADVANCE L); FOS TPRIME CD end; precedue FL; if inpuk aymbol = ‘id! then ADVANCE © ele i4 Sinpuk symbol ='(! then #8" govance Os £0; b ty inpuk ay mbol = ')? then ADVAN CEC > eve eERRoR ty end : ee ERROR» Scanned with CamScannerPor a grommasx to be duitable. dor Aecuuive descert pothiy, it phoutd be lat gadorized Ca procew o factéring othe, common -pregined att exnates.) Transition Aegrams tun ba wed A 2 gowchat or plan fer \ Tecursive ctercent parses. For each NT AS > Create an Wwitial and final Cveturn) State, 2> For each produchion A—>XiXz---Xn, Create a path -frem the inibal tm inal state, with edges Labeled Xi%o-- Xn for B: oT E29 tees toe au T: FT 30 4a T's ee € fore: 9g Epo 2 Stu possibly t simpligy tronsikien diagrams tg abst benny diagram in ont anothers _ Simpugied tronsition agrame jor et: Kets We \\ gor T? ame ie ad for Et: ¥+NeE 6 for F: jor & ebro Ered ze id for E Scanned with CamScannerA Predichve pase on eppicient way arup lemenbing recursive descent a8 ing by handling the shou. 4 ach'vakon records explicitly tha implicitly via recuratue calls. model o nonrecursive predichee, parser Stack outpur A table- driven _ pase hos an tn pur bupper 4 Atatk, « parsing dable and an eutpuk Afream, > The inpuk buyfer contains the bring & be parsed» qotlowed by ¥,a aayrnbel my CO GS right endmarker indicate the end % inpuk abirg. > The stack eontains a regent % grammar $ an the bottom 4 the stack» Gridi ally , the slack contain the on -lop % 4. —> The parsig doable wo g-cimensional arra MLA, a] , where a a termiaal oy symbol ¢. aymbola with short aymbol % the gem Ayo nontermingd , and a sche parses ia controlled by o program ‘hak behaves @ f follows 2 the program eonsidat X, Ihe aymbol 0” 4op % aback, and a, the current inpur Aymbol- Scanned with CamScannerThese 2 aymbel cleteamine the athon %a pars There are 3 possibith ett @ 4 X=a=$, the Completion 4 pausing: parser pops X %p the stack. next inpub ay imbol parser halts and annountes duceem pd ‘ @ ap x=aFF, athe and advances ' inpak pointer 4D rn tetminal , the pregram consults entry MLx,a] | Kr produch on cthe @ xX 4 a no q% the parsing 4a q che grammer oy an ay M[X, A) = 2X uvw4i the pers ek by WyU ( with u on top). ble na, This enty io either a exror entry, en ~rep laces xon ohe pore sur dop q sta pin the production weed « TL Mixa ¢ ervey + dhe parser cals an error recovery Youbine. “g: Fe laa Grammay :- eae. Ei + TE'|G TFT! tla eet' le F— Ce) lid wohith tk a configuration in : aq & ontop, apilially, the Parse~ te in athe dtark Aymbol with S, hos $3 on the aback, gor (w la om Abing) and wh in the inpak but fo parse dhe inpuk tdeid wid ,the yollewing predichve porsing dable in useds- Scanned with CamScannerde Eive | ele Blanks Ae error en’ expand athe nonterminal woh vohith with the inpub sequence, % movedi- tries; non blanks id+ id wid , the predichva parse. mal i Dicom . rhe) reexet|] wie | Tie ae aro) | sadseate 0 proaucion on the atatk. keg the he inpub pointen poink -e the, Left most symbol g the bing sp inpur column, A parse. fyaces ouk & legtmest derivation $7 dhe inpus. ' che ilp aymboly that have already been tanned 1) followed by gvammar aymbot on the Atack »make OP the, Sett- gentential qormi in the derivation . orack sneuT ouspeT id tidaid $ | grid xia S ig tidxids E> + TE! Scanned with CamScanner— ee tt ere $ i pra geiTid id pool et! ¢ #8 $ tte Pe £ 4 Eli+é The Construch'on of prediche, pauer te aided Y 2 qunehions : EIRST and FOLLOW , which allow ua ZI/ tp the enbie % cw preclich've parting dable for 4. Fh © ww any bing of grammar aymbols, tek pneliee, be the det 4 toumina that begin she sbings derived fyem ob. 44 ot Ee, then € also wy FIRST (LD Jo compute FIRST(X) Aor all grammow symbols X, apply dhe oll- Autes Wohi! ne move derminals or € tan be addec/ t any FiRsT et @ 4 x4 desminal, then FIRST(XD & Ax}. @ 4 Xe “ya producion , thea add €'4p FIRsT(A) 1 od” @ if Xv rontermined od ee a in eipst OX) WE for dome i, O& ‘is ty FIRST (Yi), then pla ond € ‘uv w all 4 pmsT id. © EIRST (Ninn) ee on) oe de RreAPOHD Eu FIRST OY) Fer all Jab do-e k jlhen 4 add € +6 FIRST Cx) “Fig compute FIRST fer ‘any sbiag Xi Xo 7~ “Xn, Qdol %& FIRSTCX1, Xa ---Kq) all now symbols od erRsT OX) - Algo add PON symbol of Frese ¥2) tH E ia Wn PIRSTCRO, 2nd add do on: Brey add € ~ FIRST (xits--Xadig FIRSTOD “ . contams € Scanned with CamScanneraE: foniider the orl. rammae . ETE! f e+e |e Toe FT! whe ee T IE ' F-> (e)lid for riest(E) = FIRST(T) = FIRST(F) =¥ Lia} “s Oh 16 FigsTCE = 24, e4 ep eiastlH)= BY, coe First (T') = 4%, e4 - ernst (A) 22K, Tee FOLLOW(A) Fora nenterminal A ia detined t» be the det gy decmina a thak can, appear immediately the righty A : in dome AMlenkal, Form ,e- the cet q@ terminal: a such that there exis a Aétivedion a the $o7™ 82> wAap www lA) aor all nenterminals A, apply oll. Arles TW @mpake FOU can be. added + any Fottiow deb. uht | nothing. “4 land B84 fu Plaw $in Fotow(S), whee Sa stat fre $ dhe inpuk Aight end marke, . Au Sh thee ‘ a prod? A> uBR then avery thing in F1RST Cp) , except gor & ih placed in Fortowle ). ph) £4 “the 0 prod” AraB, or a pred” A>aBp, whee Fras B) contain @, then every thing Wn FoLlow (A) "a Ww FOLLOW B) Scanned with CamScannerqPonider a 1st pred?” ETE 4 A > &8 aps FoLLOw(A) HM forrowce) PMD ©) len Qa pho ah % Fouow(E) » FoLLowe') and prod? el-aeTe'le : pre cane! & inst (pd oo A FoLiwl 8) sy eipsrteldia th peutewtt) Muy ond. poghow (A) ve ‘wm FoLLow le) e rE este Ga foLiow LEN) a "BA vow (1) Rule: 34 pred” \ P Mies Fovtow (A) u Ws POLLY) fol gute: + poinw (1) oy Fottow(T') Ath prod 4 ay a if Tos yet pinstip) FoLLow (8) Avr w&B co opiRsT(T') | % \gorr)ew br) Ruler fe ee na PG fe year Foriew (A) ie wm Fottew( B) Ruled i oo porvow(T') 9 &O Lrow | &) ST th 6 pred? ra (a) pirst (p) vv wo FotLow(B) p= cep LL Forrow (ED Rulez ip wt wt Soe “ be 7 Re ww sca gy poe OO) Se 2 Fouow(e) = 3 a auaeaid edie! Foirow (e') — 3 couse D5 2t4.)3 Zrimrled, rouse oe) Fouow (T) = =34.4, 73 Fottow(T') = J Fortew (v3 zat bs Fou (FI = 7 Fottowtr') , FIRST (v3 = 44,8,0.%9 Scanned with CamScannerCono a hruch’en |e pow ingle: BepO Foy eath prod A> ob 4 grammar, do akeps © ond 6 4 | OF sep@® pei y cleemaiinad A a IRSTC a), ae eed Aol ‘tb? ‘ma TAjad ‘ Oooh dkpO ty C ‘wm eiRsT (0), adel | A => o¢ 46) 4M) [Ane fev Cath Lerminad b tn FoLiow (A). 44 € ww riest to) ard 4 with, ForLow CA), add psd fe ™ [A,$) Atep@) Make, each unolskinea onby ‘4 ™M be enrey. Scanned with CamScannerUN Ae at OM ne! @ whoewry! D Kew e @) veri © roger! © vex @ rer) @ to-wid Posing Non deamins : = \ 1 . \ y f Pima (at) wade nb ind Cid yy ML este Me, id jae d he! WESTOITE I Gay oo ME +] = eleere! Se Fouuow (ED= Fy} AMCel = m[e¢] . boc rinse (rt): fiw)” OM, 2M Lt teehee =,T— ett riksr (Mets 3 ey Nevbare ars wir! Fim (ODstey ronowtt' a3 by I,44 0 aM Ertl $J = m7, )J=mOrb+) 2Tse Funsn ee: . . rmor (Ce = 905 & MDF, (] = pole) ernst! Ctelas tid gM De td] Ftd dable we” hp Ayre | vd ' x i gore! 1 beat vot t tlw & yw th Scanned with CamScannerpredickive parsing. dable ger the following By construc grammar and parse dhe sheing ink di, ida ¥ potty : Boy ope» ‘int | float taiar\ Alea yo ia, Vid apie v—=viav' qeeiny vig NE 4) rind FUR => Lead FIRST UT) = ink, jloaty cunst(v) = nh YeMyar sey Li4h rinst(VI= 34,€4 , Yi) Find FOLLow 1st prod? DoTV Ame FOUL A 88h 2 Ratue4ate we Fest (Vd Foo) ard prod” Tor iat gloat eo “ee a ae ne _couiowt) i in Fowtow( a) wld) in “FoLLowly, ea) ; torrowet | : hy 3rd pred” dy via v' Foussnta? om Feuewl 8) A> vB Lowlv) 0 » Fownow(v!) ath rod” BORON Vay Forrow(A) vin Ene i. aLLew(Vi) vin > Fottewty) | A+ aed ' ) 4 oprewl 1D = 245 forrow(W = 244 i : poiswl v= aby eouset* yy Scanned with CamScannerernst OV=2 im, jloak y M [pyint = > oty Cr guat= Dav +, pA Tint) = Tink © vs ior kph First (ink) = Aint 5 7 | @ Toqgeor Frnst (alent) = 3glot pq [Ty loet] = TH @® voiav' Futsr (id v= 314g OOMEW, id= voidy! ® i @© via ov te First (sV)= 394 LIV, Je voy @ vive Fiest (€9= 364 cowow v') = 284 i MpVvyg] = vise Le ie ~ int tan ide, Doty tak dt, vel T-2ink tdi, ida. pausing complete - Scanned with CamScanner3) eliminate Lege qactering and Corstrutk predichwe spossing table der the qotteusing » S—> aAA A+vas|bs|o Parse the 2758 aabaaa => Bey Legt Jack : 2 A aA! | kde bS Als s|é common prefix ia o Ap pred 2, obtaining FIRST, first (S) = Zay First (A) = 2ab4 first(Al) = 1h, frey Obtaining FoLtow , : pt pod? = g->aAAe first (p) “9 fo uow (B) Aer jes [rien poco J erRsH (PI = HOH FE + No fault 3 FOLLOW(A) “win Fotroto(B Spout) #7 FOLLOW (A!) ] amt pred” Apa At arabe ad pred” — pa bs [Fevidue cay sw Folipwls) ‘1 cou - on conan wo atl pred Alves [Fettowtad @ uw Foe Ae = 2 rousga,eaunaa 1 Po AA pusouts) 23, 7bf aos ce eiRsr (a) ‘ forrowl A) = Faby, by oye ee wuts) 4% ai 3) 4 . prota Sb Foe AD = 3 ar b§ 44 a ro NB FORME) iy in FOtourA) | Scanned with CamScanner ‘FIRSTLL) eurst(bs) = Zb4 oe rinst(aAA) = 4ay “ M[s,e@J] = ss aah eiest(aAl) =2a4 - M[A,@J- A>aAa' M[A,b] = A—»bS oc Fiest(S) = jay <. MPAGA] = Atos. \ Alive EiRSTUE) = Fey Forrow (al Jo ards, $4 iM [ASa ha Al>€ : MLA bl = Ale ee a Nontaminal aX J Bt Ss S-PaAnr A Aan) A->bsS \ 1 AY MSs Lee [Abe : aa Flp sheing aabaaa , } ‘ stack ay string ok ess $8 aabaaat $ Lia pang dabaaat Saha Ag | FS? cS pr * a gbeaad Aaa! “pops ee oo baa at i | vaaah | Abs " gaat | sana aak A>an | —— ——_——_-— _{—_—_—— | aA ak Noe ig Ales Aeten | = 7 a _ we need + } $A! of #4 Aah! oa j + Woe posing (pouting comple | Scanned with CamScannerLLC Grammars 7 ay” = ; for ome qramnmae , | pauing A mM ay chane some entries that corr rast ly defined « i recursina oF Ombiguous | then mM wilt have arteash dhe multiply « dagened entry. ) | A grommls ohese, pedting | table hao no. ptttiply- - oles neof enhies nad bebe LLL). The iF °L" Afandly if scanning the ilp strom lepr to right. ahe ard? ‘ M lyre oduting a Leptmest derived on, | A” atondds fib ene inplt symbol of leokanead of cath dep to coe parsing ack on qedsi ons. Crammen @ va iLbCD if! ‘and Lonlyh ig whenever A>] B are 2 olisinek'!\prod™s 94, & the ,goltowing. cand hold. ! ® Fer no terminal a do both w& an p dere, Abings \ beginning with a , be wool ° a eo o and daniidesive the en ie @ At mah one % Bp si pty ag. Oy Pee, dhen ol does dgt abetcye any. ashing beginning with o teeminal “in Fd LtowllAd , «, *N , ; 5 i . : [Pinay AK ‘N\ HINA | Scanned with CamScannerside the jollacing granmoe ' S—ricts | UctSes|o Carb tontrur the pouing table. vray , ' " SO Legt, gator Leb fort og icess |e ils es|e Cab Compathng PIRST © tCeat Ta} ee i i= First(s') = 1e,65 First (c) =4b4 ® Graputing FoLrow BF prod” € See “yp fou (0) pe BR vn FOULS) i Fiest ( \ a & 5 teks S [Ramey ue Reupulsy) ob - _ he ! ¢ —ictss' ‘st 2, FOLLOWS) 4 Fouts) Ao Par. ernst (p)=€ Not needed on pred” ‘ A : 8 oles roumatS7 = Fores?) 18 — piasrce) except © | So LESS’ peer [py wn Poumata) Fouow (s)= 14, e4 + eIRST(eSst) oo Foucw(s) Foutowl fouowls')=}gre} “Zag : Fo tLe roviow (c) = 3-4 emer tp) + € R Scanned with CamScannerS-victss! riesrictss)=4i4 2 Ms, =sei sigh ae Finst(ay=Jay M[ss¢j=soa *, en sia es Fiast (eS) =e} “MLsbe] = s'ves sive Fest (@)=7e3 2-Fouowts'= 74.64 \ VMLsigdeshe cb First (b) = 354 . M[s',e] = soe f { 1h) ceeM Ee gecee 1 | Ponsing table 4 Nt | a | & |e |. + 4 fad ie a ’ S saa id oo Sie = w | s! Sises ’ 3! Lee Ur ee c \Cob / | j the entry dr Mise] contains both S'>eS and s'se ey for (s'J= 30, $5 4 2 ohe grommas ‘wu ambiguous Thy grammar which contains multiply eegined entre ‘4 % nok LLL!) grammon . Scanned with CamScanner