hws/hw09.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 11 Oct 2024 19:13:00 +0100
changeset 967 ce5de01b9632
parent 958 fddf099a82f8
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}
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
     5
\usepackage{../grammar}
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     9
\section*{Homework 9}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    10
916
10f834eb0a9e texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 906
diff changeset
    11
%\HEADER
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 314
diff changeset
    12
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
\begin{enumerate}
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 208
diff changeset
    14
\item Describe what is meant by \emph{eliminating tail
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    15
      recursion}? When can this optimization be applied and
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    16
    why is it of benefit?
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    17
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    18
    \solution{ Tail-call optimisation replaces a recursive call (in
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    19
      tail-call position) by a jump to the beginning of a function.
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    20
      In this way a recursion is replaced by a loop. This saves stack
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    21
      space because no new stack space needs to be allocated when a
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    22
      function is called recursively.
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    23
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    24
      Tail-call optimisation can be applied when the recursive call is
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    25
    the last instruction that is run in the function.}
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    26
    
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    27
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    28
\item A programming language has arithmetic expression. For an
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    29
  arithmetic expression the compiler of this language produces the
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    30
  following snippet of JVM code.
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    31
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    32
\begin{lstlisting}[language=JVMIS,numbers=none]
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    33
ldc 1 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    34
ldc 2 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    35
ldc 3 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    36
imul 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    37
ldc 4 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    38
ldc 3 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    39
isub 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    40
iadd 
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    41
iadd
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    42
\end{lstlisting}
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    43
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    44
  Give the arithmetic expression that produced this code.  Make sure
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    45
  you give all necessary parentheses.
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    46
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    47
  \solution{
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    48
    $1 + ((2 * 3) + (4 - 3))$
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    49
  }
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    50
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    51
\item Describe what the following JVM instructions do!
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    52
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    53
  
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    54
\begin{lstlisting}[language=JVMIS2,numbers=none]
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    55
ldc 3    
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    56
iload 3
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    57
istore 1
525
a2ee4b11c976 updated
cu
parents: 459
diff changeset
    58
ifeq label
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 577
diff changeset
    59
if_icmpge label
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    60
\end{lstlisting}
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
    61
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    62
\solution{
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    63
(1) load the constant 3 onto the stack. (2) load the 4th local variable onto the stack. (3) store the top of the stack into the 2nd local variable (deleting the top element from the stack) (4) tests whether the top of the stack is equal to zero (if yes, then jump to label; delete top of the stack) (5) compares the top 2 elements of the stack whether they are greater or equal (if yes jump to label; delete two topmost elements from the stack) 
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    64
  }
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    65
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
    66
901
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    67
\item What does the following JVM function calculate?
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    68
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    69
\begin{lstlisting}[language=JVMIS2,numbers=none]    
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    70
.method public static bar(I)I
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    71
.limit locals 1
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    72
.limit stack 9
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    73
   iload 0
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    74
   ldc 0
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    75
   if_icmpne If_else_8
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    76
   ldc 0
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    77
   goto If_end_9
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    78
If_else_8:
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    79
   iload 0
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    80
   ldc 1
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    81
   if_icmpne If_else_10
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    82
   ldc 1
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    83
   goto If_end_11
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    84
If_else_10:
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    85
   iload 0
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    86
   ldc 1
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    87
   isub
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    88
   invokestatic bar(I)I
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    89
   iload 0
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    90
   ldc 2
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    91
   isub
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    92
   invokestatic bar(I)I
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    93
   iadd
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    94
If_end_11:
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    95
If_end_9:
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    96
   ireturn
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    97
.end method  
33cff35bdc1a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 805
diff changeset
    98
\end{lstlisting}
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
    99
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   100
\solution{ Fibonacci function..students should be able to read what the instructions do on the stack).}
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   101
703
57b3ae5ba5e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 649
diff changeset
   102
\item What does the following LLVM function calculate? Give the
57b3ae5ba5e2 updated
Christian Urban <urbanc@in.tum.de>
parents: 649
diff changeset
   103
  corresponding arithmetic expression .
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   104
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   105
\begin{lstlisting}[language=LLVM,numbers=none]  
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   106
define i32 @foo(i32 %a, i32 %b)  {
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   107
  %1 = mul i32 %a, %a
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   108
  %2 = mul i32 %a, 2
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   109
  %3 = mul i32 %2, %b
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   110
  %4 = add i32 %1, %3
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   111
  %5 = mul i32 %b, %b
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   112
  %6 = add i32 %5, %4
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   113
  ret i32 %6
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   114
}
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   115
\end{lstlisting}
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   116
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   117
\solution{ $a^2+a*2*b + b^2$
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   118
  }
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   119
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   120
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   121
\item As an optimisation technique, a compiler might want to detect
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   122
  `dead code' and not generate anything for this code. Why does this
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   123
  optimisation technique have the potential of speeding up the
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   124
  run-time of a program? (Hint: On what kind of CPUs are programs run
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   125
  nowadays?)
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   126
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   127
  \solution{ Modern CPUs use predictive branching (guessing which
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   128
    code-branch is run) and use the cache extensively...any code that
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   129
    isn't in the program helps with guessing the right branch and does
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   130
    not occupy anything in the cache. So in effect the code will run
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   131
    faster.  }
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   132
  
906
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   133
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   134
\item In an earlier question, we analysed the advantages of having a lexer-phase
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   135
  before running the parser (having a lexer is definitely a good thing to have). But you
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   136
  might wonder if a lexer can also be implemented by a parser and some simple
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   137
  grammar rules. Consider for example:
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   138
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   139
  \begin{plstx}[margin=1cm]
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   140
    : \meta{S\/} ::= (\meta{Kw\/}\mid \meta{Id\/}\mid \meta{Ws\/}) \cdot \meta{S\/} \;\mid\; \epsilon\\
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   141
    : \meta{Kw\/} ::= \texttt{if} \mid \texttt{then} \mid \ldots\\
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   142
    : \meta{Id\/} ::= (\texttt{a} \mid\ldots\mid \texttt{z}) \cdot \meta{Id\/} \;\mid\; \epsilon\\
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   143
    : \meta{Ws\/} ::= \ldots\\
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   144
  \end{plstx}
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   145
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   146
  What is wrong with implementing a lexer in this way?
2bf1516d730f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 901
diff changeset
   147
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   148
  \solution { There is no problem in terms of which strings are
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   149
    matched (the grammar can be defined such that it matches exactly
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   150
    the same strings. However, CFG do not obey the POSIX rules,
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   151
    meaning they cannot implement ``how regular expressions matc a
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   152
    string'' (for example longest match rule; rule priority).  }
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   153
  
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   154
705
bfc8703b1527 updated
Christian Urban <urbanc@in.tum.de>
parents: 703
diff changeset
   155
\item What is the difference between a parse tree and an abstract
bfc8703b1527 updated
Christian Urban <urbanc@in.tum.de>
parents: 703
diff changeset
   156
  syntax tree? Give some simple examples for each of them.
648
36379b038438 updated
Christian Urban <urbanc@in.tum.de>
parents: 647
diff changeset
   157
958
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   158
  \solution { Parse-trees follow the grammar rules, therefore the
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   159
    inner nodes correspond to the non-terminal symbols in CFGs. ASTs
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   160
    represent the tree-structure of the programs. }
fddf099a82f8 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 941
diff changeset
   161
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   162
\item What are the two main features of code in
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   163
  static single assignment form (SSA)?
577
7a437f1f689d updated
Christian Urban <urbanc@in.tum.de>
parents: 525
diff changeset
   164
941
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   165
  \solution{
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   166
    Variables are only assigned once and all operations are
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   167
    primitive (in the sense of simple arithmetic operations,
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   168
    function calls and so on).
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   169
  }
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   170
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   171
  
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   172
%\item Give a description of how the Brzozowski matcher works. 
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   173
%  The description should be coherent and logical.
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   174
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   175
%\item Give a description of how a compiler for the While-language can
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   176
%  be implemented. You should assume you are producing code for the JVM.
66adcae6c762 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   177
%  The description should be coherent and logical.
805
526e10d97435 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 705
diff changeset
   178
526e10d97435 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 705
diff changeset
   179
  
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
   180
\item \POSTSCRIPT  
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   181
314
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   182
%  \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
   183
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   184
%  \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
   185
%  \;\;then\;\;$L(r) \not= \{\}$ 
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   186
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   187
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   188
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   189
%  holds, or equivalently
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   190
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   191
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   192
%  $L(r) = \{\}$ \;\;implies\;\; $\varnothing$ occurs in $r$.
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   193
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   194
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   195
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   196
%  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
   197
%  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
   198
%  the following function:
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   199
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   200
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   201
%  \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
   202
%  $occurs(\varnothing)$      & $\dn$ & $true$\\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   203
%  $occurs(\epsilon)$           & $\dn$ &  $f\!alse$\\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   204
%  $occurs (c)$                    & $\dn$ &  $f\!alse$\\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   205
%  $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
   206
%  $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
   207
%  $occurs (r^*)$                & $\dn$ & $occurs(r)$ \\
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   208
%  \end{tabular}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   209
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   210
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   211
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   212
%  Now you can prove 
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   213
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   214
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   215
%  $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$.
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   216
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   217
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   218
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   219
%  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
   220
%  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
   221
%  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
   222
%  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
   223
%  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
   224
%    
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   225
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   226
%  \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
   227
%  $occurs(\sim r)$      & $\dn$ & $occurs(r)$
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   228
%  \end{tabular}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   229
%  \end{center}  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   230
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   231
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   232
%  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
   233
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   234
%  \begin{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   235
%  $L(r) = \{\}$ \;\;implies\;\; $occurs(r)$.
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   236
%  \end{center}
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   237
%  
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   238
%  \noindent
7dd5797a5ffa updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   239
%  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
   240
%  property.
208
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   241
  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   242
\end{enumerate}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   243
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   244
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   245
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   246
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   247
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   248
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   249
%%% End: