Download as pdf
Download as pdf
You are on page 1of 105
— UNIT It Syntax Directed Translation and Intermediate Code Generation Syllabus Symtax directed Definitions-Construction of Syntax Tree-Boutom-up Evaluation ¢ Definitions- Design of predictive translator - Type Systems-Specification of 0 Equivalence of Type Expressions-Type Conversions Intermediate (dress Code, Types and Declarations, Translation of Expressions, Type Contents 34 Syntax-directed Definitions May-15, Dec. Marks 8 2.2 Construction of Syntax Tree May-16, Dec-18,22, Marks 12 23 Bottom-up Evaluation of S-Attributed May-18, sharks 7 34 L-Attributed Definition Dec.-18, Marks 12 35 Design of Predictive Translator May-19, Marks 7 46 Type Systems 37 Specification of a Simple Type Checker May-12,16, Dec.-14,17, 48 Equivalence of Type Expressions 39 Type Conversions 310. Intermediate Languages May-07, 11, Marks 2 Dec.-06,07,11,12, May-11, Marks 2 3.11 Forms of Intermediate Language 212 Three Address Code Dec.-05,09,11,13, May-11,13,15, Marks 16 Types of Three Address Code May-12, Marks 8 Declarations a Compiler Design Er) Syntax Directed Transtation and Intermediate Code Generate, 419° Transtation of Expressions Deo.-11, May-11,13,14, Marks § 410 Arrays May-10,15, Dec.-09,10,14, Marks 16 $17 Boolean Expression May-06,11,12, Dac.-09,10,11,12,13,14,22, Marks 16 J16 Backpatohing May-07,08,10,11,13,14,15, Dec. -06,09, 10,12, Marks 16 319 Two Marks Questions with Answers compiler Design 3-3 Syntax Directed Translation and Intermediate Code Generation E&I syntax-directed Definitions CURLS While doing the static analys is of the language we use syntax-directed definitions. That means an augmented context free grammar is generated. In other words the set of attributes are associated with each terminal and non-terminal symbols. The attribute can be a string, a number, a type, a memory location or anything else. The syntax-directed definition is a kind of abstract specification. The conceptual view of syntax-directed translation can be as shown in Fig. 3.1.1 Input stri ‘Syntax Dependency et Fig. 3.1.1 Syntax: irected translation Firstly we parse the input token stream and a syntax tree is generated. Then the tree is being traversed for evaluating the semantic rules at the parse tree nodes. The implementation need not have to follow all the steps given in Fig. 3.1.1. In single pass implementation semantic rules can be evaluated during parsing without explicitly constructing a parse tree, or dependency graph. In such semantic evaluation, at the nodes of the syntax tree, values of the attribute are defined for the given input string, Such a parse tree containing the values of attributes at each node is called an annotated or decorated the parse tree. Let us see the form of syntax-directed definition. Definition : Syntax-directed definition is a generalization of context free grammar in which each grammar production X—> 0. is associated with it a set of semantic rules of the form a:= f(b1,b2,...---b,), where a is an attribute obtained from the function f ‘Attribute ; The attribute can be a string, a number, a type, a memory location or anything else, Consider X— a be a context free grammar and a:=f(bj,b2,...-b,) (where a is the attribute. - _ | Then there are two types of attributes 1. Synthesized attribute : The attribute ‘a’ is called synthesized attribute of X and by bp, ......by are attributes belonging to the production symbols. ‘The value of synthesized attribute at a node is computed from the values of attributes at the children of that node in the parse tree. called inherited attribute of one of the 2. Inherited attribute : The attribute ‘a’ grammar symbol on the right side of the production (ie. @) and by, by,.....by are belonging to either X or @ - The inherited attributes can be computed from the values of the attributes at the siblings and parent of that node. ® TECHNICAL PUBLICATIONS™ - an up-thrust for knowledge SOomprer eosin wee heat dette 1. Synthesized attribute Let us see how to compute synthesized attributes Example : Consider the context free grammar as, S > EN E+T E-T Tj T'F T/F F (E) amd aadiaa nm HlutbLu vou digit N> The syntax-directed definition can be written for the above grammar by using semantic actions for each production. Production rule | Ss > EN - | | E > &4T wval+Tal | E 3 £,-T Ewval:=E,.val-Tval | Eee eee Eval=Tyal T - TF Twak=T;.val x F.val | T > T/F Tal:=Ty.val/F.val T > F T.val:=F.val | Fos @ Fvak=Ewal Fo > digit F.val=digit Jexval N@ -: Can be ignored by lexical analyzer as jis ——— Z terminating symbol. | TECHNICAL PUBLICATIONS® - an up-inrust for knowledge exical analyzer. In thi lexical analyzer. In the rule S > EN, symbol $ is the start symbol. This rule is to print the final answer of the expression, + In syntax-directed definition, terminals have synthesized attributes only. + Thus there is no definition of terminal. The synthesized attributes are quite often used in syntax-directed definition, The syntax-directed definition that uses only synthesized attributes is called S-attributed de * In a parse tree, jon, at each node the semantic rule is evaluated for annotating (computing) the S-attributed definition, This processing is in bottom up fashion i.e from leaves to root. Following steps are followed to compute S-attributed definition. 1. Write the syntax-directed definition using the appropriate semantic actions for corresponding production rule of the given grammar. 2. The annotated parse tree is generated and attribute values are computed. The computation is done in bottom up manner. 3. The value obtained at the root node is supposed to be the final output. Let us take an input string for computing the S-attributed definition for the above given grammar. Construct parse tree, syntax tree and annotated parse tree for the input string is5*6+7; Solution : Refer Fig. 3.1.2. (Refer Fig. 3.1.2 on next page) For the computation of attributes we start from the pose bottommost node. The ule F — digit is used in order to reduce digit to F. The semantic action that takes place here is Eval: = digit.Jexval. The value of digit is obtained from lexical analyzer(here Parser invokes the lexical analyzer to get the token value) which becomes the value of F. Hence F.val=5. Since T is the parent node of F and semantic action suggests that T.val = F.val. We can get the T.val=5. Thus the computation of S-attributes is done from children. Then consider T > T, * F production; the corresponding semantic action is T.val = Ty.val x F.val. Hence Twal = Ty.val x F.val = 5*6=30. Similarly, The combination of E).val + T.val becomes the E node. TECHNICAL PUBLICA TIONS® - an up-thrust for knowledge Compiler Design i (0) Syntax ree from child ;“ <= ‘ mr | ee Tvel=30 eval=7 Tails + Fa=8—dton pxal=5— dgtiowal=6 dotlenal=5 (c) Annotated parse tree Fig. 3.1.2 Computation of S-attributed definition Ewal = E,.val + T.val = 3+7 Eval = 37 swe Here we get the E}-val from left child of E and T.val from right child of E- Finally © acquire the value of E as 37. Then the production § > EN is applied to reduce i 37. Then rule N-; indicates termination of the current expression. The semantis ype associated with $ > EN suggests us to print the result E.val. Hence the outPUt ™ 37. Thus S-attributed definition can be computed by a bottom-up fashion postorder traversal. y TECHNICAL PUBLICATION: an up-thrust for knowledge iss Compiler Design 3-7 ‘Syntax Directed Translation and intermediate Code Generation Construct a decorated parse tree according to the syntax directed definition for the following input statement : (4 + 7,5 * 3) /2 Solution : We will write the production rules and Semantic actions as follows |___ Production rule ‘Semantic actions | SoE : print (E.val) BS E+T Eval: = Ey-val+ T-val | EST Eval : = T.val | T+T*P Tal: = Ty-val* P-val_ | ToP Tal : = P.val P+ P/F wal : = Py-v | / Piva : = Py-val/F-val | P.val Eval : = 13.25 1 cia as Pyal: = 13.25 Tal :=4 ZN i Tval Fal Fval:=4 i 1 ! Fval:=7.5 — digit.lexval digital lexval i digit lewal : = 7.5 Fig. 3.1.3 Decorated parse tree GRERREEE) Give « syntax directed definition to differentiate expressions formed by ‘applying the arithmetic operators + and * to the variable x and constants; expression xe(Gexex¢x. CES — © TECHNICAL PUBLICATIONS® - an up-thrust for knowledge Compiler Design 3-8 syntax Directed Translation and Intermediate Code Cera Solution : The syntax directed definition can be as given below : Production rule Semantic action Print (E.val) SE ESE+T Eval : = Ey -val+ T- val EST Eval : = Tal T3T'F Tal : = Tj-val* F-val TSF Tal : = Fval Fo(E) Fival : = E.val F - digit Fal : = digit-lexval Foid Fal : = id.entry The annotated parse tree can be created as follows : E.val : = Print (x * (S*x + x*x)) | Teal : = xe(8ex + xx) Eval Tal val # Fal: =x a7 | | Tal Fval Fval:= — id.entry: =x | | | Fal identry:=x — identry : =x | digitlexval : = 3 Fig. 3.1.4 GEER) 4 syntax-directed translation scheme that takes strings of a’s, b's and ©3* input and produces as output the number of substrings in the input string that corre to the pattern a(3%) c+(%,) b. For example the translation of the input 8 “abbcabcababe” is 3 1) Write a context free grammar that generate all stings as a’s, b's and o's 2) Give the semantic attributes for the Srammar_ symbols. 3) For each production of the grammar present a set of rules for evaluation of the attributes. re TECHNICAL PUBLICATIONS” a” Up-thrust for knowledge - compiir Design 3-9 Syntax Directed Translation and Intermediate Code Generation solution : 1) The context free grammar G is a set denoted as G =(V, T, P, S) where V = Set of non-terminals = {S} T = Set of terminals = (a, b, c} S is a start symbol, P is set of production rules. which are as given below - S— Sa S— Sb S— Se Sra Sab Soc 2) If any string is parsed using above grammar then we will get left skewed tree. The grammar is left recursive. For semantic attributes we will use synthesized attributes for non terminal symbol S. These attributes are - al - denotes number of a's left to given "c" character. a2 - denotes number of a's right to given "c" character. total - captures the total number of substrings. We need to count number of a's to the left of a "c" character and to the right of that character so that we can add value of al to the running total for each occurrence of a b "c" character which recording the value of a's to the right of "c" character to the right of "c so that when we find new "c" we copy the value of the 's that were to the right of the first "ce" and which are now to the left of the second "c’. 3) The set of rules for evaluation of semantic attributes is as given below - $7 S2a Sy-al = Sp-al+1 a2 = Sy-a2 oo $)-total S, > S2b Sy-al $1-a2 = Sp Sy-total = Sp-total+ $5.02 Production Rule | Semantic Rule | TECHNICAL PUBLICATIONS® - an up-thrust for knowledge total spal=1 $1-a2=0 oie totale 0) eee Inherited attribute The value of inherited attribute at a node in a parse tree is defined using the attribute values at the parent or siblings. Consider an example and let us compute the inherited attributes. ‘Annotate the parse tree for the computation of inherited attributes for the given string : int a, b, c; the grammar is as given below. S3TL T = int = float 4 char double Ly id pid crore aa Lua For the string int a, b, c we have to distribute the data type int to all the identifies * b and c; such that a becomes integer, b becomes integer and ¢ becomes inte Following steps are to be followed : 1. Construct the syntax-directed definition using semantic action. . ot 2. Annotate the parse tee with inherited attributes by processing in top-down (= TECHNICAL PUBLICATIONS® . an yp-anust for knowledge compserDesgy ST Syntax Directed Translation and Intermediate Code Generation The syntax-directed definition for the above given grammar is, Taypersfloat char T.type:=char > double T.typersdouble Ly.ins= Lain Enter_type( id.entry, L.in) Enter_type( id.entry, L.in) - 3.1.5. Value obtained Lin = int from sibling 4 | NX he, © Value obtained /” from parent {i ‘to child 1 \Lin = int Fig. 3.1.5 Annotated parse tree The value of L nodes is first obtained from T.type(sibling). The T.type is basically lexical value obtained as int or float or char or double. Then the L nodes give the type of the identifiers a, b and c. The computation of type is done in top-down manner or in Preorder traversal. Using function Enter_type the type of identifiers a, b and c is inserted in the symbol table at corresponding id.entry (The id.entry is the address of corresponding identifier in the symbol table) TECHNICAL PUBLICATIONS® - an up-hrust for knowledge Compiier Design Sr. No. Synthesized translation Inherited translation | 1 The attribute ‘a’ is called synthesized The attribute ‘a’ is called inherited attribute of X and by, bs, _. by are attribute of one of the grammar symbol attributes belonging to the production (on the right side of the production and symbols. When x ais a rule associated by, ba, .. by are belonging to etther X oe with a: =f %(by, by, .. by) production @ When x ais 2 ruk 22 associated with 2: = f "1 In synthesized translation the synthesized In inherited translation. the inherited attributes are computed attributes are computed The synthesized attributes are computed The inherited attributes are computed using values of children. using values of parent and sibling nodes 4 Information is passed up in syntax tree. Information is passed down in syntax tree. 5. Computing synthesized attributes is Computing inherited attributes is simple. complex 6 Example : Example : sO Eval=5 + Tval=7 Type = inte Lin = int | i i Fval=7 int Lin = int > | | digit lexval = 7 a = flay, a2, a5, ...,a,) where f denotes a functionality * Each attribute has a domain of possible values. * An attribute may be assigned values from its domain durin 1g Parsing. + an up-thnust for knowledge TECHNICAL PUBLICATIONS’ Syntax Directed Translation and Intosniodiate Code Generation’ 4 The attributes can be evaluated in assignments or conditions There are two types of attributes 1) Synthesized attributes 2) Inherited attribute an example of synthesized attribute Production rule Semantic action So EN Print (Eval) E>R+T Ewal = y.val4 Toval EST Eval = Tal T= digit Tval = digitlevel Nai ignore 3. Dependency graph ‘The directed graph that represents the interdependen jnherited attributes at nodes in the parse tree is called dependency graph between synthesized and For the rule X>YZ the semantic action is given by Xx := f(Y.y, Z.z) then Synthesized attribute is Xx and X.x depends upon attributes Yoy and Z-2- Algorithm for Constructing dependency graph for (each node n in the parse tree) do { for (each attribute a of grammar at node n) do { for the attribute a, build a node of dependency graph. // Constructing all the nodes of graph. } } for (each node n in the parse tree) do { for (each semantic rule b : = f (C1, Cx 4) which is associated with productions) do for (i:= 1) tok) do Consturct edge from node ¢, to b } GEETERED Design the dependency graph for the following grammar. E> E; + E> TECHNICAL PUBLICATIONS an up-thrust for knowledge tion and Intermediate, “, syntax Dred Trensie0e” ® a gO hove grammar is as given below a al Solution : The semantic rules for the Semantic rule Production rule Or Eas, Eeval=B; eval+Ez eval 1 a aval:=E, eval X E> EE El , The dependency graph is as shown in Fig. 3, "Eeval Eval + Ezwval Fig. 3.1.6 Dependency graph The synthesized attributes can be represented by «val. Hence the < attributes are given by E «val, E; «val and E, «val. The dependencies among # given by solid arrows. The arrows from E, and E show that value of E der E, and E>. We have represented parse tree using dotted lines. GEEEEIEERED Design the dependency graph for the following grammar. S— T List T= int T = float T~ char T = double List + List,, id List + id Solution : The dotted line is for representing the parse tree The semantic rules for the above grammar is as gi belo ‘Semantic actions T.typevefloat Tiypesschar T.typer=double List in=List.in Enter_type( id.entry, List.in) Enter_type( id.entry, Listin) "From sibling From sibling Fig. 3.1.7 Dependency graph The dependencies among the nodes can be shown be solid arrows. In the above drawn dependency graph how the values can be inherited from the parent or sibling node is shown clearly. Hence the name for the attributes is inherited attributes, 4. Evaluation order The topological sort of the dependency graph decides the evaluation order in a parse tree. In deciding evaluation order the semantic rules in the syntavdirected definitions are used. Thus the translation is specified by syntax-directed definitions. Therefore the Precise definition of syntax-directed definition is required. © TECHNICAL PUBLICATIONS® - an up-thruat for knowledge oo. Compiler Design 3-16 Syntax Directed Translation and Intermediate Code Genera, _rtistin = int @ Oe" 3.1.8 Evaluation order The evaluation order can be decided as follows. 1. The type int is obtained from lexical analyzer by analyzing the input token 2. The List.in is assigned the type int from the sibling T.type. 3. The entry in the symbol table for identifier ¢ gets associated with the type int Hence variable ¢ becomes of integer type. 4. The List.in is assigned the type int from the parent List.in. . The entry in the symbol table for identifier b gets associated with the type Hence variable b becomes of integer type. 6. The List.in is assigned the type int from the parent List.in. The entry in the symbol table for identifier a gets associated with the type itt Hence variable a becomes of integer type. Thus by evaluation the semantic rules in this order st oe table entry for each identifier a, b and c DT melee) 1. Write short note on - S-attributed definitions ores the type int in the 2, Write short note on - Dependency graph TECHNICAL PUBLICATIONS® . an up-nrust for knowledge > vor D084 a4 compton Denn 7 Syntax Dnocted Transition and ilormediate Code Generation P.2] Construction of Syntax Tree CLO the grammar considered for the expression is, E> E+T E4 &-7 E> EST = («T+ T > id > mum f=I_ 1 Constructing syntax tree for an expression means translation of expression into postfix form. The nodes for each operator and operand is created. Each node can be implemented as a record with multiple fields. Following are the functions used in syntax tree for expression. 1. mknode(op,left,right) : This function creates a node with the field operator having operator as label, and the two pointers to left and right. 2. mkleaf(id,entry) : This function creates an identifier node with label id and a pointer to symbol table is given by ‘entry’. 3, mkleaf(num,val) : This function creates node for number with label num and val is for value of that number. CEIEEERD Construct the syntax tree for the expression xxy-5+2. Solution : Step 1: Convert the expression from infix to postfix xyt5-z+. Step 2: Make use of the functions mknode(),mkleaf(id,ptr) and mkleaf(num,val). Step 3: The sequence of function calls is given. Postfix expression xy'5 ~ 2+ Symbol Operation Dox py=mkleaf(id, ptr to entry x) | y p= mkleaflid, ptr to entry y) hue! Pa=mknode(", Pr-P2) © TECHNICAL PUBLICATIONS” - an up-thrust for knowledge ——= Compiter Design 3-18 Syntax Directed Translation and Intermediate Code Ge, pax mkleaf(num,5) ps=mknode(-, Ps-P4) Pe= mkleaf(id, ptr to entry z) py= mknode(+, P5,Pe) Pointer to Pointer to Symtab symtab for x fory Fig. 3.2.1 Syntax tree The syntax-directed definition ‘or the above grammar is as given below. Production rule Semantic operation ES E+ T — Enptr:=mknode(’+’,£,.nptr,T.nptr) E~E,-T E.nptr:=mknode('~’E, nptr,T.nptr) E> E,*T aes T. Tid E.nptr-=mkleaf{id id.ptr_entry) E T~ num ‘T-nptr:=mkleaf(num,num.val) TECHNICAL PUBLICATIONS” - an up-thrust for knowledge compen Desion ea Syntax Diroctod Translation and Intormadiate Code Generation Eenptr Eenpte 4 ne 7 ' ro Tenpir ' Tonptr ) ' ' num) AH oft , : ern | ar Tenptr ) entry for ' 7 Zz id! 1 Ca ig Ty Y entry for y Fig. 3.2.1 (a) Constructed syntax tree ‘As we have seen that in the function calls the pointers to various nodes are generated. Such pointer are p;,P2,P3 and so on. A synthesized attribute nptr for E and T is used to keep track of these pointers for the nodes E and T. Thus we get nptr, ptr_entry, val as synthesized attributes. (EEEIEEED construct a syntax directed definition for constructing a syntax tree for assignment statements. Sid: =E E+E1+ E2 E+ El + £2 Es-E1 Es (ED Eid ion (SDD) is as follows : - i 7] Semantic action | Solution : ‘The Syntax Directed Defin id: = E. nptr F. nptr = mknode (+, EL nptr, E2.nptr) Compiler Design 20 E : Ele Eee oo : Sof Enptr : = mknode (~', EL nptr + (El) iat E + id CESEEIRED) tops the Scattributed definition expression grammar involving only the binary operators + and -. As us: constructs syntax trees for these operator. are at the same precedence level and are jointly left associative. All non-terminal have on synthesized attribute node, which represents a node of the syntax tree. Production : E> Ey +T, E->T, T -+(E} T > id/num AU :: De Solution : Refer example 3.2.2 for SDD. Consider expression a + 10 - C. for w a syntax tree can be drawn as follows + Tenptr S Tenptr is ante : J num {Tenptr id Fi r ’ i , i. | num: 40 to entry for a Fig. 3.2.2 Syntax tree fora + 10—¢ TECHNICAL PUBLIC of code, Show the symbol tables created Procedure aC) int a, b, float c; procedure b() int a; float c; procedure c() i int d, ! i Sol. : 1) In procedure a( ), we use following semantic action var_decl > var_type id; (declar_id} var_type > int] float {int-type or float-type} id + alble lid} when we process int_type and id we can pass them to declar_i 2) In procedure b( ), we follow the above process for assigning int_type to variable a and float_type to variable c. 3) In procedure c( ), we assign int_type to variable d. EE] Bottom-up Evaluation of S-Attributed ssed how to use syntax-directed definitions to specity: translatior We have already di Now in this section we will discuss how to implement syntav-directed translatic scheme for the syntax-directed definitions. Hence a translator is built. The task ot TECHNICAL PUBLICATIONS® - an up-thrust for knowiedye 3-22 Syntax Directed Translation and Intermediate Code Generation Compt Design 3-2 : building translator for any arbitrary syntav-directed definition is very difficult. However, puilding translator for any a tl ‘ mplish this task there are large classes of syntax-directed definitions for which jt is easy to construct translators © Sattributed definition is one such class of syntax-directed definition with synthesized attributes only * Synthesized attributes can be evaluated using the bottom-up parser. * The purpose of stack is to keep track of values of the synthesized attributes associated with the grammar symbol on its stack. This stack is commonly known as parser stack. Synthesized Attributes on the Parser Stack 1 N translator tor S-attributed definition is implemented using LR parser generator \ bottom up method is used to parse the input string. \ parser stack is used to hold the values of synthesized attributes The stack is implemented as a pair of state and value. Each state entry is the Pointer fo the LR (1) parsing table. There is no need to store the grammar symbol 'mplicitly in the parser stack at the state entry, But for ease of understanding we will refer the state by unique grammar symbol that is been placed in the parser Mack. Hence parser stack ean be denoted as stack{i. And stack{i] is a combination of state[i] and value[i] For e example, for the production rule X ABC the stack can be as shown in Fig. 3.3.1. Production : X + ABC. top ~— Tees Before reduction After reduction Fig. 3.3.1 Parser stack stack is. The top symbol on the pointed by pointer top Semantic action Xx = f(AaBbCo Se ee TECHNICAL PUBLICATIONS® an up-tnust for knowledge semper D8st” 3-23 compe Syntax Directed Translation and Intermediate Code Generation Before reduction the states A, B and C can be inserted in the stack along with the Jues Aa, Bb and CeThe top pointer of valueltop] will point the value Cc similarly B.b is in value[top — 1] and A.a is in value[top — 2]-After reduction the eft hand side symbol of the production ie. X will be placed in the stack along with the value X.x at the top. Hence after reduction value[top] = X.x- 4. After ee top is decremented by 2 the state covering X is placed at the top of state[top] and value of synthesized attribute X.x is put in value[top]. 5. If the sy mbol has no attribute then the corresponding entry in the value array will be kept undefined. . For the following given grammar construct the syntax-directed definition and ate the code fragment (translator) using S-attributed definition. S > EN gen EO E+T > E-T oT 3 TF T/F oF = (E) naa damm 1 = digit N7>; Also evaluate the input string 2+3+4; with parser stack using LR parsing method, or Gite syntax directed definition for desktop calculator. Solution : © The syntax-directed detinitio a for the given grammar can be written as follows, ———_—_—_—S__ — Production rule Semantic actions S Print(E.val) $—> EN | E3E,+T Eval-E,valeTval | 1 E>E,-T Eval-Eyval-Tval oO Be: es ~ © PUBLICATIONS TECHNICAL an up-thrust for knowledge tse Design: 3-24 Syntax Directed Translation and Intermediat Compiler - “ te Code Generation — E oT Eval:=T.val To TF Tvak=Ty.val X F.val Ss - T+ 1,/F Twal:=T;.val/F.val ToF Tvak:=E.val feces ee - F>@ Fval:=E.val F digit F.val:=digit-lexval Se Sk a a No; Can be ignored by lexical analyzer as ; is terminating symbol. The LR parser table can be generated, To evaluate the attributes the code fragment can be generated by using the parser stack, The appropriate reduction of each production and corresponding code fragment is as given below. Code fragment SEN Print(value{top)) value[top}-=valueltop-2}-valueftop] value|top|:=value{top-2}*value{top] value{top|:=value(top-2]/valueltop] value{top]:=valueltop-1} an up-thrust for knowledge - compier 008i? Bee Syntax Directed Translation and Intermediate Code Generation |The sequence of moves made by the parser for the input 2-344; are as given below. E — Input string State Value Production rule used 24; | |: eae; : : | +304) | " : 2 F > digit Eo T 2 To | au; Ts A 4; T+3 2-3 4; T+ F 2-3 F > digit | M4: 1 6 ThE “ : 6 BOT 4 Ee 6 | : B44 6-4 ; E+4 6-4 | 3 E+F 6-4 F > digit | E+T 6-4 Tor : FE 10 E+E+T | E; 10 | EN 0 10 SEN | On seeing the first input symbol 2, initially the symbol 2 is recognized as digit and the parser shifts F in the state stack. F corresponding to digit and the semantic action Eval = digitdexval will be implemented and the valueltop] becomes = 2. In the next ® TECHNICAL PUBLICATIONS” - an up-thrust for knowledge - Transianon and Intermediate Code 6. Comper Design 3-26 Syntax Orected T Sereratce Move parser reduces by TF. As no code fragment is associated with ws Production +. valueltop] and stateltop] is left unchanged. Continuing in this fashion the evaluation the input string is done and the parser halts successfully when it reaches to stateftop] ~ < the start sate. In this way the bottom-up evaluation of S-attributed definitions is done. GEEEIIEED) rites ~ attributed grammar to convert the given grammar with omy operators to prefix operators. LOE ESE+T, ESE-T.EST, TST+hTOVETSF FOFTP, FP, P(E), Pid Solution : The grammar that contains all the syntactic rules along with the s ules having synthesized attributes only, is called $ - attribut grammar for converting infix operators to prefix Production Rule | LoE Lval:=E- val | EsE+T E-val: = “+ E-val T- val | Ese-T E-val: =°-"E-val T-val | Est E-val: = T-val | T>T+F T-val:="*°T- val F-val at Tival: 2°) T-val F val | TF T-val:=F val | FoFTtpe Feval:=°7°F val P val | FP F P-® P Paid P Consider the following grammar G E's E E> Eta: ng action of Give the pars @ bottom up parser for the Syntax Dwrected Translation and intermediate Code Generation tax directed translation for given CRG will be as follows aa Code fragment | ae Print (value [top]) | EE+n value {top}: = value [top - 2] + value [top] | ortuction -d| Input string State Value Production rule used ay, . a | E 5 bon | ee Ee as es eee i (EEBIEED) construct a syntax directed translation scheme that translates arithmetic expressions from infix into postfix notation. Using semantic attributes for each of the grammar symbols and semantic rules, evaluate the input :3* 4+ 5° 2 Solution : The syntax directed translation for infix to postfix conversion of expression for given grammar = E>E+T print (+) EsE-T {print (-)] Es=T {} T > id {print (‘id’)) SDT for evaluation of expression for given grammar LoE {print (E- val)} E+E+T {E, -val = Ey -val+ T-vall E> T {E-val = T-vall TaT+F (1, val = Ty -val*F-vall TF (T-val = F-vall Fs id [F-val = id -lexvall TECHNICAL PUBLICATIONS - an up-thrust for knowtedge nD WOrmectate Ce fon arn Gon cted Translator "ten syntax Directed TranSot ign 3 _ Compiier Desig sue stack for evaluation along. with sym To evaluate the expression we U6 Wve translate the input expression 3+ 4 , ng. Using, spoT we oe Which is used for parsing : above md i Syntax action Semantic rule in Symbol stack Value stack — Input string 525 Shift 304 45#28 : : F val = id-va 145028 Reduce F id F val = a T-val = Feva 115028 Reduce T> F a st 8 s445028 Shift st 8 shitt . s = id-va +5025 Reduce F > id F val = id val steid sted 1 Fo T-val = T-val 3 Reduce TT F ~ “ a * Feval ST siz +5425 Reduce E> T E-val = T-val Ss si2 +5425 Shift oa si2+ shift ho S12+5 +28 Reduce F > id F- val = id-val wer $12+5 Reduce TF T-val = F-val she $1245 22s aon [eT sizes 3 Shitt pees $ Reduce F id Feval = id val j Sree _sizesee s Reduce T- T*F T-val = T vale F she $124.10 $ Reduce E> E+ TB val= yale Tw Ls $22 $ - . Reduce L Hence as an output 22 will displ EX] L-Attributed Definition The syntax-directed definition can be rule A > X)X5--.Xy where the inherited A X:Xp...X) is such that, Print(E - val) layed defined as the L: -attributed for the prod attribute X, is such that 1 PQ P.in := p(A.in) L-attribute | Lattribute | Qin:= g(P5y)—ateribute Asy= f(Qsy) | A >XY Lattribute | Because here value of Not L-attribute fe symil 00) is ee Jependent upon be value of right symbol (ie ¥) This ic because of the definition X.in:=x(Y.sy). This semantic action suggests that calue of X.in depends upon value of Y.sy. That means value of left symbol is dependant on the value of the right symbol. This violets the 1 rule of the L-attributed definition. [Logically also while parsing the scan is done from left to right and not from right to left!!] Thus X.in -(Y.sy) is not L-attribute. Explain why every S-attributed definition is L-attribte. Solution : Following are the restrictions that can be applied on inherited attributes of X, where 1TP | | To Iprint(‘0°)) | T>1 {print(1)] | T32 tprint(29) | T33 tprint(3y) | | T34 prints) | T35 tric) | To 6 Iprint(6) \ ee tpriate7 1 | | TW78 Iprint('8) | > on | P— +TPle Iprint(’'+)) Pe The annotated parse tree for the string 2+6+1 is as given in Fig. 3.5.1. The translation scheme proceeds using depth first traversal. The annotated parse tree shows the semantic actions associated appropriate node. Thus this tree shows the translation scheme for mapping of infix form to post actions can be shown using dotted lines: form. The associated semantic © TECHNICAL PUBLICATIONS” - an up-thiust for knowledge ation Compiler Design 3-34 Syntax Directed Translation and Intermediate Code Gene Ee , aN 2 print(2) oT * PS ¢ int (+) pri P 6 print ('6') A # J print (+) Np / | print ("1) Fig. 3.5.1 Annotated parse tree Guideline for Designing the Translation Scheme While designing the translation scheme - * We have to follow one restriction and that is for every semantic action if it refers to some attribute say x then that attribute value (i.e. value of x) must be computed before that attribute x gets referred. * Computation of synthesized attribute is very easy, The synthesized attribute can be computed as follows : Production Rule Semantic Action E+EeT ( Eval:=E, val x T.val } Here val is synthesized attribute, * For the computation of both the inherited 1) In any semantic action the computation of inherited attribute for the eymbol on th right side of the production must be done before that symbol. That moame if thet? is a production A>XY then computation of X.in must be done already. ii)In the semantic action there should not be attribute of the symbol that is right. semantic action {By.s:~B,,5} and synthesized attributes any reference to the synthe That means for the production A > BiB; fo is invalid as computation of attribute for B, is bas! °° computation of the attribute of B, iii) For a comput, rst do ation of synthesized attribute for a non-terminal on the left! all the computations of the attributes that ref ‘ing the computation of all such attributes is placed end of right side of the production pically th fer this non terminal. Typic? semantic action con at the Such a design of translation scheme helps for the implementation of the syntardie definition. TECHRICAL PUBLICATIONS” ~ an up-innust for knowledge compton DOs? SYNE Diver tO Lranatation arid Intermediate Conte Ganeration mD Explain the translator py Process woin, Hable ex le RURIRRIR*N (Ral i hr Ro ator process in | | Production rules Semantic rules Ro>R'I'R (Ro vals = Ry val’ | Ry val) | | R-» RR (Ro vals = RyvalRy val} | R+R* (Reval: © Real's} R= (R) (Reval: = Ry-val} Roa (Reval: =a) | Rob (Reval: =b} + The annotated parse tree for albe will be R a R,val I “Reval b © Fig. 3.5.2 Annoted parse tree mu nmar can be converted into a translation scheme 1. Explain in detail how an L-attibuted gram ributed grammar into translated scheme 2. Explain the steps involved in converting an (attributed grammar ppropriate example 1. Describe syntax-directed translation schemes with appropriate examp CO ° just for kno TECHNICAL PUBLICATIONS” an up-thwust for knowledge ~~ Coy Serer, Type Systems Compiter Design 336 Syntax Duected Translation and intermedioys The type analysis and type checking is an important activity done im the analysis phase The need for type checking is any 1) To detect the errors arising in the expression due to incompatible operand 1) To generate intermediate code for expressions and statements yPically tang Supports two types of data types - basic and constructed. The basic data types are - Integer, character, real, Boole. And Arrays, record(structure), set and pointer are the data types are ‘an, enumerated g lata 4 onstructed types. The conc build using basic data types Tt Parse Parse Source tree nena a Parser a Program generation Fig. 3.6.1 Role of type checker Type Expression * The systematic way of expressing type of language construct is called type expression. Thus the type expression can be defined as - 1) The basic type is called type expression. Hence int, char, float, double, enum are type expressions. 2) While performing type checking two special basic types are needed such as type ere and void. The type_error is for reporting error occurred during type checking and void indicates that there is no type (NULL) associated with the statement. 3) The type name is also a type expression, For example - typedef int *INT_PTR This statements defines the type name INT_PTR as type expression which is actual a integer pointer. 4) The type constructors are also type expres product, struct, product, pointer, function, * Fach of these type expressions are explained with the i) Array - An array (I,T) is a type expression where integer and the data type of the ions. The type constructors are ar) help of an example as fle Tis an index set which is usual array elements is given by T. The type expressiot array (LT) For example - int arr{20); The type expres ion for an array ‘arr’ having the elements from 0, L198 array(0,1,..19,int) TECHNICAL PUBLICATIONS” an up-thrust for knowledge era campiier Dest” aa? Syntax Dirsetod Hraristation and Intermediate Code Generation ji) Product - The type expression tor the cart lesian product is given by TE 4 12 where py and T2 are the Owe data types Always = iva med to be left associative: ji) Struct = The structure given by keyword atr {in abo a type expression, The Hal (4 gructure is applied asa product of the type af the such a product is a type expression in struct bers) of the structure For example struct stud { char name|10}; float marks; } struct stud student/10}; Here the type name is stud having, the type expr sion as struct (name x array(0,1...9,char)) x (marks float) Thus the type expression for student is given as array(0,1,...9,stud) iv) Pointers - The type expression for pointer is given as pointer(T) where T a data type. For example - float *xyz; then type expression for identifier xyz is given as : pointer(float) v) Function - The type expression for function is given by domain-range. In the sense that the type expression for function is D> R. For example - int sum(int a,int b) The type expression for sum is - int x int — int * The type expression can be represented using, tree or DAG (Directed Acyclic Graph) Let us take an example for the same int maxi(int a,int b,float *c) TECHNICAL PUBLICATIONS” - an up-thrust for knowledge ae ton and intermediate Coue gg 7 Srnec on oe Cn Sng Comptes Design 3 The type expression for function maxi is - Pointer Pointer | Z | float int int float int (a) Tree representation (b) DAG representation (a) Tree Fig. 3.6.2 A type system is a colle ction of rules for assigning type expressions to the language constructs. The type sy: stem is implemented by type checker. The type checker whether the types are compatible to each other or not by finding the corresponding expressions. 1. What finds type do you mean by type expression ? Specification of a Simple Type Checker In this section we will write Janguage construct the identifier. 4 type checker for the type of the identifi The type checker is a transl from the type simple language construct. In this \ “F must be declared before the use of the "scheme in which th, ained. The type and functions, type of each expressio” es of subexpressions is obt checker can decide the types 4YS, pointers, statements Let us consider a grammar for source language. S4 Ds D> DDIT List Vo char tint) Aoat MIST sy tnumy 4 “Ly lia Vos Iterat mum tid ty mod B | BIEL 1 eye op E LEU) The pe checker enstires following things # Bach vtenttion must be sleclared before the use, Hi) The uv ,, Hentitior must he within the seop. -_ comer Dasiyn 190 Syntax Ouected Translation and intermediate Code Generation must noth, An identifier ns at a time within the same multiple definit scope Phe given grammar has basic data types as char, int, float and for reporting, the errors type error We assume that the index of the array start at 0. For example if int arr{100}; vads to type expression as array(0,1...99, int) similarly int *ptr, The type expression for the above statement can be pointer(int) The constructor pointer is applied to the type integer. Let us write the translation scheme for the above grammar. Production rule ‘Type expression SDF This means all declarations before expressions Do» TLSt LIST typeT type LIST + id laddlypetid entry LIST.type)h j Type checker 1 -+ char TAypes=chart nt (Ttypeint) | (Ttype-float) LIST +L {Lj Aypes pointer(LIST.type)| LIST + Lj{num] {Ly type:-array(0...numval-LLIST type} This translation scheme is given for declaration of identifiers. For the rule D sf LIST the data type is saved in the symbol table entry for identitier, For adding the entry of type for the identifier in’ the symbol table the function addtypetid entry, LIST.type) is used. If T.type=int then the type of that identifier is int or {Tiype-char then the type of that identifier is char and so on. For the declaration ot pointer type variable, consider if char *xyz; now the pointer(char) will be the d ata type of identifier xyz In the first declaration, because of DT LIST, the data type of HIST char nd LIST +L, gives L type pointer(LIST type) pointer(char) TECHNICAL PUBLICATIONS” — an up-tinust for knowledge ” Directed Translation and Intermediate Code ¢ Compier Design 0 3.40 Syntax Senate, hence pomter(char) entry will be added in the symbol table for identifier xyz. Similarhy tor int ALO] As. UST sid LiSTtype = T.type Here Ttypesint hence LISType=int 7 UST-s Ly [num] Ly types larray(0...num.val-1),LIST.type] Ly type=larray(0...9),int} as num.val=10 The entry tor ALO] will be array(0...9,int) as type expression Type Checking of Expression \iter obtaining the type expression it becomes convenient to write the semantic rug These are the semantic rules tor type checking of expression. E literal {E.type:= char} Enum — {E.type:= int} Here as E can be literal or num, the data types associated with them can be char or int respectively. Esid {E.type = look_up(id.entry) The function look_up(id.entry) can be used to obtain the data type of the id. The 'cok_up tunetion reads the symbol table for id entry and thereby it obtains the type of identitier EE, mod E, { E.type then int else type_error } if E,.type = int and E,.type = int tn the mod operation, it type of Ey and type ot E) is int then only type of E is i Otherwise the special data type type error will raise the signal for reporting error int type In the same way, shule pertorming binaty operation the data types are decided TECH PUBLICA TIGHT agen a compiler Desig? 349 Syntax Directed Transtation and Intermediate Code Generation »E, opE, {E.type:= if E,type:= int and E,.type:= int then int else if E, type = float and E, type ~ float then float else if E, type:= char and E, type’~ char then char else type_enor } In the binary operation, if type of E, and type of E is int then only type of E is int milarly the type of E, and E, should be float then only type of E will be float. Also the type of E, and Fz should be char then only type of E will be char. Otherwise, the special data type type_error will raise the signal for reporting error in the type. For the array reference E> EVIE) { Etype else type_error } = if E,.type:= int and E,.type:=array(s,t) then t Here s is the set of index and t is the data type of the array. Hence the type returned for expression E will be t For example - int x[10},z; z=x13] t int value of x{3] is int hence z is also integer. As E,.type = array(0,...2,int) hence E, type = int Hence E.type = int Similarly, char pi5.q; G= pil) tT int Array p is of character type hence q has a data type char TECHNICAL PUBLICATIONS 1 up-tinrust for knowiedge Compiter Design 3.42 Syntax Drected Translation and Intermediate Code Generate, For the pointer type variable winter(t) then t Es | Btype:sif B).type else type error ) For the tunetion call ES E\E,) | E,ypes= if Eytype:= s and E).type:=s > t then t else Eytype =s > t then t else type_error ’ In the function call E\(E3) the s is the data type for Ey and t is the retum type of E,(E,). Hence E.type is t. Type Checking of Statements Usually, the language constructs like statements do not have values. And therefore the special type void is used. The type_error is the basic type used to raise the error signal. Consider the grammar as - E Sif) 5, S$ while (E) 5, Si $355) First of all we will write the translation scheme for checking the type of statements Production rule Semantic rule So id=E 'Stype- if id.type-E.type then void else type error SH ES, 'Stype-if Euype:=boolean(True) then S, type else type error TECHNICAL PUBLICATIONS” - an up-thrust for knowledge Syntax Directed Translation and Intermediate Code Generation [Stype-if F type -boolean(True) then 5) type else type_error [Stype if S).type-void and S>.type = void then void ‘ else type_error tells us that the type of LHS and RHS of the The semantic actions for S — id: assignment statement should be same. The statements ‘S — if (E) S; and S — while (E) S; tells us that these are conditional and while statements and the type of E should be boolean i.e. either true or false. For the last statement, if the subexpression has type void then the sequence of statements has the type void. If any mismatch in the type occurs then the basic type type_error reports the error. Thus the simple type checker is a combination of production rules and semantic rules. Review Questions 1. What do you mean by type expression ? 2. Explain with some suitable example simple type checker. 5. Construct the predictive parser for the following grammar S$ (ila LoLss 4. Explain the specification of simple type checker LURES ae URS Ces Ce ments, expressions and functions Explain the specification of simple type checker for state CORSA) 3.8 | Equivalence of Type Expressions checker is to find whether two ty Name equivalence or structural equivalence e expressions are equivalent or not The job of type This type equivalence is of two categories 1» two expressions have the identical type expressions then In type checking whe “error, It is therefore necessary to check whether two return certain type else return type. expressions possess the same type. Hence checking type equivalence in’ the type checking is an important task TECHNICAL PUBLICATIONS” - an up-thrust for knowledge = Comper Design we Syiar rected Translation nd Itermedte Code Sey, sions Structural Equivalence of Type Expres: When to expressions are the same basic type or formed by applying the on those exp! constructor to structurally equivalent types then thi P equivalent same Urally sions are called struct For example : Reason Equivalence . Similar basic types char char Sy) is equivalent to Sy F Pointer(char) —Pointer(char) Sis equivalent to S “mir constructor pointer to the cha The structural equivalence of two type expressions can be checked using follow algorithm, Algorithm : Checking the str struc ctural equivalence of two type expressions using function 4 (Sy) which is of Boolean type. ‘struct_eq(x,y) if x and y have same basic type then retum TRUE else if(x=array(x,,x,) and y=array(y,,y,)) then Tetum struct_ea(x,,y,) and struct_eq(x;.y,) else if x— x, Xx, and y = y, X y, then return struct_eq(x,,y,) and struct_eq(x,.y,) else if x=pointer(x,) and y = pointer(y,) then return struct_eq(x,,y,) else if x=x, x, and y = y, retum struct_eq(x,,y, else return FALSE ~ y2 then ) and struct_eq(x,,y,) } For the compact representation of the information the type and then can be compare, expressions can be ences! d_ structurally coding the type type, function, pointer and Let_us consider an constructor. Consider the array concept of e1 example to understand the type constructor such as bis The basic types can be Boolean, char, integer, real The encoding is done using ' bits as follows - |. Basic type Encoding bootean 000 shar ooo TECHNICAL PUBLICA r/o, 7 up-thrust for knowledge mpiter Design 145 + Directod Tra Hon and Intermediate Code Generation oon The type constructor can be encoded using, two bits as follows Type constructor — Enco ing, pointer a array w function u The type expression can be 0000 0001 the ncoded as the sequence of bits. For instance char can be ightmost four bits represent the basic type. Similarly pointer(tretarn(char)) can be encoded as 000111 0001. Here freturn represents the function that re encoded ns an object of type char, The encoding for typical type expressions can be as shown below ‘Type expression Encoding char 000000 0001 freturn(char) 000011 0001 pointer(freturn 000111 0010 array(pointer({return(real 100111 0001 » The advantage of this representation is that it saves the space while representing the information. Also compiler can keep track of the type constructor appearing in the type expression. Two type expressions are similar if they have same bit sequence 1 What do you mean by structural and name equivalence of type expression ? EEEA Name Equivalence of Type Expressions In the name equivalence the type pressions are given the names. Hence two type Oxpressior are said to be name equivalent ifand only it they are identical TECHNICAL PUBLICATIONS” — an up-thrust for Knowledge ation and Intermediate Cod syntax Directed Translation a! 048 Generatnc, Compiter Design we rent as follows For example : Consiter 260 typeder struct Node { int x, jnod node “first,*second. stract Node ‘last, *last2; In the abore code: the second are name equivalent simmjgy, bles first and and last] are not name equiv aler But first different. Although all the four varab tural equivalence since they 4, varial name equivalent pressions are jer struc Variables last and last2 as the names for these two type & fist, second, lastl, last2 are similar und representing the same type pointer(‘struct Node’). Type Conversions Type conversion is the proce: one example to understand the type conversion. Consider a statement f+i where f is a float type identifier and i is a integer type identifier and addition of the two has to be done. The representation of integer and flos: is different within the computer hence different machine instructions are used i operation on integer and float. For that, the compiler needs to convert one of the operand in order to obtain the same type of operands for the operation ss of converting one type to another. We will consider Coercions - There are two types of conversions : implicit conversion and explicit conversion. lt the conversion is done automatically by the compiler then it is called implict conversion. The implicit conversions are also called as coercions. In this type conversion there is no loss of information for example integer can be converted to !e but not vice versa eans ve is t not v rsa, That means in conversion from integer to float there is N° loss ¢ information but for converting the information from float to integer there is a loss The conversion is ss 1c e it nversion is said to be explicit if the programmer specifically writes some" for converting one type to another, For example int xyz,P, p=(float)xyz, The identifier xyz is type. a identilier xyz is type-casted and this is how explicit conversion from IU fe takes place TECHNICAL PUBLICATIONS” - an up-ttrust for knowledge _ > pneration Desa” Syntax Directed Transiation and Intermediate Code Gt qi conversions in Ada are explicit while © supports implicit conversions. (It converts Jaci characters to integers in arithmetic expression) ype checking rules for coercion from integer to float are o given below 5 num E type:=integer p> num.num E.type =float eu E type =look_up(id.entry) 5 Eop E E.type:=if E, type= int and E, type = int then int else if E, type= int and E,.type = float then float else if E, type= float and E,.type = int then float else if E,.type= float and E,.type = float then float else type_error } The necessary conversion from int to float takes place implicitly. The function lookup returns the type saved in the symbol table for the corresponding, id entry Consider an array A of floats that can be initialized to 1 as follows - Implicit type conversion This code takes 4.8 microseconds to execute. Explicit type conversion fori=O;i

You might also like