hws/hw07.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 02 Dec 2023 21:37:04 +0000
changeset 958 fddf099a82f8
parent 956 ae9782e62bdd
child 959 64ec1884d860
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 194
diff changeset
     2
\usepackage{../style}
681
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
     3
\usepackage{../grammar}
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
     4
\usepackage{../graphics}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\begin{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
\section*{Homework 7}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
916
10f834eb0a9e texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 897
diff changeset
    10
%\HEADER
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 305
diff changeset
    11
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
\begin{enumerate}
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    13
\item Suppose the context-sensitive grammar
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
\begin{center}
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    16
\begin{tabular}{lcl}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    17
$S$ & $::=$ &  $bSAA\;|\; \epsilon$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    18
$A$ & $::=$ & $a$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    19
$bA$ & $::=$ & $Ab$\\
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    20
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    21
\end{center}
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    22
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    23
where $S$ is the starting symbol of the grammar. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    24
Give a derivation  of the string $"\!aaabaaabb"$. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    25
What can you say about the number of as and bs in the
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    26
strings recognised by this grammar.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    27
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    28
\solution{
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    29
  S -> bSAA ->  bbSAAAA ->
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    30
  bbbSAAAAAA ->
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    31
  bbbAAAAAA ->
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    32
  bbAbAAAAA -> .. ->
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    33
  bbAAAAAAb -> .. -> AAAbAAAbb -> .. -> aaabaaabb
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    34
  }
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    35
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
\item Consider the following grammar 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
681
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    39
\begin{plstx}[margin=1cm]
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    40
  : \meta{S\/} ::= \meta{N\/}\cdot \meta{P\/}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    41
  : \meta{P\/} ::= \meta{V\/}\cdot \meta{N\/}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    42
  : \meta{N\/} ::= \meta{N\/}\cdot \meta{N\/}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    43
  : \meta{N\/} ::= \meta{A\/}\cdot \meta{N\/}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    44
  : \meta{N\/} ::= \texttt{student} \mid \texttt{trainer} \mid \texttt{team} \mid \texttt{trains}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    45
  : \meta{V\/} ::= \texttt{trains} \mid \texttt{team}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    46
  : \meta{A\/} ::= \texttt{The} \mid \texttt{the}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    47
\end{plstx}
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    48
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
where $S$ is the start symbol and $S$, $P$, $N$, $V$ and $A$ are non-terminals.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
Using the CYK-algorithm, check whether or not the following string can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
by the grammar:
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
\texttt{The trainer trains the student team}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    58
\solution{
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    59
\begin{center}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    60
  \begin{tikzpicture}[scale=0.7,line width=0.8mm]
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    61
  \draw (-2,0) -- (4,0);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    62
  \draw (-2,1) -- (4,1);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    63
  \draw (-2,2) -- (3,2);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    64
  \draw (-2,3) -- (2,3);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    65
  \draw (-2,4) -- (1,4);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    66
  \draw (-2,5) -- (0,5);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    67
  \draw (-2,6) -- (-1,6);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    68
  
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    69
  \draw (0,0) -- (0, 5);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    70
  \draw (1,0) -- (1, 4);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    71
  \draw (2,0) -- (2, 3);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    72
  \draw (3,0) -- (3, 2);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    73
  \draw (4,0) -- (4, 1);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    74
  \draw (-1,0) -- (-1, 6);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    75
  \draw (-2,0) -- (-2, 6);
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    76
  
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    77
  \draw (-1.5,-0.5) node {\footnotesize{}\texttt{The}}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    78
  \draw (-0.5,-1.0) node {\footnotesize{}\texttt{trainer}}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    79
  \draw ( 0.5,-0.5) node {\footnotesize{}\texttt{trains}}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    80
  \draw ( 1.5,-1.0) node {\footnotesize{}\texttt{the}}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    81
  \draw ( 2.5,-0.5) node {\footnotesize{}\texttt{student}}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    82
  \draw ( 3.5,-1.0) node {\footnotesize{}\texttt{team}};
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    83
  
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    84
  \draw (-1.5,0.5) node {$A$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    85
  \draw (-0.5,0.5) node {$N$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    86
  \draw ( 0.5,0.5) node {$N,\!V$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    87
  \draw ( 1.5,0.5) node {$A$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    88
  \draw ( 2.5,0.5) node {$N$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    89
  \draw ( 3.5,0.5) node {$N,\!V$};
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    90
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    91
  \draw (-1.5,1.5) node {$N$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    92
  \draw (-0.5,1.5) node {$N$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    93
  \draw ( 0.5,1.5) node {$$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    94
  \draw ( 1.5,1.5) node {$N$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    95
  \draw ( 2.5,1.5) node {$N$};
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    96
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    97
  \draw (-1.5,2.5) node {$N$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    98
  \draw (-0.5,2.5) node {$ $}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    99
  \draw ( 0.5,2.5) node {$N,\!P$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   100
  \draw ( 1.5,2.5) node {$N$};
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   101
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   102
  \draw (-1.5,3.5) node {$$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   103
  \draw (-0.5,3.5) node {$N,\!S$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   104
  \draw ( 0.5,3.5) node {$N,\!P$};
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   105
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   106
  \draw (-1.5,4.5) node {$N,\!S$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   107
  \draw (-0.5,4.5) node {$N,\!S$};
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   108
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   109
  \draw (-1.5,5.5) node {$N,\!S$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   110
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   111
  \draw (-2.4, 5.5) node {$1$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   112
  \draw (-2.4, 4.5) node {$2$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   113
  \draw (-2.4, 3.5) node {$3$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   114
  \draw (-2.4, 2.5) node {$4$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   115
  \draw (-2.4, 1.5) node {$5$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   116
  \draw (-2.4, 0.5) node {$6$}; 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   117
  \end{tikzpicture}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   118
  \end{center}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   119
  }
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   120
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   121
\item Transform the grammar
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
   123
\begin{center}
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   124
\begin{tabular}{lcl}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   125
$A$ & $::=$ & $0A1 \;|\; BB$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   126
$B$ & $::=$ & $\epsilon \;|\; 2B$
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   127
\end{tabular}
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
   128
\end{center}
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   129
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   130
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   131
into Chomsky normal form.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   132
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   133
\solution{
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   134
  First one has to eliminate $\epsilon$. This means we obtain the rules:
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   135
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   136
  \begin{center}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   137
    \begin{tabular}{lcl}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   138
      $A$ & $::=$ & $0A1 \;|\; 01 \;|\; BB \;|\; B$\\
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   139
      $B$ & $::=$ & $2 \;|\; 2B$
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   140
    \end{tabular}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   141
  \end{center}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   142
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   143
  Now we have to bring the rules into CNF form by adding additional
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   144
  non-terminals, like $Z$, $O$, $T$,  and splitting up the rules into ``twos'':
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   145
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   146
  \begin{center}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   147
    \begin{tabular}{lcl}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   148
      $A$ & $::=$ & $ZC \;|\; ZO \;|\; BB \;|\; 2$\\
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   149
      $B$ & $::=$ & $2 \;|\; TB$\\
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   150
      $C$ & $::=$ & $AO$\\
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   151
      $Z$ & $::=$ & $0$\\
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   152
      $O$ & $::=$ & $1$\\
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   153
      $T$ & $::=$ & $2$\\                           
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   154
    \end{tabular}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   155
  \end{center}   
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   156
}
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   157
305
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   158
\item Consider the following grammar $G$
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   159
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   160
\begin{center}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   161
\begin{tabular}{l}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   162
$S ::= \texttt{if0} \cdot E \cdot \texttt{then} \cdot S$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   163
$S ::= \texttt{print} \cdot S$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   164
$S ::= \texttt{begin} \cdot B\cdot \texttt{end}$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   165
$B ::= S\cdot \texttt{;}$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   166
$B ::= S\cdot \texttt{;} \cdot B$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   167
$S ::= num$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   168
$E ::= num$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   169
$B ::= num$
305
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   170
\end{tabular}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   171
\end{center}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   172
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff 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: 292
diff changeset
   174
non-terminals.
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   175
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff 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: 292
diff 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: 292
diff changeset
   178
has more than one parse tree.
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   179
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   180
\begin{center}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   181
\begin{tabular}{rl}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   182
(i)   & $S ::= \texttt{if0} \cdot E\cdot \texttt{then} \cdot S\cdot \texttt{else} \cdot S$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   183
(ii)  & $B ::= B \cdot B$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   184
(iii) & $E ::= ( \cdot E \cdot )$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   185
(iv)  & $E ::= E \cdot + \cdot E$
305
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   186
\end{tabular}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   187
\end{center}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   188
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   189
\solution{
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   190
  (i) this is ambiguous -> it's an instance of the dangling else;
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   191
  (ii) rules of the form $B ::= B \cdot B$ are always ambiguous $B \cdot B\cdot B$
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   192
  (iii) this is fine
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   193
  (iv) same as (ii) $E\cdot + \cdot E \cdot + \cdot E$
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   194
  }
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   195
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   196
\item Suppose the string $``9-5+2''$. Give all ASTs that
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   197
  the following two grammars generate for this string.
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   198
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   199
Grammar 1, where List is the starting symbol:
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   200
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   201
\begin{center}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   202
\begin{tabular}{lcl}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   203
$List$ & $::=$ &  $List + Digit \mid List - Digit \mid Digit$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   204
$Digit$ & $::=$ & $0 \mid 1 \mid 2 \mid 3 \mid 4 \mid 5 \mid 6 \mid 7 \mid 8 \mid 9$
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   205
\end{tabular}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   206
\end{center}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   207
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   208
Grammar 2, where String is the starting symbol:
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   209
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   210
\begin{center}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   211
\begin{tabular}{@{}lcl@{}}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   212
  $String$ & $::=$ & $String + String \mid String - String \mid$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   213
  & & $0 \mid 1 \mid 2 \mid 3 \mid 4 \mid 5 \mid 6 \mid 7 \mid 8 \mid 9$
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   214
\end{tabular}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   215
\end{center}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   216
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   217
\solution{
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   218
  The point is that Grammar 1 is un-ambiguous, while the second is ambiguous.
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   219
  Grammar 1 parses the strings as (9 - 5) + 2. Grammar 2 is ambiguous and
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   220
  there are two possibilities (9 - 5) + 2 and 9 - (5 + 2).
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   221
  
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   222
}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   223
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   224
                   
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   225
%\item {\bf (Optional)} The task is to match strings where the letters are in alphabetical order---for example, 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   226
%\texttt{abcfjz} would pass, but \texttt{acb} would not. Whitespace should be ignored---for example
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   227
%\texttt{ab c d} should pass. The point is to try to get the regular expression as short as possible!
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   228
%See:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   229
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   230
%\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   231
%\url{http://callumacrae.github.com/regex-tuesday/challenge11.html}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   232
%\end{center}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   233
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   234
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   235
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   236
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   237
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   238
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   239
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   240
%%% End: 
956
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   241
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   242
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   243
The| trainer trains the student A {N,S} => N 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   244
The trainer |trains the student N {N, P} => N S
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   245
The trainer trains |the student N N => N 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   246
The trainer trains the |student 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   247
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   248
trainer |trains the student team N o {N, P} => N, S
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   249
trainer trains| the student team N o N => N
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   250
trainer trains the |student team 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   251
trainer trains the student |team {N, P} o {N, V} => N
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   252
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   253
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   254
The| trainer trains the student team A o (N,S) => N
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   255
The trainer| trains the student team N o (N,P) => N, S
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   256
The trainer trains| the student team N o N => N
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   257
The trainer trains the| student team 
ae9782e62bdd updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   258
The trainer trains the student| team (N,S) o (N,V) => N