Given grammar:
$\begin{align*} &E \rightarrow \text{num} \\ &E \rightarrow E + E\mid E*E \\ \end{align*}$
First $LR(1) \text{ item}:E^{\prime} \rightarrow \bullet E \ \text{,} \$ $
$\textbf{YACC default action on SR: Choose SHIFT action}$
While parsing $3*2+1,$ at some point of time stack content $ :\begin{array}{|c|} \hline 1\\+\\ 2\\ *\\3\\ \hline\end{array}$
Then reduce handles one by one to generate output $=9.$
- num does not create any conflict.
- Additionally here no states differ by lookahead symbols only.
- $\Rightarrow$ $\text{LALR(1) and LR(1)}$ tables are same.
- $LR(1)$ table only for state0 and state1:
So total $2+2 = 4$ SR conflict originated in two states of the DFA.
- Shift-reduce conflict: Yacc’s default action in the case of a shift-reduce conflict is to choose the shift action.
- Reduce-reduce conflict : Yacc’s default action in the case of a reduce-reduce conflict is to reduce using the production that comes first, textually, in the input grammar specification.
and LEX-YACC-gcc output after implementing the given grammar :
As we can see from the output reduction on $E \rightarrow \text{num}$ is carried out as soon as top of stack contains a num. So, no conflict related to $E \rightarrow num$.
one example : Because of YACC shift preference, even if $3*2$ ($E*E$) handle found on top of the stack at some point of time, it will shift on reading $+$ instead of reducing with $E\rightarrow E * E$. In this way, the complete input will be pushed into the stack. After that only reduce work starts as shown below.
- Equal precedence because of the given grammar $E\rightarrow E+E \ | \ E*E$ , (single level)
- and Right associativity :
Here are the required files (calc.l and calc.y) to regenerate the above interpreter.
Correct Answer: $C$