| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Sat, 02 Dec 2023 21:37:04 +0000 | |
| changeset 957 | 03c5a8987141 | 
| parent 955 | ca67172b8fa1 | 
| child 958 | 6caee1c0222e | 
| permissions | -rw-r--r-- | 
| 59 | 1 | \documentclass{article}
 | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
194diff
changeset | 2 | \usepackage{../style}
 | 
| 681 | 3 | \usepackage{../grammar}
 | 
| 955 | 4 | \usepackage{../graphics}
 | 
| 59 | 5 | |
| 6 | \begin{document}
 | |
| 7 | ||
| 8 | \section*{Homework 7}
 | |
| 9 | ||
| 916 | 10 | %\HEADER | 
| 359 
db106e5b7c4d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
305diff
changeset | 11 | |
| 59 | 12 | \begin{enumerate}
 | 
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 13 | \item Suppose the context-sensitive grammar | 
| 59 | 14 | |
| 15 | \begin{center}
 | |
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 16 | \begin{tabular}{lcl}
 | 
| 897 | 17 | $S$ & $::=$ & $bSAA\;|\; \epsilon$\\ | 
| 18 | $A$ & $::=$ & $a$\\ | |
| 19 | $bA$ & $::=$ & $Ab$\\ | |
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 20 | \end{tabular}
 | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 21 | \end{center}
 | 
| 183 
b17eff695c7f
added new stuff
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
102diff
changeset | 22 | |
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 23 | where $S$ is the starting symbol of the grammar. | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 24 | Give a derivation of the string $"\!aaabaaabb"$. | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 25 | What can you say about the number of as and bs in the | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 26 | strings recognised by this grammar. | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 27 | |
| 955 | 28 | \solution{
 | 
| 29 | S -> bSAA -> bbSAAAA -> | |
| 30 | bbbSAAAAAA -> | |
| 31 | bbbAAAAAA -> | |
| 32 | bbAbAAAAA -> .. -> | |
| 33 | bbAAAAAAb -> .. -> AAAbAAAbb -> .. -> aaabaaabb | |
| 34 | } | |
| 35 | ||
| 59 | 36 | |
| 37 | \item Consider the following grammar | |
| 38 | ||
| 681 | 39 | \begin{plstx}[margin=1cm]
 | 
| 40 |   : \meta{S\/} ::= \meta{N\/}\cdot \meta{P\/}\\
 | |
| 41 |   : \meta{P\/} ::= \meta{V\/}\cdot \meta{N\/}\\
 | |
| 42 |   : \meta{N\/} ::= \meta{N\/}\cdot \meta{N\/}\\
 | |
| 43 |   : \meta{N\/} ::= \meta{A\/}\cdot \meta{N\/}\\
 | |
| 44 |   : \meta{N\/} ::= \texttt{student} \mid \texttt{trainer} \mid \texttt{team} \mid \texttt{trains}\\
 | |
| 45 |   : \meta{V\/} ::= \texttt{trains} \mid \texttt{team}\\
 | |
| 46 |   : \meta{A\/} ::= \texttt{The} \mid \texttt{the}\\
 | |
| 47 | \end{plstx}
 | |
| 48 | ||
| 59 | 49 | |
| 50 | where $S$ is the start symbol and $S$, $P$, $N$, $V$ and $A$ are non-terminals. | |
| 51 | Using the CYK-algorithm, check whether or not the following string can be parsed | |
| 52 | by the grammar: | |
| 53 | ||
| 54 | \begin{center}
 | |
| 55 | \texttt{The trainer trains the student team}
 | |
| 56 | \end{center}
 | |
| 57 | ||
| 955 | 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 | ||
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 121 | \item Transform the grammar | 
| 59 | 122 | |
| 60 | 123 | \begin{center}
 | 
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 124 | \begin{tabular}{lcl}
 | 
| 897 | 125 | $A$ & $::=$ & $0A1 \;|\; BB$\\ | 
| 126 | $B$ & $::=$ & $\epsilon \;|\; 2B$ | |
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 127 | \end{tabular}
 | 
| 60 | 128 | \end{center}
 | 
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 129 | |
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 130 | \noindent | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 131 | into Chomsky normal form. | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 132 | |
| 955 | 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 | } | |
| 157 | ||
| 305 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 158 | \item Consider the following grammar $G$ | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 159 | |
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 160 | \begin{center}
 | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 161 | \begin{tabular}{l}
 | 
| 897 | 162 | $S ::= \texttt{if0} \cdot E \cdot \texttt{then} \cdot S$\\
 | 
| 163 | $S ::= \texttt{print} \cdot S$\\
 | |
| 164 | $S ::= \texttt{begin} \cdot B\cdot \texttt{end}$\\
 | |
| 165 | $B ::= S\cdot \texttt{;}$\\
 | |
| 166 | $B ::= S\cdot \texttt{;} \cdot B$\\
 | |
| 167 | $S ::= num$\\ | |
| 168 | $E ::= num$\\ | |
| 169 | $B ::= num$ | |
| 305 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 170 | \end{tabular}
 | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 171 | \end{center}
 | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 172 | |
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 173 | where $S$ is the start symbol and $S$, $E$ and $B$ are | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 174 | non-terminals. | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 175 | |
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 176 | Check each rule below and decide whether, when added to $G$, | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 177 | the combined grammar is ambiguous. If yes, give a string that | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 178 | has more than one parse tree. | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 179 | |
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 180 | \begin{center}
 | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 181 | \begin{tabular}{rl}
 | 
| 897 | 182 | (i)   & $S ::= \texttt{if0} \cdot E\cdot \texttt{then} \cdot S\cdot \texttt{else} \cdot S$\\
 | 
| 183 | (ii) & $B ::= B \cdot B$\\ | |
| 184 | (iii) & $E ::= ( \cdot E \cdot )$\\ | |
| 185 | (iv) & $E ::= E \cdot + \cdot E$ | |
| 305 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 186 | \end{tabular}
 | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 187 | \end{center}
 | 
| 
23045b2b0b7b
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
292diff
changeset | 188 | |
| 955 | 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 | } | |
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 195 | |
| 897 | 196 | \item Suppose the string $``9-5+2''$. Give all ASTs that | 
| 197 | the following two grammars generate for this string. | |
| 198 | ||
| 199 | Grammar 1, where List is the starting symbol: | |
| 200 | ||
| 201 | \begin{center}
 | |
| 202 | \begin{tabular}{lcl}
 | |
| 203 | $List$ & $::=$ & $List + Digit \mid List - Digit \mid Digit$\\ | |
| 204 | $Digit$ & $::=$ & $0 \mid 1 \mid 2 \mid 3 \mid 4 \mid 5 \mid 6 \mid 7 \mid 8 \mid 9$ | |
| 205 | \end{tabular}
 | |
| 206 | \end{center}
 | |
| 207 | ||
| 208 | Grammar 2, where String is the starting symbol: | |
| 209 | ||
| 210 | \begin{center}
 | |
| 211 | \begin{tabular}{@{}lcl@{}}
 | |
| 212 | $String$ & $::=$ & $String + String \mid String - String \mid$\\ | |
| 213 | & & $0 \mid 1 \mid 2 \mid 3 \mid 4 \mid 5 \mid 6 \mid 7 \mid 8 \mid 9$ | |
| 214 | \end{tabular}
 | |
| 215 | \end{center}
 | |
| 216 | ||
| 955 | 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 | } | |
| 897 | 223 | |
| 224 | ||
| 194 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 225 | %\item {\bf (Optional)} The task is to match strings where the letters are in alphabetical order---for example, 
 | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 226 | %\texttt{abcfjz} would pass, but \texttt{acb} would not. Whitespace should be ignored---for example
 | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 227 | %\texttt{ab c d} should pass. The point is to try to get the regular expression as short as possible!
 | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 228 | %See: | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 229 | |
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 230 | %\begin{center}
 | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 231 | %\url{http://callumacrae.github.com/regex-tuesday/challenge11.html}
 | 
| 
90796ee3c17a
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
183diff
changeset | 232 | %\end{center}
 | 
| 59 | 233 | \end{enumerate}
 | 
| 234 | ||
| 235 | \end{document}
 | |
| 236 | ||
| 237 | %%% Local Variables: | |
| 238 | %%% mode: latex | |
| 239 | %%% TeX-master: t | |
| 240 | %%% End: | |
| 955 | 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 |