context-free grammars – derivations lecture 15 section 2.1 mon, sep 24, 2007
TRANSCRIPT
![Page 1: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/1.jpg)
Context-Free Grammars – Derivations
Lecture 15Section 2.1
Mon, Sep 24, 2007
![Page 2: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/2.jpg)
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.
![Page 3: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/3.jpg)
Example
• Let V and be• V = {E}, = {a, b, c, +, *, (, )}.
• Let R be• E E + E | E * E | (E) | a | b | c
![Page 4: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/4.jpg)
Examples
• Find CFGs for the following languages.• {anbn | n 0}• {anbm | n m 0}• {anbm | m n 0}
![Page 5: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/5.jpg)
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}
![Page 6: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/6.jpg)
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.
![Page 7: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/7.jpg)
Equal No. of a’s and b’s
• In each case, what do we know about z?
![Page 8: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/8.jpg)
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.
![Page 9: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/9.jpg)
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?
![Page 10: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/10.jpg)
Derivations
• A string u yields a string v if we can apply a grammar rule to u and get v.
• We write u v.
![Page 11: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/11.jpg)
Derivations
• We say that u derives v 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.
![Page 12: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/12.jpg)
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.
![Page 13: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/13.jpg)
Example
• Using the grammar S SS | aSb | bSa |
find leftmost and rightmost derivations of abab.
![Page 14: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/14.jpg)
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.)
![Page 15: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/15.jpg)
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.
![Page 16: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/16.jpg)
Ambiguity
• Consider again the grammar rulesS 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?
![Page 17: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/17.jpg)
Ambiguity
• Consider the grammarE 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?
![Page 18: Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007](https://reader035.vdocuments.us/reader035/viewer/2022072015/56649ed45503460f94be4de2/html5/thumbnails/18.jpg)
Ambiguity
• See Example 2.4 on p. 103 an unambiguous grammar for this language.