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