hws/hw06.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sat, 29 Oct 2016 21:47:22 +0100
changeset 468 bab72d4ac90e
parent 459 780486571e38
child 577 7a437f1f689d
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}
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
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
     9
\HEADER
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    10
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
\begin{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
\item (i) Give the regular expressions for lexing a language
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    13
      consisting of whitespaces, identifiers (some letters
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    14
      followed by digits), numbers, operations \texttt{=},
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    15
      \texttt{<} and \texttt{>}, and the keywords \texttt{if},
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    16
      \texttt{then} and \texttt{else}. (ii) Decide whether the
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    17
      following strings can be lexed in this language?
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
\begin{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
\item \texttt{"if y4 = 3 then 1 else 3"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
\item \texttt{"if33 ifif then then23 else else 32"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
\item \texttt{"if x4x < 33 then 1 else 3"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
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
    26
Observe the maximal munch rule and priorities of your regular
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
expressions that make the process of lexing unambiguous.)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
\item Suppose the grammar
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
\begin{tabular}{lcl}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
$E$ & $\rightarrow$ &  $F \;|\; F \cdot * \cdot F \;|\; F \cdot \backslash \cdot F$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
$F$ & $\rightarrow$ & $T \;|\; T \cdot \texttt{+} \cdot T \;|\; T \cdot \texttt{-} \cdot T$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
$T$ & $\rightarrow$ & $num \;|\; \texttt{(} \cdot E \cdot \texttt{)}$\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
\end{tabular}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
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
    40
a number token. Give a parse tree for the string \texttt{(3+3)+(2*3)}.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
\item Define what it means for a grammar to be ambiguous. Give an example of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
an ambiguous grammar.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
\item Suppose boolean expressions are built up from
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
\begin{tabular}{ll}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
1.) & tokens for \texttt{true} and \texttt{false},\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
2.) & the infix operations \texttt{$\wedge$} and \texttt{$\vee$},\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
3.) & the prefix operation $\neg$, and\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
4.) & can be enclosed in parentheses.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
\end{tabular}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
(i) Give a grammar that can recognise such boolean expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
and (ii) give a sample string involving all rules given in 1.-4.~that 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
can be parsed by this grammar.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    60
\item Given the regular expression
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    62
\[(ab + a)\cdot (\ONE + b)\]
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    63
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    64
there are two values for how this regular expression can recognise
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    65
the string $ab$. Give both values and indicate which one is the
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    66
POSIX value.
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    67
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    68
\item \POSTSCRIPT        
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
%%% End: