hws/hw06.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Mon, 24 Nov 2014 09:06:27 +0000
changeset 312 5cdb4d40eb80
parent 292 7ed2a25dd115
child 359 db106e5b7c4d
permissions -rw-r--r--
update
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56
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: 102
diff changeset
     2
\usepackage{../style}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
     3
\usepackage{../graphics}
56
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 6}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
\begin{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
\item (i) Give the regular expressions for lexing a language
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
consisting of whitespaces, identifiers (some letters followed by digits), numbers,
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
operations \texttt{=}, \texttt{<} and \texttt{>}, and the keywords
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
\texttt{if}, \texttt{then} and \texttt{else}.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
(ii) Decide whether the following strings 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
can be lexed in this language?
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
\begin{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
\item \texttt{"if y4 = 3 then 1 else 3"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
\item \texttt{"if33 ifif then then23 else else 32"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
\item \texttt{"if x4x < 33 then 1 else 3"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
In case they can, give the corresponding token sequences. (Hint: 
89
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 56
diff changeset
    24
Observe the maximal munch rule and priorities of your regular
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
expressions that make the process of lexing unambiguous.)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
\item Suppose the grammar
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
\begin{tabular}{lcl}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
$E$ & $\rightarrow$ &  $F \;|\; F \cdot * \cdot F \;|\; F \cdot \backslash \cdot F$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
$F$ & $\rightarrow$ & $T \;|\; T \cdot \texttt{+} \cdot T \;|\; T \cdot \texttt{-} \cdot T$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
$T$ & $\rightarrow$ & $num \;|\; \texttt{(} \cdot E \cdot \texttt{)}$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
\end{tabular}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
where $E$, $F$ and $T$ are non-terminals, $E$ is the starting symbol of the grammar, and $num$ stands for
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
a number token. Give a parse tree for the string \texttt{(3+3)+(2*3)}.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
\item Define what it means for a grammar to be ambiguous. Give an example of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
an ambiguous grammar.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
\item Suppose boolean expressions are built up from
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
\begin{tabular}{ll}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
1.) & tokens for \texttt{true} and \texttt{false},\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
2.) & the infix operations \texttt{$\wedge$} and \texttt{$\vee$},\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
3.) & the prefix operation $\neg$, and\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
4.) & can be enclosed in parentheses.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
\end{tabular}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
(i) Give a grammar that can recognise such boolean expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
and (ii) give a sample string involving all rules given in 1.-4.~that 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
can be parsed by this grammar.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
%%% End: