Skip to content
Official PYQ · 2016

GATE CS 2016 Set 1 Q45 — Parsing (LL, LR)

Explain with AI
The attribute of three arithmetic operators in some programming language are given below.
OPERATORPRECEDENCEASSOCIATIVITYARITY+HighLeftBinary−MediumRightBinary∗LowLeftBinary\begin{array}{|c|l|}\hline \textbf{OPERATOR} & \textbf{PRECEDENCE} & \textbf{ASSOCIATIVITY} & \textbf{ARITY} \\ \hline \text{$+$} & \text{High} & \text{Left} & \text{Binary} \\ \hline \text{$-$} & \text{Medium} & \text{Right} & \text{Binary} \\ \hline \text{$*$} & \text{Low} & \text{Left} & \text{Binary} \\ \hline \end{array}
The value of the expression 2−5+1−7∗32-5+1-7*3 in this language is ________.
View worked solution

Answer: 9

To evaluate the expression 2−5+1−7∗32 - 5 + 1 - 7 * 3, we must follow the precedence and associativity rules provided in the table:

  1. Precedence: ++ (High) >> −- (Medium) >> ∗* (Low).
  2. Associativity:
    • ++ is Left-associative.
    • −- is Right-associative.
    • ∗* is Left-associative.

Let's evaluate the expression step-by-step:

Step 1: Evaluate the highest precedence operator (++)
There is only one ++ operator in the expression: 5+15 + 1.
5+1=65 + 1 = 6 Substituting this back, the expression becomes: 2−6−7∗32 - 6 - 7 * 3

Step 2: Evaluate the next highest precedence operator (−-)
We have two −- operators: 2−6−72 - 6 - 7. Since −- is right-associative, we evaluate them from right to left, grouping as 2−(6−7)2 - (6 - 7): Rightmost −  ⟹  6−7=−1\text{Rightmost } - \implies 6 - 7 = -1 Now evaluate the remaining −-: 2−(−1)=32 - (-1) = 3 Substituting this back, the expression becomes: 3∗33 * 3

Step 3: Evaluate the lowest precedence operator (∗*)
3∗3=93 * 3 = 9

Thus, the value of the expression is 9.

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

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