Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests - Download the BYJU'S Exam Prep App for free GATE/ESE preparation videos & tests -

Syntax Directed Translation

A technique of compiler execution, where the source code translation is totally conducted by the parser, is known as syntax-directed translation.

The parser primarily uses a Context-free-Grammar to check the input sequence and deliver output for the compiler’s next stage. As a result, the output could be an abstract syntax tree or maybe a parse tree in this case. Syntax Directed Translation is now used to connect the semantic and syntax analysis stages.

Table of Contents

Important Pointers for Syntax-Directed Translation

Semantic rules are a collection of informal notations that we correlate with both grammar and syntax-directed translation.

So, we can represent it as:

Grammar + semantic rule = SDT (syntax directed translation)

Now check out some important pointers:

  • Depending on the kind of the attribute, every non-terminal in syntax-directed translation may receive one, several, or even no attributes. The semantic rules connected to the production rule assess the value of these attributes.
  • The semantic rule defines an attribute as VAL, and an attribute can include any type of data, including strings, numbers, memory locations, and complicated records.
  • When a construct is translated via syntax-directed translation, the semantic rules of the target programming language are taken into account in order to decode it.

Practice Problems – Syntax Directed Translation

Q1. Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let X1, X2, X3, X4, X5, and X6 be the placeholders for the non-terminals D, T, L or L1 in the following table:

Production Rule Semantic Action
D → T L X1.type = X2.type
T → int T.type = int
T → float T.type = float
L → LI , id X3.type = X4.type addType(id.entry, X5.type)
L → id addType(id.entry, X6.type)

Which one of the following are the appropriate choices for X1, X2, X3 and X4?

(A) X1=L,X2=T,X3=L1,X4=L

(B) X1=T,X2=L,X3=L1,X4=T

(C) X1=L,X2=L,X3=L1,X4=T

(D) X1=T,X2=L,X3=T,X4=L1

Q2. Consider the following Syntax Directed Translation Scheme (SDTS), with non-terminals {S, A} and terminals {a,b}.

S→aA{print1}

S→a{print2}

S→Sb{print3}

S→aA{print1}S→a{print2}S→Sb{print3}

Using the above SDTS, the output printed by a bottom-up parser, for the input aab is:

  • (A) 1 3 2
  • (B) 2 2 3
  • (C) 2 3 1
  • (D) syntax error

Frequently Asked Questions on Syntax Directed Translation

Q1

How are syntax-directed translators executed?

Syntax directed translation is executed by creating a parse tree and completing the steps in a left to right depth-first order.

Keep learning and stay tuned to get the latest updates on GATE Exam along with GATE Eligibility Criteria, GATE 2023, GATE Admit Card, GATE Syllabus, GATE Previous Year Question Paper, and more.

Also Explore,