• edited by
924 views
2 2 votes

Image of shift reduce

In the above grammar,

1. how to determine the precedence of the operator?

2. If there is shift and reduce conflict, in who's favor to resolve?

3. Why are we not reducing E-->E+E, instead we are shifting. But incase of E-->id, we are reducing.

Consider the grammar
\[
\begin{array}{l}
\mathrm{E} \rightarrow \mathrm{E}+\mathrm{E} \\
\mathrm{E} \rightarrow \mathrm{E}^{*} \mathrm{E} \\
\mathrm{E} \rightarrow(\mathrm{E}) \\
\mathrm{E} \rightarrow \text { id }
\end{array}
\]
\begin{tabular}{|l|l|l|l|}
\hline \multirow{6}{*}{} & Stack & Input & Action \\
\hline & \$ & $\mathrm{id}_{1}+\mathrm{id}_{2}{ }^{\text {\% }} \mathrm{id}_{3} \mathrm{~S}$ & shift \\
\hline & Sid ${ }_{1}$ & $+\mathrm{id}_{2} * \mathrm{id}_{3} \mathrm{~S}$ & reduce by $\mathrm{E} \rightarrow$ id \\
\hline & SE & +id2*id3S & shift \\
\hline & SE + & $\mathrm{id}_{2}{ }^{*} \mathrm{id}_{3} \mathrm{~S}$ & shift \\
\hline \multirow[b]{2}{*}{Why are we not} & $\mathrm{SE}+\mathrm{id}_{2}$ & * ids ${ }^{\text {S }}$ & reduce by $\mathrm{E} \rightarrow$ id \\
\hline & SE + E & * $\mathrm{id}_{3}$ S & shift \\
\hline \multirow[t]{4}{*}{reducing $\mathrm{E} \rightarrow \mathrm{E}+\mathrm{E}$ ?} & SE + E* & $\mathrm{id}_{3} 5$ & shift \\
\hline & SE $+\mathrm{E} * \mathrm{id}_{3}$ & \$ & reduce by $\mathrm{E} \rightarrow$ id \\
\hline & SE + E * E & S & reduce by $\mathrm{E} \rightarrow \mathrm{E} * \mathrm{E}$ \\
\hline & SE + E & S & reduce by $\mathrm{E} \rightarrow \mathrm{E}+\mathrm{E}$ \\
\hline +id2* & SE & S & accept \\
\hline \multicolumn{4}{|l|}{parser} \\
\hline
\end{tabular}
and the input string id $+i d_{2}$ *
ids. Use the shiff-reduce parser
to check whether the input
Here reduction is done, which gives preference to * over +. But in
string is accepted by the the grammar, no such precedence is specified.

1 Answer

0 0 votes
when you can not derive any precedence out of the gramaar rules, use the general precedence of operators.

which is id > * > + > $

now if you know how the above algorithm works you will certainly know why i) has not been reduced and ii) also not reduced
Position:
Show:

Related questions

1 1 vote
1 1 answer
2.4k
2.4k views
shikharV asked Jan 14, 2016
2,357 views
Given solution:After reducing two 1's of expression to E, E*E should be reduced not the 3rd 1. So final output will be 112*311+2 instead of the given output. Please check...
0 0 votes
1 1 answer
590
590 views
Sourabh Kumar asked Jan 14, 2016
590 views
Perform a shift - reduce parser of the input aaa $+a^{*}+$ according to the grammar?\[\mathrm{A} \rightarrow \mathrm{AA}+\left|\mathrm{AA}^{*}\right| \mathrm{a}\]Show the...
1 1 vote
5 answers 5 answers
9.8k
9.8k views
0 0 votes
1 1 answer
1.2k
1.2k views
Souvik33 asked Dec 11, 2022
1,181 views
Consider the following statements regarding the “Parsing Table” of a shift reduce parser, for any given grammarNo. of states/ size of parse table is same for all Shift-Re...