-
Notifications
You must be signed in to change notification settings - Fork 7
/
chap2_CFG1.tex
216 lines (188 loc) · 4.4 KB
/
chap2_CFG1.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
\input{header}
\AtBeginSubsection[]
{
\begin{frame}<beamer>
\frametitle{Outline}
\tableofcontents[current,currentsubsection]
\end{frame}
}
\begin{document}
\begin{frame}[allowframebreaks] \frametitle{Context-free languages}
\begin{itemize}
\item In Chapter 1 we consider two ways to describe languages
\item [] automata \& regular expressions
\item Context-free grammars (CFG)
\item [] More powerful than automata
\item CFG is used in compilers and interpreters
for parsers to read programs
\end{itemize}\end{frame}
\begin{frame}[allowframebreaks] \frametitle{Context-free grammars}
\begin{itemize}
\item
A grammar $G_1$:
\begin{eqnarray*}
&& A \rightarrow 0A1\\
&& A \rightarrow B\\
&& B \rightarrow \#
\end{eqnarray*}
Each one is called a substitution rule
\item Variables: $A,B$ (capital letters)
\item Terminals: 0, 1, \# (lowercase letters,
number, special symbols)
\item Start variable: $A$
\item A grammar: a collection of substitution rules
\item Derivation: $G_1$ generates
000\#111
\begin{equation*}
\begin{split}
& A
\Rightarrow 0A1 \Rightarrow
00A11
\Rightarrow 000A111\\
&
\Rightarrow 000B111
\Rightarrow 000\#111
\end{split}
\end{equation*}
\item Parse tree
\item [] Fig 2.1
\framebreak
\scalebox{0.85}{
\begin{forest}
for tree={
parent anchor=south,
child anchor=north,
if n children=0{
font=\itshape,
tier=terminal,
}{},
}
[A
[
[0
]
]
[A
[
[0
]
]
[A
[[0
]]
[A
[B
[\#
]
]
]
[[1
]]
]
[[1
]]
]
[[1
]]
]
\end{forest}
}
\item $L(G)$: language of grammar
\item For the CFG example we just discussed,
\begin{equation*}
L(G_1) =
\{0^n \# 1^n\mid n \geq 0\}
\end{equation*}
\item CFG is more powerful than regular expressions because we showed earlier
that this language is not regular
\item Representation of rules:
\begin{center}
$A \rightarrow 0A1$
and $A \rightarrow B$
\end{center}
is often simplified to
\begin{center}
$A \rightarrow 0A1\mid B$
\end{center}
\item Example
\begin{eqnarray*}
\langle \text{S} \rangle & \Rightarrow & \langle \text{Noun-Phrase}\rangle \langle \text{Verb-Phrase} \rangle\\
& \Rightarrow & \langle \text{Complex-Noun} \rangle\langle \text{Verb-Phrase}\rangle\\
& \Rightarrow & \langle \text{Article} \rangle\langle \text{Noun}\rangle\langle \text{Verb-Phrase}\rangle\\
& \Rightarrow & {\sf a} \langle \text{Noun} \rangle \langle \text{Verb-Phrase} \rangle\\
& \Rightarrow & {\sf a \; boy} \langle \text{Verb-Phrase}\rangle\\
& \Rightarrow & {\sf a \; boy} \langle \text{Complex-Verb}\rangle\\
& \Rightarrow & {\sf a \; boy} \langle \text{Verb}\rangle\\
& \Rightarrow & {\sf a \; boy \; sees}
\end{eqnarray*}
\item Why called ``context-free'' ?
\item [] Rules independent of context
\end{itemize}\end{frame}
\begin{frame}[allowframebreaks]
\frametitle{Formal definition of a context-free
grammar}
\begin{itemize}
\item $(V,\Sigma, R, S)$
\item [] $V$: variables, finite set
\item [] $\Sigma$: terminals, finite set
\item [] $R$: rules
\begin{center}
variable
$\rightarrow$ strings of variables and
terminals (including $\epsilon$)
\end{center}
\item $S\in V$, start variable
\item For the example $G_1$:
\begin{eqnarray*}
&& A \rightarrow 0A1\\
&& A \rightarrow B\\
&& B \rightarrow \#
\end{eqnarray*}
$V=\{A,B\}, \Sigma=\{0,1,\#\}, S = A, R:$
the above three rules
\end{itemize}
\end{frame}
\begin{frame}[allowframebreaks]
\frametitle{Derivation of strings}
\begin{itemize}
\item If $u,v,w$ are strings and a rule $A \rightarrow
w$ is applied, then we say
\begin{center}
$uAv$ yields $uwv$
\end{center}
and this is denoted as
\begin{equation*}
uAv \Rightarrow uwv
\end{equation*}
\item if
\begin{center}
$u=v$ or
$u \Rightarrow u_1 \Rightarrow \cdots \Rightarrow
u_k \Rightarrow v$
\end{center}
then we say
\begin{equation*}
u \mydef{*}{\Rightarrow} v
\end{equation*}
\item Language of a CFG
\begin{equation*}
\{ w \in \Sigma^*\mid
S \mydef{*}{\Rightarrow} w\}
\end{equation*}
\end{itemize}\end{frame}
\begin{frame}[allowframebreaks] \frametitle{Example 2.3}
\begin{itemize}
\item $G_3
=(\{S\}, \{a,b\}, R, S)$
\item[] R:
\begin{equation*}
S \rightarrow aSb\mid SS \mid \epsilon
\end{equation*}
\item What is the language?
\item If we treat $a, b$ respectively as ( and ), then
we have
\begin{center}
all valid nested parentheses
\end{center}
\end{itemize}\end{frame}
\end{document}