Skip to content
Official PYQ · 2024

GATE CS 2024 Set 1 Q23 — Parsing (LL, LR)

Explain with AI

Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below. OperatorPrecedenceAssociativity+HighestLeft−HighRight∗MediumRight/LowRight\begin{array}{|c|l|}\hline \textbf{Operator} & \textbf{Precedence} & \textbf{Associativity} \\ \hline \text{$+$} & \text{Highest} & \text{Left} \\ \hline \text{$-$} & \text{High} & \text{Right} \\ \hline \text{$*$} & \text{Medium} & \text{Right} \\ \hline \text{$/$} & \text{Low} & \text{Right} \\ \hline \end{array}

The value of the expression 3+1+5∗2/7+2−4−7−6/23+1+5 * 2 / 7+2-4-7-6 / 2 as per the above rules is ________.

View worked solution

Answer: 6

To evaluate the expression, we apply the operators step-by-step in decreasing order of their precedence, respecting their associativity rules.

The given expression is:
3+1+5∗2/7+2−4−7−6/23+1+5 * 2 / 7+2-4-7-6 / 2

  1. Evaluate + (Highest Precedence, Left-Associative):

    We group the + operators from left to right:

    • 3+1+5  ⟹  ((3+1)+5)=93 + 1 + 5 \implies ((3 + 1) + 5) = 9
    • 7+2  ⟹  97 + 2 \implies 9

    Substituting these back, the expression becomes:
    9∗2/9−4−7−6/29 * 2 / 9 - 4 - 7 - 6 / 2

  2. Evaluate - (High Precedence, Right-Associative):

    Since - has higher precedence than * and /, the subtraction chain 9−4−7−69 - 4 - 7 - 6 is grouped first, from right to left:

    9−(4−(7−6))=9−(4−1)=9−3=69 - (4 - (7 - 6)) = 9 - (4 - 1) = 9 - 3 = 6

    Substituting this back, the expression becomes:
    9∗2/6/29 * 2 / 6 / 2

  3. Evaluate * (Medium Precedence, Right-Associative):

    Since * has higher precedence than /, we evaluate 9∗29 * 2 first:

    9∗2=189 * 2 = 18

    The expression becomes:
    18/6/218 / 6 / 2

  4. Evaluate / (Low Precedence, Right-Associative):

    Since / is right-associative, we group from right to left:

    18/(6/2)=18/3=618 / (6 / 2) = 18 / 3 = 6

Thus, the final value of the expression is 6.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Parsing (LL, LR) questions