edited by
12,671 views
19 19 votes
Consider the following grammar along with translation rules.
$$\begin{aligned} & S \rightarrow S_{1} \# T \qquad \{S._{\text{val}} =S_{1}. _{\text{val}} \; ^{\ast} T._{\text{val}}\}\\ & S \rightarrow T \qquad \qquad \{S._{\text{val}} = T._{\text{val}}\}\\  & T \rightarrow T_{1} \% R \qquad  \{T._{\text{val}} =T_{1}._{\text{val}} ÷ R._{\text{val}}\}\\  & T \rightarrow R \qquad \qquad \{T._{\text{val}} = R._{\text{val}}\} \\ & R \rightarrow \text{id} \qquad \qquad \{R._{\text{val}} = \text{id}._{\text{val}}\} \end{aligned}$$
Here $\#$ and $\%$ are operators and $\text{id}$ is a token that represents an integer and $\text{id}._{\text{val}}$ represents the corresponding integer value. The set of non-terminals is $\{\text{S, T, R, P}\}$ and a subscripted non-terminal indicates an instance of the non-terminal.

Using this translation scheme, the computed value of $S._{\text{val}}$ for root of the parse tree for the expression $20 \# 10 \% 5 \# 8 \% 2 \% 2$ is ________________.

4 Answers

Best answer
43 43 votes

 

Final Answer: $80$


General Rules

Operators which are deeper in the parse tree have higher precedence, since they are tried by the parser first.

  • Left-recursive rules indicate left associativity.
  • Right-recursive rules indicate right associativity.

Mapping of Operators

$\#$ corresponds to operation $*$
$\%$ corresponds to operation $\div$

Both $*$ and $\div$ are left associative, and $\div$ has higher precedence.


Given Expression

$20\#10\%5\#8\%2\%2$

Replacing operators with their actual meanings:

$\equiv 20 * (10 \div 5) * ((8 \div 2) \div 2)$


Evaluation using Precedence and Associativity

$\equiv 20 * \underbrace{(10 \div 5)}_{\text{= 2}} * \underbrace{((8 \div 2) \div 2)}_{\text{= (4 ÷ 2) = 2}}$

$\Rightarrow 20 * 2 * 2$

$\Rightarrow \underbrace{(20 * 2)}_{\text{= 40}} * 2$

$\Rightarrow 80$


Final Computed Value

$\boxed{S.val = 80}$

Hence, the value of the given expression is $80$.


Concept Summary

  • Operator precedence is governed by depth in the parse tree.
  • Left recursion ⟶ Left associativity
  • Right recursion ⟶ Right associativity
  • $\div$ binds tighter than $*$, hence evaluated first.
edited by
7 7 votes

Given that, 20#10%5#8%2%2

if we observe the grammar, we can understand the following points

  1. # has less priority than %
  2. % has left associative.
  3. # has left associative.

So we have to evaluate the given expression as : $\underbrace{\color{red}{(} \color{blue}{(}\underbrace{20\#(\underbrace{10\%5}}) \color{blue}{)} \# \underbrace{\color{green}{(} \color{cyan}{(}\underbrace{8\%2\color{cyan}{)}} \%2\color{green}{)}}\color{red}{)}}$

$= \underbrace{\color{red}{(} \underbrace{\color{blue}{(}20\#2\color{blue}{)}}\#2\color{red}{)}}=80$

4 4 votes

just simple rule 

which operator  is more away from start symbol  its  have high precedence 

and associtivity checked by  given production of the terminal if it is begining of  the RHS side of the  production  then it is left associativity  and end  side  of the  given RHS side production then it is  right associativity

According to given Question  

seeing grammer 

# operator come before % operator from the start symbol 

so precedence of % operator is > # operator 

and associativity of % is left and # is also left

given semantic action % equivalent to devision

and                           # equivalent to  product

lets  given string 

20#10%5#8%2%2

lets parenthesis according to precedence and associativity

[{20#(10%5)}#{((8%2)%2)}]

[{20#2}#{(4%2)}]

[{20#2}#2}]

80

0 0 votes

Associativity decreases Top to Down  OR  Increases  Down to Top

1.Replace the symbols with the Actions ;   here   # ---> *   and    % ----> /
2.Check the productions if Left recursive or Right recursive , accordingly apply brackets left associative or right associative respectively.
 

Here  *  and  /   both are left associative since left recursive grammar.

So, => 20 * 10 / 5 * 8 / 2 / 2 

      => [ { 20 * (10 / 5) } * ( (8 / 2) / 2 ) ]   

      =>  [ {20 * 2} * 2 ] =  80   Ans

Answer:
Position:
Show:

Related questions

21 21 votes
3 answers 3 answers
16.4k
16.4k views
Arjun asked Feb 15, 2022
16,424 views
Consider the augmented grammar with $\{ +, {\ast}, (,),\text{id} \}$ as the set of terminals.$S’ \rightarrow S$$S \rightarrow S + R\; |\; R$$R \rightarrow R {\ast} P \;| ...
25 25 votes
2 answers 2 answers
17.3k
17.3k views
Arjun asked Feb 15, 2022
17,291 views
Which one of the following statements is $\text{TRUE}?$The $\textit{LALR}(1)$ parser for a grammar $\textit{G}$ cannot have reduce-reduce conflict if the $\textit{LR}(1)$...
69 69 votes
8 8 answers
33.4k
33.4k views
Arjun asked Feb 15, 2022
33,437 views
Consider the relational database with the following four schemas and their respective instances.Student(sNo, sName, dNo) Dept(dNo, dName)Course(cNo, cName, dNo) Register(...
53 53 votes
5 answers 5 answers
23.1k
23.1k views
Arjun asked Feb 15, 2022
23,066 views
Consider a network with three routers $\text{P, Q, R}$ shown in the figure below. All the links have cost of unity.The routers exchange distance vector routing informatio...