hws/hw07.tex
changeset 956 ae9782e62bdd
parent 916 10f834eb0a9e
child 959 64ec1884d860
equal deleted inserted replaced
955:47acfd7f9096 956:ae9782e62bdd
     1 \documentclass{article}
     1 \documentclass{article}
     2 \usepackage{../style}
     2 \usepackage{../style}
     3 \usepackage{../grammar}
     3 \usepackage{../grammar}
       
     4 \usepackage{../graphics}
     4 
     5 
     5 \begin{document}
     6 \begin{document}
     6 
     7 
     7 \section*{Homework 7}
     8 \section*{Homework 7}
     8 
     9 
    21 
    22 
    22 where $S$ is the starting symbol of the grammar. 
    23 where $S$ is the starting symbol of the grammar. 
    23 Give a derivation  of the string $"\!aaabaaabb"$. 
    24 Give a derivation  of the string $"\!aaabaaabb"$. 
    24 What can you say about the number of as and bs in the
    25 What can you say about the number of as and bs in the
    25 strings recognised by this grammar.
    26 strings recognised by this grammar.
       
    27 
       
    28 \solution{
       
    29   S -> bSAA ->  bbSAAAA ->
       
    30   bbbSAAAAAA ->
       
    31   bbbAAAAAA ->
       
    32   bbAbAAAAA -> .. ->
       
    33   bbAAAAAAb -> .. -> AAAbAAAbb -> .. -> aaabaaabb
       
    34   }
    26 
    35 
    27 
    36 
    28 \item Consider the following grammar 
    37 \item Consider the following grammar 
    29 
    38 
    30 \begin{plstx}[margin=1cm]
    39 \begin{plstx}[margin=1cm]
    44 
    53 
    45 \begin{center}
    54 \begin{center}
    46 \texttt{The trainer trains the student team}
    55 \texttt{The trainer trains the student team}
    47 \end{center}
    56 \end{center}
    48 
    57 
       
    58 \solution{
       
    59 \begin{center}
       
    60   \begin{tikzpicture}[scale=0.7,line width=0.8mm]
       
    61   \draw (-2,0) -- (4,0);
       
    62   \draw (-2,1) -- (4,1);
       
    63   \draw (-2,2) -- (3,2);
       
    64   \draw (-2,3) -- (2,3);
       
    65   \draw (-2,4) -- (1,4);
       
    66   \draw (-2,5) -- (0,5);
       
    67   \draw (-2,6) -- (-1,6);
       
    68   
       
    69   \draw (0,0) -- (0, 5);
       
    70   \draw (1,0) -- (1, 4);
       
    71   \draw (2,0) -- (2, 3);
       
    72   \draw (3,0) -- (3, 2);
       
    73   \draw (4,0) -- (4, 1);
       
    74   \draw (-1,0) -- (-1, 6);
       
    75   \draw (-2,0) -- (-2, 6);
       
    76   
       
    77   \draw (-1.5,-0.5) node {\footnotesize{}\texttt{The}}; 
       
    78   \draw (-0.5,-1.0) node {\footnotesize{}\texttt{trainer}}; 
       
    79   \draw ( 0.5,-0.5) node {\footnotesize{}\texttt{trains}}; 
       
    80   \draw ( 1.5,-1.0) node {\footnotesize{}\texttt{the}}; 
       
    81   \draw ( 2.5,-0.5) node {\footnotesize{}\texttt{student}}; 
       
    82   \draw ( 3.5,-1.0) node {\footnotesize{}\texttt{team}};
       
    83   
       
    84   \draw (-1.5,0.5) node {$A$}; 
       
    85   \draw (-0.5,0.5) node {$N$}; 
       
    86   \draw ( 0.5,0.5) node {$N,\!V$}; 
       
    87   \draw ( 1.5,0.5) node {$A$}; 
       
    88   \draw ( 2.5,0.5) node {$N$}; 
       
    89   \draw ( 3.5,0.5) node {$N,\!V$};
       
    90 
       
    91   \draw (-1.5,1.5) node {$N$}; 
       
    92   \draw (-0.5,1.5) node {$N$}; 
       
    93   \draw ( 0.5,1.5) node {$$}; 
       
    94   \draw ( 1.5,1.5) node {$N$}; 
       
    95   \draw ( 2.5,1.5) node {$N$};
       
    96 
       
    97   \draw (-1.5,2.5) node {$N$}; 
       
    98   \draw (-0.5,2.5) node {$ $}; 
       
    99   \draw ( 0.5,2.5) node {$N,\!P$}; 
       
   100   \draw ( 1.5,2.5) node {$N$};
       
   101 
       
   102   \draw (-1.5,3.5) node {$$}; 
       
   103   \draw (-0.5,3.5) node {$N,\!S$}; 
       
   104   \draw ( 0.5,3.5) node {$N,\!P$};
       
   105 
       
   106   \draw (-1.5,4.5) node {$N,\!S$}; 
       
   107   \draw (-0.5,4.5) node {$N,\!S$};
       
   108 
       
   109   \draw (-1.5,5.5) node {$N,\!S$}; 
       
   110 
       
   111   \draw (-2.4, 5.5) node {$1$}; 
       
   112   \draw (-2.4, 4.5) node {$2$}; 
       
   113   \draw (-2.4, 3.5) node {$3$}; 
       
   114   \draw (-2.4, 2.5) node {$4$}; 
       
   115   \draw (-2.4, 1.5) node {$5$}; 
       
   116   \draw (-2.4, 0.5) node {$6$}; 
       
   117   \end{tikzpicture}
       
   118   \end{center}
       
   119   }
       
   120 
    49 \item Transform the grammar
   121 \item Transform the grammar
    50 
   122 
    51 \begin{center}
   123 \begin{center}
    52 \begin{tabular}{lcl}
   124 \begin{tabular}{lcl}
    53 $A$ & $::=$ & $0A1 \;|\; BB$\\
   125 $A$ & $::=$ & $0A1 \;|\; BB$\\
    55 \end{tabular}
   127 \end{tabular}
    56 \end{center}
   128 \end{center}
    57 
   129 
    58 \noindent
   130 \noindent
    59 into Chomsky normal form.
   131 into Chomsky normal form.
       
   132 
       
   133 \solution{
       
   134   First one has to eliminate $\epsilon$. This means we obtain the rules:
       
   135 
       
   136   \begin{center}
       
   137     \begin{tabular}{lcl}
       
   138       $A$ & $::=$ & $0A1 \;|\; 01 \;|\; BB \;|\; B$\\
       
   139       $B$ & $::=$ & $2 \;|\; 2B$
       
   140     \end{tabular}
       
   141   \end{center}
       
   142 
       
   143   Now we have to bring the rules into CNF form by adding additional
       
   144   non-terminals, like $Z$, $O$, $T$,  and splitting up the rules into ``twos'':
       
   145 
       
   146   \begin{center}
       
   147     \begin{tabular}{lcl}
       
   148       $A$ & $::=$ & $ZC \;|\; ZO \;|\; BB \;|\; 2$\\
       
   149       $B$ & $::=$ & $2 \;|\; TB$\\
       
   150       $C$ & $::=$ & $AO$\\
       
   151       $Z$ & $::=$ & $0$\\
       
   152       $O$ & $::=$ & $1$\\
       
   153       $T$ & $::=$ & $2$\\                           
       
   154     \end{tabular}
       
   155   \end{center}   
       
   156 }
    60 
   157 
    61 \item Consider the following grammar $G$
   158 \item Consider the following grammar $G$
    62 
   159 
    63 \begin{center}
   160 \begin{center}
    64 \begin{tabular}{l}
   161 \begin{tabular}{l}
    87 (iii) & $E ::= ( \cdot E \cdot )$\\
   184 (iii) & $E ::= ( \cdot E \cdot )$\\
    88 (iv)  & $E ::= E \cdot + \cdot E$
   185 (iv)  & $E ::= E \cdot + \cdot E$
    89 \end{tabular}
   186 \end{tabular}
    90 \end{center}
   187 \end{center}
    91 
   188 
       
   189 \solution{
       
   190   (i) this is ambiguous -> it's an instance of the dangling else;
       
   191   (ii) rules of the form $B ::= B \cdot B$ are always ambiguous $B \cdot B\cdot B$
       
   192   (iii) this is fine
       
   193   (iv) same as (ii) $E\cdot + \cdot E \cdot + \cdot E$
       
   194   }
    92 
   195 
    93 \item Suppose the string $``9-5+2''$. Give all ASTs that
   196 \item Suppose the string $``9-5+2''$. Give all ASTs that
    94   the following two grammars generate for this string.
   197   the following two grammars generate for this string.
    95 
   198 
    96 Grammar 1, where List is the starting symbol:
   199 Grammar 1, where List is the starting symbol:
   109   $String$ & $::=$ & $String + String \mid String - String \mid$\\
   212   $String$ & $::=$ & $String + String \mid String - String \mid$\\
   110   & & $0 \mid 1 \mid 2 \mid 3 \mid 4 \mid 5 \mid 6 \mid 7 \mid 8 \mid 9$
   213   & & $0 \mid 1 \mid 2 \mid 3 \mid 4 \mid 5 \mid 6 \mid 7 \mid 8 \mid 9$
   111 \end{tabular}
   214 \end{tabular}
   112 \end{center}
   215 \end{center}
   113 
   216 
       
   217 \solution{
       
   218   The point is that Grammar 1 is un-ambiguous, while the second is ambiguous.
       
   219   Grammar 1 parses the strings as (9 - 5) + 2. Grammar 2 is ambiguous and
       
   220   there are two possibilities (9 - 5) + 2 and 9 - (5 + 2).
       
   221   
       
   222 }
   114 
   223 
   115                    
   224                    
   116 %\item {\bf (Optional)} The task is to match strings where the letters are in alphabetical order---for example, 
   225 %\item {\bf (Optional)} The task is to match strings where the letters are in alphabetical order---for example, 
   117 %\texttt{abcfjz} would pass, but \texttt{acb} would not. Whitespace should be ignored---for example
   226 %\texttt{abcfjz} would pass, but \texttt{acb} would not. Whitespace should be ignored---for example
   118 %\texttt{ab c d} should pass. The point is to try to get the regular expression as short as possible!
   227 %\texttt{ab c d} should pass. The point is to try to get the regular expression as short as possible!
   127 
   236 
   128 %%% Local Variables: 
   237 %%% Local Variables: 
   129 %%% mode: latex
   238 %%% mode: latex
   130 %%% TeX-master: t
   239 %%% TeX-master: t
   131 %%% End: 
   240 %%% End: 
       
   241 
       
   242 
       
   243 The| trainer trains the student A {N,S} => N 
       
   244 The trainer |trains the student N {N, P} => N S
       
   245 The trainer trains |the student N N => N 
       
   246 The trainer trains the |student 
       
   247 
       
   248 trainer |trains the student team N o {N, P} => N, S
       
   249 trainer trains| the student team N o N => N
       
   250 trainer trains the |student team 
       
   251 trainer trains the student |team {N, P} o {N, V} => N
       
   252 
       
   253 
       
   254 The| trainer trains the student team A o (N,S) => N
       
   255 The trainer| trains the student team N o (N,P) => N, S
       
   256 The trainer trains| the student team N o N => N
       
   257 The trainer trains the| student team 
       
   258 The trainer trains the student| team (N,S) o (N,V) => N