hws/hw09.tex
author Christian Urban <urbanc@in.tum.de>
Sat, 14 Dec 2019 17:57:43 +0000
changeset 705 bfc8703b1527
parent 703 57b3ae5ba5e2
child 805 526e10d97435
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
\documentclass{article}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 208
diff changeset
     2
\usepackage{../style}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 208
diff changeset
     3
\usepackage{../graphics}
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
     4
\usepackage{../langs}
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
\section*{Homework 9}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 314
diff changeset
    10
\HEADER
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 314
diff changeset
    11
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
\begin{enumerate}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 208
diff changeset
    13
\item Describe what is meant by \emph{eliminating tail
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    14
      recursion}? When can this optimization be applied and
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    15
      why is it of benefit?
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    16
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    17
\item A programming language has arithmetic expression. For an
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    18
  arithmetic expression the compiler of this language produces the
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    19
  following snippet of JVM code.
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    20
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    21
\begin{lstlisting}[language=JVMIS,numbers=none]
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    22
ldc 1 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    23
ldc 2 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    24
ldc 3 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    25
imul 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    26
ldc 4 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    27
ldc 3 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    28
isub 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    29
iadd 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    30
iadd
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    31
\end{lstlisting}
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    32
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    33
  Give the arithmetic expression that produced this code.  Make sure
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    34
  you give all necessary parentheses.
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    35
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    36
\item Describe what the following JVM instructions do!
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    37
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    38
  
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    39
\begin{lstlisting}[language=JVMIS2,numbers=none]
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    40
ldc 3    
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    41
iload 3
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    42
istore 1
525
a2ee4b11c976 updated
cu
parents: 459
diff changeset
    43
ifeq label
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    44
if_icmpge label
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    45
\end{lstlisting}
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    46
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
    47
703
57b3ae5ba5e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 649
diff changeset
    48
\item What does the following LLVM function calculate? Give the
57b3ae5ba5e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 649
diff changeset
    49
  corresponding arithmetic expression .
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    50
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    51
\begin{lstlisting}[language=LLVM,numbers=none]  
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    52
define i32 @foo(i32 %a, i32 %b)  {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    53
  %1 = mul i32 %a, %a
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    54
  %2 = mul i32 %a, 2
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    55
  %3 = mul i32 %2, %b
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    56
  %4 = add i32 %1, %3
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    57
  %5 = mul i32 %b, %b
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    58
  %6 = add i32 %5, %4
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    59
  ret i32 %6
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    60
}
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    61
\end{lstlisting}
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    62
705
bfc8703b1527 updated
Christian Urban <urbanc@in.tum.de>
parents: 703
diff changeset
    63
\item What is the difference between a parse tree and an abstract
bfc8703b1527 updated
Christian Urban <urbanc@in.tum.de>
parents: 703
diff changeset
    64
  syntax tree? Give some simple examples for each of them.
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    65
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
    66
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
    67
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    68
\item \POSTSCRIPT  
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    69
314
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    70
%  \item It is true (I confirmed it) that
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    71
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    72
%  \begin{center} if $\varnothing$ does not occur in $r$
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    73
%  \;\;then\;\;$L(r) \not= \{\}$ 
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    74
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    75
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    76
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    77
%  holds, or equivalently
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    78
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    79
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    80
%  $L(r) = \{\}$ \;\;implies\;\; $\varnothing$ occurs in $r$.
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    81
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    82
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    83
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    84
%  You can prove either version by induction on $r$. The best way to
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    85
%  make more formal what is meant by `$\varnothing$ occurs in $r$', you can define
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    86
%  the following function:
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    87
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    88
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    89
%  \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    90
%  $occurs(\varnothing)$      & $\dn$ & $true$\\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    91
%  $occurs(\epsilon)$           & $\dn$ &  $f\!alse$\\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    92
%  $occurs (c)$                    & $\dn$ &  $f\!alse$\\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    93
%  $occurs (r_1 + r_2)$       & $\dn$ &  $occurs(r_1) \vee occurs(r_2)$\\ 
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    94
%  $occurs (r_1 \cdot r_2)$ & $\dn$ &  $occurs(r_1) \vee occurs(r_2)$\\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    95
%  $occurs (r^*)$                & $\dn$ & $occurs(r)$ \\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    96
%  \end{tabular}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    97
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    98
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    99
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   100
%  Now you can prove 
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   101
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   102
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   103
%  $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$.
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   104
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   105
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   106
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   107
%  The interesting cases are $r_1 + r_2$ and $r^*$.
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   108
%  The other direction is not true, that is if $occurs(r)$ then $L(r) = \{\}$. A counter example
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   109
%  is $\varnothing + a$: although $\varnothing$ occurs in this regular expression, the corresponding
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   110
%  language is not empty. The obvious extension to include the not-regular expression, $\sim r$,
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   111
%  also leads to an incorrect statement. Suppose we add the clause
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   112
%    
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   113
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   114
%  \begin{tabular}{@ {}l@ {\hspace{2mm}}c@ {\hspace{2mm}}l@ {}}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   115
%  $occurs(\sim r)$      & $\dn$ & $occurs(r)$
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   116
%  \end{tabular}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   117
%  \end{center}  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   118
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   119
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   120
%  to the definition above, then it will not be true that
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   121
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   122
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   123
%  $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$.
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   124
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   125
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   126
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   127
%  Assume the alphabet contains just $a$ and $b$, find a counter example to this
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   128
%  property.
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   129
  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   130
\end{enumerate}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   131
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   132
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   134
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   135
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   136
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   137
%%% End: