+ All Categories
Home > Documents > Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a...

Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a...

Date post: 30-Sep-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
25
Grammars (Section 3.3)
Transcript
Page 1: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Grammars (Section 3.3)

Page 2: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Grammars

• A grammar is a finite set of rules, called productions, that areused to describe the strings of a language.

• Notational Example: The productions take the form α→ βwhere α and β are strings over an alphabet of terminals andnonterminals. Read α→ β as “α produces β” “α derives β”or “α is replaced by β”. The following four expressions areproductions for a grammar:

• S → aSB• S → Λ• B → bB• B → b

Page 3: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Grammar terminology

First, an alternate short form for the previous grammar is:

• S → aSB|Λ• B → bB|b.

Terminology:

• Terminals: {a, b}, the alphabet of the language.

• Nonterminals: {S ,B}, the grammar symbols (uppercaseletters), disjoint from terminals.

• Start symbol: S , a specified nonterminal alone on the leftside of some production.

• Sentential form: any string of terminals and/or nonterminals.

Page 4: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Derivations

• Derivation: a transformation of sentential forms by means ofproductions as follows: if xαy is a sentential form and α→ βis a production, then the replacement of α by β in xαy toobtain xβy is a derivation step, which we denote byxαy ⇒ xβy .

• Example Derivation:S ⇒ aSB ⇒ aaSBB ⇒ aaBB ⇒ aabBB ⇒ aabbB ⇒ aabbb.

• This is a leftmost derivation, where each step replaces theleftmost nonterminal. The symbol ⇒+ means one or moresteps and ⇒∗ means zero or more steps. So we could writeS ⇒+ aabbb or S ⇒∗ aabbb or aSB ⇒∗ aSB, and so on.

Page 5: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

The Language of a Grammar

• The language of a grammar is the set of terminal stringsderived from the start symbol.

• Example: Can we find the language of the grammar:S → aSB|Λ and B → bB|b?

• Solution: Examine some derivations to see if a patternemerges

• S ⇒ Λ• S ⇒ aSB ⇒ aB ⇒ ab• S ⇒ aSB ⇒ aB ⇒ abB ⇒ abbB ⇒ abbb• S ⇒ aSB ⇒ aaSBB ⇒ aaBB ⇒ aabB ⇒ aabb• S ⇒ aSB ⇒ aaSBB ⇒ aaBB ⇒ aabBB ⇒ aabbBB ⇒

aabbbB ⇒ aabbbb

Page 6: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

The Language of a Grammar

• The language of a grammar is the set of terminal stringsderived from the start symbol.

• Example: Can we find the language of the grammar:S → aSB|Λ and B → bB|b?

• Solution: Examine some derivations to see if a patternemerges

• S ⇒ Λ• S ⇒ aSB ⇒ aB ⇒ ab• S ⇒ aSB ⇒ aB ⇒ abB ⇒ abbB ⇒ abbb• S ⇒ aSB ⇒ aaSBB ⇒ aaBB ⇒ aabB ⇒ aabb• S ⇒ aSB ⇒ aaSBB ⇒ aaBB ⇒ aabBB ⇒ aabbBB ⇒

aabbbB ⇒ aabbbb

Page 7: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Solution

• So, we have a pretty good idea that the language of thegrammar is:

{anbn+k |n, k ∈ N}• Quiz: Describe the language of the grammar S → a|bcS• Solution: {(bc)na|n ∈ N}.

Page 8: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Solution

• So, we have a pretty good idea that the language of thegrammar is: {anbn+k |n, k ∈ N}

• Quiz: Describe the language of the grammar S → a|bcS

• Solution: {(bc)na|n ∈ N}.

Page 9: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Solution

• So, we have a pretty good idea that the language of thegrammar is: {anbn+k |n, k ∈ N}

• Quiz: Describe the language of the grammar S → a|bcS• Solution: {(bc)na|n ∈ N}.

Page 10: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Construction of Grammars

• Example: Find a grammar for {anb|n ∈ N}

• Solution: We need to derive any string of a’s followed by b.The production S → aS can be used to derive strings of a’s.The production S → b will stop the derivation and producethe desired string ending with b. So a grammar for thelanguage is S → aS |b.

• Quiz: Find a grammar for {ban|n ∈ N}.• Solution: S → Sa|b.

• Quiz: Find a grammar for {(ab)n|n ∈ N}.• Solution: S → Sab|Λ or S → abS |Λ.

Page 11: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Construction of Grammars

• Example: Find a grammar for {anb|n ∈ N}• Solution: We need to derive any string of a’s followed by b.

The production S → aS can be used to derive strings of a’s.The production S → b will stop the derivation and producethe desired string ending with b. So a grammar for thelanguage is S → aS |b.

• Quiz: Find a grammar for {ban|n ∈ N}.

• Solution: S → Sa|b.

• Quiz: Find a grammar for {(ab)n|n ∈ N}.• Solution: S → Sab|Λ or S → abS |Λ.

Page 12: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Construction of Grammars

• Example: Find a grammar for {anb|n ∈ N}• Solution: We need to derive any string of a’s followed by b.

The production S → aS can be used to derive strings of a’s.The production S → b will stop the derivation and producethe desired string ending with b. So a grammar for thelanguage is S → aS |b.

• Quiz: Find a grammar for {ban|n ∈ N}.• Solution: S → Sa|b.

• Quiz: Find a grammar for {(ab)n|n ∈ N}.

• Solution: S → Sab|Λ or S → abS |Λ.

Page 13: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Construction of Grammars

• Example: Find a grammar for {anb|n ∈ N}• Solution: We need to derive any string of a’s followed by b.

The production S → aS can be used to derive strings of a’s.The production S → b will stop the derivation and producethe desired string ending with b. So a grammar for thelanguage is S → aS |b.

• Quiz: Find a grammar for {ban|n ∈ N}.• Solution: S → Sa|b.

• Quiz: Find a grammar for {(ab)n|n ∈ N}.• Solution: S → Sab|Λ or S → abS |Λ.

Page 14: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Rules for Combining Grammars

Let L and M be two languages with grammars that have startsymbols A and B respectively, and with disjoint sets ofnonterminals. Then the following rules apply:

• L ∪M has a grammar starting with S → A|B.

• LM has a grammar starting with S → AB.

• L∗ has a grammar starting with S → AS |Λ.

Example: Find a grammar for {ambmcn|m, n ∈ N}

Solution: Thelanguage is the product LM, where L = {ambm|m ∈ N} andM = {cn|n ∈ N}. So a grammar for LM can be written in terms ofgrammars for L and M as follows:

• S → AB

• A→ aAb|Λ• B → cB|Λ.

Page 15: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Rules for Combining Grammars

Let L and M be two languages with grammars that have startsymbols A and B respectively, and with disjoint sets ofnonterminals. Then the following rules apply:

• L ∪M has a grammar starting with S → A|B.

• LM has a grammar starting with S → AB.

• L∗ has a grammar starting with S → AS |Λ.

Example: Find a grammar for {ambmcn|m, n ∈ N} Solution: Thelanguage is the product LM, where L = {ambm|m ∈ N} andM = {cn|n ∈ N}. So a grammar for LM can be written in terms ofgrammars for L and M as follows:

• S → AB

• A→ aAb|Λ• B → cB|Λ.

Page 16: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Inductive definitions

Example: Find a grammar for the language L defined inductivelyby:

• Basis: a, b, c ∈ L

• Induction: If x , y ∈ L then f (x), g(x , y) ∈ L

Solution: We can get some idea about L by listing some of itsstrings.

•a, b, c , f (a), f (b), . . . , g(a, a), . . . , g(f (a), f (a)), . . . , f (g(b, c)), . . . , g(f (a), g(b, f (c))), . . .

So L is the set of all algebraic expressions made up from the lettersa, b, c and the function symbols f and g of arities 1 and 2,respectively. A grammar for L can be written as:

• S → a|b|c |f (S)|g(S ,S).

Page 17: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Example derivation

For example, a leftmost derivation of g(f (a), g(b, f (c))) can bewritten as:

• S ⇒ g(S ,S)⇒ g(f (S), S)⇒ g(f (a),S)⇒g(f (a), g(S ,S))⇒ g(f (a), g(b, S))⇒ g(f (a), g(b, f (S)))⇒g(f (a), g(b, f (c))).

Page 18: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Parse Trees

• A Parse Tree is a tree that represents a derivation. The root isthe start symbol and the children of a nonterminal node arethe symbols (terminals, nonterminals, or Λ) on the right sideof the production used in the derivation step that replacesthat node.

• Example: The tree shown in the next slide is the parse treefor the following derivation:

• S ⇒ g(S ,S)⇒ g(f (S),S)⇒ g(f (a),S)⇒ g(f (a), b).

Page 19: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Parse Tree

S

)S

b

,S

)S

a

(f

(g

Page 20: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Ambiguous Grammar

• The term ambiguous grammar means that there is at leastone string with two distinct parse trees, or equivalently, twodistinct leftmost derivations or two distinct rightmostderivations.

• Example: Is the grammar S → SaS |b ambiguous?

• Solution: Yes. For example, the string babab has two distinctleftmost derivations:

• S ⇒ SaS ⇒ SaSaS ⇒ baSaS ⇒ babaS ⇒ babab• S ⇒ SaS ⇒ baS ⇒ baSaS ⇒ babaS ⇒ babab

Page 21: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Ambiguous Grammar

• The term ambiguous grammar means that there is at leastone string with two distinct parse trees, or equivalently, twodistinct leftmost derivations or two distinct rightmostderivations.

• Example: Is the grammar S → SaS |b ambiguous?

• Solution: Yes. For example, the string babab has two distinctleftmost derivations:

• S ⇒ SaS ⇒ SaSaS ⇒ baSaS ⇒ babaS ⇒ babab• S ⇒ SaS ⇒ baS ⇒ baSaS ⇒ babaS ⇒ babab

Page 22: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Ambiguous Parse Trees

• This ambiguity is perhaps best shown through the distinctparse trees:

S

S

b

aS

S

b

aS

b

S

S

S

b

aS

b

aS

b

Page 23: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Another example

• Show that the grammar S → abS |Sab|c is ambiguous:

S

S

baS

c

ba

S

baS

S

c

ba

Page 24: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Another example

• Show that the grammar S → abS |Sab|c is ambiguous:

S

S

baS

c

ba

S

baS

S

c

ba

Page 25: Grammars (Section 3.3)vh216602.truman.edu/agarvey/cs291/section3.3.pdf · Grammars Agrammaris a nite set of rules, called productions, that are used to describe the strings of a language.

Unambiguous Grammars

• Sometimes you can find a grammar that is not ambiguous forthe language of an ambiguous grammar.

• Example: The previous example showed S → SaS |b isambiguous. The languages of the grammar is{b, bab, babab, . . .}. Another grammar for the language isS → baS |b. It is unambiguous because S produces either baSor b, which can’t derive the same string.

• Example: Previously we showed S → c |abS |Sab isambiguous. Its language is {(ab)mc(ab)n|m, n ∈ N}. Anothergrammar for the language is: S → abS |cT and T → abT |Λ.


Recommended