0% found this document useful (0 votes)
5 views3 pages

Document From Aditya Tripathi

Syntax Directed Translation (SDT) is a compiler design technique that integrates parsing with semantic analysis by associating attributes with grammar symbols and defining semantic rules for production rules. It includes concepts like Syntax Directed Definitions (SDD), inherited and synthesized attributes, and methods for constructing syntax trees and evaluating expressions. The document also covers top-down and bottom-up translation methods, dependency graphs, and the use of postfix notation in expression evaluation.

Uploaded by

Aditya Tripathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Document From Aditya Tripathi

Syntax Directed Translation (SDT) is a compiler design technique that integrates parsing with semantic analysis by associating attributes with grammar symbols and defining semantic rules for production rules. It includes concepts like Syntax Directed Definitions (SDD), inherited and synthesized attributes, and methods for constructing syntax trees and evaluating expressions. The document also covers top-down and bottom-up translation methods, dependency graphs, and the use of postfix notation in expression evaluation.

Uploaded by

Aditya Tripathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

UNIT-III: Syntax Directed Translation

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.

1. Definitions (Syntax Directed Definitions - SDD)


A Syntax Directed Definition (SDD) is a context-free grammar where each grammar symbol has
a set of attributes, and each production rule has a set of semantic rules. These semantic rules describe
how to compute the values of attributes associated with the symbols in that production.
* Attributes: Properties associated with grammar symbols. They can be: * Synthesized At-
tributes: Attributes whose values are computed from the values of attributes of their children nodes in
a parse tree. They ”synthesize” information up the tree. * Inherited Attributes: Attributes whose
values are computed from the values of attributes of the parent node or siblings in a parse tree. They
”inherit” information down or across the tree.
Reference Links:
• GeeksforGeeks - Syntax Directed Definitions (SDD)
• TutorialsPoint - Compiler Design - Syntax Directed Translation

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:

• GeeksforGeeks - Synthesized and Inherited Attributes


• NPTEL - Compiler Design - Module 6, Lecture 2 (Syntax Directed Translation)

3. S-attributed Definitions (Synthesized Attributes)


An S-attributed Definition is a syntax directed definition that uses only synthesized attributes.
The value of a synthesized attribute at a node is computed solely from the values of attributes of its
children nodes in the parse tree. S-attributed SDDs are evaluated by bottom-up parsers naturally.
* Example (Arithmetic Expression Evaluation):

• 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:

• GeeksforGeeks - S-attributed and L-attributed SDTs


• TutorialsPoint - Compiler Design - Syntax Directed Translation (Look for S-attributed Grammars
section)

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:

• GeeksforGeeks - S-attributed and L-attributed SDTs


• Javatpoint - L-attributed Definition in Compiler Design

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:

• GeeksforGeeks - Dependency Graph in Compiler Design


• NPTEL - Compiler Design - Module 6, Lecture 3 (Dependency Graph)

6. Construction of Syntax Trees (Abstract Syntax Trees)


A syntax tree (more specifically, an Abstract Syntax Tree - AST) is a condensed form of a parse
tree. It captures the essential syntactic structure of the source code, omitting redundant details like
parentheses, commas, or keywords that don’t contribute to the meaning. SDDs are often used to construct
ASTs by attaching semantic actions that create nodes and link them.

• 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)

8. Postfix Notation (for SDT)


Postfix (Reverse Polish Notation) is an intermediate form of expression where operators follow their
operands. SDTs are commonly used to translate infix expressions into postfix notation.
• Semantic Rules for Infix to Postfix:
– E → E1 + T { print(’+’) }
– E → T { }
– T → T1 * F { print(’*’) }
– T → F { }
– F → (E) { }
– F → id { print(id.lexeme) }
For input a + b * c, the output would be a b c * +.
• Use: Postfix notation simplifies expression evaluation as it eliminates the need for parentheses and
operator precedence rules.
Reference Links:
• GeeksforGeeks - Syntax Directed Translation for Arithmetic Expressions to Postfix Notation
• TutorialsPoint - Compiler Design - Syntax Directed Translation (Example section)

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)

You might also like