readingMaterialweek4-5
readingMaterialweek4-5
CONTEXT-FREE
LANGUAGES
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
102 CHAPTER 2 / CONTEXT-FREE LANGUAGES
2.1
CONTEXT-FREE GRAMMARS
The following is an example of a context-free grammar, which we call G1 .
A → 0A1
A→B
B →#
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
2.1 CONTEXT-FREE GRAMMARS 103
FIGURE 2.1
Parse tree for 000#111 in grammar G1
All strings generated in this way constitute the language of the grammar.
We write L(G1 ) for the language of grammar G1 . Some experimentation with
the grammar G1 shows us that L(G1 ) is {0n #1n | n ≥ 0}. Any language that can
be generated by some context-free grammar is called a context-free language
(CFL). For convenience when presenting a context-free grammar, we abbreviate
several rules with the same left-hand variable, such as A → 0A1 and A → B,
into a single line A → 0A1 | B, using the symbol “ | ” as an “or”.
The following is a second example of a context-free grammar, called G2 ,
which describes a fragment of the English language.
a boy sees
the boy sees a flower
a girl with a flower likes the boy
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
104 CHAPTER 2 / CONTEXT-FREE LANGUAGES
DEFINITION 2.2
A context-free grammar is a 4-tuple (V, Σ, R, S), where
1. V is a finite set called the variables,
2. Σ is a finite set, disjoint from V , called the terminals,
3. R is a finite set of rules, with each rule being a variable and a
string of variables and terminals, and
4. S ∈ V is the start variable.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
2.1 CONTEXT-FREE GRAMMARS 105
EXAMPLE 2.3
Consider grammar G3 = ({S}, {a, b}, R, S). The set of rules, R, is
S → aSb | SS | ε.
This grammar generates strings such as abab, aaabbb, and aababb. You can
see more easily what this language is if you think of a as a left parenthesis “(”
and b as a right parenthesis “)”. Viewed in this way, L(G3 ) is the language of
all strings of properly nested parentheses. Observe that the right-hand side of a
rule may be the empty string ε.
EXAMPLE 2.4
Consider grammar G4 = (V, Σ, R, ⟨EXPR ⟩).
V is {⟨EXPR⟩, ⟨TERM ⟩, ⟨FACTOR ⟩} and Σ is {a, +, x, (, )}. The rules are
⟨EXPR ⟩ → ⟨EXPR ⟩+⟨TERM⟩ | ⟨TERM ⟩
⟨TERM ⟩ → ⟨TERM ⟩x⟨FACTOR ⟩ | ⟨FACTOR ⟩
⟨FACTOR ⟩ → ( ⟨EXPR ⟩ ) | a
The two strings a+a xa and (a+a) xa can be generated with grammar G4 .
The parse trees are shown in the following figure.
FIGURE 2.5
Parse trees for the strings a+a xa and (a+a) xa
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
106 CHAPTER 2 / CONTEXT-FREE LANGUAGES
the meaning of the code to be compiled in a process called parsing. One rep-
resentation of this meaning is the parse tree for the code, in the context-free
grammar for the programming language. We discuss an algorithm that parses
context-free languages later in Theorem 7.16 and in Problem 7.45.
Grammar G4 describes a fragment of a programming language concerned
with arithmetic expressions. Observe how the parse trees in Figure 2.5 “group”
the operations. The tree for a+a xa groups the x operator and its operands
(the second two a’s) as one operand of the + operator. In the tree for (a+a) xa,
the grouping is reversed. These groupings fit the standard precedence of mul-
tiplication before addition and the use of parentheses to override the standard
precedence. Grammar G4 is designed to capture these precedence relations.
S1 → 0S1 1 | ε
S2 → 1S2 0 | ε
for the language {1n 0n | n ≥ 0} and then add the rule S → S1 | S2 to give the
grammar
S → S1 | S2
S1 → 0S1 1 | ε
S2 → 1S2 0 | ε.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
2.1 CONTEXT-FREE GRAMMARS 107
AMBIGUITY
Sometimes a grammar can generate the same string in several different ways.
Such a string will have several different parse trees and thus several different
meanings. This result may be undesirable for certain applications, such as pro-
gramming languages, where a program should have a unique interpretation.
If a grammar generates the same string in several different ways, we say that
the string is derived ambiguously in that grammar. If a grammar generates some
string ambiguously, we say that the grammar is ambiguous.
For example, consider grammar G5 :
This grammar generates the string a+a xa ambiguously. The following figure
shows the two different parse trees.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
108 CHAPTER 2 / CONTEXT-FREE LANGUAGES
FIGURE 2.6
The two parse trees for the string a+a xa in grammar G5
This grammar doesn’t capture the usual precedence relations and so may
group the + before the × or vice versa. In contrast, grammar G4 generates
exactly the same language, but every generated string has a unique parse tree.
Hence G4 is unambiguous, whereas G5 is ambiguous.
Grammar G2 (page 103) is another example of an ambiguous grammar. The
sentence the girl touches the boy with the flower has two different
derivations. In Exercise 2.8 you are asked to give the two parse trees and observe
their correspondence with the two different ways to read that sentence.
Now we formalize the notion of ambiguity. When we say that a grammar
generates a string ambiguously, we mean that the string has two different parse
trees, not two different derivations. Two derivations may differ merely in the
order in which they replace variables yet not in their overall structure. To con-
centrate on structure, we define a type of derivation that replaces variables in a
fixed order. A derivation of a string w in a grammar G is a leftmost derivation if
at every step the leftmost remaining variable is the one replaced. The derivation
preceding Definition 2.2 (page 104) is a leftmost derivation.
DEFINITION 2.7
A string w is derived ambiguously in context-free grammar G if
it has two or more different leftmost derivations. Grammar G is
ambiguous if it generates some string ambiguously.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
2.1 CONTEXT-FREE GRAMMARS 109
DEFINITION 2.8
A context-free grammar is in Chomsky normal form if every rule is
of the form
A → BC
A→a
where a is any terminal and A, B, and C are any variables—except
that B and C may not be the start variable. In addition, we permit
the rule S → ε, where S is the start variable.
THEOREM 2.9
Any context-free language is generated by a context-free grammar in Chomsky
normal form.
PROOF IDEA We can convert any grammar G into Chomsky normal form.
The conversion has several stages wherein rules that violate the conditions are
replaced with equivalent ones that are satisfactory. First, we add a new start
variable. Then, we eliminate all ε-rules of the form A → ε. We also eliminate
all unit rules of the form A → B. In both cases we patch up the grammar to be
sure that it still generates the same language. Finally, we convert the remaining
rules into the proper form.
PROOF First, we add a new start variable S0 and the rule S0 → S, where
S was the original start variable. This change guarantees that the start variable
doesn’t occur on the right-hand side of a rule.
Second, we take care of all ε-rules. We remove an ε-rule A → ε, where A
is not the start variable. Then for each occurrence of an A on the right-hand
side of a rule, we add a new rule with that occurrence deleted. In other words,
if R → uAv is a rule in which u and v are strings of variables and terminals, we
add rule R → uv. We do so for each occurrence of an A, so the rule R → uAvAw
causes us to add R → uvAw, R → uAvw, and R → uvw. If we have the rule
R → A, we add R → ε unless we had previously removed the rule R → ε. We
repeat these steps until we eliminate all ε-rules not involving the start variable.
Third, we handle all unit rules. We remove a unit rule A → B. Then,
whenever a rule B → u appears, we add the rule A → u unless this was a unit
rule previously removed. As before, u is a string of variables and terminals. We
repeat these steps until we eliminate all unit rules.
Finally, we convert all remaining rules into the proper form. We replace each
rule A → u1 u2 · · · uk , where k ≥ 3 and each ui is a variable or terminal symbol,
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
110 CHAPTER 2 / CONTEXT-FREE LANGUAGES
EXAMPLE 2.10
Let G6 be the following CFG and convert it to Chomsky normal form by using
the conversion procedure just given. The series of grammars presented illus-
trates the steps in the conversion. Rules shown in bold have just been added.
Rules shown in gray have just been removed.
1. The original CFG G6 is shown on the left. The result of applying the first
step to make a new start variable appears on the right.
S0 → S
S → ASA | aB
S → ASA | aB
A → B|S
A → B|S
B → b|ε
B → b|ε
3a. Remove unit rules S → S, shown on the left, and S0 → S, shown on the
right.
S0 → S S0 → S | ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS | S S → ASA | aB | a | SA | AS
A → B|S A → B|S
B → b B → b
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
2.2 PUSHDOWN AUTOMATA 111
4. Convert the remaining rules into the proper form by adding additional vari-
ables and rules. The final grammar in Chomsky normal form is equivalent to G6 .
(Actually the procedure given in Theorem 2.9 produces several variables Ui and
several rules Ui → a. We simplified the resulting grammar by using a single
variable U and rule U → a.)
S0 → AA1 | U B | a | SA | AS
S → AA1 | U B | a | SA | AS
A → b | AA1 | U B | a | SA | AS
A1 → SA
U → a
B → b
2.2
PUSHDOWN AUTOMATA
In this section we introduce a new type of computational model called pushdown
automata. These automata are like nondeterministic finite automata but have an
extra component called a stack. The stack provides additional memory beyond
the finite amount available in the control. The stack allows pushdown automata
to recognize some nonregular languages.
Pushdown automata are equivalent in power to context-free grammars. This
equivalence is useful because it gives us two options for proving that a language is
context free. We can give either a context-free grammar generating it or a push-
down automaton recognizing it. Certain languages are more easily described in
terms of generators, whereas others are more easily described by recognizers.
The following figure is a schematic representation of a finite automaton. The
control represents the states and transition function, the tape contains the in-
put string, and the arrow represents the input head, pointing at the next input
symbol to be read.
FIGURE 2.11
Schematic of a finite automaton
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
112 CHAPTER 2 / CONTEXT-FREE LANGUAGES
FIGURE 2.12
Schematic of a pushdown automaton
A pushdown automaton (PDA) can write symbols on the stack and read them
back later. Writing a symbol “pushes down” all the other symbols on the stack.
At any time the symbol on the top of the stack can be read and removed. The
remaining symbols then move back up. Writing a symbol on the stack is of-
ten referred to as pushing the symbol, and removing a symbol is referred to as
popping it. Note that all access to the stack, for both reading and writing, may
be done only at the top. In other words a stack is a “last in, first out” storage
device. If certain information is written on the stack and additional information
is written afterward, the earlier information becomes inaccessible until the later
information is removed.
Plates on a cafeteria serving counter illustrate a stack. The stack of plates
rests on a spring so that when a new plate is placed on top of the stack, the plates
below it move down. The stack on a pushdown automaton is like a stack of
plates, with each plate having a symbol written on it.
A stack is valuable because it can hold an unlimited amount of information.
Recall that a finite automaton is unable to recognize the language {0n 1n | n ≥ 0}
because it cannot store very large numbers in its finite memory. A PDA is able to
recognize this language because it can use its stack to store the number of 0s it
has seen. Thus the unlimited nature of a stack allows the PDA to store numbers of
unbounded size. The following informal description shows how the automaton
for this language works.
Read symbols from the input. As each 0 is read, push it onto the stack. As
soon as 1s are seen, pop a 0 off the stack for each 1 read. If reading the
input is finished exactly when the stack becomes empty of 0s, accept the
input. If the stack becomes empty while 1s remain or if the 1s are finished
while the stack still contains 0s or if any 0s appear in the input following
1s, reject the input.
As mentioned earlier, pushdown automata may be nondeterministic. Deter-
ministic and nondeterministic pushdown automata are not equivalent in power.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
2.2 PUSHDOWN AUTOMATA 113
DEFINITION 2.13
A pushdown automaton is a 6-tuple (Q, Σ, Γ, δ, q0 , F ), where Q, Σ,
Γ, and F are all finite sets, and
1. Q is the set of states,
2. Σ is the input alphabet,
3. Γ is the stack alphabet,
4. δ : Q × Σε × Γε −→P(Q × Γε ) is the transition function,
5. q0 ∈ Q is the start state, and
6. F ⊆ Q is the set of accept states.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
114 CHAPTER 2 / CONTEXT-FREE LANGUAGES
EXAMPLE 2.14
The following is the formal description of the PDA (page 112) that recognizes
the language {0n 1n | n ≥ 0}. Let M1 be (Q, Σ, Γ, δ, q1 , F ), where
Q = {q1 , q2 , q3 , q4 },
Σ = {0,1},
Γ = {0, $},
F = {q1 , q4 }, and
δ is given by the following table, wherein blank entries signify ∅.
Input: 0 1 ε
Stack: 0 $ ε 0 $ ε 0 $ ε
q1 {(q2 , $)}
q2 {(q2 , 0)} {(q3 , ε)}
q3 {(q3 , ε)} {(q4 , ε)}
q4
We can also use a state diagram to describe a PDA, as in Figures 2.15, 2.17,
and 2.19. Such diagrams are similar to the state diagrams used to describe finite
automata, modified to show how the PDA uses its stack when going from state
to state. We write “a,b → c” to signify that when the machine is reading an
a from the input, it may replace the symbol b on the top of the stack with a c.
Any of a, b, and c may be ε. If a is ε, the machine may make this transition
without reading any symbol from the input. If b is ε, the machine may make
this transition without reading and popping any symbol from the stack. If c
is ε, the machine does not write any symbol on the stack when going along this
transition.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
2.2 PUSHDOWN AUTOMATA 115
FIGURE 2.15
State diagram for the PDA M1 that recognizes {0n 1n | n ≥ 0}
EXAMPLE 2.16
This example illustrates a pushdown automaton that recognizes the language
Informally, the PDA for this language works by first reading and pushing
the a’s. When the a’s are done, the machine has all of them on the stack so
that it can match, them with either the b’s or the c’s. This maneuver is a bit
tricky because the machine doesn’t know in advance whether to match the a’s
with the b’s or the c’s. Nondeterminism comes in handy here.
Using its nondeterminism, the PDA can guess whether to match the a’s with
the b’s or with the c’s, as shown in Figure 2.17. Think of the machine as having
two branches of its nondeterminism, one for each possible guess. If either of
them matches, that branch accepts and the entire machine accepts. Problem 2.57
asks you to show that nondeterminism is essential for recognizing this language
with a PDA.
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.
116 CHAPTER 2 / CONTEXT-FREE LANGUAGES
FIGURE 2.17
State diagram for PDA M2 that recognizes
{ai bj ck | i, j, k ≥ 0 and i = j or i = k}
EXAMPLE 2.18
In this example we give a PDA M3 recognizing the language {wwR | w ∈ {0,1}∗ }.
Recall that wR means w written backwards. The informal description and state
diagram of the PDA follow.
Begin by pushing the symbols that are read onto the stack. At each point,
nondeterministically guess that the middle of the string has been reached and
then change into popping off the stack for each symbol read, checking to see that
they are the same. If they were always the same symbol and the stack empties at
the same time as the input is finished, accept; otherwise reject.
FIGURE 2.19
State diagram for the PDA M3 that recognizes {wwR | w ∈ {0, 1}∗}
Copyright 2012 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part. Due to electronic rights, some third party content may be suppressed from the
eBook and/or eChapter(s). Editorial review has deemed that any suppressed content does not materially affect the overall learning experience. Cengage Learning reserves the right to remove additional
content at any time if subsequent rights restrictions require it.