hws/hw06.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 25 Oct 2024 18:54:08 +0100
changeset 971 51e00f223792
parent 955 47acfd7f9096
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}
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
     4
\usepackage{../grammar}
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
\begin{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
\section*{Homework 6}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
916
10f834eb0a9e texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 898
diff changeset
    10
%\HEADER
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    11
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
\begin{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
\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
    14
      consisting of whitespaces, identifiers (some letters
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    15
      followed by digits), numbers, operations \texttt{=},
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    16
      \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
    17
      \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
    18
      following strings can be lexed in this language?
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
\begin{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
\item \texttt{"if y4 = 3 then 1 else 3"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
\item \texttt{"if33 ifif then then23 else else 32"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
\item \texttt{"if x4x < 33 then 1 else 3"}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
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
    27
Observe the maximal munch rule and priorities of your regular
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
expressions that make the process of lexing unambiguous.)
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    30
\solution{
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    31
  a and b are ok; c is not matched because x4x is not an identifier according to the rules.
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    32
}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    33
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    34
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
\item Suppose the grammar
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    37
\begin{plstx}[margin=1cm]    
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    38
:\meta{E} ::= \meta{F} \;|\; \meta{F} \cdot \texttt{*} \cdot \meta{F} \;|\; \meta{F} \cdot \backslash \cdot \meta{F}\\
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    39
:\meta{F} ::= \meta{T} \;|\; \meta{T} \cdot \texttt{+} \cdot \meta{T} \;|\; \meta{T} \cdot \texttt{-} \cdot \meta{T}\\
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    40
:\meta{T} ::= $num$ \;|\; \texttt{(} \cdot E \cdot \texttt{)}\\
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    41
\end{plstx}
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    43
where \meta{E}, \meta{F} and \meta{T} are non-terminals, \meta{E} is
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    44
the starting symbol of the grammar, and $num$ stands for a number
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    45
token. Give a parse tree for the string \texttt{(3+3)+(2*3)}. Note that
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    46
\meta{F} and \meta{T} are ``exchanged'' in this grammar in comparison with
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    47
the usual grammar for arithmetic expressions. What does this mean in terms
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    48
of precedences of the operators?
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    49
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    50
\solution{
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    51
  \begin{tikzpicture}[level distance=10mm, black,sibling distance=10mm]
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    52
  \node {\meta{E}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    53
  child[sibling distance=20mm] {node {\meta{F}} 
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    54
    child {node {\meta{T}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    55
      child[sibling distance=5mm] {node {(}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    56
      child[sibling distance=5mm]  {node {\meta{E}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    57
        child {node {\meta{F}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    58
          child {node {\meta{T}} child {node {3}}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    59
          child {node {+}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    60
          child {node {\meta{T}} child {node {3}}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    61
        }  
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    62
      }
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    63
      child[sibling distance=5mm]  {node {)}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    64
    }
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    65
    child {node {+}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    66
    child {node {\meta{T}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    67
      child[sibling distance=5mm] {node {(}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    68
      child[sibling distance=5mm] {node {\meta{E}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    69
        child {node {\meta{F}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    70
          child {node {\meta{T}} child {node {2}}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    71
        }
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    72
        child {node {*}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    73
        child {node {\meta{F}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    74
          child {node {\meta{T}} child {node {3}}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    75
        }
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    76
        }
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    77
      child[sibling distance=5mm] {node {)}}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    78
    }
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    79
    };
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    80
\end{tikzpicture}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    81
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
    82
  
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    83
  For the second part "1+2*3" will be parsed as (1+2)*3, meaning + and - bind
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    84
  tighter than * and $\backslash$
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    85
}
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
\item Define what it means for a grammar to be ambiguous. Give an example of
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
an ambiguous grammar.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    90
\solution{
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    91
  Already the grammar
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    92
  \begin{plstx}[margin=1cm]
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    93
    : \meta{E} ::= \meta{E}\cdot +\cdot\meta{E} | num\\
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    94
  \end{plstx}
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    95
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    96
  is ambiguous because a string like "1+2+3" can be parsed
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    97
  as (1+2)+3 or 1+(2+3).
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    98
  }
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
    99
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   100
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
\item Suppose boolean expressions are built up from
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
\begin{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
\begin{tabular}{ll}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
1.) & tokens for \texttt{true} and \texttt{false},\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
2.) & the infix operations \texttt{$\wedge$} and \texttt{$\vee$},\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
3.) & the prefix operation $\neg$, and\\
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
4.) & can be enclosed in parentheses.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   109
\end{tabular}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
\end{center}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
(i) Give a grammar that can recognise such boolean expressions
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
and (ii) give a sample string involving all rules given in 1.-4.~that 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
can be parsed by this grammar.
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   116
\solution{
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   117
The simplest grammar is
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   118
  
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   119
\begin{plstx}[margin=1cm]    
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   120
  :\meta{B} ::= true \;|\; false \;|\;\meta{B} \cdot $\wedge$ \cdot \meta{B} \;|\; \meta{B} \cdot $\vee$ \cdot \meta{B}
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   121
  \;|\; $\neg$\meta{B} \;|\; (\cdot\meta{B}\cdot)\\
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   122
\end{plstx}
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
   123
955
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   124
This is unfortunately a left-recursive grammar. Giving a not left-recursive one is a bit more involved.
47acfd7f9096 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 954
diff changeset
   125
  }
721
e3c64f22dd31 added slides from Rochester
Christian Urban <christian.urban@kcl.ac.uk>
parents: 647
diff changeset
   126
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 596
diff changeset
   127
\item Parsing combinators consist of atomic parsers, alternative
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 596
diff changeset
   128
  parsers, sequence parsers and semantic actions.  What is the purpose
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   129
  of (1) atomic parsers and of (2) map-parsers (also called semantic actions)?
647
180600c04da2 updated
Christian Urban <urbanc@in.tum.de>
parents: 596
diff changeset
   130
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   131
  \solution{
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   132
    Atomic parsers look at a concrete prefix of the input, like num-tokens or identifiers.
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   133
    Map-parsers apply a function to the output of a parser. In this way you can transform
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   134
    the output from, for example, a string to an integer.
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   135
  }
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   136
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   137
  
726
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 721
diff changeset
   138
\item Parser combinators can directly be given a string as
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 721
diff changeset
   139
      input, without the need of a lexer. What are the
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 721
diff changeset
   140
      advantages to first lex a string and then feed a
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 721
diff changeset
   141
      sequence of tokens as input to the parser?
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 721
diff changeset
   142
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   143
      \solution{ Reason 1 you can filter out whitespaces and comments,
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   144
        which makes the grammar rules simpler. If you have to make
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   145
        sure that a whitespace comes after a variable say, then your
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   146
        parser rule for variables gets more complicated. Same with
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   147
        comments which do not contribute anything to the parse tree.
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   148
        
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   149
        Reason 2) The lexer can already classify tokens, for example
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   150
        as numbers, keywords or identifiers. This again makes the grammar
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   151
        rules more deterministic and as a result faster to parse.
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   152
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   153
        The point is that parser combinators can be used to process
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   154
        strings, but in case of compilers where whitespaces and
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   155
        comments need to be filtered out, the lexing phase is actually
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   156
        quite useful.  }
898
45a48c47dcca updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 894
diff changeset
   157
      
894
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   158
\item The injection function for sequence regular expressions is defined
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   159
      by three clauses:
726
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 721
diff changeset
   160
894
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   161
\begin{center}
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   162
\begin{tabular}{l@{\hspace{1mm}}c@{\hspace{1mm}}l}
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   163
  $\inj\,(r_1 \cdot r_2)\,c\,\,Seq(v_1,v_2)$ & $\dn$  & $Seq(\inj\,r_1\,c\,v_1,v_2)$\\
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   164
  $\inj\,(r_1 \cdot r_2)\,c\,\,\Left(Seq(v_1,v_2))$ & $\dn$  & $Seq(\inj\,r_1\,c\,v_1,v_2)$\\
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   165
  $\inj\,(r_1 \cdot r_2)\,c\,\,Right(v)$ & $\dn$  & $Seq(\textit{mkeps}(r_1),\inj\,r_2\,c\,v)$\\
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   166
\end{tabular}
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   167
\end{center}
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   168
954
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   169
     Explain why there are three cases in the injection function for sequence
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   170
     regular expressions.
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   171
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   172
     \solution{
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   173
       This is because the derivative of sequences can be of the form
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   174
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   175
       \begin{itemize}
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   176
       \item $(der\,c\,r_1)\cdot r_2$
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   177
       \item $(der\,c\,r_1)\cdot r_2 \;+\; der\,c\,r_2$  
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   178
       \end{itemize}
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   179
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   180
       In the first case the value needs to be of the form $Seq$, in the second case $\Left$ or $Right$.
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   181
       Therefore 3 cases.
eda0ccf56c72 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   182
       }
894
02ef5c3abc51 updatedHG: added solutions/cw5/fun_tokens.sc
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
   183
      
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
   184
\item \POSTSCRIPT        
56
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
\end{enumerate}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
%%% End: