Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this article, we will cover the application of Syntax Directed Translation where we will also see a real example and how we can solve the problem with these applications.
The primary application of Syntax Directed Translation is the construction of syntax trees. Since some compilers use syntax trees as an intermediate representation, a common form of SDD(Syntax Directed Definition) turns its input string into a tree. To finalize the translation to intermediate code, the compiler may then walk the syntax tree, using another set of rules that affect an SDD on the syntax tree rather than the parse tree.
What is Syntax Directed Translation?
We use it for semantic analysis, and SDT(Syntax Directed Translation) constructs the parse tree with Grammar and Semantic action. In grammar, the need to decide who has the highest priority will be done first, and in semantic action, the determination of what type of action is done by grammar.
Here, we will cover an example of the application of SDT(Syntax Directed Translation) for a better understanding of the SDT application uses. Let’s consider an example of an arithmetic expression, and then you will see how we construct an SDT.
For Example: 2*(4+5)
Parse Tree of SDT construction
Semantic action is given as follows:
Production
Semantic Rules
E -> E + T
E1.trans = E2.trans + T.trans
E -> T
E.trans = T.trans
T -> T * F
T1.trans = T2.trans * F.trans
T -> F
T.trans = F.trans
F -> int
F.trans = int.value
F -> ( E )
F.trans = E.trans
Abstract Syntax Tree
AST(Abstract Syntax Tree) is a condensed form of an SDT tree. It has operators at internal nodes, not at leaves. Syntactic details like parenthesis, commas, semi-commas, etc., are omitted in AST.
AST is better to structure for later compiler stages since it omits details with the source language. It only contains information about the essential structure of the program.
For Example: 2*(4+5)
Abstract Syntax Tree Presentation
Frequently Asked Questions
What are the applications of syntax-directed translation?
Syntax Directed Translation is used for executing arithmetic expressions. It can also be used to convert infix to postfix/prefix expressions. It is also used for converting binary numbers to decimal numbers. It's commonly used for type checking, counting the total number of reductions, and creating a syntax tree.
What are the two types of attributes for syntax-directed translation?
The two types of attributes for syntax-directed translation: Synthesized attributes and Inherited attributes. Synthesized attributes are attributes of a non-terminal on the left-hand side of a production, while Inherited attributes are attributes of a non-terminal on the right-hand side of a production.
What is the use of syntax-directed translation in compiler design?
The syntax-directed translation is useful in compiler design as it allows the designer to specify the generation of intermediate code directly into the terms of the syntactic structure of the source language. It divides it into synthesized and inherited grammar attributes.
What is the difference between SDT and SDD?
SDD specifies the value of attributes by associating semantic rules with productions, while SDT puts program fragments within the production bodies themselves. SDD is easier to read and specify. But SDT is more efficient and is also easy to implement.
Conclusion
This article teaches about Syntax Directed Translation. We also discussed the characteristics, benefits, and applications of Syntax Directed Translation in compiler design.