Title: Contextfree Grammars
1Context-free Grammars
Definition 5.1 A grammar G (V, T, S, P) is
said to be context-free if all production rules
in P have the form A ? x where A ? V and x ? (V
? T) A language is said to be context-free iff
there is a context free grammar G such that L
L(G).
2Context-free Grammars
Context-free means that there is a single
variable on the left side of each grammar rule.
You can imagine other kinds of grammatical
rules where this condition does not hold. For
example 1Z1 ? 101 In this rule, the variable Z
goes to 0 only in the context of a 1 on its left
and a 1 to the right. This is a
context-sensitive rule.
3Non-regular languages
- There are non-regular languages that can be
generated by context-free grammars. - The language anbn n ? 0
- is generated by the grammar
- S ? aSb ?
- The language L w na(w) nb(w)
- is generated by the grammar
- S ? SS ? aSb bSa
4Example of a Context-free Grammar
The grammar G (S, a, b, S, P), with
production rules S ? aSa bSb ? is
context-free. This grammar is linear (that is,
there is at most a single variable on the
right-hand side of every rule), but is neither
right-linear nor left-linear (the variable is not
always the rightmost leftmost character on the
right-hand side of the rule), so it is not
regular.
5Example of a Context-free Grammar
Given the grammar G (S, a, b, S, P), with
production rules S ? aSa bSb ? a typical
derivation in this grammar might be S ? aSa ?
aaSaa ? aabSbaa ? aabbaa The language generated
by this grammar is L(G) wwR w ? a,b
6Palindromes
- Palindromes are strings which are spelled the
same way backwards and forwards. - The language of palindromes, PAL, is not regular
- For any two strings x, y a string z can be found
which distinguishes them. - For x, y which are different strings and xy,
if z xreverse is appended to each, then xz is
accepted and yz is not accepted - Therefore there must be an infinite number of
states in any FA accepting PAL, so PAL is not
regular
7Example of a Non-linear Context-free Grammar
Consider the grammar G (S, a, b, S, P),
with production rules S ? aSa SS ? This
grammar is context-free. Why? Is this grammar
linear? Why or why not?
8Regular vs. context-free
Are regular languages context-free? Yes,
because context-free means that there is a single
variable on the left side of each grammar rule.
All regular languages are generated by grammars
that have a single variable on the left side of
each grammar rule. But, as we have seen, not
all context-free grammars are regular. So
regular languages are a proper subset of the
class of context-free languages.
9Derivation
Given the grammar, S ? aaSB ? B ?
bB b the string aab can be derived in
different ways. S ? aaSB ? aaB ? aab S ? aaSB
? aaSb ? aab
10Parse tree
Both derivations on the previous slide correspond
to the following parse (or derivation) tree.
The tree structure shows the rule that is applied
to each nonterminal, without showing the order of
rule applications. Each internal node of the
tree corresponds to a nonterminal, and the leaves
of the derivation tree represent the string of
terminals.
11Derivation
In the derivation S ? aaSB ? aaB ? aab the first
step was to replace S with ?, and then to replace
B with b. We moved from left to right, replacing
the leftmost variable at each step. This is
called a leftmost derivation. Similarly, the
derivation S ? aaSB ? aaSb ? aab is called a
rightmost derivation.
12Leftmost (rightmost) derivation
Definition 5.2 In a leftmost derivation, the
leftmost nonterminal is replaced at each step.
In a rightmost derivation, the rightmost
nonterminal is replaced at each step. Many
derivations are neither leftmost nor
rightmost. If there is a single parse tree,
there is also a single leftmost derivation.
13Parse (derivation) trees
Definition 5.3 Let G (V, T, S, P) be a
context-free grammar. An ordered tree is a
derivation tree for G iff it has the following
properties 1. The root is labeled S 2. Every
leaf has a label from T ? ? 3. Every interior
vertex (not a leaf) has a label from V. 4. If a
vertex has label A ? V, and its children are
labeled (from left to right) a1, a2,..., an, then
P must contain a production of the form A ?
a1a2...an 5. A leaf labeled ? has no siblings
that is, a vertex with a child labeled ? can have
no other children
14Parse (derivation) trees
A partial derivation tree is one in which
property 1 does not necessarily hold and in which
property 2 is replaced by 2a. Every leaf has a
label from V ? T ? ? The yield of the tree is
the string of symbols in the order they are
encountered when the tree is traversed in a
depth-first manner, always taking the leftmost
unexplored branch.
15Example 5.6
- Consider the grammar G, with productions
- S ? aAB
- A ? bBb
- B ? A ?
16Example 5.6
17Parse (derivation) trees
A partial derivation tree yields a sentential
form of the grammar G that the tree is associated
with. (???????????????sentential form ) A
derivation tree yields a sentence of the grammar
G that the tree is associated with.
18Parse (derivation) trees
Theorem 5.1 Let G (V, T, S, P) be a
context-free grammar. Then for every w ? L(G)
there exists a derivation tree of G whose yield
is w. Conversely, the yield of any derivation
tree of G is in L(G). If tG is any partial
derivation tree for G whose root is labeled S,
then the yield of tG is a sentential form of
G. Any w ? L(G) has a leftmost and a rightmost
derivation. The leftmost derivation is obtained
by always expanding the leftmost variable in the
derivation tree at each step, and similarly for
the rightmost derivation.
19Ambiguity
A grammar is ambiguous if there is a string
with two possible parse trees. (A string has
more than one parse tree if and only if it has
more than one leftmost derivation.) English can
be ambiguous. Example Disabled fly to see
Carter.
20Example 5.10
- S ? aSb SS ?? ambiguous,?? abbb ???
derivation trees.
21Example
V S T , , (, ), 0, 1 P S ? S S
S S (S) 1 0 The string 0 0 1 has
two different parse trees. The derivation begins
like this S What is the leftmost
variable? S What can we replace it with? S S
or S S or (S) or 1 or 0 Pick one
of these at random, say S S
22S ? S S S S (S) 1 0
Here is the parse tree
S S S S S 1 0 0 Our
string is 0 0 1. This parse corresponds to
compute 0 0 first, then add it to 1, which
equals 1
23Example
S ? S S S S (S) 1 0 But there is
another different parse tree that also generates
the string 0 0 1 The derivation begins like
this S What is the leftmost variable? S What
can we replace it with? S S or S S or
(S) or 1 or 0 Pick another one of these
at random, say S S
24S ? S S S S (S) 1 0
Here is the parse tree
S S S 0 S S
0 1 Our string is still 0 0 1,
but this parse corresponds to take 0, and then
multiply it by the sum of 0 1, which equals 0
25S ? S S S S (S) 1 0
We can clearly indicate that the addition is to
be done first. Here is the parse tree
S S S 0 ( S ) S
S 0 1 Our string is now 0
(0 1). This parse corresponds to take 0, and
then multiply it by the sum of 0 1, which
equals 0
26Equivalent grammars
Here is a non-ambiguous grammar that
generates the same language. S ? S A A A
? A B B B ? (S) 1 0 Two grammars that
generate the same language are said to be
equivalent. To make parsing easier, we prefer
grammars that are not ambiguous.
27Ambiguous grammars equivalent grammars
- There is no general algorithm for determining
whether a given CFG is ambiguous. - There is no general algorithm for determining
whether a given CFG is equivalent to another CFG. - ?? language ????? ambiguous ? grammar?
28Ambiguous grammars
Definition 5.6 If L is a context-free language
for which there an unambiguous grammar, then L is
said to be unambiguous. If every grammar that
generates L is ambiguous, then the language is
called inherently ambiguous. Example L
anbncm ? anbmcm with n and m non-negative, is
inherently ambiguous.
29Exercise
Show that the following grammar is ambiguous. S
? AB aaB A ? a Aa B ? b Construct an
equivalent grammar that is unambiguous.