Document From Aditya Tripathi
Document From Aditya Tripathi
Syntax Directed Translation (SDT) is a powerful technique in compiler design that combines parsing
(syntax analysis) with semantic analysis. It associates information (attributes) with grammar symbols
and attaches actions (semantic rules) to production rules. These actions compute the values of attributes
and perform other tasks, such as generating intermediate code or building symbol tables.
2. Inherited Attributes
An inherited attribute is an attribute of a grammar symbol whose value is defined by its parent or sibling
nodes in the parse tree. They are used to pass context information from higher levels of the parse tree
down to lower levels, or from a left sibling to a right sibling.
* Example (Type Propagation): Consider a declaration. The type of a variable might be inherited
from the declaration statement to the identifier.
• Production: D → T L
• Semantic Rule: L.in = T.type (The type from T is inherited by L)
Reference Links:
• Production: E → E1 + T
• Semantic Rule: E.val = E1.val + T.val
• Here, val is a synthesized attribute, computed from the values of its children.
Reference Links:
1
4. L-attributed Definitions
An L-attributed Definition is a syntax directed definition where each attribute in a production rule
A → X1 X2 ... Xn is either:
• A synthesized attribute.
• An inherited attribute of Xj (for some j) that depends only on:
– Attributes of the parent A.
– Attributes of siblings X1, X2, ..., Xj-1 (to its left).
– Synthesized attributes of Xj itself.
L-attributed SDDs are more general than S-attributed and can be evaluated during both top-down and
bottom-up parsing passes.
Reference Links:
5. Dependency Graph
A dependency graph is a directed graph that shows the interdependencies among the attribute in-
stances in a parse tree. An edge from node A to node B means that attribute A must be evaluated before
attribute B, because the value of B depends on the value of A.
• Construction: For each node in the parse tree, if an attribute b at that node depends on an
attribute a (at the same node, parent, or child/sibling), a directed edge from a to b is drawn.
• Use: A topological sort of the dependency graph gives a valid order for evaluating the attributes.
If the graph contains cycles, the SDD is said to be circular and cannot be evaluated.
Reference Links:
• Process: Semantic rules typically call functions to create nodes for operators and operands and
establish parent-child relationships.
• Example (for a + b * c): A parse tree is verbose; an AST is concise:
+
/ \
a *
/ \
b c
Reference Links:
• GeeksforGeeks - Abstract Syntax Tree (AST)
• TutorialsPoint - Compiler Design - Syntax Tree
2
7. Top-down Translation
In top-down translation (e.g., using recursive descent or LL parsers), semantic actions are usually placed
at the beginning or end of production rules, or within the rules.
• Placement of Actions:
– Actions for synthesized attributes are typically placed at the end of the RHS of a produc-
tion, after all children’s attributes have been computed.
– Actions for inherited attributes are usually placed before the symbol whose inherited at-
tribute is being computed, often right after its parent or left siblings are processed.
• Evaluation Order: Semantic actions are executed as the parser descends (predicts) and then
ascends (matches input) the parse tree. For L-attributed definitions, a top-down parser can directly
evaluate attributes.
Reference Links:
• GeeksforGeeks - Syntax Directed Translation with Top-Down Parsers
• NPTEL - Compiler Design - Module 6, Lecture 6 (Top-Down Translation)
9. Bottom-up Evaluation
In bottom-up translation (e.g., using LR parsers), semantic actions are associated with the reduction
steps. When a handle is reduced to a non-terminal, the semantic rules associated with that production
are executed.
• Placement of Actions: Actions are typically placed at the end of the RHS of a production rule,
meaning they are executed after all symbols on the RHS have been parsed and their attributes (if
synthesized) have been computed.
• Evaluation Order: Bottom-up parsers naturally evaluate S-attributed definitions as syn-
thesized attributes are computed from children, and bottom-up parsing processes children before
parents. L-attributed definitions can also be evaluated, often requiring the use of a semantic stack
to manage attribute values.
Reference Links:
• GeeksforGeeks - Syntax Directed Translation with Bottom-Up Parsers
• NPTEL - Compiler Design - Module 6, Lecture 5 (Bottom-Up Evaluation)