| author | Christian Urban <urbanc@in.tum.de> | 
| Tue, 06 Nov 2018 08:18:53 +0000 | |
| changeset 598 | 278b8b05f286 | 
| parent 596 | f683589c8022 | 
| child 647 | d74702cba346 | 
| permissions | -rw-r--r-- | 
| 56 | 1  | 
\documentclass{article}
 | 
| 
292
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
102 
diff
changeset
 | 
2  | 
\usepackage{../style}
 | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
102 
diff
changeset
 | 
3  | 
\usepackage{../graphics}
 | 
| 56 | 4  | 
|
5  | 
\begin{document}
 | 
|
6  | 
||
7  | 
\section*{Homework 6}
 | 
|
8  | 
||
| 
359
 
db106e5b7c4d
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
292 
diff
changeset
 | 
9  | 
\HEADER  | 
| 
 
db106e5b7c4d
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
292 
diff
changeset
 | 
10  | 
|
| 56 | 11  | 
\begin{enumerate}
 | 
12  | 
\item (i) Give the regular expressions for lexing a language  | 
|
| 
401
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
359 
diff
changeset
 | 
13  | 
consisting of whitespaces, identifiers (some letters  | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
359 
diff
changeset
 | 
14  | 
      followed by digits), numbers, operations \texttt{=},
 | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
359 
diff
changeset
 | 
15  | 
      \texttt{<} and \texttt{>}, and the keywords \texttt{if},
 | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
359 
diff
changeset
 | 
16  | 
      \texttt{then} and \texttt{else}. (ii) Decide whether the
 | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
359 
diff
changeset
 | 
17  | 
following strings can be lexed in this language?  | 
| 56 | 18  | 
|
19  | 
\begin{enumerate}
 | 
|
20  | 
\item \texttt{"if y4 = 3 then 1 else 3"}
 | 
|
21  | 
\item \texttt{"if33 ifif then then23 else else 32"}
 | 
|
22  | 
\item \texttt{"if x4x < 33 then 1 else 3"}
 | 
|
23  | 
\end{enumerate}
 | 
|
24  | 
||
25  | 
In case they can, give the corresponding token sequences. (Hint:  | 
|
| 
89
 
24adcc265f2e
typo
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
56 
diff
changeset
 | 
26  | 
Observe the maximal munch rule and priorities of your regular  | 
| 56 | 27  | 
expressions that make the process of lexing unambiguous.)  | 
28  | 
||
29  | 
\item Suppose the grammar  | 
|
30  | 
||
31  | 
\begin{center}
 | 
|
32  | 
\begin{tabular}{lcl}
 | 
|
33  | 
$E$ & $\rightarrow$ & $F \;|\; F \cdot * \cdot F \;|\; F \cdot \backslash \cdot F$\\  | 
|
34  | 
$F$ & $\rightarrow$ & $T \;|\; T \cdot \texttt{+} \cdot T \;|\; T \cdot \texttt{-} \cdot T$\\
 | 
|
35  | 
$T$ & $\rightarrow$ & $num \;|\; \texttt{(} \cdot E \cdot \texttt{)}$\\
 | 
|
36  | 
\end{tabular}
 | 
|
37  | 
\end{center}
 | 
|
38  | 
||
39  | 
where $E$, $F$ and $T$ are non-terminals, $E$ is the starting symbol of the grammar, and $num$ stands for  | 
|
40  | 
a number token. Give a parse tree for the string \texttt{(3+3)+(2*3)}.
 | 
|
41  | 
||
42  | 
\item Define what it means for a grammar to be ambiguous. Give an example of  | 
|
43  | 
an ambiguous grammar.  | 
|
44  | 
||
45  | 
\item Suppose boolean expressions are built up from  | 
|
46  | 
||
47  | 
\begin{center}
 | 
|
48  | 
\begin{tabular}{ll}
 | 
|
49  | 
1.) & tokens for \texttt{true} and \texttt{false},\\
 | 
|
50  | 
2.) & the infix operations \texttt{$\wedge$} and \texttt{$\vee$},\\
 | 
|
51  | 
3.) & the prefix operation $\neg$, and\\  | 
|
52  | 
4.) & can be enclosed in parentheses.  | 
|
53  | 
\end{tabular}
 | 
|
54  | 
\end{center}
 | 
|
55  | 
||
56  | 
(i) Give a grammar that can recognise such boolean expressions  | 
|
57  | 
and (ii) give a sample string involving all rules given in 1.-4.~that  | 
|
58  | 
can be parsed by this grammar.  | 
|
59  | 
||
| 577 | 60  | 
\item Given the regular expressions  | 
| 459 | 61  | 
|
| 577 | 62  | 
\begin{center}
 | 
63  | 
\begin{tabular}{ll}    
 | 
|
64  | 
1) & $(ab + a)\cdot (\ONE + b)$\\  | 
|
65  | 
2) & $(aa + a)^*$\\  | 
|
66  | 
\end{tabular}
 | 
|
67  | 
\end{center}
 | 
|
| 459 | 68  | 
|
| 595 | 69  | 
there are several values for how these regular expressions can  | 
| 596 | 70  | 
recognise the strings (for 1) $ab$ and (for 2) $aaa$. Give in each case  | 
71  | 
\emph{all} the values and indicate which one is the POSIX value.
 | 
|
| 459 | 72  | 
\item \POSTSCRIPT  | 
| 56 | 73  | 
\end{enumerate}
 | 
74  | 
||
75  | 
\end{document}
 | 
|
76  | 
||
77  | 
%%% Local Variables:  | 
|
78  | 
%%% mode: latex  | 
|
79  | 
%%% TeX-master: t  | 
|
80  | 
%%% End:  |