Grammars (Section 3.3)
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
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.
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.
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
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
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}.
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}.
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}.
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 |Λ.
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 |Λ.
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 |Λ.
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 |Λ.
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|Λ.
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|Λ.
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).
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))).
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).
Parse Tree
S
)S
b
,S
)S
a
(f
(g
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
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
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
Another example
• Show that the grammar S → abS |Sab|c is ambiguous:
S
S
baS
c
ba
S
baS
S
c
ba
Another example
• Show that the grammar S → abS |Sab|c is ambiguous:
S
S
baS
c
ba
S
baS
S
c
ba
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 |Λ.