hws/hw05.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 11 Oct 2024 19:13:00 +0100
changeset 967 ce5de01b9632
parent 964 da1f8c033b8e
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
4794759139ea better organised
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: 267
diff changeset
     2
\usepackage{../style}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 267
diff changeset
     3
\usepackage{../graphics}
527
2a62f0845f98 updated
cu
parents: 444
diff changeset
     4
\usepackage{../grammar}
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\begin{document}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
     8
% explain what is a context-free grammar and the language it generates 
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
     9
%
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
    10
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 147
diff changeset
    11
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
\section*{Homework 5}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
964
da1f8c033b8e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 963
diff changeset
    14
%%%\HEADER
359
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 322
diff changeset
    15
db106e5b7c4d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 322
diff changeset
    16
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
\begin{enumerate}
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    18
\item Consider the basic regular expressions
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    19
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    20
\begin{center}
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    21
$r ::= \ZERO \;|\; \ONE \;|\; c  \;|\; r_1 + r_2  \;|\; r_1 \cdot r_2 \;|\; r^*$
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    22
\end{center}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    23
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    24
      and suppose you want to show a property $P(r)$ for all
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    25
      regular expressions $r$ by structural induction. Write
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    26
      down which cases do you need to analyse. State clearly
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    27
      the induction hypotheses if applicable in a case.
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    28
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    29
\item Define a regular expression, written $ALL$, that can
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    30
      match every string. This definition should be in terms
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    31
      of the following extended regular expressions:
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    32
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    33
\begin{center}
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    34
$r ::= \ZERO \;|\; 
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    35
       \ONE \;|\;  
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    36
       c  \;|\; 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    37
       r_1 + r_2 \;|\; 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    38
       r_1 \cdot r_2 \;|\; 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    39
       r^* \;|\;
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    40
       \sim r$
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    41
\end{center}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    42
953
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    43
\solution{
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    44
  There is the obvious solution $\sim{}\ZERO$, but also $a + \sim{}a$ would work.
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    45
}  
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    46
     
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    47
322
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    48
%\item Assume the delimiters for comments are \texttt{$\slash$*}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    49
%and \texttt{*$\slash$}. Give a regular expression that can
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    50
%recognise comments of the form
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    51
%
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    52
%\begin{center}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    53
%\texttt{$\slash$*~\ldots{}~*$\slash$} 
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    54
%\end{center}
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    55
%
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    56
%where the three dots stand for arbitrary characters, but not
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    57
%comment delimiters.
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    58
963
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    59
\item The \emph{not}-regular expression is definitely useful for recognising
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    60
  comments for example, but can
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    61
  sometimes be quite unintuitive when it comes to deciding which strings are
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    62
  matched or not. Consider
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    63
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    64
  \[
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    65
  (\sim{}a)^*  \quad\text{and}\quad \sim{}(a^*)\;.  
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    66
  \]  
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    67
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    68
  What is the language of each regular expression?
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    69
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    70
  \solution{
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    71
    The first one is ``all strings, except $[a]$''; the second
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    72
    ``all strings except strings of the form $a^*$''.
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    73
  }
85bb0ef99fc7 texupdate
Christian Urban <christian.urban@kcl.ac.uk>
parents: 953
diff changeset
    74
  
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
\item Define the following regular expressions 
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
\begin{center}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    78
\begin{tabular}{ll}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    79
$r^+$ & (one or more matches)\\
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    80
$r^?$   & (zero or one match)\\
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    81
$r^{\{n\}}$ & (exactly $n$ matches)\\
953
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    82
$r^{\{m.. n\}}$ & (at least $m$ and maximal $n$ matches, with the\\
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    83
&  \phantom{(}assumption $m \le n$)\\
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    84
\end{tabular}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
\end{center}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    86
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
    87
in terms of the usual basic regular expressions
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    89
\begin{center}
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
    90
$r ::= \ZERO \;|\; \ONE \;|\; c  \;|\; r_1 + r_2  \;|\; r_1 \cdot r_2 \;|\; r^*$
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    91
\end{center}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    92
953
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    93
\solution{
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    94
  $r^+ \dn r\cdot r^*$\\
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    95
  $r^? \dn r + 1$\\
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    96
  $r^{\{0\}} = \ONE$\\
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    97
  $r^{\{n\}} \dn r\cdot r^{\{n-1\}}$\\
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    98
  $r^{\{..n\}} \dn (r^?)^{\{n\}}$\\
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
    99
  $r^{\{n..m\}} \dn r^{\{..m-n\}}\cdot r^{\{n\}}$\\
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   100
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   101
  BTW, $r^{\{n..m\}}$ cannot be defined in terms of $r^{\{n..\}} \;\&\; r^{\{..m\}}$ where $\&$ is
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   102
  the intersection operator I introduced this year. For example assume $r=aaa + aaaaaaa$, then 
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   103
  $r^{\{4..6\}}$ cannot match 21 a's, but $r^{\{4..\}} \;\&\; r^{\{..6\}}$.
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   104
  }
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   105
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   106
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   107
\item 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
   108
      consisting of identifiers, left-parenthesis \texttt{(},
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   109
      right-parenthesis \texttt{)}, numbers that can be either
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   110
      positive or negative, and the operations \texttt{+},
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   111
      \texttt{-} and \texttt{*}. 
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   112
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   113
      Decide whether the following strings can be lexed in
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   114
      this language?
147
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
   115
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   116
\begin{enumerate}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   117
\item \texttt{"(a3+3)*b"}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   118
\item \texttt{")()++-33"}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   119
\item \texttt{"(b42/3)*3"}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   120
\end{enumerate}
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   121
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   122
In case they can, give the corresponding token sequences. (Hint: 
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   123
Observe the maximal munch rule and the priorities of your regular
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   124
expressions that make the process of lexing unambiguous.)
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   125
953
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   126
\solution{
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   127
  The first two strings can be lexed. But not the last ($/$ is not part of the language).
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   128
}
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   129
602
9aa901039e33 updated
Christian Urban <urbanc@in.tum.de>
parents: 527
diff changeset
   130
\item Suppose the following context-free grammar $G$
527
2a62f0845f98 updated
cu
parents: 444
diff changeset
   131
  
2a62f0845f98 updated
cu
parents: 444
diff changeset
   132
\begin{plstx}[margin=1cm]
2a62f0845f98 updated
cu
parents: 444
diff changeset
   133
  : \meta{S\/} ::= \meta{A\/}\cdot\meta{S\/}\cdot\meta{B\/} \;\mid\;
2a62f0845f98 updated
cu
parents: 444
diff changeset
   134
                   \meta{B\/}\cdot\meta{S\/}\cdot\meta{A\/} \;\mid\; \epsilon\\
2a62f0845f98 updated
cu
parents: 444
diff changeset
   135
  : \meta{A\/} ::= a \mid \epsilon\\
2a62f0845f98 updated
cu
parents: 444
diff changeset
   136
  : \meta{B\/} ::= b\\
2a62f0845f98 updated
cu
parents: 444
diff changeset
   137
\end{plstx}
2a62f0845f98 updated
cu
parents: 444
diff changeset
   138
602
9aa901039e33 updated
Christian Urban <urbanc@in.tum.de>
parents: 527
diff changeset
   139
where the starting symbol is $\meta{S}$.
9aa901039e33 updated
Christian Urban <urbanc@in.tum.de>
parents: 527
diff changeset
   140
Which of the following strings are in the language of $G$?
527
2a62f0845f98 updated
cu
parents: 444
diff changeset
   141
2a62f0845f98 updated
cu
parents: 444
diff changeset
   142
\begin{itemize}
2a62f0845f98 updated
cu
parents: 444
diff changeset
   143
\item[$\bullet$] $a$
2a62f0845f98 updated
cu
parents: 444
diff changeset
   144
\item[$\bullet$] $b$
2a62f0845f98 updated
cu
parents: 444
diff changeset
   145
\item[$\bullet$] $ab$
2a62f0845f98 updated
cu
parents: 444
diff changeset
   146
\item[$\bullet$] $ba$
2a62f0845f98 updated
cu
parents: 444
diff changeset
   147
\item[$\bullet$] $bb$
2a62f0845f98 updated
cu
parents: 444
diff changeset
   148
\item[$\bullet$] $baa$
619
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   149
\end{itemize}
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   150
953
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   151
\solution{
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   152
  The first and the last cannot be matched. Maybe it is a good exercise to
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   153
  write down the derivations for the rest.
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   154
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   155
  BTW, the language recognised by this grammar is strings consisting of
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   156
  a's and b's where there are equal or more number of b's than a's (including the
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   157
  empty string).
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   158
}
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   159
619
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   160
\item Suppose the following context-free grammar 
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   161
  
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   162
  \begin{plstx}[margin=1cm]
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   163
  : \meta{S\/} ::= a\cdot \meta{S\/}\cdot a\;\mid\;
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   164
                   b\cdot \meta{S\/}\cdot b\;\mid\; \epsilon\\
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   165
  \end{plstx}
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   166
136517d67d40 updated
Christian Urban <urbanc@in.tum.de>
parents: 602
diff changeset
   167
Describe which language is generated by this grammar.
953
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   168
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   169
\solution{Palindromes with the same number of a's and b's, including
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   170
  the empty string}
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   171
5e070fb0332a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 937
diff changeset
   172
937
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   173
\item Remember we have specified identifiers with regular expressions as
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   174
  strings that start with a letter followed by letters, digits and
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   175
  underscores. This can also be specified by a grammar rule or rules.
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   176
  What would the rule(s) look like for identifiers? 
527
2a62f0845f98 updated
cu
parents: 444
diff changeset
   177
937
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   178
  \solution{
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   179
  \begin{plstx}[margin=1cm]
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   180
  : \meta{Id\/} ::= \meta{Let\/}\cdot \meta{R}\\
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   181
  : \meta{Let\/} ::= a \;\mid\; \dots \;\mid\; z\\
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   182
  : \meta{Dig\/} ::= 0 \;\mid\; \dots \;\mid\; 9\\
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   183
  : \meta{R\/} ::= \meta{Let\/} \cdot \meta{R\/} \;\mid\;
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   184
                  \meta{Dig\/} \cdot \meta{R\/} \;\mid\;
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   185
                  $\_$ \cdot \meta{R\/} \;\mid\; \epsilon\\
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   186
  \end{plstx}  
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   187
}
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   188
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   189
\item If we specify keywords, identifiers (see above) and programs
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   190
  by grammar rules, are there any problems you need to be careful
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   191
  about when using a parser for identifying tokens?
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   192
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   193
  \solution{Parsers do not have the POSIX rules (e.g.~longest munch
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   194
    rule) built in. I am not aware that any parser does this out of
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   195
    the box and you would need to build in such constraints into the
dc5ab66b11cc updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 619
diff changeset
   196
    grammar rules or parsing mechanism.}
527
2a62f0845f98 updated
cu
parents: 444
diff changeset
   197
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 401
diff changeset
   198
\item {\bf(Optional)} Recall the definitions for $Der$ and $der$
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   199
      from the lectures. Prove by induction on $r$ the
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   200
      property that 
147
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
   201
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
   202
\[
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
   203
L(der\,c\,r) = Der\,c\,(L(r))
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
   204
\]
4725bba8ef26 added slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
   205
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 359
diff changeset
   206
      holds.
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   207
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 401
diff changeset
   208
\item \POSTSCRIPT        
93
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   209
\end{enumerate}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   210
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   211
\end{document}
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   212
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   213
%%% Local Variables: 
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   214
%%% mode: latex
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   215
%%% TeX-master: t
4794759139ea better organised
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   216
%%% End: