Chapter 12: Context-Free Grammars
Chapter 12: Context-Free Grammars
Peter Cappello
Department of Computer Science
University of California, Santa Barbara
Santa Barbara, CA 93106
[email protected]
1
Symbolism for Generative Grammars
• The book chapter gives a good explanation of the background and
reason for studying this material.
• A generative grammar is a grammar with which one can generate
all the words (sentences) in a language.
2
Definition A context-free grammar (CFG) is a collection of 3 things:
By convention, we use:
3
Definition The language generated by a CFG is the set of all strings
of terminals produced from S using productions as substitutions.
A language generated by a CFG is a context-free language (CFL).
4
Example
Production 1 S → aS
Production 2 S → Λ
Applying production 1 two times, followed by production 2, yields:
S ⇒ aS
⇒ aaS
⇒ aaΛ
= aa
• The language generated by this CFG is a∗.
• → means “can be replaced by”.
• ⇒ (used in a derivation) means “can develop into”.
• A derivation’s right hand side (RHS) is a working string when it
contains nonterminals.
5
Example
Define a CFG that accepts (a + b)∗.
S → aS
S → bS
S→Λ
6
Example
Define a CFG that accepts (a + b)∗aa(a + b)∗.
S → XaaX
X → aX
X → bX
X→Λ
7
Example
Define a CFG that accepts {anbn}.
S → aSb
S→Λ
Equivalently:
S → aSb | Λ
8
Trees
Given the CFG
S → AA
A → AAA|bA|Ab|a
9
Lukasiewicz Notation
• Consider the CFG:
S → +| ∗ |n
+ → + + | + ∗| + n| ∗ +| ∗ ∗| ∗ n|n + |n ∗ |n n
∗ → + + | + ∗| + n| ∗ +| ∗ ∗| ∗ n|n + |n ∗ |n n
n → 1|2|3|4|5|6|7|8|9
10
• Think of these items as having been pushed on a stack in the order of
visitation.
• To evaluate the expression, when the top 2 items are numbers:
11
Ambiguity
Definition: A CFG G is ambiguous if there exists a w ∈ L(G) with
2 derivations that correspond to different parse trees.
If a CFG is not ambiguous it is unambiguous.
Example Let CFG G be S → aS|Sa|a, the regular language aa∗.
The word aa has 2 derivations:
• S ⇒ Sa ⇒ aa
• S ⇒ aS ⇒ aa
distinct with a distinct parse tree.
However a+ can be defined by S → aS|a, which is not ambiguous.
12
The Total Language Tree
Definition: For a given CFG, define a tree:
• Its root is S
• For each nonterminal node in the tree,
For each nonterminal, N at that node, construct a child node in
the tree for each production with N as the LHS.
Example Let CFG G be:
S → aa|bX|aXX
X → ab|b
• What is the total language tree for this CFG?
• What is the total language tree for S → Λ|aSb ?
13