hws/hw07.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 01 Oct 2023 10:57:32 +0100
changeset 932 5678414a3898
parent 916 10f834eb0a9e
child 956 ae9782e62bdd
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}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
\begin{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\section*{Homework 7}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
916
10f834eb0a9e texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 897
diff changeset
     9
%\HEADER
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 305
diff changeset
    10
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
\begin{enumerate}
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    12
\item Suppose the context-sensitive grammar
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
\begin{center}
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    15
\begin{tabular}{lcl}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    16
$S$ & $::=$ &  $bSAA\;|\; \epsilon$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    17
$A$ & $::=$ & $a$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    18
$bA$ & $::=$ & $Ab$\\
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    19
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    20
\end{center}
183
b17eff695c7f added new stuff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    21
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    22
where $S$ is the starting symbol of the grammar. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    23
Give a derivation  of the string $"\!aaabaaabb"$. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    24
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
    25
strings recognised by this grammar.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    26
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
\item Consider the following grammar 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
681
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    30
\begin{plstx}[margin=1cm]
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    31
  : \meta{S\/} ::= \meta{N\/}\cdot \meta{P\/}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    32
  : \meta{P\/} ::= \meta{V\/}\cdot \meta{N\/}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    33
  : \meta{N\/} ::= \meta{N\/}\cdot \meta{N\/}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    34
  : \meta{N\/} ::= \meta{A\/}\cdot \meta{N\/}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    35
  : \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
    36
  : \meta{V\/} ::= \texttt{trains} \mid \texttt{team}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    37
  : \meta{A\/} ::= \texttt{The} \mid \texttt{the}\\
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    38
\end{plstx}
7b7736bea3ca updated
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
    39
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
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
    42
Using the CYK-algorithm, check whether or not the following string can be parsed
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
by the grammar:
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
\texttt{The trainer trains the student team}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    49
\item Transform the grammar
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    51
\begin{center}
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    52
\begin{tabular}{lcl}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    53
$A$ & $::=$ & $0A1 \;|\; BB$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    54
$B$ & $::=$ & $\epsilon \;|\; 2B$
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    55
\end{tabular}
60
68d664c204d2 updated
Christian Urban <urbanc@in.tum.de>
parents: 59
diff changeset
    56
\end{center}
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    57
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    58
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    59
into Chomsky normal form.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    60
305
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    61
\item Consider the following grammar $G$
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    62
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    63
\begin{center}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    64
\begin{tabular}{l}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    65
$S ::= \texttt{if0} \cdot E \cdot \texttt{then} \cdot S$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    66
$S ::= \texttt{print} \cdot S$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    67
$S ::= \texttt{begin} \cdot B\cdot \texttt{end}$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    68
$B ::= S\cdot \texttt{;}$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    69
$B ::= S\cdot \texttt{;} \cdot B$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    70
$S ::= num$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    71
$E ::= num$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    72
$B ::= num$
305
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    73
\end{tabular}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    74
\end{center}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    75
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    76
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
    77
non-terminals.
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    78
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    79
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
    80
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
    81
has more than one parse tree.
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    82
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    83
\begin{center}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    84
\begin{tabular}{rl}
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    85
(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
    86
(ii)  & $B ::= B \cdot B$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    87
(iii) & $E ::= ( \cdot E \cdot )$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    88
(iv)  & $E ::= E \cdot + \cdot E$
305
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    89
\end{tabular}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    90
\end{center}
23045b2b0b7b updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    91
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
    92
897
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    93
\item Suppose the string $``9-5+2''$. Give all ASTs that
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    94
  the following two grammars generate for this string.
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    95
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    96
Grammar 1, where List is the starting symbol:
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    97
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    98
\begin{center}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
    99
\begin{tabular}{lcl}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   100
$List$ & $::=$ &  $List + Digit \mid List - Digit \mid Digit$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   101
$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
   102
\end{tabular}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   103
\end{center}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   104
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   105
Grammar 2, where String is the starting symbol:
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   106
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   107
\begin{center}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   108
\begin{tabular}{@{}lcl@{}}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   109
  $String$ & $::=$ & $String + String \mid String - String \mid$\\
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   110
  & & $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
   111
\end{tabular}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   112
\end{center}
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   113
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   114
904de68a27a4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 681
diff changeset
   115
                   
194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   116
%\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
   117
%\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
   118
%\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
   119
%See:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   120
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   121
%\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   122
%\url{http://callumacrae.github.com/regex-tuesday/challenge11.html}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 183
diff changeset
   123
%\end{center}
59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
%%% End: