Table of contents
1.
Introduction
2.
What is Syntax Directed Translation?
3.
Application of Syntax Directed Translation
3.1.
Example
4.
Abstract Syntax Tree
5.
Frequently Asked Questions
5.1.
What are the applications of syntax-directed translation?
5.2.
What are the two types of attributes for syntax-directed translation?
5.3.
What is the use of syntax-directed translation in compiler design?
5.4.
What is the difference between SDT and SDD?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Applications of Syntax Directed Translation

Author Abhay Trivedi
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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.

Applications of Syntax Directed Translation

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.

Also read About, Specifications of Tokens in Compiler Design

Application of Syntax Directed Translation

We use SDT(Syntax Directed Translation) for

  • Executing Arithmetic Expressions
  • Conversion from infix to postfix expression
  • Conversion from infix to prefix expression
  • For Binary to decimal conversion
  • Counting the number of Reductions
  • Creating a Syntax tree
  • Generating intermediate code
  • Storing information into the symbol table
  • Type checking

Example

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

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

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.

Recommended Readings: 


Do check out some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, etc. on Coding Ninjas Studio

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Happy Learning!

Live masterclass