You are on page 1of 18
3) - “UNIT-3 Syntox dizuctedl translaHon xyYz § Y.Val += 2*A. Val? Tstanslation on the Parse Tour t- Prectuction Considen dha Ayrtax clinectecl translatt on “ . for a “clesk Caleulaton” program given as follow’ 3 Producten, Semantic AcHona Eas 4 {ENal:= Eval +6? val $ E> Digit § ever = digit} Digit > oft/af--I3 We can conéfruct parse tree for 243-44 ‘Scanned wih CamScanner E.val=9 & is E + E 1 E.Val=5 _ 4 eo 4 E.NVal=2 Simple Posse Tuy | 3 SSS va Syntex Dineckd Tonslatoo Tre, dmplementation of Syntax clineckd Travslation — O Syntax clinected Tronslation is implemented by construc ting a parse tree and penfoorming the actions in a eft to Stight elepth fixst onden. @ 8dT is implementing by parse the input anc prrecuuce A pourrse tree AS A result . yEx- Give the Syntax clineckd Translation Achome fon the following “elesk Calculator ” grarnman - SEF EDET+E EPE*E £5) Emr rT digit . xz cligi€ ; Also cenguct He SDT Tree fon sting 23¥5tUF, where Aig't > 0/1/2~ -/9. ® = > mea ) w » , ‘Scanned wih CamScanner vey) GI". Finsk we finel fhe SDT -schome by acta) Sematic AcHons- for Ha pPstecuctions, Prraclurc fren SemomHe. Actions (|) GS EF a) Ese ye” SEVal t= Eval Feat} ms S$.Val := E.Val 3 Eval t= Eval + €? val E.val t= &". Val} E.Nol += T. Vol} 4| bane ee | | aa ey Ren a T.Val += 1% P'?. Vad + LexVal 6) p> 1cligit Vad t= 7] Tout j7-val r= Lexval ? Hence , Lexval ig volute at the Leaf ancl in prtoctuction (6), 2ught side of Us semantic. ccHon is for concatenation of kvo cligits » fike 12 con be be trough of as 1oXI +2. New we construct sre Purse tee wide aboue Spr, Ved 2 Tenwals © Tesvale3 Seevate Postse Tare &k Translation ® ree a + mah ae at ‘Scanned wih CamScanner “sf Hi oY geoplementing _clesk calculator <5 ERED het us le fin a Pere stack Top 3 3 Stack Twplementation Se: ampub § State 4 Valur | Production 2B*S4y$ _ = _ BH 5 +4h Zi _ _ siege E 2 Trdigit Bx strug 13 oe — ~B5 44% u @3) LT Adigit ) “¥54u$ Ee (25) er Sts Ex 23 _ hg Exs 23-- ie “eth Exl 23.5 Tatigit +u$ ExE 23-5 EI Hu $ E ns EEX*E ug Et (i = ¢ E+4 (us) _ $ EL Cusy-4 LT ctigit $ E+E as)-4 Baa $ E ug Ex Ete — Et aig) = = s =a sm Et ‘Scanned wih CamScanner . A Pager, OP pagel Cocle Gienenahm :- Synlox clinecleet tatanala tion tan blak Ha = AowtcR cocla oof fongquage ink’ inten mectiate Cocke , Whch fs bekoeen high DeveR Language and machina. Songuage - Foun types of intermediak, cect, om ofkn usec in compivens: C1) Postfix Notation Syntox Trees “ (3) Quadsxyples 9 @) “Tiptes/ Tvclinect totjples CD Postfix Notations :- 3,, this type of ee NO parentheaig ana mréactedt . Polish notation (Prefix , Postfix and Infix) is a mean to mepzesent authmatte / ogheal epenations in a marmett , which specifies #he onden Mm which epenatarg ana evaluated. One Zanguage that mormallly used postfix intemmecttake fomguage is ¥ SNOBoL . Ee G|@+b)xC | Hs postfix eguiralent 16 abtox (Q+b) (ct) , its postfix equivalent is ab+cclt+x valuation of & Postfix Expscession:- Seep) GP fe scan symbol is Idenbifrerr ox constenk, push ifs value ints stack and scay Fhe moxt 4ymbol. ‘Scanned wih CamScanner 2 dh the sean aymMblok is Dimouy openalon, <” f apply ib to She two oPercmels Popped from Shs / top of Slack ancl push «Fhe suswk into elack- (/giee3- df the scan 42ymbok fs unary operalor , Specifiect by dome special aymbo2 , apphy it to the top of stack ane push sre resul at the top of Slacks We ean Aho Fe a resule of unescy opencian fo she openanal using Pore nthesis eg: (-B8). Fe. Evaluate dhe following Poatfix expmession . 642/35 %* +94 a) a Scammed op) oP2 ResulE Shad Syrebo! 6 = = — 6 4 — = — 64 2 _ _— ae 642 ty / 4 2 2 62 3 — _ => 623 x 2 3 6 66 + 6 é 1 12 g = _ e 12 + 2 g Dil Q) ‘Scanned wih CamScanner Parse Ttee and Syntax Duet A ponse tree ar x a ayntax tree Iga that dhows fhe Ayndactic atructure® of a tree prtcgeram , Tre pome tree iS a usefl intenmedide Lorrguage fin a dowel progxam, A pose treet, howeven , contains Some sepeakel infos rmoabion Whick can be elimincded , Fhus pstoducing % 3 mmone econorniced ropresentadion of the Sowice prtogeram . One gue. variant of parse fre fs éyntex tree. fy Fon tha Given Gorammar , Es E+ T2TxXF/F . F2(e)/id Design Parse Tree for te ating _alt Ponse Trae wid tid «id E 3 ~aI™N, = T 7 l ~~ (PS por id ih ‘Scanned wih CamScanner & Give tha Syntax Tree fan expression @ arbsexea , ® iRa=b then ais I” a x Cea else bec wed ® H ~ then - ebee " - a \ y ¥ | x w~%. ge a. te ‘ A fe 7% /\ eo, e & yg. ® _ a /N ra J Na Three Addxess Gcle'- Three actress cocle fs a type cliale cock, which TS 1 ay sly 5 1a of inlenme: 4 easy Jo gerenale. arel can be use of at most Fur easily converted to 1 qrachme cocle. Jt makes ] adaensés and ame opendor supresent an expression and Are value cornmpuled at each Inehuction js diened fn a femppreuy variable genenatecl by compifen . The compiven otecller the orden of openodion given by Howe. adolwrr cole. a= b op Cc when. a,b and ¢ suprusents openancls Like | Names , Constants or Ccompilen genenokd: femporrarvies and op xepnesents Pha openosan. BB ars (-c¥b)t (He *A) y Find tra thorn | addbwas cole for given expression. \ a “Three adldlrssxag cocke t) HSE ty= tot tz bx t, as ty ax t, @ ‘Scanned wih CamScanner Sit han The Three acldress cocle tee forms (YD Quackiup ler G) Tniples @ Inclinect tuples. cam be supsesenta Mm thot (1) Quocsuples *- The quaclruyples have fours fielols to implement He shnee adebwrs cool. if the fiekel of quackuples conkuns tha nome of the operator , the fist source openand, He AECond sounct epenand ane fre sesult sespéctuely. Openalon | Source! |] Sounce 2 Destnahen} Fy! Q:=—b%¥C +4 Threr - Adelers cocle is as follows. gi t= —b > to= +, %*C t3= to+d , at= tz 4 These atucmonts ane noapresentect by quaclruples a ) as fellows . & (Urany, oe DestinaHon 4 90) entyon | (0) 4 x single spend] ce to ; @ a ts i &) _ a (2) Teiplesi- TRe thipler have thre Pieldly to implement Phe trea aclbuss cole The fell of Aplus Containg fhe mame of Ihe. openctan , da first Soren oPenanck anol the deconal oPenand . dn tuples, He mecull of Iespectve sub- expressions ane clenoted by de position of . expression. Triple IS equivalent te DAG while | ® sepnasenting €xphessions. yr UN ‘Scanned with CamScanner ae [eo [Seer] See ] _Ex- Represent expression ar=-b ee td im a tsuphes form (Three aclelass Coclt) —_ ee Qi ab tots tC tai= t2+d O:e ty These. slalemenlg can be represented in Truples- (3) Inclinect Truples ?~ This mapreseniation makes Use —— of pointens to roake Listing of ald sefeamers 7° computations whch is mace epenctely ancl senso. Jt jig dimitri obi Ling Qs camparral quacheuphes otopnesentation but saquines fers Space than ik. Tempanasdes ove implicit and easiot fo srasremge codlea . gt is triples with operational Jalil, . Tupfea ana saiuitten afte Yemoval of Sedundant Frvphea , bol befor, sumevel , entry ig maole inte openation fable, uch Prod, stedunctamt— fruple refers-to previous triple lolly wohicly ib mateher + For Pxampie . ®e ‘Scanned with ComScanner ~ Q Comaiten Expression — A= b¥ vc +b¥-C 11 Pind de Ancinect Friptes ? “So!” Firedty we dave fo urtide thre aclobwrs cocle fer te given expression. | tse to = b*t, t3:sc-e¢ ty i= bets f5 ta tah by are ts Fan the indirect trup2e ,we make o List of pointer Jo tabhe jlohiche awe ony joe, add. of swal triples. Thats why it is Callact indinect suples. Lisof pointen fable Gx\. Waite. quaduples, Truples amel Trotinect triples for | ~ ha gver expression At (x +Y) © (YZ) + Oy +2) Qr Commtuct fhe trip dea , quacbupes anol Be tinct duintss of He following expasssion A:= BxC+D Br:= A+C#*D @ ‘Scanned wih CamScanner PP tiene @) The P scckioms. He entry for felname in the Symbol fabke . @® The Emit fanctin is used fan appencling dhe Thnae odllxrs code bs the ofttput Ailes ofbaxwise supert an enon. . @ The mewlemp() is a function used fo genenake me demponasy vardalbes . BD Eplaca Aolely fre value of E. Boolean Expression ~ Beolean Expression comisk of boolean openatans AND, OR, Not _ The . beofean expsiessim +taye too petimany pwiporca’ They ane used for com puting the fegical values. Thay ase also used as condifionel expression using if-than-ghse an Lahife-clo . Consiclen a Pammat E-2E oR E EE ANDE E> &@NoT E E — (Ee) E = id nebo id E> Tee | E> FALSE Tha xelop is clenofect by 2,2 The AND and OR am Geft associttive | NOT has Jha highen pnececlence Jhan AND and OR, The &£emantle acHms for above gramman con be written ads follows. ‘Scanned wih CamScanner ~ Translation of Assignment Stademenss ! SS Abmt An the Syndew ctistectect farang tdion, assignment is mainly cleats wilh expressions - The EAXpste SHOW, Can be >tweal, inlegen , cuurey ancl sceconels. Ex: Cowsiclens She reammone 3 idi= E>E)+ Ea EOE ©, E—- Cr) E— id The “fstanslation scume fora she alove grommet is gluen as follows. E Paocluction Rwes} Semantic Action ! S—iclisE $ P= Pookup Ciel-nama); if PAoil then Emit (P=E.place) Fice Esven; Ee, ES 3 Fiplace! = newtemp(); Emit (E place = F)-place's? F,. place) E> ££ 3 E.place :=-nawotemped; Pit (E.place =£)-place'x? Fa.place) =) £ E.place = &.placeg eS i Ps Dockup (tel. Mame) Of REMIT then Emit (P= £ place) | £lse | Coron } ‘Scanned wih CamScanner Produictton Rule Semantic AcHons. GQ) | ESE, ok & { Esplace = newotemp ©); Emit (E-place''=' 2,-place ‘oR’ £p-place) 3 (2) | EE, ANDE, § E.place = newtempl): Exit (Eplace tz” £,-place ‘AND’ &2 place) 3 (3) | E> Nov E, § E-place = mewtempl)3 Emit C£.place'!=) ‘NOT’ £). Place) (A) | E> Cen) § E.place = Fi.phces (S) | Ens ict refop id $ E:place = newtempO)} Emit ('if'ict-place sefop.op ida-place égote! vaxtetale +3) 5 Emit (£.place ="o!) Emit ('golo-'nextstale +2.) Emit C Eiplace t=!) 3 (6) | E> Teve Se.place = rewhempOd; Emit { &-place i=''1') 3 (2) | £- FALSE § E.place t= mato temp: Emit (6 place ':5"'0") Sxplanation!= (1) The Emit function iS used to generate OO the three acleness coca, ancl mewternp() used fo genende dhe Fernportery variables, ida contains te mext_state amd tb moxt three, adelbuss statements in function 7S OQ) The E-= idsefep gives the taclex of ane cutpub Aequence. 69 ‘Scanned with CamScanner Statement thet allen Ha flow of control Tha goto Stalement aliens +e flow of control, dfs we implement goto sinkement Fem we meek, 4o clefine a LABEL for a Atedement . Aas A paeductan can be addled for This pusmporc. - S— LABEL: $ LABEL — id. Sn this producHon aysiem , aemantic. acon ig attached, #& sacar to LABEL anol thy valut in dee Aymbel table. : = S—> if E then S s—> if E thys else S g-5 while E do S$ ) g=> begm L enct SA L—uLus$ L_—s Hene , 8S Ig a dsledernent, L is o Atademank List, A is an adstignment statement and £ is & Boolean valutch exprussim. Bonslation scheme for Slrkment Hat alters fous NN a De ee of Contnl :- eareeries ‘ @ Add a Maker mone tenmined of grammar fen boolean expression . @ This mM is pub befare platoment in beth if they ,elase . In case of whita-ce , pub mi befone mead te Come back to ie offer Mag in Core E ao we executing Ge (3) dn case of if-then-else, if Bwe evaluale £ to be fru. , finsk 9 will he executed. . ‘Scanned wih CamScanner @ After this we shoulel enswm shah Insieack of Becond §, Ha cCocte aften the if-- Han else wilt be executect . then we plact avothor non- termina’ mastken N aftn fist S. The, grammar is ay follows = S— if E thn MS $3 if £ then ms else MIS 3 while ME do MS S— begin L end S33 A Lei Eh putas: Ls Moe Nn OE eas TRe tmemslation scheme for ths geiammot 1S lloeas ! / . Production Rule Semantic Achions Reckpatch(F. true, m. Quacl) Sif E then ms, S.Next = Menge CE-False,S1-Next) Sif E then mS Backpalch (E. Tue 5 My auc) else M282 Backpatch (E:Fadse, Ma-Quad.) S.Nexk = Menge (S1.Next NeNesk, $p+Next) $uwhile ME elo Backpaleh ($1.Next, M1,Quad) M2S, Backpatch (€.Trux ,M_.@uact) S.Next = E. False GEN (golo 1, Quad) S$ begin L evel S-Next = L.Next Sa A S.Next = MIARELIST() LbLjms Backpatch CL}.Next, M.Quad) L.Next = S.Next Los L.Nexk = S.Next Mme M. Quad = NextQuad Qs) ‘Scanned with CamScanner Noe N-Nexb = MakeList (Next Quad) Gen (goto_) sé cogent “Mia ' a 7a), $4,. Ta * ‘Scanned wih CamScanner

You might also like