All Exams Test series for 1 year @ ₹349 only
Question

Given the following syntax directed translation rules:
Rule 1: $R \to AB$ {$B. i = R. i - 1$; $A. i = B. i$; $R. i = A. i + 1$; }
Rule 2: $P \to CD$ {$P.i = C.i + D.i$; $D. i = C. i + 2$; }
Rule 3: $Q \to EF$ {$Q.i = E.i + F. i$; }
Which ONE is the CORRECT option among the following?

The correct answer is
Rule 1 is neither S-attributed nor L-attributed; Rule 2 is not S-attributed and is L-attributed; Rule 3 is S-attributed and L-attributed

Understanding Syntax Directed Translation & Attribute Grammars

Syntax-directed translation (SDT) is a powerful technique used in compilers to associate semantic information with grammar productions. It involves defining attribute values that are computed based on the structure of the parse tree. The way these attributes are defined and computed leads to classifications like S-attributed and L-attributed grammars.

S-Attributed Grammars

In an S-attributed grammar, all attributes are synthesized attributes. This means that the value of any attribute for a non-terminal symbol depends solely on:

  • The attribute values of its children in the parse tree.
  • Its own attribute values.

For a production rule like $X \to Y_1 Y_2 \dots Y_n$, the synthesized attributes of $X$ can only depend on the attributes of $Y_1, Y_2, \dots, Y_n$.

L-Attributed Grammars

L-attributed grammars are a more general class that includes S-attributed grammars. For a production rule $X \to Y_1 Y_2 \dots Y_n$, attributes can be evaluated in a single pass from left to right. This implies that the value of an attribute for a symbol $Y_i$ can depend on:

  • Attributes of the parent non-terminal $X$.
  • Attributes of its left siblings ($Y_1, Y_2, \dots, Y_{i-1}$).
  • Its own attribute values.

A key restriction for L-attributed grammars is that an attribute of $Y_i$ cannot depend on an attribute of $Y_j$ if $j > i$ (i.e., symbols appearing to its right in the production rule), unless that dependency is passed through the parent $X$.

Analysis of Provided Translation Rules

Rule 1 Analysis: $R \to AB$

The given translation rules are:

  • $B.i = R.i - 1$
  • $A.i = B.i$
  • $R.i = A.i + 1$

Let's examine the dependencies:

  • $R.i$: This attribute of the parent $R$ depends on attributes of its children, $A.i$ and $B.i$. This is characteristic of a synthesized attribute for $R$.
  • $B.i$: This attribute of child $B$ depends on $R.i$, the attribute of its parent. This indicates an inherited attribute for $B$.
  • $A.i$: This attribute of child $A$ depends on $B.i$, the attribute of its right sibling. In the production $R \to AB$, $A$ is $Y_1$ and $B$ is $Y_2$. The rule $A.i = B.i$ signifies that an attribute of $Y_1$ depends on an attribute of $Y_2$.

Classification for Rule 1:

  • It is not S-attributed because $B.i$ is an inherited attribute (depends on parent), and $A.i$ depends on $B.i$ (a sibling's attribute), which is not permitted for synthesized attributes in S-attributed grammars.
  • It is not L-attributed because the dependency $A.i = B.i$ violates the rule that an attribute of $Y_i$ cannot depend on an attribute of $Y_j$ where $j > i$. Here, $A$ is $Y_1$ and $B$ is $Y_2$, so $i=1$ and $j=2$.

Therefore, Rule 1 is neither S-attributed nor L-attributed.

Rule 2 Analysis: $P \to CD$

The given translation rules are:

  • $P.i = C.i + D.i$
  • $D.i = C.i + 2$

Let's examine the dependencies:

  • $P.i$: This attribute of the parent $P$ depends on the attributes of its children, $C.i$ and $D.i$. This is a synthesized attribute for $P$.
  • $D.i$: This attribute of child $D$ depends on $C.i$, the attribute of its left sibling, and a constant. In the production $P \to CD$, $C$ is $Y_1$ and $D$ is $Y_2$. The rule $D.i = C.i + 2$ signifies that an attribute of $Y_2$ depends on an attribute of $Y_1$ (its left sibling).

Classification for Rule 2:

  • It is not S-attributed because $D.i$ depends on $C.i$. In S-attributed grammars, attributes of children cannot depend on sibling attributes.
  • It is L-attributed because the synthesized attribute $P.i$ depends on its children, and the attribute $D.i$ (of $Y_2$) depends on $C.i$ (of $Y_1$, its left sibling). This dependency on a left sibling is permissible in L-attributed grammars.

Therefore, Rule 2 is not S-attributed and is L-attributed.

Rule 3 Analysis: $Q \to EF$

The given translation rule is:

  • $Q.i = E.i + F.i$

Let's examine the dependencies:

  • $Q.i$: This attribute of the parent $Q$ depends on the attributes of its children, $E.i$ and $F.i$. This is a synthesized attribute for $Q$.

Classification for Rule 3:

  • It is S-attributed because the only attribute defined, $Q.i$, is synthesized and depends solely on the attributes of its children ($E.i$ and $F.i$).
  • It is L-attributed because all S-attributed grammars are also L-attributed. There are no dependencies that violate the conditions for L-attributed grammars.

Therefore, Rule 3 is S-attributed and L-attributed.

Summary of Analysis

Rule Dependencies S-Attributed? L-Attributed?
$R \to AB$
${$B.i = R.i - 1$; $A.i = B.i$; $R.i = A.i + 1$; }$
$A.i$ depends on $B.i$ (right sibling).
$B.i$ depends on $R.i$ (parent).
No No
$P \to CD$
${$P.i = C.i + D.i$; $D.i = C.i + 2$; }$
$D.i$ depends on $C.i$ (left sibling).
$P.i$ depends on $C.i, D.i$ (children).
No Yes
$Q \to EF$
${$Q.i = E.i + F.i$; }$
$Q.i$ depends on $E.i, F.i$ (children). Yes Yes

Based on the detailed analysis:

  • Rule 1 is neither S-attributed nor L-attributed.
  • Rule 2 is not S-attributed and is L-attributed.
  • Rule 3 is S-attributed and L-attributed.
Was this answer helpful?

Important Questions from Syntax Directed Translation

  1. In which graph, semantic rules set up dependencies between attributes ?
  2. COMPILER is used to?

  3. Consider the following two syntax-directed definitions SDD1 and SDD2 for type declarations.

    SDD1
    Grammar (G1)Semantic Rules
    $D \rightarrow T\ V$$D.type = T.type$
    $V.type = T.type$
    $T \rightarrow int$$T.type = int$
    $T \rightarrow float$$T.type = float$
    $V \rightarrow V_1\ id$$V_1.type = V.type$
    $put(id.entry, V.type)$
    $V \rightarrow id$$put(id.entry, V.type)$

    SDD2
    Grammar (G2)Semantic Rules
    $D \rightarrow D_1\ id$$D.type = D_1.type$
    $put(id.entry, D_1.type)$
    $D \rightarrow T\ id$$D.type = T.type$
    $put(id.entry, T.type)$
    $T \rightarrow int$$T.type = int$
    $T \rightarrow float$$T.type = float$

    $D$ is the start symbol, and $int$, $float$ and $id$ are the three terminals. The non-terminal $V_1$ is the same as $V$ and the non-terminal $D_1$ is the same as $D$. Here, the subscript is used to differentiate the grammar symbols on the two sides of a production. The function $put$ updates the symbol table with the type information for an identifier.
    Let P and Q be the languages specified by grammars G1 and G2, respectively.
    Which of the following statements is/are true?
  4. Which of the following statement(s) are correct?

    A. Syntax Directed Translation can be applied in Type Checking.

    B. Syntax Directed Translation can be used in 3-address code generation.

    C. Syntax Directed Translation can be used in construction of syntax tree.

    D. Syntax Directed Translation can be used to recognize valid tokens.

    Choose the most appropriate answer from the options given below:

  5. Which of the following tasks could be attained using syntax trees in compiler design ?

    (A) Type Checking

    (B) Code Generation

    (C) Code Optimization

    (D) Error Handling

    Choose the correct answer from the options given below :

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App