What is syntax-directed translation schemes in compiler design?

Compiler DesignProgramming LanguagesComputer Programming

It is a kind of notation in which each production of Context-Free Grammar is related with a set of semantic rules or actions, and each grammar symbol is related to a set of Attributes. Thus, the grammar and the group of semantic Actions combine to make syntax-directed definitions.

The translation may be the generation of intermediate code, object code, or adding the information in symbol table about constructs type. The modern compiler uses the syntax-directed translation that makes the user’s life easy by hiding many implementation details and free the user from having to specify explicitly the order in which semantic rules are to be evaluated.

Semantic Actions− It is an action that is executed whenever the Parser will recognize the input string generated by context-free grammar.

For Example, A → BC {Semantic Action}

Semantic Action is written in curly braces Attached with a production.

In Top-Down Parser, semantic action will be taken when A will be expanded to derive BC which will further derive string w.

In Bottom-Up Parser, Semantic Action is generated when BC is reduced to A.

Syntax Directed Translation Scheme for Postfix Code

In postfix notation, the operator appears after the operands, i.e., the operator between operands is taken out & is attached after operands.

For example,

Consider Grammar

E → E(1) + E(2)

E → E(1) ∗ E(2)

E → (E(1))

E → id

The following table shows the Postfix Translation for the grammar.

Production
Semantic Action
   E → E(1) + E(2)
E. CODE = E(1). CODE| |E(2). CODE | |'+'
   E → E(1) ∗ E(2)
E. CODE = E(1). CODE| |E(2). CODE | |'∗'
   E → (E(1))
E. CODE = E(1). CODE
   E → id
E. CODE = id

Translation for Postfix Notation

Here, E. CODE represents an attribute or translation of grammar symbol E. It means the sequence of three-address statements evaluating E. The translation of the nonterminal on the left of each production is the concatenation (| |) of the translation of the non-terminals on the right followed by the operator.

In the first productionE → E(1) + E(2), the value of translation E. CODE is the concatenation of two translation E(1). CODE & E(2). CODE and symbol '+'.

In the second productionE → E(1) ∗ E(2), the value of translation E. CODE is the concatenation of two translation E(1). CODE & E(2). CODE and symbol '∗'.

Here, concatenation is represented by the symbol (| |).

In the third productionE → (E(1)), the translation of parenthesized expression is the same as that for unparenthesized expression.

In the fourth productionE → id, the translation of any identifier is the identifier itself.

Following are various attributes or translations for grammar symbol 𝐄.

  • 𝐄. 𝐕𝐀𝐋 → It tells the value of E.
  • 𝐄. 𝐏𝐋𝐀𝐂𝐄 →It describes the name that will hold the value of the expression.
  • 𝐄. 𝐂𝐎𝐃𝐄 →The sequence of three address statements computing the expression.
  • 𝐄. 𝐌𝐎𝐃𝐄 →It describes the data type of E.


raja
Updated on 08-Nov-2021 11:25:31

Advertisements