Open In App

Syntax-Directed Translation Schemes

Last Updated : 25 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • The syntax-directed translation which has its semantic actions at the end of the production is called the postfix translation scheme.
  • This type of translation of SDT has its corresponding semantics at the last in the RHS of the production.
  • SDTs which contain the semantic actions at the right ends of the production are called postfix SDTs. 
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.

  • The parser stack contains the record for the non-terminals in the grammar and their corresponding attributes.
  • The non-terminal symbols of the production are pushed onto the parser stack.
  • If the attributes are synthesized and semantic actions are at the right ends then attributes of the non-terminals are evaluated for the symbol in the top of the stack.
  • When the reduction occurs at the top of the stack, the attributes are available in the stack, and after the action occurs these attributes are replaced by the corresponding LHS non-terminal and its attribute.
  • Now, the LHS non-terminal and its attributes are at the top of the stack.
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:

  • When the attributes are inherited attributes of any non-terminal then place the action immediately before the non-terminal in the production.
  • When the attributes of the non-terminal are synthesized then, place the action at the right end of that production.

Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads