\documentclass[12pt]{exam}
\renewcommand{\solutiontitle}{\noindent\textbf{Answer:}\par\noindent}
\pagestyle{headandfoot}
\header{CS 460/660}{Handout WA4}{}
\footrule
\headrule
\footer{}{Page \thepage\ of \numpages}
  {\iflastpage{End of Assignment}{Please go on to the next page\ldots}}
  
  \usepackage{graphicx}
%\printanswers

\begin{document}
This assignment asks you to prepare written answers to questions on
bottom-up parsing.  The answers are longer but more mechanical than 
on previous assignments.
You may discuss this assignment with other students and work
the problems together.  However, your writeup should be your own
individual work.  Remember written assignments are to be turned in in
class on the date due.


One of the least loved and least understood aspects of the C 
programming language is its type declarators.  We can improve
on the latter problem by considering the following simplified
declarator grammar.
\begin{center}
{\tt
\begin{tabular}{lll}
S 	&$\rightarrow$ 	& T P \\
 \\
T 	&$\rightarrow$ 	& int | char \\
 \\
P 	&$\rightarrow$ 	& *P | D \\
\\
D	&$\rightarrow$ 	& id | D()
\end{tabular}
}
\end{center}

In this grammar, $S$ is the start symbol, $T$ is a $C$ type, 
$P$ is a pointer declarator, and $D$ is an ordinary declarator.  
The terminals are the punctuation marks, ``int'', and ``char''.

	\begin{questions}
	\question  Give the LR(0) item sets, and the DFA of the 
		LR(0) item sets for this grammar. \\
		Please show all of your work.
		\begin{solution}
		\begin{itemize}
		\item \underline{{\bf 0}}
			\begin{itemize}
			\item Z $->$ .S
			\item S $->$ .TP
			\item T $->$ .int
			\item T $->$ .char
			\end{itemize}
		\item \underline{{\bf 1 (0,S)}}
			\begin{itemize}
			\item {\it Z $->$ S.}
			\end{itemize}
		\item \underline{{\bf 2 (0,T)}}
			\begin{itemize}
			\item S $->$ T.P
			\item P $->$ .*P
			\item P $->$ .D
			\item D $->$ .id
			\item D $->$ .D()
			\end{itemize}
		\item \underline{{\bf 3 (0,int)}}
			\begin{itemize}
			\item {\it T $->$ int.}
			\end{itemize}
		\item \underline{{\bf 4 (0,char)}}
			\begin{itemize}
			\item {\it T $->$ char.}
			\end{itemize}
		\item \underline{{\bf 5 (2,P)}}
			\begin{itemize}
			\item {\it S $->$ TP.}
			\end{itemize}
		\item \underline{{\bf 6 (2,*)}}
			\begin{itemize}
			\item P $->$ *.P
			\item P $->$ .*P
			\item P $->$ .D
			\item D $->$ .id
			\item D $->$ .D()
			\end{itemize}
		\item \underline{{\bf 7 (2,D)}}
			\begin{itemize}
			\item {\it P $->$ D.}
			\item D $->$ D.()
			\end{itemize}
		\item \underline{{\bf 8 (2,id)}}
			\begin{itemize}
			\item {\it D $->$ id.}
			\end{itemize}
		\item \underline{{\bf 9 (6,P)}}
			\begin{itemize}
			\item {\it P $->$ *P.}
			\end{itemize}
		\item \underline{{\bf (6,*) = 6}}
		\item \underline{{\bf (6,D) = 7}}		
		\item \underline{{\bf (6,id) = 8}}
		\item \underline{{\bf 10 (7,( )}}
			\begin{itemize}
			\item D $->$ D(.)
			\end{itemize}
		\item \underline{{\bf 11 (10,) )}}
			\begin{itemize}
			\item {\it D $->$ D().}
			\end{itemize}

		\end{itemize}
		\end{solution}
		
	\question  Is this grammar SLR(1)? Why or why not?
		\begin{solution}
		Yes. \\
		You must build the table to say this - and in order to build the table 
		you have to build all the follow sets to build the table.
		
		{\bf First Sets} (only those needed)
		\begin{itemize}
		\item First(P) = \{ * \} + ( First(D) - \{ $\epsilon$ \} ) = \{ * , id \}
		\item First(D) = \{ id \}
		\end{itemize}
		{\bf First Sets} (those not needed)
		\begin{itemize}
			\item First(S) = First(T) - \{ $\epsilon$ \} = \{ int, char \}
			\item First(T) = \{ int, char \}
		\end{itemize}
		
		{\bf Follow Sets}
		\begin{itemize}
		\item Follow(S) = \{ \verb+$+ \}
		\item Follow(T) = First(P) - \{ $\epsilon$ \} = \{ * \} + ( First(D) - \{ $\epsilon$ \} ) = \{ * , id \}
		\item Follow(P) = Follow(S) = \{ \verb+$+ \}
		\item Follow(D) = Follow(P) + \{ ( \} = \{ \verb+$+  ( \}
		\end{itemize}
		
		{\bf Number the Productions}
		
		{\tt
\begin{tabular}{lll}
(0) Z 	&$\rightarrow$ 	& S \\
(1) S 	&$\rightarrow$ 	& T P \\
 \\
(2) T 	&$\rightarrow$ 	& int \\
(3) 		&								& | char \\
 \\
(4) P 	&$\rightarrow$ 	& *P  \\
(5)		&								&	| D \\
\\
(6) D	&$\rightarrow$ 	& id  \\
(7) 		&							&  | D()
\end{tabular}
}

		{\bf Build the Table}
		
		\begin{tabular}{|c|c|c|c|c|c|c|c||c|c|c|c|}\hline
		State & id & * & ( & ) & int & char & \verb+$+ & S & T & P & D \\ \hline \hline
		0     &    &   &   &   & s3  & s4   &          & 1 & 2 &   &   \\ \hline
		1     &    &   &   &   &     &      & acc      &   &   &   &   \\ \hline
		2     & s8 & s6&   &   &     &      &          &   &   & 5 & 7 \\ \hline
		3     & r2 & r2&   &   &     &      &          &   &   &   &   \\ \hline
		4     & r3 & r3&   &   &     &      &          &   &   &   &   \\ \hline
		5     &    &   &   &   &     &      & r1       &   &   &   &   \\ \hline
		6     & s8 & s6&   &   &     &      &          &   &   & 9 & 7 \\ \hline
		7     &    &   &s10&   &     &      & r5       &   &   &   &   \\ \hline
		8     &    &   & r6&   &     &      & r6       &   &   &   &   \\ \hline
		9     &    &   &   &   &     &      & r4       &   &   &   &   \\ \hline
		10    &    &   &   &s11&     &      &          &   &   &   &   \\ \hline
		11    &    &   & r7&   &     &      & r7       &   &   &   &   \\ \hline
		\end{tabular} 
		\end{solution}

	\question  Show the sequence of moves of an SLR(1) parser on 
		the following input.  Resolve any shift-reduce conflicts
		in favor of shifting. \\
		\verb+ char * id()+
		\begin{solution}
		\begin{tabular}{l|c|c|l}
		Stack															& Input			& Entry	& Action \\\hline
		\verb+$+ 0												& char			&	s4	& Shift, enter state 4 \\
		\verb+$+ 0 char 4									& *					&	r3	& Reduce by (3) T $->$ char Also[0,T]=2 \\
		\verb+$+ 0 T 2										& *					&	s6	& Shift, enter state 6 \\
		\verb+$+ 0 T 2 * 6								& id				&	s8	& Shift, enter state 8 \\
		\verb+$+ 0 T 2 * 6 id 8						& (					&	r6	& Reduce by (6) D $->$ id Also [6,D] = 7 \\
		\verb+$+ 0 T 2 * 6 D 7						& (					&	s10	& Shift, enter state 10 \\
		\verb+$+ 0 T 2 * 6 D 7 ( 10 			& )					&	s11	& Shift, enter state 11 \\
		\verb+$+ 0 T 2 * 6 D 7 ( 10 ) 11	& \verb+$+	&	r7	& Reduce by (7) D $->$ D() Also [6,D] = 7 \\
		\verb+$+ 0 T 2 * 6 D 7						& \verb+$+	&	r5	& Reduce by (5) P $->$ D() Also [6,P] = 9 \\
		\verb+$+ 0 T 2 * 6 P 9						& \verb+$+	&	r4	& Reduce by (4) P $->$ *P  Also [2,P] = 5 \\
		\verb+$+ 0 T 2 P 5								& \verb+$+	&	r1	& Reduce by (1) S $->$ TP  Also [0,S] = 1 \\
		\verb+$+ 0 S 1										& \verb+$+	&	acc	& Accept \\
		\end{tabular}
		\end{solution}

	\end{questions}
\end{document}

