1 / 14

Syntax Directed Translation

Syntax Directed Translation. Why do we need Syntax-directed?. To translate a programming language construct, a compiler may need to keep track of many quantities besides the code generated for the construct. Syntax-directed

Download Presentation

Syntax Directed Translation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Syntax Directed Translation

  2. Why do we need Syntax-directed? • To translate a programming language construct, a compiler may need to keep track of many quantities besides the code generated for the construct. • Syntax-directed • We augment a context-free grammar by adding semantic rules or actions to each production. • These rules define the values of attributes and how to compute them. • We call this augmented grammar an attribute grammar. • Two types of attributes: • synthesized attribute which computes the value based on the children • inherited attribute computed based on the parent and the older siblings. • We can use two methods to translate the programming language: • a formalism, syntax- directed definition • a more procedural notation called translation scheme.

  3. Syntax-directed definition(1/2) • Syntax-directed definition • A grammar -oriented compiling technique • It is very helpful for organizing the compiler's front end and it translates infix expression into post form • Postfix notation 1) If E is a variable or constant, then the postfix notation for E is E itself. 2) If E is an expression of the form E1 op E2 where op is any binary operator, then the postfix notation foe E is E1' E2' op. 3) If E is an expression of the form (E1), then the postfix notation for E1 is also the postfix notation for E.

  4. Syntax-directed definition(2/2) • Essentially a formal treatment of two simple concepts • Each node of a parse tree can have a set of associated values (attributes). • The creation and traversal of parse trees can be automated • Example if we have a production A --> X1 X2 ... Xn we might have a rule A.a = f(X1.a,X1.b,X2.a,X2.b,...,Xn.a,Xn.b) which describes how the attribute a of A is defined in terms of attributes of the children of A.

  5. Translation scheme(1/2) • A translation is an iuput-output mapping • The output for each input x is specified in the following way • construct a parse tree for x • compute the value using semantic rule for attribute associated with the X-production used at node n • two types of attributes • synthesized attribute which computes the value based on the children • inherited attribute computed based on the parent and the older siblings • A translation scheme gives a procedural description of an action to be taken when a production is applied

  6. Translation scheme(2/2) • The actions may be interspersed with the symbols on the right-hand side of the production • thus indicating an evaluation order • For example, in a translation scheme used to generate assembly code while-stmt --> while { generate label L1,L2; emit("L1:" ) expression { emit("beqz L2"); } statement { emit("b L1); emit("L2:"); } • Attributes may be evaluated during parsing or in a separate pass over the parse tree • Different strategies are used, depending whether a top-down or bottom-up parser is used

  7. How attributes are evaluated depends on whether they are synthsized or inherited • A synthesized attribute of a symbol is one which depends only on the attributes of its children (in the parse tree) • An inherited attribute is one which depends on attributes of the parent or siblings (ex. the data type of an identifier) • In general, inherited attributes are more difficult to deal with, particularly for bottom-up parsers

  8. How attributes are evaluated depends on whether they are synthesized or inherited. • A synthesized attribute of a symbol is one which depends only on the attributes of its children (in the parse tree); • for example, the value of an expression. • An inherited attribute is one which depends on attributes of the parent or siblings; • for example the data type of an identifier. • In general, inherited attributes are more difficult to deal with, particularly for bottom-up parsers.

  9. When the syntax-directed definition has only synthesized attribute, it is a S-attribute definitions. It can be computed by using bottom-up parser. While a syntax-directed definition is L-attribute if each inherited attribute of X_j, 1<= j <= n, on the right side of A-> X1X2¡­Xn, depends only on • 1) the attribute of the symbols X1,X2,¡­,Xj-1 to the left of Xj in the production and • 2) the inherited attributes of A.

  10. Every S-attributed definition is L-attributed, because the restriction 1) and 2) apply only to inherited attributes. It can be computed in depth-first order. All syntax-directed definition based on LL(1) grammars are L-attributed. • Following are some examples of S-attributed and L-attributed: 1)synthesized attribute on the parser stack: 2) A non-L-attributed syntax-directed definition:

More Related