author | Christian Urban <christian.urban@kcl.ac.uk> |
Mon, 10 Oct 2022 12:42:06 +0100 | |
changeset 884 | 183bfb52d26e |
parent 681 | 7b7736bea3ca |
child 897 | 904de68a27a4 |
permissions | -rw-r--r-- |
59 | 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 | 3 |
\usepackage{../grammar} |
59 | 4 |
|
5 |
\begin{document} |
|
6 |
||
7 |
\section*{Homework 7} |
|
8 |
||
359
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
305
diff
changeset
|
9 |
\HEADER |
db106e5b7c4d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
305
diff
changeset
|
10 |
|
59 | 11 |
\begin{enumerate} |
194
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
12 |
\item Suppose the context-sensitive grammar |
59 | 13 |
|
14 |
\begin{center} |
|
194
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
15 |
\begin{tabular}{lcl} |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
16 |
$S$ & $\rightarrow$ & $bSAA\;|\; \epsilon$\\ |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
17 |
$A$ & $\rightarrow$ & $a$\\ |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
18 |
$bA$ & $\rightarrow$ & $Ab$\\ |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
19 |
\end{tabular} |
90796ee3c17a
added
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
90796ee3c17a
added
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. |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
23 |
Give a derivation of the string $"\!aaabaaabb"$. |
90796ee3c17a
added
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 |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
25 |
strings recognised by this grammar. |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
26 |
|
59 | 27 |
|
28 |
\item Consider the following grammar |
|
29 |
||
681 | 30 |
\begin{plstx}[margin=1cm] |
31 |
: \meta{S\/} ::= \meta{N\/}\cdot \meta{P\/}\\ |
|
32 |
: \meta{P\/} ::= \meta{V\/}\cdot \meta{N\/}\\ |
|
33 |
: \meta{N\/} ::= \meta{N\/}\cdot \meta{N\/}\\ |
|
34 |
: \meta{N\/} ::= \meta{A\/}\cdot \meta{N\/}\\ |
|
35 |
: \meta{N\/} ::= \texttt{student} \mid \texttt{trainer} \mid \texttt{team} \mid \texttt{trains}\\ |
|
36 |
: \meta{V\/} ::= \texttt{trains} \mid \texttt{team}\\ |
|
37 |
: \meta{A\/} ::= \texttt{The} \mid \texttt{the}\\ |
|
38 |
\end{plstx} |
|
39 |
||
59 | 40 |
|
41 |
where $S$ is the start symbol and $S$, $P$, $N$, $V$ and $A$ are non-terminals. |
|
42 |
Using the CYK-algorithm, check whether or not the following string can be parsed |
|
43 |
by the grammar: |
|
44 |
||
45 |
\begin{center} |
|
46 |
\texttt{The trainer trains the student team} |
|
47 |
\end{center} |
|
48 |
||
194
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
49 |
\item Transform the grammar |
59 | 50 |
|
60 | 51 |
\begin{center} |
194
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
52 |
\begin{tabular}{lcl} |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
53 |
$A$ & $\rightarrow$ & $0A1 \;|\; BB$\\ |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
54 |
$B$ & $\rightarrow$ & $\epsilon \;|\; 2B$ |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
55 |
\end{tabular} |
60 | 56 |
\end{center} |
194
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
57 |
|
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
58 |
\noindent |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
59 |
into Chomsky normal form. |
90796ee3c17a
added
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} |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
65 |
$S \rightarrow \texttt{if0} \cdot E \cdot \texttt{then} \cdot S$\\ |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
66 |
$S \rightarrow \texttt{print} \cdot S$\\ |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
67 |
$S \rightarrow \texttt{begin} \cdot B\cdot \texttt{end}$\\ |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
68 |
$B \rightarrow S\cdot \texttt{;}$\\ |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
69 |
$B \rightarrow S\cdot \texttt{;} \cdot B$\\ |
393
494b44b439bf
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
359
diff
changeset
|
70 |
$S \rightarrow num$\\ |
494b44b439bf
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
359
diff
changeset
|
71 |
$E \rightarrow num$\\ |
494b44b439bf
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
359
diff
changeset
|
72 |
$B \rightarrow 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} |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
85 |
(i) & $S \rightarrow \texttt{if0} \cdot E\cdot \texttt{then} \cdot S\cdot \texttt{else} \cdot S$\\ |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
86 |
(ii) & $B \rightarrow B \cdot B$\\ |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
87 |
(iii) & $E \rightarrow ( \cdot E \cdot )$\\ |
23045b2b0b7b
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
292
diff
changeset
|
88 |
(iv) & $E \rightarrow E \cdot + \cdot E$ |
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
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
92 |
|
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
93 |
%\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:
183
diff
changeset
|
94 |
%\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:
183
diff
changeset
|
95 |
%\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:
183
diff
changeset
|
96 |
%See: |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
97 |
|
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
98 |
%\begin{center} |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
99 |
%\url{http://callumacrae.github.com/regex-tuesday/challenge11.html} |
90796ee3c17a
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
183
diff
changeset
|
100 |
%\end{center} |
59 | 101 |
\end{enumerate} |
102 |
||
103 |
\end{document} |
|
104 |
||
105 |
%%% Local Variables: |
|
106 |
%%% mode: latex |
|
107 |
%%% TeX-master: t |
|
108 |
%%% End: |