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

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?

Analyzing Syntax-Directed Definitions (SDD1 and SDD2)

This solution examines two syntax-directed definitions (SDD1 and SDD2) to determine the validity of statements regarding their languages and attribute computations.

Statement A: Languages P and Q are the Same

Grammar G1 (associated with SDD1) has the following structure:

  • $D \rightarrow T\ V$
  • $T \rightarrow int \mid float$
  • $V \rightarrow V_1\ id \mid id$

Grammar G1 generates strings starting with a type ($int$ or $float$) followed by one or more identifiers. For instance, $int a$ or $float x y$ are valid strings in language P.

Grammar G2 (associated with SDD2) has the following structure:

  • $D \rightarrow D_1\ id$
  • $D \rightarrow T\ id$
  • $T \rightarrow int \mid float$

Grammar G2 also generates strings starting with a type ($int$ or $float$) followed by one or more identifiers. For instance, $int a$ or $float x y$ are valid strings in language Q.

Since both grammars generate the same set of strings (a type followed by one or more identifiers), languages P and Q are identical. Thus, Statement A is true.

Statement B: SDD2 is S-attributed

Let's analyze the attributes in SDD2:

  • For $T \rightarrow int$, $T.type = int$ is a synthesized attribute.
  • For $T \rightarrow float$, $T.type = float$ is a synthesized attribute.
  • For $D \rightarrow T\ id$, $D.type = T.type$ is synthesized, and $put(id.entry, T.type)$ uses the synthesized $T.type$.
  • For $D \rightarrow D_1\ id$, $D.type = D_1.type$ is synthesized, and $put(id.entry, D_1.type)$ uses the synthesized $D_1.type$.

All attributes computed and used ($T.type$, $D.type$, and the type argument for $put$) are synthesized. An S-attributed definition requires all attributes to be synthesized. Therefore, SDD2 is S-attributed. Thus, Statement B is true.

Statement C: SDD1 is L-attributed and contains only inherited attributes

Analyzing attributes in SDD1:

  • For $D \rightarrow T\ V$, $D.type = T.type$ is synthesized, and $V.type = T.type$ assigns a synthesized attribute of $T$ to $V.type$.
  • For $T \rightarrow int \mid float$, $T.type$ is synthesized.
  • For $V \rightarrow V_1\ id$, $V_1.type = V.type$ implies $V.type$ is passed as an inherited attribute to $V_1$. $put(id.entry, V.type)$ uses $V.type$ as an inherited attribute for $id$.
  • For $V \rightarrow id$, $put(id.entry, V.type)$ uses $V.type$ as an inherited attribute for $id$.

SDD1 contains synthesized attributes ($D.type$, $T.type$) and uses attributes ($V.type$) in a way consistent with L-attributed definitions. However, because it possesses synthesized attributes, it does not contain *only* inherited attributes. Thus, Statement C is false.

Statement D: Symbol Table Entries are the Same

We compare the $put$ operations in both SDDs:

In SDD1:

  • The attribute $V.type$ obtains its value from $T.type$ (which is $int$ or $float$).
  • The $put$ calls are $put(id.entry, V.type)$. Each $id$ receives the declaration type ($int$ or $float$) from $V.type$.

In SDD2:

  • The attribute $T.type$ is $int$ or $float$.
  • The attribute $D_1.type$ also holds the declaration type, derived from $T.type$ or previous rules.
  • The $put$ calls are $put(id.entry, T.type)$ or $put(id.entry, D_1.type)$. Each $id$ receives the declaration type ($int$ or $float$).

Both SDD1 and SDD2 correctly associate the declared type ($int$ or $float$) with each identifier via the $put$ function. Therefore, the same entries are added to the symbol table. Thus, Statement D is true.

Was this answer helpful?

Important Questions from Syntax Directed Translation

  1. 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?
  2. In which graph, semantic rules set up dependencies between attributes ?
  3. COMPILER is used to?

  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