edited by
19,483 views
38 38 votes

Consider the following expression grammar. The semantic rules for expression evaluation are stated next to each grammar production.$$\begin{array}{l|l} E\rightarrow number & E.val = {number.val} \\\qquad \mid \ E \ \ ‘+\text{'} \ E & E^{(1)}.val = E^{(2)}.val + E^{(3)}.val     \\\qquad \mid \ E \ \ ‘\times\text{'} \  E & E^{(1)}.val = E^{(2)}.val \times E^{(3)}.val  \end{array}$$

Assume the conflicts of this question are resolved using yacc tool and an LALR(1) parser is generated for parsing arithmetic expressions as per the given grammar. Consider an expression $3 \times 2 + 1$. What precedence and associativity properties does the generated parser realize?

  1. Equal precedence and left associativity; expression is evaluated to $7$

  2. Equal precedence and right associativity; expression is evaluated to $9$

  3. Precedence of ‘$\times$’ is higher than that of ‘$+$’, and both operators are left associative; expression is evaluated to $7$

  4. Precedence of ‘$+$’ is higher than that of ‘$\times$’, and both operators are left associative; expression is evaluated to $9$

3 Answers

Best answer
68 68 votes

LALR Parser is type of Bottom up Parser which uses Right most Derivation

For $3×2+1$

$E \rightarrow E * E$ (Both shift and reduce possible but yacc prefers shift)

     $ \rightarrow E * E + E$ 

     $ \rightarrow E * E + 1$

     $ \rightarrow E * 2 + 1$

     $ \rightarrow E * 3$

     $ \rightarrow 3 * 3$

     $ \rightarrow 9$

All the productions are in same level therefore all have same precedence

Therefore Ans is B. Equal precedence and right associativity; expression is evaluated to 9.

edited by
1 1 vote

None of the Options is correct here ... The answer has to be "precedence of + is higher than * " and "both * and + are right associative"

Here we will never come across an RR conflict because we dont have 2 productions with the same RHS but different LHS ... 

EX : In the grammar, 

S->A/a, 

A->a
 

we have 2 productions with the same RHS (which is a) but different LHS (S and A) ... Now while parsing a string I might come across a single state with productions as A->a. and S->a. Now this state will create a conflict on whether should I reduce string "a" to S or A ... So clearly there is an RR conflict here .... 

But in the given grammar it is not the case ... 

While parsing a string say "num+num*num" from the above grammar,I will come across an SR conflict ... When ?? after scanning num+num , I have a choice on whether should I shift on * (as good as giving higher precedence to * over +) or reduce "num+num" to E (as good as giving higher precedence to + over *) ... So here there is an SR conflict ... 

YACC tool always goes in-favour of SHIFT incase of SR conflict (and first reduce incase of RR conflict) ...

So,since we are using YACC to resolve conflicts, here + will be given higher precedence over * but incase if we come across a string like 2+3+5 , it will be right associative ... 

None of the Options is correct here ...

edited by
1 flag:
✌ Low quality ( “Wrong Answer”)
Answer:
Position:
Show:

Related questions

197 197 votes
8 answers 8 answers
76.5k
76.5k views
Kathleen asked Sep 22, 2014
76,484 views
A $5$ stage pipelined CPU has the following sequence of stages:IF – instruction fetch from instruction memoryRD – Instruction decode and register readEX – Execute: ALU op...
32 32 votes
3 answers 3 answers
23.2k
23.2k views
Kathleen asked Sep 22, 2014
23,193 views
Consider the grammar:$$S \rightarrow (S) \mid a$$Let the number of states in SLR (1), LR(1) and LALR(1) parsers for the grammar be $n_1, n_2$ and $n_3$ respectively. The ...
44 44 votes
4 answers 4 answers
12.3k
12.3k views
Ishrat Jahan asked Nov 3, 2014
12,264 views
Consider the context-free grammar$E\rightarrow E+E$$E\rightarrow (E *E)$$E\rightarrow \text{id}$where $E$ is the starting symbol, the set of terminals is $\{id, (,+,),*\}...
32 32 votes
3 answers 3 answers
15.2k
15.2k views
gatecse asked Sep 21, 2014
15,211 views
Let $f(x)$ be the continuous probability density function of a random variable $x$, the probability that $a < x \leq b$, is :$f(b-a)$$f(b) - f(a)$$\int\limits_a^b f(x) dx...