author | Christian Urban <christian.urban@kcl.ac.uk> |
Fri, 29 Nov 2024 18:59:32 +0000 | |
changeset 976 | e9eac62928f5 |
parent 971 | 51e00f223792 |
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} |
871 | 5 |
\usepackage{../graphicss} |
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] |
|
893 | 33 |
\LARGE Compilers and \\[-1mm] |
34 |
\LARGE Formal Languages\\[-5mm] |
|
44 | 35 |
\end{tabular}} |
36 |
||
37 |
\normalsize |
|
38 |
\begin{center} |
|
39 |
\begin{tabular}{ll} |
|
893 | 40 |
Email: & christian.urban at kcl.ac.uk\\ |
971 | 41 |
Office Hour: & Fridays 12 -- 14\\ |
893 | 42 |
Location: & N7.07 (North Wing, Bush House)\\ |
43 |
Slides \& Progs: & KEATS\\ |
|
44 |
Pollev: & \texttt{\alert{https://pollev.com/cfltutoratki576}}\\ |
|
44 | 45 |
\end{tabular} |
893 | 46 |
\end{center} |
743 | 47 |
|
48 |
\begin{center} |
|
49 |
\begin{tikzpicture} |
|
50 |
\node[drop shadow,fill=white,inner sep=0pt] |
|
51 |
{\footnotesize\rowcolors{1}{capri!10}{white} |
|
52 |
\begin{tabular}{|p{4.8cm}|p{4.8cm}|}\hline |
|
53 |
1 Introduction, Languages & 6 While-Language \\ |
|
54 |
2 Regular Expressions, Derivatives & 7 Compilation, JVM \\ |
|
55 |
3 Automata, Regular Languages & 8 Compiling Functional Languages \\ |
|
56 |
4 Lexing, Tokenising & 9 Optimisations \\ |
|
57 |
\cellcolor{blue!50} |
|
58 |
5 Grammars, Parsing & 10 LLVM \\ \hline |
|
59 |
\end{tabular}% |
|
60 |
}; |
|
61 |
\end{tikzpicture} |
|
44 | 62 |
\end{center} |
270
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
215
diff
changeset
|
63 |
\end{frame} |
289
c22c8baff491
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
272
diff
changeset
|
64 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
44 | 65 |
|
667 | 66 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
743 | 67 |
%\begin{frame}[c] |
68 |
% \frametitle{Coursework 1: Submissions} |
|
69 |
% |
|
70 |
% \begin{itemize} |
|
71 |
% \item Scala (29) |
|
72 |
% \item Haskell (1) |
|
73 |
% \item Kotlin (1) |
|
74 |
% \item Rust (1) |
|
75 |
% \end{itemize}\bigskip\bigskip |
|
76 |
% |
|
77 |
% \small |
|
78 |
% Please get in contact if you intend to do CW Strand 2. No zips please. |
|
79 |
% Give definitions also on paper if asked. BTW, simp |
|
80 |
% can stay unchanged. Use \texttt{ders} for CW2, not \texttt{ders2}! |
|
81 |
% \end{frame} |
|
289
c22c8baff491
updated slides
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
272
diff
changeset
|
82 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
945 | 83 |
|
84 |
{ |
|
85 |
\setbeamercolor{background canvas}{bg=cream} |
|
86 |
\begin{frame}<1-10>[c] |
|
87 |
\end{frame} |
|
88 |
||
89 |
||
90 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
91 |
\begin{frame}[c] |
|
92 |
\frametitle{Coursework 1: Submissions} |
|
93 |
||
94 |
\begin{itemize} |
|
95 |
\item Scala (162) |
|
96 |
\item Ocaml (1) |
|
97 |
\item Java (1) \ldots uses new features of Java 21 |
|
98 |
\item Rust (6) |
|
99 |
\end{itemize}\bigskip\bigskip |
|
100 |
||
101 |
||
102 |
\end{frame} |
|
103 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
104 |
} |
|
105 |
||
106 |
||
528 | 107 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
108 |
\begin{frame}[t] |
|
795 | 109 |
\frametitle{Parser} |
528 | 110 |
\mbox{}\\[-16mm]\mbox{} |
111 |
||
112 |
\begin{center} |
|
113 |
\begin{tikzpicture}[scale=1, |
|
114 |
node/.style={ |
|
115 |
rectangle,rounded corners=3mm, |
|
116 |
very thick,draw=black!50, |
|
117 |
minimum height=18mm, minimum width=20mm, |
|
743 | 118 |
top color=white,bottom color=black!20,drop shadow}] |
528 | 119 |
\node (0) at (-2.3,0) {}; |
120 |
||
121 |
\node (A) at (0,0) [node] {}; |
|
122 |
\node [below right] at (A.north west) {lexer}; |
|
123 |
||
124 |
\node (B) at (3,0) [node] {}; |
|
125 |
\node [below right=1mm] at (B.north west) |
|
126 |
{\mbox{}\hspace{-1mm}parser}; |
|
127 |
||
128 |
\node (C) at (6,0) [node] {}; |
|
129 |
\node [below right] at (C.north west) |
|
130 |
{\mbox{}\hspace{-1mm}code gen}; |
|
131 |
||
132 |
\node (1) at (8.4,0) {}; |
|
133 |
||
134 |
\draw [->,line width=4mm] (0) -- (A); |
|
135 |
\draw [->,line width=4mm] (A) -- (B); |
|
136 |
\draw [->,line width=4mm] (B) -- (C); |
|
137 |
\draw [->,line width=4mm] (C) -- (1); |
|
138 |
\end{tikzpicture} |
|
139 |
\end{center} |
|
140 |
||
795 | 141 |
|
142 |
\only<2>{ |
|
143 |
\begin{textblock}{1}(3,6) |
|
144 |
\begin{bubble}[8.5cm] |
|
145 |
\normalsize |
|
146 |
parser input: a sequence of tokens\smallskip\\ |
|
147 |
||
148 |
{\small\hspace{5mm}\code{key(read) lpar id(n) rpar semi}}\smallskip\\ |
|
149 |
||
150 |
parser output: an abstract syntax tree\smallskip\\ |
|
151 |
\footnotesize |
|
152 |
\hspace{2cm}\begin{tikzpicture} |
|
153 |
\node {\code{read}} |
|
154 |
child {node {\code{lpar}}} |
|
155 |
child {node {\code{n}}} |
|
156 |
child {node {\code{rpar}}}; |
|
157 |
\end{tikzpicture} |
|
158 |
\end{bubble} |
|
159 |
\end{textblock}} |
|
528 | 160 |
\end{frame} |
161 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
162 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
163 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
164 |
\begin{frame}[c] |
550 | 165 |
\frametitle{What Parsing is Not} |
166 |
||
167 |
Usually parsing does not check semantic correctness, e.g. |
|
168 |
||
169 |
\begin{itemize} |
|
170 |
\item whether a function is not used before it |
|
171 |
is defined |
|
172 |
\item whether a function has the correct number of arguments |
|
173 |
or are of correct type |
|
174 |
\item whether a variable can be declared twice in a scope |
|
175 |
\end{itemize} |
|
176 |
||
177 |
\end{frame} |
|
178 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
179 |
||
945 | 180 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
181 |
{ |
|
182 |
\setbeamercolor{background canvas}{bg=cream} |
|
183 |
\begin{frame}[c] |
|
184 |
||
185 |
\begin{center} |
|
186 |
\begin{tikzpicture}[scale=1.5,>=stealth',very thick, |
|
187 |
every state/.style={minimum size=0pt, |
|
188 |
draw=blue!50,very thick,fill=blue!20}] |
|
189 |
\node[state,initial] (q0) at (0,2) {$q_0$}; |
|
190 |
\node[state,accepting] (q1) at (2,2) {$q_1$}; |
|
191 |
\node[state] (q2) at (0,0) {$q_2$}; |
|
192 |
\node[state] (q3) at (2,0) {$q_3$}; |
|
193 |
||
194 |
\path[->] (q0) edge[bend left] node[above] {\alert{$a$}} (q1) |
|
195 |
(q1) edge[bend left] node[above] {\alert{$a$}} (q0) |
|
196 |
(q2) edge[bend left] node[above] {\alert{$a$}} (q3) |
|
197 |
(q3) edge[bend left] node[above] {\alert{$a$}} (q2) |
|
198 |
(q0) edge[bend left] node[right] {\alert{$b$}} (q2) |
|
199 |
(q2) edge[bend left] node[left] {\alert{$b$}} (q0) |
|
200 |
(q1) edge[bend left] node[right] {\alert{$b$}} (q3) |
|
201 |
(q3) edge[bend left] node[left] {\alert{$b$}} (q1); |
|
202 |
\end{tikzpicture} |
|
203 |
\end{center} |
|
204 |
||
205 |
\hfill{}Which language? |
|
206 |
||
207 |
\end{frame} |
|
208 |
} |
|
209 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
550 | 210 |
|
211 |
||
212 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
213 |
\begin{frame}[c] |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
214 |
\frametitle{Regular Languages} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
215 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
216 |
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
|
217 |
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
|
218 |
\bl{$a^nb^n$}.\bigskip |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
219 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
220 |
\begin{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
221 |
\bl{$(((()()))())$} \;\;vs.\;\; \bl{$(((()()))()))$} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
222 |
\end{center}\bigskip\bigskip |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
223 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
224 |
\small |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
225 |
\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
|
226 |
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
|
227 |
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
|
228 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
229 |
\end{frame} |
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 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
232 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
233 |
\begin{frame}[c] |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
234 |
\frametitle{Hierarchy of Languages} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
235 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
236 |
\begin{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
237 |
\begin{tikzpicture} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
238 |
[rect/.style={draw=black!50, |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
239 |
top color=white, |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
240 |
bottom color=black!20, |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
241 |
rectangle, |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
242 |
very thick, |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
243 |
rounded corners}, scale=1.2] |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
244 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
245 |
\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
|
246 |
\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
|
247 |
\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
|
248 |
\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
|
249 |
\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
|
250 |
\end{tikzpicture} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
251 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
252 |
\end{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
253 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
254 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
255 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
256 |
|
795 | 257 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
258 |
\begin{frame}[c] |
|
259 |
\LARGE |
|
260 |
\begin{center} |
|
261 |
Time flies like an arrow.\\ |
|
262 |
Fruit flies like bananas. |
|
263 |
\end{center} |
|
264 |
\end{frame} |
|
265 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
266 |
||
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
267 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
268 |
\begin{frame}[c] |
792 | 269 |
\frametitle{CFGs} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
270 |
|
360
c6c574d2ca0c
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
358
diff
changeset
|
271 |
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
|
272 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
273 |
\begin{itemize} |
664 | 274 |
\item a finite set of nonterminal symbols (e.g.~$\meta{A}$ upper case) |
275 |
\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
|
276 |
\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
|
277 |
\item a set of rules |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
278 |
\begin{center} |
451 | 279 |
\bl{$\meta{A} ::= \textit{rhs}$} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
280 |
\end{center} |
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 |
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
|
283 |
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
|
284 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
285 |
We also allow rules |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
286 |
\begin{center} |
451 | 287 |
\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
|
288 |
\end{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
289 |
\end{itemize} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
290 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
291 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
292 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
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 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
582 | 295 |
\begin{frame}[t] |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
296 |
\frametitle{Palindromes} |
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 |
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
|
299 |
|
795 | 300 |
\only<1>{% |
301 |
\bl{\begin{plstx}[margin=1cm] |
|
451 | 302 |
: \meta{S} ::= a\cdot\meta{S}\cdot a\\ |
303 |
: \meta{S} ::= b\cdot\meta{S}\cdot b\\ |
|
588 | 304 |
: \meta{S} ::= a\\ |
305 |
: \meta{S} ::= b\\ |
|
306 |
: \meta{S} ::= \epsilon\\ |
|
795 | 307 |
\end{plstx}}} |
308 |
% |
|
309 |
\only<2>{% |
|
310 |
\bl{\begin{plstx}[margin=1cm] |
|
311 |
: \meta{S} ::= a\cdot \meta{S}\cdot a | b\cdot \meta{S}\cdot b | a | b | \epsilon\\ |
|
312 |
\end{plstx}}} |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
313 |
|
795 | 314 |
%\small |
315 |
%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
|
316 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
317 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
318 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
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 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
321 |
\begin{frame}[c] |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
322 |
\frametitle{Arithmetic Expressions} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
323 |
|
451 | 324 |
\bl{\begin{plstx}[margin=3cm,one per line] |
795 | 325 |
: \meta{E} ::= 0 \mid 1 \mid 2 \mid ... \mid 9 |
451 | 326 |
| \meta{E} \cdot + \cdot \meta{E} |
327 |
| \meta{E} \cdot - \cdot \meta{E} |
|
328 |
| \meta{E} \cdot * \cdot \meta{E} |
|
329 |
| ( \cdot \meta{E} \cdot ) \\ |
|
330 |
\end{plstx}}\pause |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
331 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
332 |
\bl{\texttt{1 + 2 * 3 + 4}} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
333 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
334 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
335 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
336 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
337 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
338 |
\begin{frame}[c] |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
339 |
\frametitle{A CFG Derivation} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
340 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
341 |
\begin{enumerate} |
451 | 342 |
\item Begin with a string containing only the start symbol, say \bl{\meta{S}}\bigskip |
343 |
\item Replace any nonterminal \bl{\meta{X}} in the string by the |
|
344 |
right-hand side of some production \bl{$\meta{X} ::= \textit{rhs}$}\bigskip |
|
582 | 345 |
\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
|
346 |
\end{enumerate} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
347 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
348 |
\begin{center} |
451 | 349 |
\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
|
350 |
\end{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
351 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
352 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
353 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
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 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
451 | 356 |
\begin{frame}[t] |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
357 |
\frametitle{Example Derivation} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
358 |
|
451 | 359 |
\bl{\begin{plstx}[margin=2cm] |
360 |
: \meta{S} ::= \epsilon | a\cdot \meta{S}\cdot a | b\cdot \meta{S}\cdot b \\ |
|
361 |
\end{plstx}}\bigskip |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
362 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
363 |
\begin{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
364 |
\begin{tabular}{lcl} |
451 | 365 |
\bl{\meta{S}} & \bl{$\rightarrow$} & \bl{$a\meta{S}a$}\\ |
366 |
& \bl{$\rightarrow$} & \bl{$ab\meta{S}ba$}\\ |
|
367 |
& \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
|
368 |
& \bl{$\rightarrow$} & \bl{$abaaba$}\\ |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
369 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
370 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
371 |
\end{tabular} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
372 |
\end{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
373 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
374 |
\end{frame} |
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 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
377 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
451 | 378 |
\begin{frame}[t] |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
379 |
\frametitle{Example Derivation} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
380 |
|
451 | 381 |
\bl{\begin{plstx}[margin=3cm,one per line] |
795 | 382 |
: \meta{E} ::= 0 \mid 1 \mid 2 \mid ... \mid 9 |
451 | 383 |
| \meta{E} \cdot + \cdot \meta{E} |
384 |
| \meta{E} \cdot - \cdot \meta{E} |
|
385 |
| \meta{E} \cdot * \cdot \meta{E} |
|
386 |
| ( \cdot \meta{E} \cdot ) \\ |
|
387 |
\end{plstx}} |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
388 |
|
451 | 389 |
\small |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
390 |
\begin{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
391 |
\begin{tabular}{@{}c@{}c@{}} |
451 | 392 |
\begin{tabular}{@{\hspace{-2mm}}l@{\hspace{1mm}}l@{\hspace{1mm}}l@{\hspace{4mm}}} |
393 |
\bl{\meta{E}} & \bl{$\rightarrow$} & \bl{$\meta{E}*\meta{E}$}\\ |
|
394 |
& \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}*\meta{E}$}\\ |
|
395 |
& \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
|
396 |
& \bl{$\rightarrow^+$} & \bl{$1+2*3+4$}\\ |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
397 |
\end{tabular} &\pause |
451 | 398 |
\begin{tabular}{@{}l@{\hspace{0mm}}l@{\hspace{1mm}}l} |
399 |
\bl{$\meta{E}$} & \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}$}\\ |
|
400 |
& \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}+\meta{E}$}\\ |
|
401 |
& \bl{$\rightarrow$} & \bl{$\meta{E}+\meta{E}*\meta{E}+\meta{E}$}\\ |
|
402 |
& \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
|
403 |
\end{tabular} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
404 |
\end{tabular} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
405 |
\end{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
406 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
407 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
408 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
409 |
|
796 | 410 |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
411 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
412 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
413 |
\begin{frame}[c] |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
414 |
\frametitle{Language of a CFG} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
415 |
|
451 | 416 |
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
|
417 |
Then the language \bl{$L(G)$} is: |
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 |
\begin{center} |
451 | 420 |
\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
|
421 |
\end{center}\pause |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
422 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
423 |
\begin{itemize} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
424 |
\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
|
425 |
\item Once generated, terminals are ``permanent''. |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
426 |
\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
|
427 |
(but can also be strings). |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
428 |
\end{itemize} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
429 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
430 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
431 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
432 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
433 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
451 | 434 |
\begin{frame}[t] |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
435 |
\frametitle{Parse Trees} |
582 | 436 |
\mbox{}\\[-12mm] |
451 | 437 |
|
593 | 438 |
\bl{\begin{plstx}: \meta{E} ::= \meta{T} | \meta{T} \cdot + \cdot \meta{E} | \meta{T} \cdot - \cdot \meta{E}\\ |
529 | 439 |
: \meta{T} ::= \meta{F} | \meta{F} \cdot * \cdot \meta{T}\\ |
795 | 440 |
: \meta{F} ::= 0 ... 9 | ( \cdot \meta{E} \cdot )\\ |
451 | 441 |
\end{plstx}} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
442 |
|
795 | 443 |
\begin{textblock}{5}(6, 5) |
444 |
\small |
|
445 |
\begin{tikzpicture}[level distance=10mm, blue] |
|
446 |
\node {$\meta{E}$} |
|
447 |
child {node {$\meta{T}$} |
|
448 |
child {node {$\meta{F}$} child {node {1}}} |
|
449 |
} |
|
450 |
child {node {+}} |
|
451 |
child {node {$\meta{E}$} |
|
452 |
child[sibling distance=10mm] {node {$\meta{T}$} |
|
453 |
child {node {$\meta{F}$} child {node {2}}} |
|
798 | 454 |
child {node {*}} |
795 | 455 |
child {node {$\meta{T}$} child {node {$\meta{F}$} child {node {3}}}} |
456 |
} |
|
457 |
child {node {+}} |
|
458 |
child {node {$\meta{E}$} child {node {$\meta{T}$} |
|
459 |
child {node {$\meta{F}$} child {node {4}}}}} |
|
460 |
} |
|
461 |
; |
|
462 |
\end{tikzpicture} |
|
463 |
\end{textblock} |
|
464 |
||
465 |
\begin{textblock}{5}(1, 10) |
|
466 |
\bl{\texttt{1 + 2 * 3 + 4}} |
|
467 |
\end{textblock} |
|
468 |
||
469 |
\end{frame} |
|
470 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
471 |
||
358
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 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
451 | 474 |
\begin{frame}[t] |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
475 |
\frametitle{Arithmetic Expressions} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
476 |
|
451 | 477 |
\bl{\begin{plstx}[margin=3cm,one per line] |
795 | 478 |
: \meta{E} ::= 0..9 |
451 | 479 |
| \meta{E} \cdot + \cdot \meta{E} |
480 |
| \meta{E} \cdot - \cdot \meta{E} |
|
481 |
| \meta{E} \cdot * \cdot \meta{E} |
|
482 |
| ( \cdot \meta{E} \cdot ) \\ |
|
483 |
\end{plstx}}\pause\bigskip |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
484 |
|
451 | 485 |
A CFG is \alert{\bf left-recursive} if it has a nonterminal \bl{$\meta{E}$} such |
486 |
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
|
487 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
488 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
489 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
490 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
491 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
451 | 492 |
\begin{frame}[t] |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
493 |
\frametitle{Ambiguous Grammars} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
494 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
495 |
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
|
496 |
has at least two different parse trees. |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
497 |
|
796 | 498 |
\bl{\begin{plstx}[margin=3cm,one per line]: \meta{E} ::= 0 ... 9 |
451 | 499 |
| \meta{E} \cdot + \cdot \meta{E} |
500 |
| \meta{E} \cdot - \cdot \meta{E} |
|
501 |
| \meta{E} \cdot * \cdot \meta{E} |
|
502 |
| ( \cdot \meta{E} \cdot ) \\ |
|
503 |
\end{plstx}} |
|
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
504 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
505 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
506 |
\bl{\texttt{1 + 2 * 3 + 4}} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
507 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
508 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
509 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
510 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
511 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
512 |
\begin{frame}[c] |
582 | 513 |
\frametitle{`Dangling' Else} |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
514 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
515 |
Another ambiguous grammar:\bigskip |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
516 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
517 |
\begin{center} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
518 |
\bl{\begin{tabular}{lcl} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
519 |
$E$ & $\rightarrow$ & if $E$ then $E$\\ |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
520 |
& $|$ & if $E$ then $E$ else $E$ \\ |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
521 |
& $|$ & \ldots |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
522 |
\end{tabular}} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
523 |
\end{center}\bigskip |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
524 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
525 |
\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
|
526 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
527 |
\end{frame} |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
528 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
529 |
|
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
530 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
531 |
\begin{frame}[c] |
798 | 532 |
\frametitle{CYK Algorithm} |
533 |
||
534 |
Suppose the grammar: |
|
535 |
||
536 |
\begin{center} |
|
537 |
\bl{\begin{tabular}{@ {}lcl@ {}} |
|
538 |
$\meta{S}$ & $::=$ & $\meta{N}\cdot \meta{P}$ \\ |
|
539 |
$\meta{P}$ & $::=$ & $\meta{V}\cdot \meta{N}$ \\ |
|
540 |
$\meta{N}$ & $::=$ & $\meta{N}\cdot \meta{N}$ \\ |
|
541 |
$\meta{N}$ & $::=$ & $\texttt{students} \;|\; \texttt{Jeff} \;|\; \texttt{geometry} \;|\; \texttt{trains} $ \\ |
|
542 |
$\meta{V}$ & $::=$ & $\texttt{trains}$ |
|
543 |
\end{tabular}} |
|
544 |
\end{center} |
|
545 |
||
546 |
\bl{\texttt{Jeff trains geometry students}} |
|
547 |
||
548 |
\end{frame} |
|
549 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
550 |
||
551 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
552 |
\begin{frame}[c] |
|
553 |
\frametitle{CYK Algorithm} |
|
554 |
||
555 |
\begin{center} |
|
556 |
\begin{tikzpicture}[scale=1,line width=0.8mm] |
|
557 |
\draw (-2,0) -- (2,0); |
|
558 |
\draw (-2,1) -- (2,1); |
|
559 |
\draw (-2,2) -- (1,2); |
|
560 |
\draw (-2,3) -- (0,3); |
|
561 |
\draw (-2,4) -- (-1,4); |
|
562 |
||
563 |
\draw (0,0) -- (0, 3); |
|
564 |
\draw (1,0) -- (1, 2); |
|
565 |
\draw (2,0) -- (2, 1); |
|
566 |
\draw (-1,0) -- (-1, 4); |
|
567 |
\draw (-2,0) -- (-2, 4); |
|
568 |
||
569 |
\draw (-1.5,-0.5) node {\footnotesize{}\texttt{Jeff}}; |
|
570 |
\draw (-0.5,-1.0) node {\footnotesize{}\texttt{trains}}; |
|
571 |
\draw ( 0.5,-0.5) node {\footnotesize{}\texttt{geometry}}; |
|
572 |
\draw ( 1.5,-1.0) node {\footnotesize{}\texttt{students}}; |
|
573 |
||
574 |
\draw (-1.5,0.5) node {$N$}; |
|
575 |
\draw (-0.5,0.5) node {$N,V$}; |
|
576 |
\draw ( 0.5,0.5) node {$N$}; |
|
577 |
\draw ( 1.5,0.5) node {$N$}; |
|
578 |
||
579 |
\draw (-2.4, 3.5) node {$1$}; |
|
580 |
\draw (-2.4, 2.5) node {$2$}; |
|
581 |
\draw (-2.4, 1.5) node {$3$}; |
|
582 |
\draw (-2.4, 0.5) node {$4$}; |
|
583 |
\end{tikzpicture} |
|
584 |
\end{center} |
|
585 |
||
586 |
\begin{textblock}{5}(10,10) |
|
587 |
\small\bl{\begin{tabular}{@ {}lcl@ {}} |
|
588 |
$\meta{S}$ & $::=$ & $\meta{N}\cdot \meta{P}$ \\ |
|
589 |
$\meta{P}$ & $::=$ & $\meta{V}\cdot \meta{N}$ \\ |
|
590 |
$\meta{N}$ & $::=$ & $\meta{N}\cdot \meta{N}$ \\ |
|
591 |
$\meta{N}$ & $::=$ & $\texttt{students} \;|\; \texttt{Jeff}$\\ |
|
592 |
& & $\;|\; \texttt{geometry} \;|\; \texttt{trains} $ \\ |
|
593 |
$\meta{V}$ & $::=$ & $\texttt{trains}$ |
|
594 |
\end{tabular}} |
|
595 |
\end{textblock} |
|
596 |
||
597 |
\end{frame} |
|
598 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
599 |
||
600 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
601 |
\begin{frame}[t] |
|
602 |
\frametitle{Chomsky Normal Form} |
|
603 |
||
604 |
A grammar for palindromes over the alphabet~\bl{$\{a,b\}$}: |
|
605 |
||
606 |
\bl{\begin{plstx}[margin=0cm] |
|
607 |
: \meta{S} ::= a\cdot \meta{S}\cdot a | b\cdot \meta{S}\cdot b | a\cdot a | b\cdot b | a | b \\ |
|
608 |
\end{plstx}} |
|
609 |
||
610 |
\end{frame} |
|
611 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
612 |
||
613 |
||
614 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
615 |
\begin{frame}[c] |
|
616 |
\frametitle{CYK Algorithm} |
|
617 |
||
618 |
||
619 |
\begin{itemize} |
|
620 |
\item fastest possible algorithm for recognition problem |
|
621 |
\item runtime is \bl{$O(n^3)$}\bigskip |
|
622 |
\item grammars need to be transformed into CNF |
|
623 |
\end{itemize} |
|
624 |
||
625 |
\end{frame} |
|
626 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
627 |
||
628 |
||
799 | 629 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
630 |
\begin{frame}[c,fragile] |
|
631 |
\begin{mybox3}{}\it |
|
632 |
"The C++ grammar is ambiguous, context-dependent and potentially |
|
633 |
requires infinite lookahead to resolve some ambiguities." |
|
634 |
\end{mybox3}\bigskip |
|
635 |
||
636 |
||
637 |
\hfill from the \href{http://www.computing.surrey.ac.uk/research/dsrg/fog/FogThesis.pdf}{PhD thesis} by Willink (2001) |
|
638 |
||
639 |
\small |
|
640 |
\begin{center} |
|
641 |
\begin{lstlisting}[language={},numbers=none] |
|
642 |
int(x), y, *const z; |
|
643 |
int(x), y, new int; |
|
644 |
\end{lstlisting} |
|
645 |
\end{center} |
|
646 |
\end{frame} |
|
647 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
798 | 648 |
|
649 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
650 |
\begin{frame}[c] |
|
796 | 651 |
\frametitle{Context Sensitive Grammars} |
652 |
||
653 |
It is much harder to find out whether a string is parsed |
|
654 |
by a context sensitive grammar: |
|
655 |
||
656 |
\bl{\begin{plstx}[margin=2cm] |
|
657 |
: \meta{S} ::= b\meta{S}\meta{A}\meta{A} | \epsilon\\ |
|
658 |
: \meta{A} ::= a\\ |
|
659 |
: b\meta{A} ::= \meta{A}b\\ |
|
660 |
\end{plstx}}\pause |
|
661 |
||
662 |
\begin{center} |
|
663 |
\bl{$\meta{S} \rightarrow\ldots\rightarrow^? ababaa$} |
|
799 | 664 |
\end{center} |
796 | 665 |
|
666 |
\end{frame} |
|
667 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
668 |
||
465 | 669 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
670 |
|
798 | 671 |
\begin{frame}[t,fragile] |
672 |
\begin{mybox3}{} |
|
673 |
For CW2, please include '$\backslash$' as a symbol in strings, because |
|
674 |
the collatz program contains |
|
675 |
\begin{lstlisting}[language=Scala, numbers=none] |
|
676 |
write "\n";\end{lstlisting} |
|
677 |
\end{mybox3} |
|
678 |
\end{frame} |
|
679 |
||
796 | 680 |
\begin{frame}[t] |
681 |
\begin{mybox3}{} |
|
682 |
val (r1s, f1s) = simp(r1)\\ |
|
683 |
val (r2s, f2s) = simp(r2)\\ |
|
684 |
how are the |
|
685 |
first rectification functions f1s and f2s made? could you maybe |
|
686 |
show an example? |
|
687 |
\end{mybox3} |
|
688 |
\end{frame} |
|
689 |
||
849 | 690 |
\begin{frame}<1-24>[c] |
796 | 691 |
\end{frame} |
692 |
||
358
b3129cff41e9
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
290
diff
changeset
|
693 |
|
945 | 694 |
\begin{frame}[t] |
695 |
\begin{minipage}{1.2\textwidth} |
|
696 |
\begin{mybox3}{}\small |
|
697 |
\textbf{Questions regarding CFL CW1} |
|
698 |
||
699 |
Dear Dr Urban |
|
700 |
||
701 |
Regarding CW1, I am stuck on finding the nullable and derivative rules for some important regexes.\smallskip |
|
702 |
||
703 |
The NOT Regex nullable rule: I am not sure how to approach this, I am inclined to simply put this as the negation of the nullable function on the input regex (e.g !nullable(r)). However I have found instances where negating a nullable does not make it un-nullable. For example the negation of r* can still match regex ab (which is not nullable). So I would like some actual clarification, pointers and help in this area.\smallskip |
|
704 |
||
705 |
The NOT Regex derivation rule: again I am dumbfounded here, I am inclined to think that I should derive the regex and then negate that derivation. But none of this ever works. Please provide some helpful information so I can solve this. |
|
706 |
\end{mybox3} |
|
707 |
\end{minipage} |
|
708 |
\end{frame} |
|
709 |
||
710 |
||
711 |
||
44 | 712 |
\end{document} |
713 |
||
714 |
%%% Local Variables: |
|
715 |
%%% mode: latex |
|
716 |
%%% TeX-master: t |
|
717 |
%%% End: |
|
718 |