1 of 6

Compiler Design

Lecture-1

Unit-III: Syntax directed definition

 

By

Dr.G.Janardhan

Associate Professor

CSE

2 of 6

Syntax directed definition

Syntax directed definition :

  • A SDD is a Context free grammar together with Semantic rules.
  • Attributes are Associated with grammar Symbols and Semantic rules are Associated with productions
  • If ‘x’ is a Symbol and ‘a’ is one of its attribute then X.a denotes value at node ‘X’
  • Attributes may be numbers,strings,datatypes etc.
  • Production Semantic Rules

E -> E + T E.val = E.val + T.val

 

 

3 of 6

Syntax directed definition

Types of attributes – 

There are two types of attributes:

Synthesized Attributes – These are those attributes which derive their values from their children nodes i.e. value of synthesized attribute at node is computed from the values of attributes at children nodes in parse tree.  

Ex:A->BCD

A be a parent node. B,C,D are children nodes.

A.S=B.S

A.S=C.S

A.S=D.S

Here A is parent node taking values from its children nodes B,C,D

 

 

4 of 6

Syntax directed definition

Inherited Attributes – These are the attributes which derive their values from their parent or sibling nodes i.e. value of inherited attributes are computed by value of parent or sibling nodes. 

Ex:A->BCD

C.i=A.i taking value from parent node

C.i=B.i taking value from Sibling node

 C.i=D.i taking value from Sibling node

 

5 of 6

Syntax directed definition

Types of SDD:

  1. S-Attributed SDD (or) S-Attributed Grammar
  2. L-Attributed SDD (or) L-Attributed Grammar

S-Attributed SDD:

  • A SDD That uses only Synthesized attributes is called as S-Attributed SDD

 Ex:A->BCD

A be a parent node. B,C,D are children nodes.

A.S=B.S

A.S=C.S

A.S=D.S

  • Semantic Actions are always placed at right hand end of the production.it is also called as Postfix SDD

6 of 6

Syntax directed definition

  • Attributes are Evaluated with bottom-up parsing.

L-Attributed SDD:

  • A SDD That uses both Synthesized attributes and Inherited attributes is called as L-Attributed SDD but each inherited attribute is restricted to inherits from parent or left sibling

 Ex:A->XYZ

A be a parent node. B,C,D are children nodes.

Y.S=A.S

Y.S=X.S

  • Semantic Actions are always placed anywhere in RHS.
  • Attributes are Evaluated with traversing parse tree depth first, left to right order.