Question

Consider the grammar defined by the following production rules, with two operators ∗* and ++

S⟶T∗PT⟶U∣T∗UP⟶Q+P∣QQ⟶IdU⟶Id\begin{aligned} S &\longrightarrow T * P \\ T &\longrightarrow U \mid T * U \\ P &\longrightarrow Q + P \mid Q \\ Q &\longrightarrow Id \\ U &\longrightarrow Id \end{aligned}

Which one of the following is TRUE?

Choose one option
Official answer and solution
Official answer
B
From
Official GATE 2014 answer key

Solution

Analyze the grammar productions for left-recursion and right-recursion associated with each operator.

  1. Production Rules for ∗*:

    T⟶U∣T∗UT \longrightarrow U \mid T * U

    In the recursive rule T⟶T∗UT \longrightarrow T * U, the non-terminal TT appears as the leftmost symbol on the right-hand side. This is left recursion, which enforces left associativity for operator ∗*.

  2. Production Rules for ++:

    P⟶Q+P∣QP \longrightarrow Q + P \mid Q

    In the recursive rule P⟶Q+PP \longrightarrow Q + P, the non-terminal PP appears on the right of ++. This is right recursion, which enforces right associativity for operator ++.

  3. Conclusion:
    ++ is right associative, while ∗* is left associative, which corresponds to option (B).

Final answer

B

Common trap

Assuming standard arithmetic associativity rules instead of reading the recursion structure directly from the grammar.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2014 Computer Science and Information Technology (CS) Session 2
Question
27 of 65, printed as CS Q.17
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.