582
|
1 |
|
664
|
2 |
% !TEX program = xelatex
|
743
|
3 |
\documentclass[dvipsnames,14pt,t,xelatex,aspectratio=169,xcolor={table}]{beamer}
|
270
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
4 |
\usepackage{../slides}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
5 |
\usepackage{../graphics}
|
215
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
6 |
\usepackage{../langs}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
7 |
\usepackage{../data}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
8 |
\usepackage{../grammar}
|
44
|
9 |
|
270
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
10 |
\hfuzz=220pt
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
11 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
12 |
\pgfplotsset{compat=1.11}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
13 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
14 |
\newcommand{\bl}[1]{\textcolor{blue}{#1}}
|
44
|
15 |
|
|
16 |
% beamer stuff
|
445
|
17 |
\renewcommand{\slidecaption}{CFL 05, King's College London}
|
270
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
18 |
|
44
|
19 |
|
|
20 |
\begin{document}
|
|
21 |
|
|
22 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
270
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
23 |
\begin{frame}[t]
|
44
|
24 |
\frametitle{%
|
|
25 |
\begin{tabular}{@ {}c@ {}}
|
|
26 |
\\[-3mm]
|
445
|
27 |
\LARGE Compilers and \\[-2mm]
|
743
|
28 |
\LARGE Formal Languages\\[3mm]
|
44
|
29 |
\end{tabular}}
|
|
30 |
|
|
31 |
\normalsize
|
|
32 |
\begin{center}
|
|
33 |
\begin{tabular}{ll}
|
664
|
34 |
Email: & christian.urban at kcl.ac.uk\\
|
743
|
35 |
%Office Hours: & Thursdays 12 -- 14\\
|
|
36 |
%Location: & N7.07 (North Wing, Bush House)\\
|
664
|
37 |
Slides \& Progs: & KEATS (also homework is there)\\
|
44
|
38 |
\end{tabular}
|
743
|
39 |
\end{center}
|
|
40 |
|
|
41 |
\begin{center}
|
|
42 |
\begin{tikzpicture}
|
|
43 |
\node[drop shadow,fill=white,inner sep=0pt]
|
|
44 |
{\footnotesize\rowcolors{1}{capri!10}{white}
|
|
45 |
\begin{tabular}{|p{4.8cm}|p{4.8cm}|}\hline
|
|
46 |
1 Introduction, Languages & 6 While-Language \\
|
|
47 |
2 Regular Expressions, Derivatives & 7 Compilation, JVM \\
|
|
48 |
3 Automata, Regular Languages & 8 Compiling Functional Languages \\
|
|
49 |
4 Lexing, Tokenising & 9 Optimisations \\
|
|
50 |
\cellcolor{blue!50}
|
|
51 |
5 Grammars, Parsing & 10 LLVM \\ \hline
|
|
52 |
\end{tabular}%
|
|
53 |
};
|
|
54 |
\end{tikzpicture}
|
44
|
55 |
\end{center}
|
270
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
56 |
\end{frame}
|
289
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
57 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
44
|
58 |
|
667
|
59 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
743
|
60 |
%\begin{frame}[c]
|
|
61 |
% \frametitle{Coursework 1: Submissions}
|
|
62 |
%
|
|
63 |
% \begin{itemize}
|
|
64 |
% \item Scala (29)
|
|
65 |
% \item Haskell (1)
|
|
66 |
% \item Kotlin (1)
|
|
67 |
% \item Rust (1)
|
|
68 |
% \end{itemize}\bigskip\bigskip
|
|
69 |
%
|
|
70 |
% \small
|
|
71 |
% Please get in contact if you intend to do CW Strand 2. No zips please.
|
|
72 |
% Give definitions also on paper if asked. BTW, simp
|
|
73 |
% can stay unchanged. Use \texttt{ders} for CW2, not \texttt{ders2}!
|
|
74 |
% \end{frame}
|
289
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
75 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
743
|
76 |
|
528
|
77 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
78 |
\begin{frame}[t]
|
|
79 |
\frametitle{Lexer, Parser}
|
|
80 |
\mbox{}\\[-16mm]\mbox{}
|
|
81 |
|
|
82 |
\begin{center}
|
|
83 |
\begin{tikzpicture}[scale=1,
|
|
84 |
node/.style={
|
|
85 |
rectangle,rounded corners=3mm,
|
|
86 |
very thick,draw=black!50,
|
|
87 |
minimum height=18mm, minimum width=20mm,
|
743
|
88 |
top color=white,bottom color=black!20,drop shadow}]
|
528
|
89 |
\node (0) at (-2.3,0) {};
|
|
90 |
|
|
91 |
\node (A) at (0,0) [node] {};
|
|
92 |
\node [below right] at (A.north west) {lexer};
|
|
93 |
|
|
94 |
\node (B) at (3,0) [node] {};
|
|
95 |
\node [below right=1mm] at (B.north west)
|
|
96 |
{\mbox{}\hspace{-1mm}parser};
|
|
97 |
|
|
98 |
\node (C) at (6,0) [node] {};
|
|
99 |
\node [below right] at (C.north west)
|
|
100 |
{\mbox{}\hspace{-1mm}code gen};
|
|
101 |
|
|
102 |
\node (1) at (8.4,0) {};
|
|
103 |
|
|
104 |
\draw [->,line width=4mm] (0) -- (A);
|
|
105 |
\draw [->,line width=4mm] (A) -- (B);
|
|
106 |
\draw [->,line width=4mm] (B) -- (C);
|
|
107 |
\draw [->,line width=4mm] (C) -- (1);
|
|
108 |
\end{tikzpicture}
|
|
109 |
\end{center}
|
|
110 |
|
|
111 |
Today a parser.
|
|
112 |
|
|
113 |
\end{frame}
|
|
114 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
115 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
116 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
117 |
\begin{frame}[c]
|
550
|
118 |
\frametitle{What Parsing is Not}
|
|
119 |
|
|
120 |
Usually parsing does not check semantic correctness, e.g.
|
|
121 |
|
|
122 |
\begin{itemize}
|
|
123 |
\item whether a function is not used before it
|
|
124 |
is defined
|
|
125 |
\item whether a function has the correct number of arguments
|
|
126 |
or are of correct type
|
|
127 |
\item whether a variable can be declared twice in a scope
|
|
128 |
\end{itemize}
|
|
129 |
|
|
130 |
\end{frame}
|
|
131 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
136 |
\begin{frame}[c]
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
137 |
\frametitle{Regular Languages}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
138 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
139 |
While regular expressions are very useful for lexing, there is
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
140 |
no regular expression that can recognise the language
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
141 |
\bl{$a^nb^n$}.\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
142 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
143 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
144 |
\bl{$(((()()))())$} \;\;vs.\;\; \bl{$(((()()))()))$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
145 |
\end{center}\bigskip\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
146 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
147 |
\small
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
148 |
\noindent So we cannot find out with regular expressions
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
149 |
whether parentheses are matched or unmatched. Also regular
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
150 |
expressions are not recursive, e.g.~\bl{$(1 + 2) + 3$}.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
151 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
152 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
153 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
154 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
155 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
156 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
157 |
\frametitle{Hierarchy of Languages}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
158 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
159 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
160 |
\begin{tikzpicture}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
161 |
[rect/.style={draw=black!50,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
162 |
top color=white,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
163 |
bottom color=black!20,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
164 |
rectangle,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
165 |
very thick,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
166 |
rounded corners}, scale=1.2]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
167 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
168 |
\draw (0,0) node [rect, text depth=39mm, text width=68mm] {all languages};
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
169 |
\draw (0,-0.4) node [rect, text depth=28.5mm, text width=64mm] {decidable languages};
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
170 |
\draw (0,-0.85) node [rect, text depth=17mm] {context sensitive languages};
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
171 |
\draw (0,-1.14) node [rect, text depth=9mm, text width=50mm] {context-free languages};
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
172 |
\draw (0,-1.4) node [rect] {regular languages};
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
173 |
\end{tikzpicture}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
174 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
175 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
176 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
177 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
178 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
179 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
180 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
181 |
\begin{frame}[c]
|
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
182 |
\frametitle{CF Grammars}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
183 |
|
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
184 |
A \alert{\bf context-free grammar} \bl{$G$} consists of
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
185 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
186 |
\begin{itemize}
|
664
|
187 |
\item a finite set of nonterminal symbols (e.g.~$\meta{A}$ upper case)
|
|
188 |
\item a finite set terminal symbols or tokens (lower case)
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
189 |
\item a start symbol (which must be a nonterminal)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
190 |
\item a set of rules
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
191 |
\begin{center}
|
451
|
192 |
\bl{$\meta{A} ::= \textit{rhs}$}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
193 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
194 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
195 |
where \bl{\textit{rhs}} are sequences involving terminals and nonterminals,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
196 |
including the empty sequence \bl{$\epsilon$}.\medskip\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
197 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
198 |
We also allow rules
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
199 |
\begin{center}
|
451
|
200 |
\bl{$\meta{A} ::= \textit{rhs}_1 | \textit{rhs}_2 | \ldots$}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
201 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
202 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
203 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
204 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
205 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
206 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
207 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
582
|
208 |
\begin{frame}[t]
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
209 |
\frametitle{Palindromes}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
210 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
211 |
A grammar for palindromes over the alphabet~\bl{$\{a,b\}$}:
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
212 |
|
451
|
213 |
\bl{\begin{plstx}[margin=3cm]
|
|
214 |
: \meta{S} ::= a\cdot\meta{S}\cdot a\\
|
|
215 |
: \meta{S} ::= b\cdot\meta{S}\cdot b\\
|
588
|
216 |
: \meta{S} ::= a\\
|
|
217 |
: \meta{S} ::= b\\
|
|
218 |
: \meta{S} ::= \epsilon\\
|
451
|
219 |
\end{plstx}}\pause
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
220 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
221 |
or
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
222 |
|
451
|
223 |
\bl{\begin{plstx}[margin=2cm]
|
588
|
224 |
: \meta{S} ::= a\cdot \meta{S}\cdot a | b\cdot \meta{S}\cdot b | a | b | \epsilon\\
|
451
|
225 |
\end{plstx}}\pause\bigskip
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
226 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
227 |
\small
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
228 |
Can you find the grammar rules for matched parentheses?
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
229 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
230 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
231 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
232 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
233 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
234 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
235 |
\frametitle{Arithmetic Expressions}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
236 |
|
451
|
237 |
\bl{\begin{plstx}[margin=3cm,one per line]
|
|
238 |
: \meta{E} ::= num\_token
|
|
239 |
| \meta{E} \cdot + \cdot \meta{E}
|
|
240 |
| \meta{E} \cdot - \cdot \meta{E}
|
|
241 |
| \meta{E} \cdot * \cdot \meta{E}
|
|
242 |
| ( \cdot \meta{E} \cdot ) \\
|
|
243 |
\end{plstx}}\pause
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
244 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
245 |
\bl{\texttt{1 + 2 * 3 + 4}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
246 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
247 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
248 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
249 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
250 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
251 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
252 |
\frametitle{A CFG Derivation}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
253 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
254 |
\begin{enumerate}
|
451
|
255 |
\item Begin with a string containing only the start symbol, say \bl{\meta{S}}\bigskip
|
|
256 |
\item Replace any nonterminal \bl{\meta{X}} in the string by the
|
|
257 |
right-hand side of some production \bl{$\meta{X} ::= \textit{rhs}$}\bigskip
|
582
|
258 |
\item Repeat 2 until there are no nonterminals left
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
259 |
\end{enumerate}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
260 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
261 |
\begin{center}
|
451
|
262 |
\bl{$\meta{S} \rightarrow \ldots \rightarrow \ldots \rightarrow \ldots \rightarrow \ldots $}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
263 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
264 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
265 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
266 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
267 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
268 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
451
|
269 |
\begin{frame}[t]
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
270 |
\frametitle{Example Derivation}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
271 |
|
451
|
272 |
\bl{\begin{plstx}[margin=2cm]
|
|
273 |
: \meta{S} ::= \epsilon | a\cdot \meta{S}\cdot a | b\cdot \meta{S}\cdot b \\
|
|
274 |
\end{plstx}}\bigskip
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
275 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
276 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
277 |
\begin{tabular}{lcl}
|
451
|
278 |
\bl{\meta{S}} & \bl{$\rightarrow$} & \bl{$a\meta{S}a$}\\
|
|
279 |
& \bl{$\rightarrow$} & \bl{$ab\meta{S}ba$}\\
|
|
280 |
& \bl{$\rightarrow$} & \bl{$aba\meta{S}aba$}\\
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
281 |
& \bl{$\rightarrow$} & \bl{$abaaba$}\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
282 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
283 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
284 |
\end{tabular}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
285 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
286 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
287 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
288 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
289 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
290 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
451
|
291 |
\begin{frame}[t]
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
292 |
\frametitle{Example Derivation}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
293 |
|
451
|
294 |
\bl{\begin{plstx}[margin=3cm,one per line]
|
|
295 |
: \meta{E} ::= num\_token
|
|
296 |
| \meta{E} \cdot + \cdot \meta{E}
|
|
297 |
| \meta{E} \cdot - \cdot \meta{E}
|
|
298 |
| \meta{E} \cdot * \cdot \meta{E}
|
|
299 |
| ( \cdot \meta{E} \cdot ) \\
|
|
300 |
\end{plstx}}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
301 |
|
451
|
302 |
\small
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
303 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
304 |
\begin{tabular}{@{}c@{}c@{}}
|
451
|
305 |
\begin{tabular}{@{\hspace{-2mm}}l@{\hspace{1mm}}l@{\hspace{1mm}}l@{\hspace{4mm}}}
|
|
306 |
\bl{\meta{E}} & \bl{$\rightarrow$} & \bl{$\meta{E}*\meta{E}$}\\
|
|
307 |
& \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}*\meta{E}$}\\
|
|
308 |
& \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}*\meta{E}+\meta{E}$}\\
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
309 |
& \bl{$\rightarrow^+$} & \bl{$1+2*3+4$}\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
310 |
\end{tabular} &\pause
|
451
|
311 |
\begin{tabular}{@{}l@{\hspace{0mm}}l@{\hspace{1mm}}l}
|
|
312 |
\bl{$\meta{E}$} & \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}$}\\
|
|
313 |
& \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}+\meta{E}$}\\
|
|
314 |
& \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}*\meta{E}+\meta{E}$}\\
|
|
315 |
& \bl{$\rightarrow^+$} & \bl{$1+2*3+4$}\\
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
316 |
\end{tabular}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
317 |
\end{tabular}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
318 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
319 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
320 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
321 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
322 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
323 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
324 |
\begin{frame}[c]
|
664
|
325 |
\frametitle{Context Sensitive Grammars}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
326 |
|
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
327 |
It is much harder to find out whether a string is parsed
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
328 |
by a context sensitive grammar:
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
329 |
|
451
|
330 |
\bl{\begin{plstx}[margin=2cm]
|
|
331 |
: \meta{S} ::= b\meta{S}\meta{A}\meta{A} | \epsilon\\
|
|
332 |
: \meta{A} ::= a\\
|
|
333 |
: b\meta{A} ::= \meta{A}b\\
|
|
334 |
\end{plstx}}\pause
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
335 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
336 |
\begin{center}
|
451
|
337 |
\bl{$\meta{S} \rightarrow\ldots\rightarrow^? ababaa$}
|
665
|
338 |
\end{center}\pause
|
|
339 |
|
|
340 |
\begin{center}
|
|
341 |
\tt Time flies like an arrow;\\
|
|
342 |
fruit flies like bananas.
|
|
343 |
\end{center}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
344 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
345 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
346 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
347 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
348 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
349 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
350 |
\frametitle{Language of a CFG}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
351 |
|
451
|
352 |
Let \bl{$G$} be a context-free grammar with start symbol \bl{\meta{S}}.
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
353 |
Then the language \bl{$L(G)$} is:
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
354 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
355 |
\begin{center}
|
451
|
356 |
\bl{$\{c_1\ldots c_n \;|\; \forall i.\; c_i \in T \wedge \meta{S} \rightarrow^* c_1\ldots c_n \}$}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
357 |
\end{center}\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
358 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
359 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
360 |
\item Terminals, because there are no rules for replacing them.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
361 |
\item Once generated, terminals are ``permanent''.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
362 |
\item Terminals ought to be tokens of the language\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
363 |
(but can also be strings).
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
364 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
365 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
366 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
367 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
368 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
369 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
451
|
370 |
\begin{frame}[t]
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
371 |
\frametitle{Parse Trees}
|
582
|
372 |
\mbox{}\\[-12mm]
|
451
|
373 |
|
593
|
374 |
\bl{\begin{plstx}: \meta{E} ::= \meta{T} | \meta{T} \cdot + \cdot \meta{E} | \meta{T} \cdot - \cdot \meta{E}\\
|
529
|
375 |
: \meta{T} ::= \meta{F} | \meta{F} \cdot * \cdot \meta{T}\\
|
|
376 |
: \meta{F} ::= num\_token | ( \cdot \meta{E} \cdot )\\
|
451
|
377 |
\end{plstx}}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
378 |
|
451
|
379 |
\begin{center}\small
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
380 |
\begin{tikzpicture}[level distance=8mm, blue]
|
451
|
381 |
\node {$\meta{E}$}
|
529
|
382 |
child {node {$\meta{T}$}
|
451
|
383 |
child {node {$\meta{T}$}
|
|
384 |
child {node {(\,$\meta{E}$\,)}
|
|
385 |
child {node{$\meta{F}$ *{} $\meta{F}$}
|
|
386 |
child {node {$\meta{T}$} child {node {2}}}
|
|
387 |
child {node {$\meta{T}$} child {node {3}}}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
388 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
389 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
390 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
391 |
child {node {+}}
|
451
|
392 |
child {node {$\meta{T}$}
|
|
393 |
child {node {(\,$\meta{E}$\,)}
|
|
394 |
child {node {$\meta{F}$}
|
|
395 |
child {node {$\meta{T}$ +{} $\meta{T}$}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
396 |
child {node {3}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
397 |
child {node {4}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
398 |
}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
399 |
}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
400 |
}};
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
401 |
\end{tikzpicture}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
402 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
403 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
404 |
\begin{textblock}{5}(1, 6.5)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
405 |
\bl{\texttt{(2*3)+(3+4)}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
406 |
\end{textblock}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
407 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
408 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
409 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
410 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
411 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
451
|
412 |
\begin{frame}[t]
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
413 |
\frametitle{Arithmetic Expressions}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
414 |
|
451
|
415 |
\bl{\begin{plstx}[margin=3cm,one per line]
|
|
416 |
: \meta{E} ::= num\_token
|
|
417 |
| \meta{E} \cdot + \cdot \meta{E}
|
|
418 |
| \meta{E} \cdot - \cdot \meta{E}
|
|
419 |
| \meta{E} \cdot * \cdot \meta{E}
|
|
420 |
| ( \cdot \meta{E} \cdot ) \\
|
|
421 |
\end{plstx}}\pause\bigskip
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
422 |
|
451
|
423 |
A CFG is \alert{\bf left-recursive} if it has a nonterminal \bl{$\meta{E}$} such
|
|
424 |
that \bl{$\meta{E} \rightarrow^+ \meta{E}\cdot \ldots$}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
425 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
426 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
427 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
428 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
429 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
451
|
430 |
\begin{frame}[t]
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
431 |
\frametitle{Ambiguous Grammars}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
432 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
433 |
A grammar is \alert{\bf ambiguous} if there is a string that
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
434 |
has at least two different parse trees.
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
435 |
|
451
|
436 |
\bl{\begin{plstx}[margin=3cm,one per line]: \meta{E} ::= num\_token
|
|
437 |
| \meta{E} \cdot + \cdot \meta{E}
|
|
438 |
| \meta{E} \cdot - \cdot \meta{E}
|
|
439 |
| \meta{E} \cdot * \cdot \meta{E}
|
|
440 |
| ( \cdot \meta{E} \cdot ) \\
|
|
441 |
\end{plstx}}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
442 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
443 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
444 |
\bl{\texttt{1 + 2 * 3 + 4}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
445 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
446 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
447 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
448 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
449 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
450 |
\begin{frame}[c]
|
582
|
451 |
\frametitle{`Dangling' Else}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
452 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
453 |
Another ambiguous grammar:\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
454 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
455 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
456 |
\bl{\begin{tabular}{lcl}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
457 |
$E$ & $\rightarrow$ & if $E$ then $E$\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
458 |
& $|$ & if $E$ then $E$ else $E$ \\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
459 |
& $|$ & \ldots
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
460 |
\end{tabular}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
461 |
\end{center}\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
462 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
463 |
\bl{\texttt{if a then if x then y else c}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
464 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
465 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
466 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
467 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
468 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
469 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
470 |
\frametitle{Parser Combinators}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
471 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
472 |
One of the simplest ways to implement a parser, see
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
473 |
{\small\url{https://vimeo.com/142341803}}\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
474 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
475 |
Parser combinators: \bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
476 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
477 |
\begin{minipage}{1.1\textwidth}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
478 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
479 |
\mbox{}\hspace{-12mm}\mbox{}$\underbrace{\text{list of tokens}}_{\text{input}}$ \bl{$\Rightarrow$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
480 |
$\underbrace{\text{set of (parsed input, unparsed input)}}_{\text{output}}$
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
481 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
482 |
\end{minipage}\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
483 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
484 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
485 |
\item atomic parsers
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
486 |
\item sequencing
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
487 |
\item alternative
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
488 |
\item semantic action
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
489 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
490 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
491 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
492 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
493 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
494 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
495 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
496 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
497 |
Atomic parsers, for example, number tokens
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
498 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
499 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
500 |
\bl{$\texttt{Num(123)}::rest \;\Rightarrow\; \{(\texttt{Num(123)}, rest)\}$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
501 |
\end{center}\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
502 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
503 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
504 |
\item you consume one or more token from the\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
505 |
input (stream)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
506 |
\item also works for characters and strings
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
507 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
508 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
509 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
510 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
511 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
512 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
513 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
514 |
Alternative parser (code \bl{$p\;||\;q$})\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
515 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
516 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
517 |
\item apply \bl{$p$} and also \bl{$q$}; then combine
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
518 |
the outputs
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
519 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
520 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
521 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
522 |
\large \bl{$p(\text{input}) \cup q(\text{input})$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
523 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
524 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
525 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
526 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
527 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
528 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
529 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
530 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
531 |
Sequence parser (code \bl{$p\sim q$})\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
532 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
533 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
534 |
\item apply first \bl{$p$} producing a set of pairs
|
528
|
535 |
\item then apply \bl{$q$} to the unparsed part
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
536 |
\item then combine the results:\medskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
537 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
538 |
((output$_1$, output$_2$), unparsed part)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
539 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
540 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
541 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
542 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
543 |
\begin{tabular}{l}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
544 |
\large \bl{$\{((o_1, o_2), u_2) \;|\;$}\\[2mm]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
545 |
\large\mbox{}\hspace{15mm} \bl{$(o_1, u_1) \in p(\text{input}) \wedge$}\\[2mm]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
546 |
\large\mbox{}\hspace{15mm} \bl{$(o_2, u_2) \in q(u_1)\}$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
547 |
\end{tabular}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
548 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
549 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
550 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
551 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
552 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
553 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
554 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
555 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
556 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
557 |
Function parser (code \bl{$p \Rightarrow f\;$})\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
558 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
559 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
560 |
\item apply \bl{$p$} producing a set of pairs
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
561 |
\item then apply the function \bl{$f$} to each first component
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
562 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
563 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
564 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
565 |
\begin{tabular}{l}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
566 |
\large \bl{$\{(f(o_1), u_1) \;|\; (o_1, u_1) \in p(\text{input})\}$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
567 |
\end{tabular}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
568 |
\end{center}\bigskip\bigskip\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
569 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
570 |
\bl{$f$} is the semantic action (``what to do with the parsed input'')
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
571 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
572 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
573 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
574 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
575 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
576 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
577 |
\mode<presentation>{
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
578 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
579 |
\frametitle{\begin{tabular}{c}Semantic Actions\end{tabular}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
580 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
581 |
Addition
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
582 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
583 |
\begin{center}
|
582
|
584 |
\bl{$\meta{T} \sim + \sim \meta{E} \Rightarrow \underbrace{f\,((x,y), z) \Rightarrow x + z}_{\text{semantic action}}$}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
585 |
\end{center}\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
586 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
587 |
Multiplication
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
588 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
589 |
\begin{center}
|
582
|
590 |
\bl{$\meta{F} \sim * \sim \meta{T} \Rightarrow f\,((x,y), z) \Rightarrow x * z$}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
591 |
\end{center}\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
592 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
593 |
Parenthesis
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
594 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
595 |
\begin{center}
|
582
|
596 |
\bl{$\text{(} \sim \meta{E} \sim \text{)} \Rightarrow f\,((x,y), z) \Rightarrow y$}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
597 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
598 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
599 |
\end{frame}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
600 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
601 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
602 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
603 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
604 |
\frametitle{Types of Parsers}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
605 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
606 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
607 |
\item {\bf Sequencing}: if \bl{$p$} returns results of type \bl{$T$}, and \bl{$q$} results of type \bl{$S$},
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
608 |
then \bl{$p \sim q$} returns results of type
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
609 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
610 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
611 |
\bl{$T \times S$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
612 |
\end{center}\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
613 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
614 |
\item {\bf Alternative}: if \bl{$p$} returns results of type \bl{$T$} then \bl{$q$} \alert{must} also have results of type \bl{$T$},
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
615 |
and \bl{$p \;||\; q$} returns results of type
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
616 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
617 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
618 |
\bl{$T$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
619 |
\end{center}\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
620 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
621 |
\item {\bf Semantic Action}: if \bl{$p$} returns results of type \bl{$T$} and \bl{$f$} is a function from
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
622 |
\bl{$T$} to \bl{$S$}, then
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
623 |
\bl{$p \Rightarrow f$} returns results of type
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
624 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
625 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
626 |
\bl{$S$}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
627 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
628 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
629 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
630 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
631 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
632 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
633 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
634 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
635 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
636 |
\frametitle{Input Types of Parsers}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
637 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
638 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
639 |
\item input: \alert{token list}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
640 |
\item output: set of (output\_type, \alert{token list})
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
641 |
\end{itemize}\bigskip\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
642 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
643 |
actually it can be any input type as long as it is a kind of
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
644 |
sequence (for example a string)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
645 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
646 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
647 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
648 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
649 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
650 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
651 |
\frametitle{Scannerless Parsers}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
652 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
653 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
654 |
\item input: \alert{string}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
655 |
\item output: set of (output\_type, \alert{string})
|
664
|
656 |
\end{itemize}\bigskip\bigskip
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
657 |
|
664
|
658 |
but using lexers is better because whitespaces or comments can be
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
659 |
filtered out; then input is a sequence of tokens
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
660 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
661 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
662 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
663 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
664 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
665 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
666 |
\frametitle{Successful Parses}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
667 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
668 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
669 |
\item input: string
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
670 |
\item output: \alert{set of} (output\_type, string)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
671 |
\end{itemize}\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
672 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
673 |
a parse is successful whenever the input has been fully
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
674 |
``consumed'' (that is the second component is empty)
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
675 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
676 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
677 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
678 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
679 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
680 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
681 |
\frametitle{Abstract Parser Class}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
682 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
683 |
\small
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
684 |
\lstinputlisting[language=Scala,xleftmargin=1mm]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
685 |
{../progs/app7.scala}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
686 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
687 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
688 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
689 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
690 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
691 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
692 |
\small
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
693 |
\fontsize{10}{12}\selectfont
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
694 |
\lstinputlisting[language=Scala,xleftmargin=1mm]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
695 |
{../progs/app8.scala}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
696 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
697 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
698 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
699 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
700 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
701 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
702 |
\frametitle{Two Grammars}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
703 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
704 |
Which languages are recognised by the following two grammars?
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
705 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
706 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
707 |
\bl{\begin{tabular}{lcl}
|
582
|
708 |
$\meta{S}$ & $\rightarrow$ & $1 \cdot \meta{S} \cdot \meta{S}$\\
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
709 |
& $|$ & $\epsilon$
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
710 |
\end{tabular}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
711 |
\end{center}\bigskip
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
712 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
713 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
714 |
\bl{\begin{tabular}{lcl}
|
582
|
715 |
$\meta{U}$ & $\rightarrow$ & $1 \cdot \meta{U}$\\
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
716 |
& $|$ & $\epsilon$
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
717 |
\end{tabular}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
718 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
719 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
720 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
721 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
722 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
723 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
724 |
\begin{frame}[t]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
725 |
\frametitle{Ambiguous Grammars}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
726 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
727 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
728 |
\begin{tikzpicture}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
729 |
\begin{axis}[xlabel={\pcode{1}s},ylabel={time in secs},
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
730 |
enlargelimits=false,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
731 |
xtick={0,100,...,1000},
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
732 |
xmax=1050,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
733 |
ymax=33,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
734 |
ytick={0,5,...,30},
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
735 |
scaled ticks=false,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
736 |
axis lines=left,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
737 |
width=11cm,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
738 |
height=7cm,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
739 |
legend entries={unambiguous,ambiguous},
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
740 |
legend pos=north east,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
741 |
legend cell align=left,
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
742 |
x tick label style={font=\small,/pgf/number format/1000 sep={}}]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
743 |
\addplot[blue,mark=*, mark options={fill=white}]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
744 |
table {s-grammar1.data};
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
745 |
\only<2>{
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
746 |
\addplot[red,mark=triangle*, mark options={fill=white}]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
747 |
table {s-grammar2.data};}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
748 |
\end{axis}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
749 |
\end{tikzpicture}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
750 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
751 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
752 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
753 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
754 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
755 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
756 |
\begin{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
757 |
\frametitle{While-Language}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
758 |
\mbox{}\\[-23mm]\mbox{}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
759 |
|
370
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
760 |
\bl{\begin{plstx}[rhs style=,one per line]: \meta{Stmt} ::= skip
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
761 |
| \meta{Id} := \meta{AExp}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
762 |
| if \meta{BExp} then \meta{Block} else \meta{Block}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
763 |
| while \meta{BExp} do \meta{Block}\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
764 |
: \meta{Stmts} ::= \meta{Stmt} ; \meta{Stmts}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
765 |
| \meta{Stmt}\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
766 |
: \meta{Block} ::= \{ \meta{Stmts} \}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
767 |
| \meta{Stmt}\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
768 |
: \meta{AExp} ::= \ldots\\
|
370
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
769 |
: \meta{BExp} ::= \ldots\\\end{plstx}}
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
770 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
771 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
772 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
773 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
774 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
775 |
\begin{frame}[c]
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
776 |
\frametitle{An Interpreter}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
777 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
778 |
\begin{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
779 |
\bl{\begin{tabular}{l}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
780 |
$\{$\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
781 |
\;\;$x := 5 \text{;}$\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
782 |
\;\;$y := x * 3\text{;}$\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
783 |
\;\;$y := x * 4\text{;}$\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
784 |
\;\;$x := u * 3$\\
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
785 |
$\}$
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
786 |
\end{tabular}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
787 |
\end{center}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
788 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
789 |
\begin{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
790 |
\item the interpreter has to record the value of \bl{$x$} before assigning a value to \bl{$y$}\pause
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
791 |
\item \bl{\texttt{eval(stmt, env)}}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
792 |
\end{itemize}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
793 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
794 |
\end{frame}
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
795 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
796 |
|
465
|
797 |
|
|
798 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
799 |
\mode<presentation>{
|
|
800 |
\begin{frame}[c]
|
|
801 |
\frametitle{\begin{tabular}{c}Interpreter\end{tabular}}
|
|
802 |
|
|
803 |
\begin{center}
|
|
804 |
\bl{\begin{tabular}{@{}lcl@{}}
|
|
805 |
$\text{eval}(n, E)$ & $\dn$ & $n$\\
|
|
806 |
$\text{eval}(x, E)$ & $\dn$ & $E(x)$ \;\;\;\textcolor{black}{lookup \bl{$x$} in \bl{$E$}}\\
|
|
807 |
$\text{eval}(a_1 + a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) + \text{eval}(a_2, E)$\\
|
|
808 |
$\text{eval}(a_1 - a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) - \text{eval}(a_2, E)$\\
|
|
809 |
$\text{eval}(a_1 * a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) * \text{eval}(a_2, E)$\bigskip\\
|
|
810 |
$\text{eval}(a_1 = a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) = \text{eval}(a_2, E)$\\
|
|
811 |
$\text{eval}(a_1\,!\!= a_2, E)$ & $\dn$ & $\neg(\text{eval}(a_1, E) = \text{eval}(a_2, E))$\\
|
|
812 |
$\text{eval}(a_1 < a_2, E)$ & $\dn$ & $\text{eval}(a_1, E) < \text{eval}(a_2, E)$\
|
|
813 |
\end{tabular}}
|
|
814 |
\end{center}
|
|
815 |
|
|
816 |
\end{frame}}
|
|
817 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
818 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
819 |
\mode<presentation>{
|
|
820 |
\begin{frame}[c]
|
|
821 |
\frametitle{\begin{tabular}{c}Interpreter (2)\end{tabular}}
|
|
822 |
|
|
823 |
\begin{center}
|
|
824 |
\bl{\begin{tabular}{@{}lcl@{}}
|
|
825 |
$\text{eval}(\text{skip}, E)$ & $\dn$ & $E$\\
|
|
826 |
$\text{eval}(x:=a, E)$ & $\dn$ & \bl{$E(x \mapsto \text{eval}(a, E))$}\\
|
|
827 |
\multicolumn{3}{@{}l@{}}{$\text{eval}(\text{if}\;b\;\text{then}\;cs_1\;\text{else}\;cs_2 , E) \dn$}\\
|
|
828 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)\;\text{then}\;
|
|
829 |
\text{eval}(cs_1,E)$}\\
|
|
830 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\phantom{\text{if}\;\text{eval}(b,E)\;}\text{else}\;\text{eval}(cs_2,E)$}\\
|
|
831 |
\multicolumn{3}{@{}l@{}}{$\text{eval}(\text{while}\;b\;\text{do}\;cs, E) \dn$}\\
|
|
832 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{if}\;\text{eval}(b,E)$}\\
|
|
833 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{then}\;
|
|
834 |
\text{eval}(\text{while}\;b\;\text{do}\;cs, \text{eval}(cs,E))$}\\
|
|
835 |
\multicolumn{3}{@{}l@{}}{\hspace{2cm}$\text{else}\; E$}\\
|
|
836 |
$\text{eval}(\text{write}\; x, E)$ & $\dn$ & $\{\;\text{println}(E(x))\; ;\;E\;\}$\\
|
|
837 |
\end{tabular}}
|
|
838 |
\end{center}
|
|
839 |
|
|
840 |
\end{frame}}
|
|
841 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
842 |
|
|
843 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
844 |
\begin{frame}[c]
|
582
|
845 |
\frametitle{Test Program}
|
465
|
846 |
|
|
847 |
\mbox{}\\[-18mm]\mbox{}
|
|
848 |
|
743
|
849 |
??%{\lstset{language=While}%%\fontsize{10}{12}\selectfont
|
|
850 |
%\texttt{\lstinputlisting{../progs/loops.while}}}
|
465
|
851 |
|
|
852 |
\end{frame}
|
|
853 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
854 |
|
|
855 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
856 |
\mode<presentation>{
|
|
857 |
\begin{frame}[t]
|
|
858 |
\frametitle{\begin{tabular}{c}Interpreted Code\end{tabular}}
|
|
859 |
|
|
860 |
\begin{center}
|
|
861 |
\begin{tikzpicture}
|
|
862 |
\begin{axis}[axis x line=bottom, axis y line=left, xlabel=n, ylabel=secs, legend style=small]
|
|
863 |
\addplot+[smooth] file {interpreted.data};
|
|
864 |
\end{axis}
|
|
865 |
\end{tikzpicture}
|
|
866 |
\end{center}
|
|
867 |
|
|
868 |
\end{frame}}
|
|
869 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
870 |
|
|
871 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
872 |
\mode<presentation>{
|
|
873 |
\begin{frame}[c]
|
|
874 |
\frametitle{\begin{tabular}{c}Java Virtual Machine\end{tabular}}
|
|
875 |
|
|
876 |
\begin{itemize}
|
|
877 |
\item introduced in 1995
|
|
878 |
\item is a stack-based VM (like Postscript, CLR of .Net)
|
|
879 |
\item contains a JIT compiler
|
|
880 |
\item many languages take advantage of JVM's infrastructure (JRE)
|
|
881 |
\item is garbage collected $\Rightarrow$ no buffer overflows
|
|
882 |
\item some languages compile to the JVM: Scala, Clojure\ldots
|
|
883 |
\end{itemize}
|
|
884 |
|
|
885 |
\end{frame}}
|
|
886 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
358
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
887 |
|
Christian Urban <christian dot urban at kcl dot ac dot uk>
diff
changeset
|
888 |
|
44
|
889 |
\end{document}
|
|
890 |
|
|
891 |
%%% Local Variables:
|
|
892 |
%%% mode: latex
|
|
893 |
%%% TeX-master: t
|
|
894 |
%%% End:
|
|
895 |
|