19.05.2013 Views

Chapter 3 Context-Free Grammars, Context-Free Languages, Parse ...

Chapter 3 Context-Free Grammars, Context-Free Languages, Parse ...

Chapter 3 Context-Free Grammars, Context-Free Languages, Parse ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

38 CHAPTER 3. CONTEXT-FREE GRAMMARS AND LANGUAGES<br />

Lemma 3.2.4 Let G = (V,Σ,P,S) be a context-free grammar. For every w ∈ Σ ∗ , for<br />

every derivation S +<br />

=⇒ w, there is a leftmost derivation S +<br />

=⇒ w, and there is a rightmost<br />

lm<br />

derivation S +<br />

=⇒ w.<br />

rm<br />

Proof . Of course, we have to somehow use induction on derivations, but this is a little<br />

tricky, and it is necessary to prove a stronger fact. We treat leftmost derivations, rightmost<br />

derivations being handled in a similar way.<br />

=⇒ w, then there is a<br />

leftmost derivation α n<br />

=⇒ w.<br />

lm<br />

The claim is proved by induction on n.<br />

For n =1,thereexistsomeλ, ρ ∈ V ∗ and some production A → γ, such that α = λAρ<br />

and w = λγρ. Sincewis a terminal string, λ, ρ, andγ, are terminal strings. Thus, A is the<br />

only nonterminal in α, and the derivation step α 1<br />

=⇒ w is a leftmost step (and a rightmost<br />

step!).<br />

If n>1, then the derivation α n<br />

=⇒ w is of the form<br />

Claim: For every w ∈ Σ ∗ , for every α ∈ V + , for every n ≥ 1, if α n<br />

α =⇒ α1<br />

n−1<br />

=⇒ w.<br />

There are two subcases.<br />

Case 1. If the derivation step α =⇒ α1 is a leftmost step α =⇒ α1, by the induction<br />

lm<br />

hypothesis, there is a leftmost derivation α1<br />

α =⇒<br />

lm α1<br />

n−1<br />

=⇒<br />

lm w, and we get the leftmost derivation<br />

n−1<br />

=⇒<br />

lm w.<br />

Case 2. The derivation step α =⇒ α1 is a not a leftmost step. In this case, there must<br />

be some u ∈ Σ ∗ , µ, ρ ∈ V ∗ , some nonterminals A and B, and some production B → δ, such<br />

that<br />

α = uAµBρ and α1 = uAµδρ,<br />

where A is the leftmost nonterminal in α. Since we have a derivation α1<br />

n − 1, by the induction hypothesis, there is a leftmost derivation<br />

α1<br />

n−1<br />

=⇒<br />

lm w.<br />

n−1<br />

=⇒ w of length<br />

Since α1 = uAµδρ where A is the leftmost terminal in α1, the first step in the leftmost<br />

n−1<br />

derivation α1 =⇒ w is of the form<br />

lm<br />

uAµδρ =⇒<br />

lm uγµδρ,

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!