What is the Syntax Directed Translation?

In syntax directed translation, along with the grammar it can identify some informal notations and these notations are known as as semantic rules.

After implementing the Semantic Analysis, the source program is modified to an intermediate form.

There is some information that is required by the Intermediate code generation phase to convert the semantically checked parse tree into intermediate code. But this information or attributes of variables cannot be represented alone by Context- Free Grammar.

So, some semantic actions have to be attached with Context-Free grammar which helps the intermediate code generation phase to generate intermediate code.

So, Attaching attributes to the variables of the context Free Grammar and defining semantic action (meaning) of each production of grammar is called Syntax Directed Translation.

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.

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.

Semantic Action can perform −

  • Computation of value of variables

S → S(1) + S(2)                                 {S. VAL = S(1). VAL + S(2). VAL}

Here S. VAL will compute the sum of S(1) and S(2)values.

  • Printing of Error Messages

Example −                    A → BC            {error ( ); }

Whenever A will be expanded to BC, an error function will be called to print an error message.

The syntax-directed translation scheme is beneficial because it allows the compiler designer to define the generation of intermediate code directly in terms of the syntactic structure of the source language. It is division into two subsets known as synthesized and inherited attributes of grammar.

Attributes are related to the grammar symbol that are the labels of the parse tree node. In other terms, attributes are associated information with language construct by attaching them to grammar symbols representing that construct. An attribute can describe anything (reasonable) that it can select a string, a number, a type, a memory location, a code fragment, etc.

For example, an attribute for an identifier can include name, scope, type, actual arguments (number of parameters), and type of parameters, return type, etc. The value of an attribute at the parse tree node is represented by a semantic rule related with the production applied at that node.