In the following grammar X :: = X⊕Y / Y Y :: = Z*Y / Z Z :: = id Which of the…

1997

In the following grammar

X :: = X⊕Y / Y

Y :: = Z*Y / Z

Z :: = id

Which of the following is true?

Answer: A. '⊕' is left associative while '*' is right associativeFor multiple '⊕', the derivation is possible only via 'X' which is on left side of '⊕' in the production. Hence it is left associative. For multiple '∗', the…

  1. A.
    '⊕' is left associative while '*' is right associative

  2. B.
    Both '⊕' and '*' are left associative

  3. C.
    '⊕' is right associative while '*' is left associative

  4. D.
     None of the above

Attempted by 73 students.

Show answer & explanation

Correct answer: A

For multiple '⊕', the derivation is possible only via 'X' which is on left side of '⊕' in the production. Hence it is left associative.

For multiple '∗', the derivation is possible only via 'Y' which is on the right side of '∗' in the production. Hence it is right associative.


Note for more Understanding

1. Showing that is Left-Associative


Let's look at the grammar rule: X X Y | Y

We will generate the string: id1 id2 id3

The Step-by-Step Derivation:

To get two ⊕ signs, we must apply the left-recursive rule X X Y first:

  1. Start with the root: X

  2. Apply X X Y

  3. Expand the leftmost X again using X X Y(X ⊕ Y) ⊕ Y

  4. Now, safely turn the remaining non-terminals into IDs using the base rules (X Y Z id):

    • First X id1

    • Middle Y id2

    • Rightmost Y id3

The Parse Tree Structure:

Plaintext

         X
       / | \
     X   ⊕  Y
    /|\      |
   X ⊕ Y     Z
   |   |     |
   Y   Z     id₃
   |   |
   Z   id₂
   |
  id₁

Why this means Left-Associative:

In a compiler parse tree, bottom-up execution takes place. The operations deeper in the tree must be calculated before their parent nodes can resolve.

  • Look at the tree: ( id1 ⊕ id2) is grouped together at the very bottom left.

  • The compiler is forced to calculate ( id1 ⊕ id2) first, and then take that result to compute ⊕ id3

  • Execution moves from Left to Right.

2. Showing that * is Right-Associative

Let's look at the grammar rule: Y Z * Y | Z

We will generate the string: id1 * id2 * id3

The Step-by-Step Derivation:

Because the recursive variable Y is on the right side, the expansion grows to the right:

  1. Start with the root variable for this level: Y

  2. Apply Y Z * Y

  3. Expand the rightmost Y using Y Z * Y Z * (Z * Y)

  4. Turn all non-terminals into IDs via Z id and the terminating Y Z id:

    • Leftmost Z id1

    • Middle Z id2

    • Rightmost Y id3

The Parse Tree Structure:

Plaintext

      Y
    / |  \
   Z  *   Y
   |     /|\
  id₁   Z * Y
        |    |
        id₂  Z
             |
            id₃

Why this means Right-Associative:

Look at where the grouping happens now:

  • The sub-tree containing (id2 * id3) is isolated at the very bottom right.

  • The compiler cannot evaluate the first * operator until it knows what the right-hand side equals. Therefore, it is forced to evaluate (id2 * id3) first.

  • Once that is calculated, it multiplies it with id1

  • Execution moves from Right to Left.

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…