edited by
6,290 views
7 votes
7 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 ________________.
edited by

3 Answers

Best answer
18 votes
18 votes

Answer: 80

General rules – 

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

Left-recursive rules indicates left associativity

Right-recursive rules indicates right associativity

---------

In the question – 

$\#$ has corresponding operation: $*$

$\%$ has corresponding operation: $\div$

$*$ and $\div$ both are left associative and $\div$ is having higher precedence.

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

$\equiv20∗(10÷5)∗((8÷2)÷2) $ 

$\equiv 20∗2∗2 \\
=  80$

edited by
5 votes
5 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$

0 votes
0 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

Answer:

Related questions

7.7k
views
3 answers
6 votes
Arjun asked Feb 15, 2022
7,722 views
Consider the augmented grammar with $\{ +, {\ast}, (,),\text{id} \}$ as the set of terminals.$S' \rightarrow S$ ... $\textit{goto(closure}(I_{0}), +)$ contains exactly ______________ items.
13.6k
views
5 answers
25 votes
Arjun asked Feb 15, 2022
13,643 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) ... number of rows returned by the above $\text{SQL}$ query is ____________.
10.4k
views
2 answers
23 votes
Arjun asked Feb 15, 2022
10,353 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 ... $\text{Q},$ leading to count-to-infinity problem, is _______________.
7.9k
views
3 answers
20 votes
Arjun asked Feb 15, 2022
7,942 views
Let $\textit{G(V,E)}$ be a directed graph, where $\textit{V} = \{ 1, 2, 3, 4, 5 \}$ is the set of vertices and $\textit{E}$ is ... other vertex in $V.$ The number of such directed spanning trees rooted at vertex $5$ is __________________.