05 Context-Free
05 Context-Free
Week 5
1
Topics
• Context-Free Grammars
• Parsing and Ambiguity
• Context-Free Grammars and Programming Languages
2
Context-Free Grammars
3
Context-Free Grammars
Definition 5.1
• A → x,
• where A ∈ V and x ∈ (V ∪ T)*.
• A language L is said to be context-free iff. there is a context-free grammar G
s.t. L = L(G).
4
Context-Free Grammars
Example 5.1
5
Context-Free Grammars
Example 5.2
• A → λ.
• is context-free.
n n
• L(G) = {ab(bbaa) bba(ba) : n ≥ 0}.
6
Context-Free Grammars
Example 5.3
n m
• The language L = {a b : n ≠ m} is context-free.
• To show this, we can produce a context-free grammar. (Def. 5.1)
• If n ≠ m, then either n > m or n < m.
• On top of the same number of a's and b's.
• For n > m, we need to add extra a's on the left, and for n < m, extra b's
on the right.
• We will construct productions for each case, and combine them together.
7
Context-Free Grammars
Example 5.3
• For non-linear grammars, a derivation may have sentential forms with more than one
variable.
• (1) S → AB,
• (2) A → aaA, (3) A → λ,
• (4) B → Bb, (5) B → λ.
2n m
• The grammar generates the language L(G) = {a b : n ≥ 0,m ≥ 0}.
1 2 3 4 5
• S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab.
1 4 2 5 3
• S ⇒ AB ⇒ ABb ⇒ aaABb ⇒ aaAb ⇒ aab.
11
Leftmost and Rightmost Derivations
Definition 5.2
12
Leftmost and Rightmost Derivations
Example 5.5
• Remind that there exist more than one derivation for the same string.
• To complete derivation, you have to be careful about the choices of
productions.
• S → aAB, A → bBb, B → A | λ.
• For string abbbb, two leftmost derivations
• S ⇒ aAB ⇒ abBbB ⇒ abAbB ⇒ abbBbbB ⇒ abbbbB ⇒ abbbb,
• S ⇒ aAB ⇒ abBbB ⇒ abbB ⇒ abbA ⇒ abbbBb ⇒ abbbb.
14
Derivation Trees Figure 5.1
• It is an ordered tree in which nodes are labeled with the left side of productions,
and in which the children of a node represent its corresponding right sides.
• A → abABc.
• Beginning with the root (start symbol) and ending in leaf nodes (terminals), it
shows how each variable is replaced in the derivation.
15
Derivation Trees
Definition 5.3
• Let G = (V, T, S, P) be a context-free grammar. An ordered tree is a derivation tree for G iff. it has the following
properties.
4. If a vertex has label A ∈ V, and its children are labeled (from left to right) a1, a2, ⋯, an, then P must contain
a production of the form A → a1a2⋯an.
5. A leaf labeled λ has no siblings, that is, a vertex with a child labeled λ can have no other children.
• A tree w/ properties 3, 4, 5, but not necessarily 1 and with '2a. Every leaf has a label from V ∪ T ∪ {λ}',
• is said to be a partial derivation tree.
16
Derivation Trees Figure 5.2
Example 5.6
17
Relation btw. Sentential Forms and Derivation Trees
Theorem 5.1
18
Relation btw. Sentential Forms and Derivation Trees
Theorem 5.1
• Proof:
• A sentential form ➞ a partial derivation tree.
• Proof by induction on the number of steps in the derivation.
• (Basis) the claim is true for one step derivation.
• S ⇒ u indicates S → u in G.
• We have a partial derivation tree for a production (property 4).
19
Relation btw. Sentential Forms and Derivation Trees
Theorem 5.1
• (Inductive Assumption) For every sentential form with n steps, there is a corresponding partial
derivation tree.
• (Inductive Assumption) For every partial derivation tree with height n, there exists a
corresponding sequential form identical to the yield of the tree.
• (Inductive Step) Now think about a partial derivation tree with height n + 1.
• Suppose the yield of this tree is of the form x1x2⋯xia1a2⋯ak y1y2⋯yj, where ai are the
symbols from leaf nodes at level n.
21
Relation btw. Sentential Forms and Derivation Trees
Theorem 5.1
• The yield of this tree is of the form x1x2⋯xia1a2⋯ak y1y2⋯yj, where ai are the symbols from
leaf nodes at level n.
• Now let p1, p2, ⋯pt are the symbols of the parent nodes having leaves with ai's as their
children.
• We can replace a1a2⋯ak with p1p2⋯pt, to obtain a yield of a partial derivation tree with height
n.
• By definition, there must be productions which have p's on the left side, and a's on the right
side.
• The resulting string of applying productions on a sentential form is also a sentential form, which
proves the claim by induction.
22
Parsing and Ambiguity
23
Parsing and Membership
24
Parsing and Membership
25
Parsing and Membership
• Since we have a derivation tree for w, we can always traverse the tree to
obtain derivation within finite steps.
26
Exhaustive Search Parsing
Example 5.7
• The length of the sentential form grows by at least one symbol in each
round.
29
Exhaustive Search Parsing
Theorem 5.2
• A → λ or A → B,
• where A, B ∈ V.
• Then the exhaustive search parsing method can be made into an algorithm
that, for any w ∈ Σ*, either produces a parsing of w or tells us that no
parsing is possible.
30
Exhaustive Search Parsing
Theorem 5.2
• Proof:
• For each sentential form, consider both its length and the number of terminal symbols.
• Each step in the derivation increases at least one of these.
• Since neither the length of a sentential form nor the number of terminal symbols can exceed | w | ,
• if it exceeds | w | , there is no way to reduce it, cause we don't have A → λ.
• a derivation cannot involve more than 2 | w | rounds, at which time we either have a successful
parsing or w cannot be generated by the grammar.
• Cause in each step either we have one more terminal symbol (at most | w | ), or the length
increased by one (at most | w | times).
31
Exhaustive Search Parsing
Efficiency
• For every context-free grammar there exists an algorithm that parses any w ∈ L(G) in a
3
number of steps proportional to | w | (CYK algorithm).
• e.g.) a LR parser has O(n) time complexity for parsing, but details are beyond this
course.
33
Simple Grammars
Definition 5.4
• A → ax,
• where A ∈ V, a ∈ T, x ∈ V*, and any pair (A, a) occurs at most once in
P.
• Basically it indicates that there is only one production to replace a certain
variable to a certain terminal symbol.
• The grammar
• S → aS | bSS | c
• is an s-grammar.
• The grammar
• S → aS | bSS | aSS | c
• is not an s-grammar.
• The pair (S, a) occurs twice in S → aS and S → aSS.
35
Simple Grammars
• If G is an s-grammar, then any string w in L(G) can be parsed with an effort proportional to | w | .
• Consider the string w = a1a2⋯an, parsing it with exhaustive search method.
• We have only one choice for (S, a1), hence a leftmost derivation must begin with
• S ⇒ a1A1A2⋯Am.
• Next, we replace A1, and again since we have only one choice for A1,
• S ⇒* a1a2B1B2⋯A2⋯Am.
• From these, we can see that each step produces only one terminal symbol and the whole process
must be completed in no more than | w | steps.
36
Ambiguity
Definition 5.5
37
Ambiguity
Example 5.10
• The sentence aabb has the two derivation trees shown in Figure 5.4.
Figure 5.4
38
Ambiguity
Example 5.11
39
Ambiguity
Example 5.11
a + b*c
• The strings (a + b) * c and
a * b + c are in L(G).
• The grammar generates a
restricted subset of arithmetic
expression in PLs.
40
Resolving Ambiguity
41
Resolving Ambiguity
Example 5.12
• E → T | E + T,
• T → F | T * F,
• F → I | (E),
• I → a | b | c.
• The grammar is unambiguous, and equivalent to
the grammar in Example 5.11.
Figure 5.6
• However, it is not easy to show that two context-
free grammars are equivalent in general.
43
Ambiguous and Unambiguous Languages
Definition 5.6
44
Inherently Ambiguous Languages
Example 5.13
n n m n m m
• The language L = {a b c } ∪ {a b c }
• with n and m nonnegative, is an inherently ambiguous context-free language.
• It's easy to show that L is context-free.
• L = L1 ∪ L2, where L1 is generated by
• S1 → S1c | A, A → aAb | λ, and
• L2 is generated by
• S2 → aS2 | B, B → bBc | λ.
• Then we can add one more production S → S1 | S2 to combine them together.
45
Inherently Ambiguous Languages
Example 5.13
• However, there are conflicting requirements on a's and b's in L1, and b's and c's in L2.
• It's impossible to satisfy both with a single set of rules that cover n = m uniquely.
46
Context-Free Grammars and
Programming Languages
47
Context-Free Grammars and PLs
48
Backus Naur Form
49
Context-Free Grammars and PLs
• https://docs.python.org/3/reference/grammar.html
• https://spec.dart.dev/DartLangSpecDraft.pdf
50
Summary