Syntax Directed Definition in Compiler Design
Last Updated :
24 Sep, 2024
Syntax Directed Definitions (SDD) are formal methods of attaching semantic information to the syntactic structure of a programming language. SDDs improve the means of context-free high-level by instances, adding a semantic rule set for every production of the grammar. The rules described in these definitions state how to derive values such as types, memory locations, or fragments of code from the structure of an input object.
Syntax Directed Translation (SDT) is the action of translating a high-level language program into an intermediate language or machine language according to the semantic rules imposed by SDDs. Semantic actions in SDT, act in coordination with the parsing process in order to provide the translation of the input code. These actions are declarative and they are triggered during the parsing phase of the message to yield the result.
What is Syntax Directed Translation?
Syntax Directed Translation (SDT), is a technical manner utilized in semantics and language translation whereby a source language is translated into an intermediate language or a target language through semantic actions, and attributes attached to the grammar. The translation process follows the structure of the grammar, besides the performance of semantic actions is interwoven with the processes of input parsing.
The semantic actions are normally included in the grammatical rules and can be either performed synchronously or asynchronously with the parsing actions. Integrated development systems such as SDT offer tools to compilers such as code generation, lexical analysis, evaluation of expressions, definition, grammar, and checking of types among other services.
What are Annotated Parse Trees?
The parse tree containing the values of attributes at each node for given input string is called annotated or decorated parse tree.Â
Features of Annotated Parse Trees
- High level specification
- Hides implementation details
- Explicit order of evaluation is not specified
Types of Attributes
There are two types of attributes:Â
1. 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.Â
Example:Â Â
E --> E1 + T { E.val = E1.val + T.val}
In this, E.val derive its values from E1.val and T.valÂ
Computation of Synthesized Attributes
- Write the SDD using appropriate semantic rules for each production in given grammar.
- The annotated parse tree is generated and attribute values are computed in bottom up manner.
- The value obtained at root node is the final output.
Example: Consider the following grammar Â
S --> E
E --> E1 + T
E --> T
T --> T1 * F
T --> F
F --> digit
The SDD for the above grammar can be written as followÂ

Let us assume an input string 4 * 5 + 6 for computing synthesized attributes. The annotated parse tree for the input string isÂ

For computation of attributes we start from leftmost bottom node. The rule F --> digit is used to reduce digit to F and the value of digit is obtained from lexical analyzer which becomes value of F i.e. from semantic action F.val = digit.lexval. Hence, F.val = 4 and since T is parent node of F so, we get T.val = 4 from semantic action T.val = F.val. Then, for T --> T1 * F production, the corresponding semantic action is T.val = T1.val * F.val . Hence, T.val = 4 * 5 = 20Â
Similarly, combination of E1.val + T.val becomes E.val i.e. E.val = E1.val + T.val = 26. Then, the production S --> E is applied to reduce E.val = 26 and semantic action associated with it prints the result E.val . Hence, the output will be 26.Â
2. 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.Â
Example:Â Â
A --> BCD { C.in = A.in, C.type = B.type }
Computation of Inherited Attributes
- Construct the SDD using semantic actions.
- The annotated parse tree is generated and attribute values are computed in top down manner.
Example: Consider the following grammar Â
S --> T L
T --> int
T --> float
T --> double
L --> L1, id
L --> id
The SDD for the above grammar can be written as followÂ

Let us assume an input string int a, c for computing inherited attributes. The annotated parse tree for the input string isÂ

The value of L nodes is obtained from T.type (sibling) which is basically lexical value obtained as int, float or double. Then L node gives type of identifiers a and c. The computation of type is done in top down manner or preorder traversal. Using function Enter_type the type of identifiers a and c is inserted in symbol table at corresponding id.entry.Â
Conclusion
Syntax Directed Definitions (SDD) and Syntax Directed Translation (SDT) are essential tools in the design and implementation of compilers and interpreters. They allow for the generation of intermediate code, type-checking, and expression evaluation in a modular and structured manner. By defining attributes and associating them with grammar productions, we can control the semantic processing of programming languages efficiently. Synthesized and inherited attributes form the basis of attribute evaluation, supporting a broad range of programming language features.
Similar Reads
Syntax Directed Translation in Compiler Design
Syntax-Directed Translation (SDT) is a method used in compiler design to convert source code into another form while analyzing its structure. It integrates syntax analysis (parsing) with semantic rules to produce intermediate code, machine code, or optimized instructions.In SDT, each grammar rule is
8 min read
Depth First Ordering in Compiler Design
Depth-first ordering is a traversal technique used in compiler design to visit nodes in a tree or graph. In depth-first ordering, the nodes are visited in a depth-first manner, meaning that the nodes are visited from the root to the deepest level before backtracking and visiting the next node. This
6 min read
Introduction to Syntax Analysis in Compiler Design
Syntax Analysis (also known as parsing) is the step after Lexical Analysis. The Lexical analysis breaks source code into tokens.Tokens are inputs for Syntax Analysis.The goal of Syntax Analysis is to interpret the meaning of these tokens. It checks whether the tokens produced by the lexical analyzer
7 min read
Runtime Environments in Compiler Design
A translation needs to relate the static source text of a program to the dynamic actions that must occur at runtime to implement the program. The program consists of names for procedures, identifiers, etc., that require mapping with the actual memory location at runtime. Runtime environment is a sta
8 min read
Dependency Graph in Compiler Design
A dependency graph is used to represent the flow of information among the attributes in a parse tree. In a parse tree, a dependency graph basically helps to determine the evaluation order for the attributes. The main aim of the dependency graphs is to help the compiler to check for various types of
6 min read
Introduction of Compiler Design
A compiler is software that translates or converts a program written in a high-level language (Source Language) into a low-level language (Machine Language or Assembly Language). Compiler design is the process of developing a compiler.The development of compilers is closely tied to the evolution of
9 min read
Error Handling in YACC in Compiler Design
A compiler is a computer program that translates code written in one programming language (the source language) into another programming language (the target language). The process of compiling involves several stages, including lexical analysis, syntax analysis, semantic analysis, code generation,
9 min read
Compiler Design - Variants of Syntax Tree
A syntax tree is a tree in which each leaf node represents an operand, while each inside node represents an operator. The Parse Tree is abbreviated as the syntax tree. The syntax tree is usually used when representing a program in a tree structure. Rules of Constructing a Syntax Tree A syntax tree's
7 min read
Language Processing System in Compiler Design
The computer is an intelligent combination of software and hardware. Hardware is simply a piece of mechanical equipment and its functions are being compiled by the relevant software. The hardware considers instructions as electronic charge, which is equivalent to the binary language in software prog
4 min read
Next use information in compiler design
In compiler design, the next use information is a type of data flow analysis that can be used to optimize the allocation of registers in a computer's central processing unit (CPU). The goal of next use analysis is to determine which variables in a program are needed in the immediate future and shoul
6 min read