hws/hw06.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 13 Sep 2014 04:30:25 +0100
changeset 242 35104ee14f87
parent 102 1ab41c59e3d3
child 292 7ed2a25dd115
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\usepackage{charter}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
\usepackage{hyperref}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
\usepackage{amssymb}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
\usepackage{amsmath}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\usepackage{tikz}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\usetikzlibrary{automata}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}% for definitions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
\begin{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
\section*{Homework 6}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
\begin{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
\item (i) Give the regular expressions for lexing a language
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
consisting of whitespaces, identifiers (some letters followed by digits), numbers,
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
operations \texttt{=}, \texttt{<} and \texttt{>}, and the keywords
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
\texttt{if}, \texttt{then} and \texttt{else}.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
(ii) Decide whether the following strings 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
can be lexed in this language?
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
\begin{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
\item \texttt{"if y4 = 3 then 1 else 3"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
\item \texttt{"if33 ifif then then23 else else 32"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
\item \texttt{"if x4x < 33 then 1 else 3"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
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
    30
Observe the maximal munch rule and priorities of your regular
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
expressions that make the process of lexing unambiguous.)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
\item Suppose the grammar
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
\begin{tabular}{lcl}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
$E$ & $\rightarrow$ &  $F \;|\; F \cdot * \cdot F \;|\; F \cdot \backslash \cdot F$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
$F$ & $\rightarrow$ & $T \;|\; T \cdot \texttt{+} \cdot T \;|\; T \cdot \texttt{-} \cdot T$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
$T$ & $\rightarrow$ & $num \;|\; \texttt{(} \cdot E \cdot \texttt{)}$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
\end{tabular}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
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
    44
a number token. Give a parse tree for the string \texttt{(3+3)+(2*3)}.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
\item Define what it means for a grammar to be ambiguous. Give an example of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
an ambiguous grammar.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
\item Suppose boolean expressions are built up from
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
\begin{tabular}{ll}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
1.) & tokens for \texttt{true} and \texttt{false},\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
2.) & the infix operations \texttt{$\wedge$} and \texttt{$\vee$},\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
3.) & the prefix operation $\neg$, and\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
4.) & can be enclosed in parentheses.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
\end{tabular}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
(i) Give a grammar that can recognise such boolean expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
and (ii) give a sample string involving all rules given in 1.-4.~that 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
can be parsed by this grammar.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
%%% End: