author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Mon, 24 Nov 2014 09:06:27 +0000 | |
changeset 312 | 5cdb4d40eb80 |
parent 292 | 7ed2a25dd115 |
child 359 | db106e5b7c4d |
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 |
||
9 |
\begin{enumerate} |
|
10 |
\item (i) Give the regular expressions for lexing a language |
|
11 |
consisting of whitespaces, identifiers (some letters followed by digits), numbers, |
|
12 |
operations \texttt{=}, \texttt{<} and \texttt{>}, and the keywords |
|
13 |
\texttt{if}, \texttt{then} and \texttt{else}. |
|
14 |
(ii) Decide whether the following strings |
|
15 |
can be lexed in this language? |
|
16 |
||
17 |
\begin{enumerate} |
|
18 |
\item \texttt{"if y4 = 3 then 1 else 3"} |
|
19 |
\item \texttt{"if33 ifif then then23 else else 32"} |
|
20 |
\item \texttt{"if x4x < 33 then 1 else 3"} |
|
21 |
\end{enumerate} |
|
22 |
||
23 |
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
|
24 |
Observe the maximal munch rule and priorities of your regular |
56 | 25 |
expressions that make the process of lexing unambiguous.) |
26 |
||
27 |
\item Suppose the grammar |
|
28 |
||
29 |
\begin{center} |
|
30 |
\begin{tabular}{lcl} |
|
31 |
$E$ & $\rightarrow$ & $F \;|\; F \cdot * \cdot F \;|\; F \cdot \backslash \cdot F$\\ |
|
32 |
$F$ & $\rightarrow$ & $T \;|\; T \cdot \texttt{+} \cdot T \;|\; T \cdot \texttt{-} \cdot T$\\ |
|
33 |
$T$ & $\rightarrow$ & $num \;|\; \texttt{(} \cdot E \cdot \texttt{)}$\\ |
|
34 |
\end{tabular} |
|
35 |
\end{center} |
|
36 |
||
37 |
where $E$, $F$ and $T$ are non-terminals, $E$ is the starting symbol of the grammar, and $num$ stands for |
|
38 |
a number token. Give a parse tree for the string \texttt{(3+3)+(2*3)}. |
|
39 |
||
40 |
\item Define what it means for a grammar to be ambiguous. Give an example of |
|
41 |
an ambiguous grammar. |
|
42 |
||
43 |
\item Suppose boolean expressions are built up from |
|
44 |
||
45 |
\begin{center} |
|
46 |
\begin{tabular}{ll} |
|
47 |
1.) & tokens for \texttt{true} and \texttt{false},\\ |
|
48 |
2.) & the infix operations \texttt{$\wedge$} and \texttt{$\vee$},\\ |
|
49 |
3.) & the prefix operation $\neg$, and\\ |
|
50 |
4.) & can be enclosed in parentheses. |
|
51 |
\end{tabular} |
|
52 |
\end{center} |
|
53 |
||
54 |
(i) Give a grammar that can recognise such boolean expressions |
|
55 |
and (ii) give a sample string involving all rules given in 1.-4.~that |
|
56 |
can be parsed by this grammar. |
|
57 |
||
58 |
||
59 |
\end{enumerate} |
|
60 |
||
61 |
\end{document} |
|
62 |
||
63 |
%%% Local Variables: |
|
64 |
%%% mode: latex |
|
65 |
%%% TeX-master: t |
|
66 |
%%% End: |