1 / 18

Context-Free Grammars – Derivations

Context-Free Grammars – Derivations. Lecture 15 Section 2.1 Mon, Sep 24, 2007. Context-Free Grammars. A context-free grammar is a grammar whose rules have the following properties: The left-hand is a single variable. The right-hand side is any string of variables and terminals. Example.

christmas
Download Presentation

Context-Free Grammars – Derivations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007

  2. Context-Free Grammars • A context-free grammar is a grammar whose rules have the following properties: • The left-hand is a single variable. • The right-hand side is any string of variables and terminals.

  3. Example • Let V and  be • V = {E}, •  = {a, b, c, +, *, (, )}. • Let R be • E E + E | E * E | (E) | a | b | c

  4. Examples • Find CFGs for the following languages. • {anbn | n 0} • {anbm | nm 0} • {anbm | mn 0}

  5. Equal No. of a’s and b’s • Find CFGs for the language {w | w contains an equal number of a’s and b’s}

  6. Equal No. of a’s and b’s • Let w be a string with an equal number of a’s and b’s. • There are four cases: • w = aza • w = azb • w = bza • w = bzb.

  7. Equal No. of a’s and b’s • In each case, what do we know about z?

  8. Equal No. of a’s and b’s • In the cases azb and bza, z is again a string with an equal number of a’s and b’s.

  9. Equal No. of a’s and b’s • In the cases aza and bzb, the string w must necessarily split into a concatenation of strings w1w2, where w1 and w2 each have an equal number of a’s and b’s. • Why?

  10. Derivations • A string uyields a string v if we can apply a grammar rule to u and get v. • We write u v.

  11. Derivations • We say that uderivesv if there is a sequence u1, u2, …, uk (k  0), where u = u1, v = uk, and u1  u2  …  uk. • We write u * v. • The sequence is called a derivation.

  12. Leftmost and Rightmost Derivations • A leftmost derivation is a derivation in which, at each step, the leftmost variable is replaced with a string. • Similarly for a rightmost derivation.

  13. Example • Using the grammar SSS | aSb | bSa |  find leftmost and rightmost derivations of abab.

  14. Ambiguity • Some grammars provide more than one way to derive a string. • For example, abab can be derived in two different ways using the grammar rules SSS | aSb | bSa |  • (Typically the leftmost and rightmost derivations are different.)

  15. Ambiguity • If a language contains a string that has more than one leftmost derivation, then the grammar is ambiguous. • If every grammar for a language is ambiguous, then the language is inherently ambiguous.

  16. Ambiguity • Consider again the grammar rules SSS | aSb | bSa |  • Suppose that, for every string, we need to associate with each a a unique b. • Does the grammar show us how to do this?

  17. Ambiguity • Consider the grammar E E + E | E * E | (E) | a | b | c • Derive the string a + b * c in two different ways. • Is this grammar ambiguous? • Is this language inherently ambiguous?

  18. Ambiguity • See Example 2.4 on p. 103 an unambiguous grammar for this language.

More Related