SyntaxDirected Translation Schemes (original) (raw)

Syntax-Directed Translation Schemes

Last Updated : 23 Jul, 2025

Syntax Directed Translation is a set of productions that have semantic rules embedded inside it. The syntax-directed translation helps in the semantic analysis phase in the compiler. SDT has semantic actions along with the production in the grammar. This article is about postfix SDT and postfix translation schemes with parser stack implementation of it. Postfix SDTs are the SDTs that have semantic actions at the right end of the production. This article also includes SDT with actions inside the production, eliminating left recursion from SDT and SDTs for L-attributed definitions.

Postfix Translation Schemes:

Example of Postfix SDT S ⇢ A#B{S.val = A.val * B.val} A ⇢B@1{A.val = B.val + 1} B ⇢num{B.val = num.lexval}

Parser-Stack Implementation of Postfix SDTs:

Postfix SDTs are implemented when the semantic actions are at the right end of the production and with the bottom-up parser(LR parser or shift-reduce parser) with the non-terminals having synthesized attributes.

Production A ⇢ BC{A.str = B.str . C.str} B ⇢a {B.str = a} C ⇢b{C.str = b}

Initially, the parser stack:

B C Non-terminals B.str C.str Synthesized attributes ⇡ Top of Stack

After the reduction occurs A ⇢BC then after B, C and their attributes are replaced by A and in the attribute. Now, the stack:

A Non-terminals A.str Synthesized attributes ⇡

Top of stack

SDT with action inside the production:

When the semantic actions are present anywhere on the right side of the production then it is SDT with action inside the production.

It is evaluated and actions are performed immediately after the left non-terminal is processed.

This type of SDT includes both S-attributed and L-attributed SDTs.

If the SDT is parsed in a bottom-up parser then, actions are performed immediately after the occurrence of a non-terminal at the top of the parser stack.

If the SDT is parsed in a top-down parser then, actions are before the expansion of the non-terminal or if the terminal checks for input.

Example of SDT with action inside the production S ⇢ A +{print '+'} B A ⇢ {print 'num'}B
B ⇢ num{print 'num'}

Eliminating Left Recursion from SDT:

The grammar with left recursion cannot be parsed by the top-down parser. So, left recursion should be eliminated and the grammar can be transformed by eliminating it.

Grammar with Left Recursion Grammar after eliminating left recursion P ⇢ Pr | q P ⇢ qA A ⇢ rA | ∈

SDT for L-attributed Definitions:

SDT with L-attributed definitions involves both synthesized and inherited attributes in the production.

To convert an L-attributed definition into its equivalent SDT follow the underlying rules: