Syntax Directed Translation - PowerPoint PPT Presentation

About This Presentation
Title:

Syntax Directed Translation

Description:

We augment a context-free grammar by adding semantic rules or actions to each production. ... We call this augmented grammar an attribute grammar. Two types of ... – PowerPoint PPT presentation

Number of Views:126
Avg rating:3.0/5.0
Slides: 15
Provided by: borameCs
Category:

less

Transcript and Presenter's Notes

Title: Syntax Directed Translation


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 --gt 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  --gt  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
(No Transcript)
10
(No Transcript)
11
(No Transcript)
12
(No Transcript)
13
  • 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, 1lt j lt n, on the right side
    of A-gt X1X2Xn, 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.

14
  • 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


X X.x
Y Y.y
Z Z.z

Production Semantic rule
A -gt LM L.i l(A,I)
M.i m(L.s)
A.s f(M.s)

A -gt QR R.i r(A.i)
Q.i q(R.s)
A.s f(Q.s)
Write a Comment
User Comments (0)
About PowerShow.com