• edited by
23,854 views
21 21 votes

Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below. $$\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 / 2$ as per the above rules is ________.

4 Answers

Best answer
34 34 votes

The given expression is evaluated as follows:

$\implies \underbrace{\underbrace{3+1}+5}*2/\underbrace{7+2}-4-7-6/2\; [ \text{As '+' has higher predence with left associative in the operators present in the expression.}]$

$\implies(3+1)+5*2/7+2-4-7-6/2$

$\implies(4+5)*2/7+2-4-7-6/2$

$\implies9*2/(7+2)-4-7-6/2$

$\implies9*2/\underbrace{9-\underbrace{4-\underbrace{7-6}}}/2\; [ \text{As '-' has higher predence with right associative in the operators present in the expression.}]$

$\implies9*2/9-4-(7-6)/2$

$\implies9*2/9-(4-1)/2$

$\implies9*2/(9-3)/2$

$\implies \underbrace{9*2}/6/2 \; [ \text{As '*' has higher predence with right associative in the operators present in the expression.}]$

$\implies(9*2)/6/2$

$\implies \underbrace{18/\underbrace{6/2}} \; [ \text{As '/' has higher predence with right associative in the operators present in the expression.}]$

$\implies18/(6/2)$

$\implies18/3=6$

Similar kind of questions  asked in GATE CSE 2016 Set 1 | Question: 45

• selected by
Answer:
Position:
Show:

Related questions

33 33 votes
5 5 answers
15.4k
15.4k views
Arjun asked Feb 16, 2024
15,434 views
Let $G=(V, \Sigma, S, P)$ be a context-free grammar in Chomsky Normal Form with $\Sigma=\{a, b, c\}$ and $V$ containing $10$ variable symbols including the start symbol $...
52 52 votes
4 answers 4 answers
20.1k
20.1k views
Sandeep Singh asked Feb 12, 2016
20,117 views
The attribute of three arithmetic operators in some programming language are given below.$$\begin{array}{|c|l|}\hline \textbf{OPERATOR} & \textbf{PRECEDENCE} & \textbf{...
19 19 votes
3 3 answers
14.2k
14.2k views
Arjun asked Feb 16, 2024
14,240 views
Which of the following is/are Bottom-Up Parser(s)?Shift-reduce ParserPredictive ParserLL$(1)$ Parser LR Parser 
15 15 votes
4 answers 4 answers
14.2k
14.2k views
Arjun asked Feb 16, 2024
14,215 views
​Consider the following syntax-directed definition $\text{(SDD)}$.$$\begin{array}{|l|l|} \hline \text{$S \rightarrow D H T U$} & \text{$ \left\{S.v a l = D.val + H.val +...