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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

every derivation from E is of the form<br />

or<br />

where n ≥ 0.<br />

E ∗<br />

=⇒ a n Eb n =⇒ a n abb n = a n+1 b n+1 ,<br />

E ∗<br />

=⇒ a n Eb n =⇒ a n aEbb n = a n+1 Eb n+1 ,<br />

Grammar G1 is very simple: every string a n b n has a unique derivation. This is usually<br />

not the case. For example, using the grammar G2 =({E,+, ∗, (, ),a}, {+, ∗, (, ),a},P,E),<br />

where P is the set of rules<br />

E −→ E + E,<br />

E −→ E ∗ E,<br />

E −→ (E),<br />

E −→ a,<br />

the string a + a ∗ a has the following distinct derivations, where the boldface indicates which<br />

occurrence of E is rewritten:<br />

and<br />

E =⇒ E ∗ E =⇒ E + E ∗ E<br />

=⇒ a + E ∗ E =⇒ a + a ∗ E =⇒ a + a ∗ a,<br />

E =⇒ E + E =⇒ a + E<br />

=⇒ a + E ∗ E =⇒ a + a ∗ E =⇒ a + a ∗ a.<br />

In the above derivations, the leftmost occurrence of a nonterminal is chosen at each step.<br />

Such derivations are called leftmost derivations. We could systematically rewrite the rightmost<br />

occurrence of a nonterminal, getting rightmost derivations. The string a + a ∗ a also<br />

has the following two rightmost derivations, where the boldface indicates which occurrence<br />

of E is rewritten:<br />

and<br />

E =⇒ E + E =⇒ E + E ∗ E<br />

=⇒ E + E ∗ a =⇒ E + a ∗ a =⇒ a + a ∗ a,<br />

E =⇒ E ∗ E =⇒ E ∗ a<br />

=⇒ E + E ∗ a =⇒ E + a ∗ a =⇒ a + a ∗ a.<br />

The language generated by a context-free grammar is defined as follows.

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

Saved successfully!

Ooh no, something went wrong!