Skip to content
Related Articles

Related Articles

Evaluation Order For SDD

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 29 Apr, 2022
Improve Article
Save Article

Evaluation order for SDD includes how the SDD(Syntax Directed Definition) is evaluated with the help of attributes, dependency graphs, semantic rules, and S and L attributed definitions. SDD helps in the semantic analysis in the compiler so it’s important to know about how SDDs are evaluated and their evaluation order. This article provides detailed information about the SDD evaluation. It requires some basic knowledge of grammar, production, parses tree, annotated parse tree, synthesized and inherited attributes.   

Terminologies:

  • Parse Tree: A parse tree is a tree that represents the syntax of the production hierarchically.
  • Annotated Parse Tree: Annotated Parse tree contains the values and attributes at each node.
  • Synthesized Attributes: When the evaluation of any node’s attribute is based on children.
  • Inherited Attributes: When the evaluation of any node’s attribute is based on children or parents. 

Dependency Graphs:

A dependency graph provides information about the order of evaluation of attributes with the help of edges.  It is used to determine the order of evaluation of attributes according to the semantic rules of the production. An edge from the first node attribute to the second node attribute gives the information that first node attribute evaluation is required for the evaluation of the second node attribute. Edges represent the semantic rules of the corresponding production. 

Dependency Graph Rules: A node in the dependency graph corresponds to the node of the parse tree for each attribute. Edges (first node from the second node)of the dependency graph represent that the attribute of the first node is evaluated before the attribute of the second node.

Ordering the Evaluation of Attributes:

The dependency graph provides the evaluation order of attributes of the nodes of the parse tree. An edge( i.e. first node to the second node) in the dependency graph represents that the attribute of the second node is dependent on the attribute of the first node for further evaluation. This order of evaluation gives a linear order called topological order.

There is no way to evaluate SDD on a parse tree when there is a cycle present in the graph and due to the cycle, no topological order exists.  

                    Production Table
S.No. Productions   Semantic Rules
  1. S ⇢ A & B    S.val = A.syn + B.syn
  2. A ⇢ A1 # B

 A.syn = A1.syn * B.syn

 A1.inh = A.syn

  3. A1 ⇢ B  A1.syn = B.syn
  4. B ⇢ digit  B.syn = digit.lexval

Annotated Parse Tree For 1#2&3

Annotated Parse Tree For 1#2&3

Dependency Graph For 1#2&3

Dependency Graph For 1#2&3

Explanation of dependency graph:

Node number in the graph represents the order of the evaluation of the associated attribute. Edges in the graph represent that the second value is dependent on the first value. 

Table-1 represents the attributes corresponding to each node. 
Table-2 represents the semantic rules corresponding to each edge.  

      Table-1
Node Attribute
   1 digit.lexval
   2 digit.lexval
   3 digit.lexval
   4 B.syn
   5 B.syn
   6 B.syn
   7 A1.syn
   8 A.syn
   9 A1.inh
  10 S.val
                          Table-2
   Edge

Corresponding Semantic Rule

 (From the production table)

From  To 
   1   4       B.syn   = digit.lexval
   2   5     B.syn   = digit.lexval
   3   6     B.syn   = digit.lexval
   4   7     A1.syn = B.syn
   5   8     A.syn   = A1.syn * B.syn
   6   10       S.val    = A.syn + B.syn
   7    8       A.syn   = A1.syn * B.syn
   8   10      S.val    = A.syn + B.syn
   8    9     A1.inh = A.syn  

S-Attributed Definitions:

S-attributed SDD can have only synthesized attributes. In this type of definitions semantic rules are placed at the end of the production only. Its evaluation is based on bottom up parsing. 

Example: S ⇢ AB { S.x = f(A.x | B.x) }

L-Attributed Definitions:

L-attributed SDD can have both synthesized and inherited (restricted inherited as attributes can only be taken from the parent or left siblings). In this type of definition, semantics rules can be placed anywhere in the RHS of the production. Its evaluation is based on inorder (topological sorting). 

Example: S ⇢ AB {A.x = S.x + 2} or S ⇢ AB { B.x = f(A.x | B.x) } or S ⇢ AB { S.x = f(A.x | B.x) }

Note:

  • Every S-attributed grammar is also L-attributed.
  • For L-attributed evaluation in order of the annotated parse tree is used.
  • For S-attributed reverse of the rightmost derivation is used.

Semantic Rules with controlled side-effects:

Side effects are the program fragment contained within semantic rules. These side effects in SDD can be controlled in two ways: Permit incidental side effects and constraint admissible evaluation orders to have the same translation as any admissible order.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!