Docsity

Syntax directed translations, Slides of Compiler Design

Partial preview of the text

Download Syntax directed translations and more Slides Compiler Design in PDF only on Docsity!

G22.2130-

Compiler Construction

Lecture 8:

Syntax-Directed Translation

Mohamed Zahran (aka Z)

[email protected]

A Step-Back

A Step-Back

Chapter 4

  • Grammars
  • Derivations
  • Parse-trees
  • Top-down parsing (LL)
  • Bottom-up paring (LR, SLR,LALR)

We Need Some Tools

• To help in semantic analysis

• To help in intermediate code generation

• Two such tools

– Semantic rules (Syntax-Directed Definitions)

– Semantic actions (Syntax Directed Translations)

Two Types of Attributes

Two Types of Attributes

Synthesized Attributes

Attribute of the node is defined in terms of:

  • Attribute values at children of the node
  • Attribute value at node itself

SDD involving only synthesized attributes is called S-attributed

3*5+4n

A parse tree showing the values of its attributes is called annotated parse tree.

Example

Give the annotated parse tree of (3+4)*(5+6)n

Example

Give annotated parse-trees for: int a, b, c

Evaluation Orders of SDDs

• Annotated parse tree shows attribute

values

• Dependency graph helps us determine

how those values are computed

S-Attributed Definitions

• Every attribute is synthesized

• We can evaluate its attribute in any

bottom-up order of the nodes of the

parse tree

(e.g. postorder traversal -> LR parser).

L-Attributed Definitions

• Dependency graph edges can only go

from left to right

– i.e. use attributes from above or from the

left

Syntax-Directed Translations

• Context-free grammar

• Can implement SDDs

• Program fragments embedded within

production bodies

– called semantic rules

– Can appear anywhere within the production

body

• Steps are usually as follows

– Build parse tree

– perform actions as you traverse left-to-right,

depth-first (preorder)

Example