Open In App

Syntax Directed Translation in Compiler Design

Last Updated : 07 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 linked with semantic actions that define how translation should occur. These actions help in tasks like evaluating expressions, checking types, generating code, and handling errors.

SDT ensures a systematic and structured way of translating programs, allowing information to be processed bottom-up or top-down through the parse tree. This makes translation efficient and accurate, ensuring that every part of the input program is correctly transformed into its executable form.

12

SDT relies on three key elements:

  1. Lexical values of nodes (such as variable names or numbers).
  2. Constants used in computations.
  3. Attributes associated with non-terminals that store intermediate results.

The general process of SDT involves constructing a parse tree or syntax tree, then computing the values of attributes by visiting its nodes in a specific order. However, in many cases, translation can be performed directly during parsing, without explicitly building the tree.

SDD v/s SDT Scheme

Syntax Directed DefinitionSyntax Directed Translation
It is a context-free grammar where attributes and rules are combined and associated with grammar symbols and productions, respectively.It refers to the translation of a string into an array of actions. This is done by adding an action to a rule of context-free grammar. It is a type of compiler interpretation.
Attribute GrammarTranslation Schemes
SDD: Specifies the values of attributes by associating semantic rules with the productions.SDT: Embeds program fragments (also called semantic actions) within production bodies.
E → E + T { E.val := E1.val + T.val }E → E + T { print(‘+’); }
Always written at the end of the body of production.The position of the action defines the order in which the action is executed (in the middle of production or at the end).
More ReadableMore Efficient
Used to specify the non-terminals.Used to implement S-Attributed SDD and L-Attributed SDD.
Specifies what calculation is to be done at each production.Specifies what calculation is to be done at each production and at what time they must be done.
Left to right evaluation.Left to right evaluation.
Used to know the value of non-terminals.Used to generate Intermediate Code.

Attributes in Syntax-Directed Translation

An attribute is any quantity associated with a programming construct in a parse tree. Attributes help in carrying semantic information during the compilation process.

Examples of Attributes:

  • Data types of variables
  • Line numbers for error handling
  • Instruction details for code generation

Types of Attributes

1. Synthesized Attributes

  • Defined by a semantic rule associated with the production at node N in the parse tree.
  • Computed only using the attribute values of the children and the node itself.
  • Mostly used in bottom-up evaluation.

2. Inherited Attributes

  • Defined by a semantic rule associated with the parent production of node N.
  • Computed using the attribute values of the parent, siblings, and the node itself.
  • Used in top-down evaluation.

Read about Differences Between Synthesized and Inherited Attributes.

Attribute Grammars

An Attributed Grammar is a special type of grammar used in compiler design to add extra information (attributes) to syntax rules. This helps in semantic analysis, such as type checking, variable classification, and ensuring correctness in programming languages.

Think of it like a regular grammar with extra labels that help check things like variable types, correctness of expressions, and rule enforcement.

Example of an Attribute Grammar

Production RuleSemantic Rule
D → T LL.in := T.type (Passes type information)
T → intT.type := integer (Defines type as integer)
T → realT.type := real (Defines type as real)
L → L1 , idL1.in := L.in
addtype (id.entry, L.in) (Passes type info to child and updates symbol table)
L → idaddtype (id.entry, L.in) (Adds type info to symbol table)
  • D → T L → The type from T is passed to L.
  • T → int / real → Assigns type integer or real to T.
  • L → id → Assigns type information to identifier (id).

Grammar and Translation Rules

SDT SchemeSDD Scheme
E → E + T{ print('+') }E → E + T E.code = E.code || T.code || '+'
E → E - T{ print('-') }E → E - T `E.code = E.code || T.code || '-'
E → TE → TE.code = T.code
T → 0{ print('0') }T → 0T.code = '0'
T → 1{ print('1') }T → 1T.code = '1'
......
T → 9{ print('9') }T → 9T.code = '9'

To evaluate translation rules, we can employ one depth-first search traversal on the parse tree. This is possible only because SDT rules don’t impose any specific order on evaluation until children's attributes are computed before parents for a grammar having all synthesized attributes. Otherwise, we would have to figure out the best-suited plan to traverse through the parse tree and evaluate all the attributes in one or more traversals. For better understanding, we will move bottom-up in the left to right fashion for computing the translation rules . 

Syntax-Directed Translation (SDT) allows us to evaluate arithmetic expressions while parsing. It uses attributes associated with grammar symbols and rules to compute values as we process the parse tree.

The following context-free grammar (CFG) defines an arithmetic expression with addition (+) and multiplication (*):

E -> E + T     { E.val = E.val + T.val }   // PR#1
E -> T { E.val = T.val } // PR#2
T -> T * F { T.val = T.val * F.val } // PR#3
T -> F { T.val = F.val } // PR#4
F -> INTLIT { F.val = INTLIT.lexval } // PR#5

Each production rule has a semantic action in {} that defines how values are computed.

  • E, T, and F are non-terminals (expression components).
  • INTLIT represents an integer literal (actual number).
  • val is an attribute used to store computed values at each step.

Let’s evaluate the expression: S = 2 + 3 * 4

Step 1: Build the Parse Tree
The parse tree for 2 + 3 * 4 is structured like this:

E

/|\

E + T

/ /|\

T T * F

/ / |

F F 4

| |

2 3

Step 2: Apply Translation Rules (Bottom-Up Evaluation)
We evaluate the expression step by step in a bottom-up manner (from leaves to root)

F → 2 → F.val = 2
F → 3 → F.val = 3
F → 4 → F.val = 4
T → F (T gets F's value) → T.val = 3
T → T * F → T.val = 3 * 4 = 12
E → T (E gets T's value) → E.val = 12
E → E + T → E.val = 2 + 12 = 14

Thus, the final computed value of 2 + 3 * 4 is 14.

 Advantages of Syntax Directed Translation:

Ease of implementation: SDT is a simple and easy-to-implement method for translating a programming language. It provides a clear and structured way to specify translation rules using grammar rules.

Separation of concerns: SDT separates the translation process from the parsing process, making it easier to modify and maintain the compiler. It also separates the translation concerns from the parsing concerns, allowing for more modular and extensible compiler designs.

Efficient code generation: SDT enables the generation of efficient code by optimizing the translation process. It allows for the use of techniques such as intermediate code generation and code optimization.

Disadvantages of Syntax Directed Translation:

Limited expressiveness: SDT has limited expressiveness in comparison to other translation methods, such as attribute grammars. This limits the types of translations that can be performed using SDT.

Inflexibility: SDT can be inflexible in situations where the translation rules are complex and cannot be easily expressed using grammar rules.

Limited error recovery: SDT is limited in its ability to recover from errors during the translation process. This can result in poor error messages and may make it difficult to locate and fix errors in the input program.


Next Article
Article Tags :

Similar Reads