Chapter 5: Syntax directed translation - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 5: Syntax directed translation

Description:

... definition, how to build a translator? ... For some special definitions, we can perform translation while parsing ... Syntax directed translation scheme: ... – PowerPoint PPT presentation

Number of Views:906
Avg rating:3.0/5.0
Slides: 12
Provided by: xyu
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 5: Syntax directed translation


1
  • Chapter 5 Syntax directed translation
  • use the grammar to direct the translation
  • the grammar defines the syntax of the input
    language. Attributes are attached to grammar
    symbols. Semantic rules are associated with
    grammar productions.
  • Attributes -- type of values associated with
    grammar symbols representing programming language
    constructs. These values are computed by the
    semantic rules.
  • 2 notations for associating semantic rules with
    productions.
  • Syntax directed definitions
  • Syntax directed translation schemes

2
  • Syntax directed definitions
  • Each grammar production A-gta is associated with a
    set of semantic rules of the form
    bf(c1, c2, , ck)
  • If b is an attributed associated with A, it is
    called a synthesized attribute.
  • If b is an attributed associated with a grammar
    symbol on the right side of the production, b is
    called an inherited attribute.
  • The definitions does not specify explicitly the
    order in which the attributes can be evaluated.
  • The semantic rules implicitly indicate the order
    (b depends on c1, c2, , ck).
  • Syntax directed definitions hide many
    implementation details.

3
  • Example an S-attributed definition
  • A syntax directed definition that uses
    synthesized attributes exclusively is said to be
    an S-attributed definition.
  • Production semantic rules
  • L -gtE n print(E.val)
  • E-gtE1 T E.val E1.val
    T.val
  • E-gtT E.val
    T.val
  • T-gtT1 F T.val T1.val
    F.val
  • T-gtF T.val
    F.val
  • F-gt(E) F.val
    E.val
  • F-gtdigits F.val
    digits.lexval
  • 354n

4
  • Example inherited attributes
  • production semantic
    rules
  • D -gtT L L.in
    T.type
  • T-gtint T.type
    integer
  • T-gtreal T.type
    real
  • L-gtL1, id L1.in
    L.in, addtype(id.entry, L.in)
  • L-gtid
    addtype(id.entry, L.in)
  • real id1, id2, id3

5
  • Syntax-directed construction of syntax trees
  • Tree mkleaf(type, value)
  • Tree mknode(op, left, right)
  • Production
  • E-gtE1T
  • E-gtE1-T
  • E-gtT
  • T-gt(E)
  • T-gtnum
  • how is the tree for 1-23 constructed?

6
  • L-attributed definitions
  • A syntax directed definition is L-attributed if
    each inherited attribute of Xj, 1ltjltn, on the
    right side of A-gtX1X2Xn depends only on
  • attributes of the symbols X1, X2, , Xj-1.
  • the inherited attributes of A.
  • L stands for Left since information appears to
    flow from left to right in the compilation
    process.
  • Example
  • A-gtLM L.iA.i M.iL.s A.s
    M.s
  • A-gtQR R.i A.i Q.i R.s A.s
    Q.s
  • Relation between S-attributed definitions and
    L-attributed definitions?
  • Why L-attributed definitions are important?

7
  • Given a syntax directed definition, how to build
    a translator?
  • For general definitions, to evaluate the semantic
    rules correctly, we need to follow the dependence
    of the attributes (defined by the semantic
    rules).
  • Build a dependency graph. Topologically sort the
    graph, then evaluate the rules accordingly.
  • Example real id, id, id
  • For some special definitions, we can perform
    translation while parsing
  • e.g. bottom-up evaluation of S-attributed
    definitions.
  • Most L-attributed definitions also works.

8
  • Syntax directed translation scheme
  • a context-free grammar in which attributes are
    associated with grammar symbols and the semantic
    actions are enclosed between and are inserted
    within the right side of productions to indicate
    the order in which translation takes place --
    must be careful with the order.
  • Example
  • E-gtT R
  • R-gt T print() R - T print(-) R e
  • T-gtnum print(num.val)

9
  • Syntax directed translation scheme
  • another example
  • E-gtT R.I T.val
  • R E.val R.s
  • R-gt
  • T R1.I R.I R.val
  • R R.s R1.s
  • R-gt R.s R.I
  • T-gtnum T.val num.val
  • evaluation of 9-52

10
  • S-attributed definitions can directly translated
    into a translation scheme by placing the semantic
    actions at the end of each productions.
  • Actions in the middle of productions can be
    removed to be put at the end of productions by
    changing the grammar.
  • Example previous page.

11
  • Syntax directed translation with YACC.
  • Only has synthesized attributes
  • parser stack with field for synthesized
    attributes
  • default attribute is of integer type.
  • How to handle inherited attributes?
  • Semantic actions only happen at the end of a
    production?
Write a Comment
User Comments (0)
About PowerShow.com