Docsity

syntax directed translation, Lecture notes of Compiler Design

Partial preview of the text

Download syntax directed translation and more Lecture notes Compiler Design in PDF only on Docsity!

B.Tech 6th Sem Gw CSITG@Allied 7 Unit-3 ; Lecture- 1 ) AKTU Today’s Target » SDT and SDD > Attribute and its types > AKTU PYQs DA) ZY ew Unit-3 Syllabus or Syntax-directed Translation: Syntax-directed Translation schemes, Implementation of Syntaxdirected Translators, Intermediate code, postfix notation, Parse trees & syntax trees, seibacesthiad A three address code, quadruple & triples, translation of assignment statements, Boolean expressions, statements that alter the flow of control, postfix translation, translation with a top down parser. More about translation: Array references in arithmetic expressions, procedures call, declarations and case statements a (SF coucrion RULE | SDD (SYNATX DIRECTE)DEFINITION) | SDT (SYNTAX DIRECTED meen Wa cat = Enat + Fel TRANSLATION) eT ——e IE eS, 2 ; ene are be’ & Fevol es Ert ¢ evel =a vate t vet 5 = ty a) = Tvok Ta Tee — 7 Lived pevah 7 [Ent tEvel ener all TE | eva = dagit-tenvad Tatar {Tels Fa hig ————~ Tak fTvod= Fveh5 4+ 5 F.vVol = lexudl 3 Synthesis Atmbuds Foagrt “e {o od g on Feature SDD (Syntax Directed Definition) SDT (Syntax Directed Translation) Meaning Grammar + attributes + semantic rules Grammar + actions (code) Rules location Rules are written separately from grammar +- Actions are written inside the grammar — — u_—" Focus Describes what to compute~ Describes how to compute Type Declarative / Procedural Use Used in syntax.analysis (like expression Used in trapGlation (like code evaluation) generation) Example E > E1+T, E.val = El val + T.val E >EF1+T { E.val = E1.val + T.val } RaeeSase Btarisash lh 7 \ |G ATTRIBUTE > An attribute is a value attached to a grammar symbol (non-terminal or termirral) that carries semantic information. Attributes help in tasks like: > Evaluating expressions > Type checking > Generating code > Printing postfix gl Synthesis attribute If a node takes value from its children E>@+)P E.val = E1.val + T.val €- Synthesized from E1 and T Flow bottom-up. Inherited attribute > If anode takes value from its parents or sibling. > Flow top-down or sideways. > sed when a non-terminal needs context from outside. A>BC C.in=A.in+1 € Cinherits froomA CaTuwar cLgnees 7% éw tree and annotated parse tree an (2) 43+4)*(5+6)n (AKTU 2017-18) (3)(4*7+1)*2 (4) 1*2*3*(4+5)n 4. 7, Write the SDD for simple calculator and also make parse, 2 UAW ss Ww wVTkF 7 =F 7 Ty F+(—E) —» | Fvak> Fadsgd & vol=Tvak Tol = Wet e Fv ah SFr Evol Fol = dagh-lexvod CaTuwar cLgnees ne aig leaf 5 Eva: <3, des get tend =3 ey AKTU QUESTIONS gaerilt AT#E\F Q.1. | What are two attribute associated with grammar ARTY 2021-22 Q.2. | Consider the following grammar and give the SDD to AKTU 2020-21 construct parse tree for the i/p expression 4*7+3*9 and also construct the annotated parse tree according to your directed definition Q.3 Define syntax directed translation. Construct an AKTU 2018-19 annotated parse tree for the expression (4 * 7 + 1) * 2, using the simple desk calculator grammar. = ® B.Tech 6th Sem Gw CSITG@Allied 7 Unit-3 : Lecture- 2 ) AKTU Today’s Target » SDD Questions > Parse tree >» AKTU PYQs a) ie SDD AND ANNONATED PARSE TREE for binary to decimal conversion 2oduchm kul Sal S.cku = Ld |-lB Mh dur= Dk Lid t BAS =o . = a - Lcko= Bdw elo-2l0 ~ B.dv = oO ) ae Bd = A ¥ a Zw, br P Pee uel Sv: 0 R : Lido-5 Bd 20 6 PAE: 2 Bdvet Ng ave pe votibd Po B.der-0 y } vw ® 7 \ > g Ww cw “construction of syntax tree > Example: Mkleaf("id", "x") creates a leaf for variable 1.mknode(op, left, right) X. mkleaf(num, val > Creates an internal node in the tree ( ) . > Creates a leaf node for a number or constant. with an operator op. S lank dhe Hghk-are the G@ind riodes > num is a label (like "num"), and val is the numeric (subtrees or leaves). value. 2.mkleaf(id, entry) Example: Mkleaf("num", 5) creates a leaf for the > Creates a leaf node for an identifier |} Mumber 5. (like a variable name). > idis a label (like "id"), and entry is the variable or symbol table entry. gi Suter tree for x*y-5+z Py = mk leo Cid, entrg-x } \ Po = mklco} C id, enreg-y) “a P3 = mknode (& » Fig P2 ) 5 fy = mkle Cnuvn 5) 2 ig bs ied I Py) : ih endry2) Pr= mbnodt( +, 05, Po) b- ELE\ ERE EEA | ' 4 y 4 . ~~» Ce a c= EP “oda = Minode (—; Enodk, T: wode ) ie %,% TE-Ndda = VN2k, TE kE node Mmknode(k | Ty. nods | F woate) + : 1A \ F-nody = meat C \n identry ) Rus F. node = MeleAL_Cyum rials) —— — = "De | Pa