Open In App

Evaluation Order For SDD

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:

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

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:

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.

Article Tags :