Theory of Computation: Lecture 7: Context-Free Grammar
Theory of Computation: Lecture 7: Context-Free Grammar
) ?
Context-free Grammars
A context-free grammar (CFG) is a 4-tuple (V, , R, S) where
) ?
Context-free Grammars
A context-free grammar (CFG) is a 4-tuple (V, , R, S) where
) ?
Derivation
Substitution rules is convenient way to represent a context-free
grammar:
A 0A1
A B
B #
Q: What are (V, , R, S) for this grammar?
A derivation of a string w in a given grammar G is a sequence of
substitutions starting with the start symbol and resulting in w, e.g.:
A 0A1 00A11 000A111 000B111 000#111
is a derivation of the string 000#111 in the grammar dened
above. The symbol reads yields.
We say u derives v and write u
v if there is a derivation (for
some k 0):
u u
1
u
2
u
k
v.
Derivation
Substitution rules is convenient way to represent a context-free
grammar:
A 0A1
A B
B #
Q: What are (V, , R, S) for this grammar?
A derivation of a string w in a given grammar G is a sequence of
substitutions starting with the start symbol and resulting in w, e.g.:
A 0A1 00A11 000A111 000B111 000#111
is a derivation of the string 000#111 in the grammar dened
above. The symbol reads yields.
We say u derives v and write u
v if there is a derivation (for
some k 0):
u u
1
u
2
u
k
v.
Derivation
Substitution rules is convenient way to represent a context-free
grammar:
A 0A1
A B
B #
Q: What are (V, , R, S) for this grammar?
A derivation of a string w in a given grammar G is a sequence of
substitutions starting with the start symbol and resulting in w, e.g.:
A 0A1 00A11 000A111 000B111 000#111
is a derivation of the string 000#111 in the grammar dened
above. The symbol reads yields.
We say u derives v and write u
v if there is a derivation (for
some k 0):
u u
1
u
2
u
k
v.
Context-free Languages
The set of all strings that can be derived (generated) in a given
grammar G = (V, , R, S) is called the language of the grammar
G and denoted L(G). In other words,
L(G) = w
[ S
w.
A language is called a context-free language (CFL) if it is
generated by some context-free grammar.
We already considered two examples of context-free languages:
L(G
1
) = 0
n
1
n
[ n 0 and L(G
2
) = 0
n
#1
n
[ n 0.
Given two CFLs, it is easy to construct a CFG for their union, e.g.,
combining CFGs for L(G
1
) and L(G
2
):
S S
1
[ S
2
S
1
0S
1
1 [
S
2
0S
2
1 [ #
Context-free Languages
The set of all strings that can be derived (generated) in a given
grammar G = (V, , R, S) is called the language of the grammar
G and denoted L(G). In other words,
L(G) = w
[ S
w.
A language is called a context-free language (CFL) if it is
generated by some context-free grammar.
We already considered two examples of context-free languages:
L(G
1
) = 0
n
1
n
[ n 0 and L(G
2
) = 0
n
#1
n
[ n 0.
Given two CFLs, it is easy to construct a CFG for their union, e.g.,
combining CFGs for L(G
1
) and L(G
2
):
S S
1
[ S
2
S
1
0S
1
1 [
S
2
0S
2
1 [ #
From DFA to CFG
CFGs have more power that DFA. It is not surprising that for any
DFA recognizing a language L, we can construct into a CFG
generating the same language L.
Q: What such construction would prove?
For a given DFA (Q, , , q
0
, F), an equivalent CFG can be
constructed as follows:
Make R
0
a start variable of the CFG;
Make R
0
a start variable of the CFG;