context-free grammars and languagesotternes/comp455/5-context-free-grammars.pdfformal definition of...
TRANSCRIPT
Context-free Grammars and Languages
COMP 455 β 002, Spring 2019
Jim Anderson (modified by Nathan Otterness) 1
Context-free Grammars
Context-free grammars provide another way to specify languages.
Jim Anderson (modified by Nathan Otterness) 2
Example: A context-free grammar for mathematical expressions:β’ πΈ β πΈ + πΈβ’ πΈ β πΈ β πΈβ’ πΈ β πΈβ’ πΈ β π’
Show that a string is in the language using a derivation:β’ πΈ β πΈ + πΈβ’ β πΈ + πΈβ’ β πΈ + πΈ β πΈβ’ β π’ + πΈ β πΈβ’ β π’ + π’ β πΈβ’ β π’ + π’ β π’
Formal Definition of CFGs
A context-free grammar (CFG) is denoted using a 4-tuple πΊ = π, π, π, π , where:
βπ is a finite set of variables
βπ is a finite set of terminals
βπ is a finite set of productions of the form π£πππππππ β π π‘ππππ
βπ is the start symbol. (π is a variable in π)
Jim Anderson (modified by Nathan Otterness) 3
βheadβ
βbodyβ
Formal CFG Definition: Example
To define our example grammar using this tuple notation:
π = πΈ
π = +,β, , , π’
π is the set of rules defined previously:
π = πΈ
Jim Anderson (modified by Nathan Otterness) 4
πΈ β πΈ + πΈπΈ β πΈ β πΈπΈ β πΈπΈ β π’
More CFG Examples
In our discussion of the Pumping Lemma for Regular Languages, we discussed the following language:
πΏ = π₯ | π₯ = π₯π β§ π₯ β π + π β
Can we show this language is context-free?
Yes:
π = π
π = 0, 1
π = π
Jim Anderson (modified by Nathan Otterness) 5
π = {β’ π β 0π 0 ,β’ π β 1π 1 ,β’ π β 0 ,β’ π β 1 ,β’ π β π ,}
More CFG Examples
What about the language πΏ consisting of all strings containing an equal number of 0s and 1s?
π = π
π = 0, 1
π = π
π =
βπ β 0π 1π
βπ β 1π 0π
βπ β πJim Anderson (modified by Nathan Otterness) 6
A Historical Note
We are talking about context-free languages, but what about a language that is not context-free?
These languages exist and are called context-sensitive.
βContext-sensitive languages allow production rules with strings, e.g. 1π0 β 110.
Context-sensitive languages were used in the study of natural languages, but ended up with few practical applications.
Jim Anderson (modified by Nathan Otterness) 7
Derivations
We will be following the notational conventions from page 178 of the textbook (Section 5.1.4)
We say that string πΌ1 directly derives πΌ2 if and only if:
βπΌ1 = πΌπ΄πΎ,
βπΌ2 = πΌπ½πΎ, and
βπ΄ β π½ is a production rule in π.
This can be denoted πΌπ΄πΎβπΊπΌπ½πΎ
Jim Anderson (modified by Nathan Otterness) 8
Derivations
We will be following the notational conventions from page 178 of the textbook (Section 5.1.4)
We say that string πΌ1 directly derives πΌ2 if and only if:
βπΌ1 = πΌπ΄πΎ,
βπΌ2 = ππ½πΎ, and
βπ΄ β π½ is a production rule in π.
This can be denoted πΌπ΄πΎβπΊπΌπ½πΎ
Jim Anderson (modified by Nathan Otterness) 9
Lowercase Greek letters: strings (including variables and terminals)
Uppercase letters near the start of the alphabet: variables
A derivation using a single invocation of a production rule in the grammar πΊ. (We can omit the πΊ if the grammar weβre talking about is obvious.)
Derivations (continued)
πΌ1βπΊ
βπΌπ means πΌ1 derives πΌπ (in 0 or more steps).
β i.e., πΌ1 β πΌ2, πΌ2 β πΌ3, β¦, πΌπβ1 β πΌπ
πΌβππ½ means πΌ derives π½ in exactly π steps.
πΌ is a sentential form if and only if π ββπΌ.
Jim Anderson (modified by Nathan Otterness) 10
Leftmost and Rightmost Derivations
It can be useful to restrict a derivation to only replace the leftmost variables in a string. This is called a leftmost derivation.
βSteps in a leftmost derivation are indicated using ππ
for a single step or ππ
βfor many steps.
A string encountered during a leftmost derivation is called a left sentential form.
β i.e., πΌ is a left-sentential form if and only if πππ
βπΌ.
Jim Anderson (modified by Nathan Otterness) 11
Leftmost and Rightmost Derivations
Similarly to a leftmost derivation, a rightmost derivation only replaces the rightmost variable in each step.
βSteps in a rightmost derivation are indicated
using ππ
or ππ
β.
A right-sentential form is a string encountered during a rightmost derivation from the start symbol.
Jim Anderson (modified by Nathan Otterness) 12
Leftmost and Rightmost Derivations
Example using the grammar from before:
Jim Anderson (modified by Nathan Otterness) 13
πΈ β πΈ + πΈπΈ β πΈ β πΈπΈ β πΈπΈ β π’
First example Leftmost Rightmost
πΈ β πΈ + πΈ πΈ β πΈ + πΈ πΈ β πΈ + πΈ
β πΈ + πΈ β πΈ + πΈ β πΈ + πΈ β πΈ
β πΈ + πΈ β πΈ β π’ + πΈ β πΈ + πΈ β π’
β π’ + πΈ β πΈ β π’ + πΈ β πΈ β πΈ + π’ β π’
β π’ + π’ β πΈ β π’ + π’ β πΈ β πΈ + π’ β π’
β π’ + π’ β π’ β π’ + π’ β π’ β π’ + π’ β π’
The Language of a CFG
For a CFG πΊ, πΏ πΊ β‘ π€ | π€ β πβ and π ββ
πΊπ€
πΏ is a context-free language if and only if πΏ = πΏ πΊfor some CFG G.
Grammars πΊ1 and πΊ2 are equivalent if and only if πΏ πΊ1 = πΏ πΊ2 .
Jim Anderson (modified by Nathan Otterness) 14
π€ consists only of terminal symbols
Showing Membership in a CFG
Demonstrating that a string is in the language of a CFG can be accomplished two ways:
Top-down: Give a derivation of the string. i.e., Begin with the start symbol and use production rules to create the string.
Bottom-up: Start with the string, and try to apply production rules βbackwardsβ to end up with a single start symbol.
We will now consider a technique called recursive inference, which is basically a bottom-up approach.
Jim Anderson (modified by Nathan Otterness) 15
Recursive Inference
Define a language πΏ π for each variable π. πΏ πcontains all strings that can be derived from π.
βIf π β π1π2β¦ππ is a production rule, then all strings π₯1π₯2β¦π₯π are in πΏ π , where:
βIf ππ is a terminal symbol, then π₯π = ππ,
βIf ππ is a variable, then π₯π is in πΏ ππ .
Productions with only terminal symbols in the body give us the base case. (So, we basically end up applying productions backwards.)
A string π₯ is in πΏ πΊ if and only if it is in πΏ π .
Jim Anderson (modified by Nathan Otterness) 16
Strings that can be derived from the start symbol π.
Recursive Inference
The goal of recursive inference is to look at successively larger substrings of some string π₯ to determine if π₯ is in πΏ π .
Jim Anderson (modified by Nathan Otterness) 17
Recursive Inference: Example
(This example is from Figure 5.3 in the book.)
We want to use recursive inference to show that π β π + π00 is in πΏ πΊ .
Jim Anderson (modified by Nathan Otterness) 18
Production rules:1. πΈ β πΌ2. πΈ β πΈ + πΈ3. πΈ β πΈ β πΈ4. πΈ β πΈ5. πΌ β π6. πΌ β π7. πΌ β πΌπ8. πΌ β πΌπ9. πΌ β πΌ010. πΌ β πΌ1
Grammar πΊ for simple expressions:β’ π = πΈ, πΌβ’ π = {
}π, π, 0, π, +,β
, ,β’ πΈ is the start
symbol
i. π β πΏ πΌ , by Production rule 5ii. π β πΏ πΌ , by Production rule 6iii. π0 β πΏ πΌ , by Production rule 9 and iiiv. π00 β πΏ πΌ , by Production rule 9 and iiiv. π β πΏ πΈ , by Production rule 1 and ivi. π00 β πΏ πΈ , by Production rule 1 and ivvii.π + π00 β πΏ πΈ , by Production rule 2 and v and viviii. π + π00 β πΏ πΈ , by Production rule 4 and viiix. π β π + π00 β πΏ πΈ , by Production rule 3 and v and viii.
Parse Trees
Parse trees show how symbols of a string are grouped into substrings, and the variables and productions used.
In general, the root is π, internal nodes are variables, and leaves are variables or terminals.
Jim Anderson (modified by Nathan Otterness) 19
π΄
π1 ππβ¦
If , then π΄ β π1β¦ππ
Parse Tree Example
Example grammar:
π β ππ΄π | π
π΄ β πππ΄ | ππ | ππ
Jim Anderson (modified by Nathan Otterness) 20
π
π π΄ π
π π π΄
π
Note: new notation
An example parse tree
A Parse Treeβs βYieldβ
Example grammar, again: π β ππ΄π | π, π΄ β πππ΄ | ππ | ππ.
The yield of a parse tree is the string obtained from reading its leaves left-to-right.
Jim Anderson (modified by Nathan Otterness) 21
The yield of this tree is ππππ΄π.
Note that the yield of a parse tree is a sentential form.
π
π π΄ π
π π π΄
π
A Parse Treeβs βYieldβ
Example grammar, again: π β ππ΄π | π, π΄ β πππ΄ | ππ | ππ.
The yield of a parse tree is the string obtained from reading its leaves left-to-right.
Jim Anderson (modified by Nathan Otterness) 22
The yield of this tree is ππππ΄π.
Note that the yield of a parse tree is a sentential form.
π
π π΄ π
π π π΄
π
ππππ΄π
Inference, Derivation, and Parse Trees
We will show that all of these are equivalent ways for showing that a string is in a CFL. Specifically, we show:
Jim Anderson (modified by Nathan Otterness) 23
Theorem 5.12
Theorem 5.14
Theorem 5.18
Leftmost Derivation
Derivation
Rightmost Derivation
Parse Tree
Recursive Inference
From Recursive Inference to Parse Tree
Theorem 5.12: Let πΊ = π, π, π, π be a CFG. Ifrecursive inference tells us that string π€ β πβ is in the language of variable π΄ β π, then a parse tree exists with root π΄ and yield π€.
We will prove this by induction on the number of steps in the recursive inference.
Base case: One step. This means that there is a production rule π΄ β π€. The tree for this is:
Jim Anderson (modified by Nathan Otterness) 24
π΄
π₯1 π₯2 β¦ π₯π
Where π€ = π₯1π₯2β¦π₯π.
From Recursive Inference to Parse Tree
Inductive step: Assume that the last inference step looked at the production π΄ β π1π2β¦ππ, and previous inference steps verified that π₯π β πΏ ππ , for each π₯π in π€ = π₯1π₯2β¦π₯π. The tree for this is:
Jim Anderson (modified by Nathan Otterness) 25
π΄
π1 π2 β¦ ππ
π₯1 π₯2 β¦ π₯π
π€
The tree from π΄ to π1π2β¦ππ.
The inductive hypothesis lets us assume we already have trees yielding the terminal strings.
From Parse Tree to Derivation
Theorem 5.14: Let πΊ = π, π, π, π be a CFG, and suppose there is a parse tree with a root of variable π΄with yield π€ β πβ. Then there is a leftmost derivation
π΄ππ
βπ€ in πΊ.
We will prove this by induction on tree height.
Base case: The treeβs height is one. The tree looks like this:
Jim Anderson (modified by Nathan Otterness) 26
So, there must be a production π΄ β π1π2β¦ππin πΊ, where π€ = π1π2β¦ππ.
π΄
π1 π2 β¦ ππ
From Parse Tree to Derivation
Inductive step:
The treeβs height exceeds 1, so the tree looks like this:
Note that π΄ may produce some terminal strings (like π₯2 and π₯π) and other strings containing variables (like π1 and ππβ1).
Jim Anderson (modified by Nathan Otterness) 27
π₯1 π₯πβ1
π€
π΄
π1 π2 β¦ ππβ1 ππ= π₯2 = π₯π
From Parse Tree to Derivation
Inductive step:
The treeβs height exceeds 1, so the tree looks like this:
Note that π΄ may produce some terminal strings (like π₯2 and π₯π) and other strings containing variables (like π1 and ππβ1).
Jim Anderson (modified by Nathan Otterness) 28
π₯1 π₯πβ1
π€
π΄
π1 π2 β¦ ππβ1 ππ= π₯2 = π₯π
From Parse Tree to Derivation
Inductive step (continued):
By the inductive hypothesis,
π1ππ
βπ₯1, ππβ1
ππ
βπ₯πβ1, etc.
Trivially, π2ππ
βπ₯2, ππ
ππ
βπ₯π, etc.,
because they are terminals only.
Since π΄ β π1π2β¦ππβ1ππ, and π€ = π₯1π₯2β¦π₯πβ1π₯π, we know that
π΄ππ
βπ€.
Jim Anderson (modified by Nathan Otterness) 29
π₯1 π₯πβ1
π€
π΄
π1 π2 β¦ ππβ1 ππ= π₯2 = π₯π
Notes on Derivations From Parse Trees
The leftmost derivation corresponding to a parse tree will be unique.
We can prove the same conversion is possible for rightmost derivations.
βSuch a rightmost derivation will also be unique.
Example:
Jim Anderson (modified by Nathan Otterness) 30
π
π π΄ π
ππ π΄ π
π π π
Leftmost derivation: π β ππ΄π βππππ΄π β ππππ΄π β ππππππ β ππππππ.
Rightmost derivation: π β ππ΄π βππ΄π β ππππ΄π β ππππππ β ππππππ.
From Derivation to Recursive Inference
Theorem 5.18: Let πΊ = π, π, π, π be a CFG, π€ β πβ, and
π΄ β π. If a derivation π΄ ββπ€ exists in grammar πΊ, then
π€ β πΏ π΄ can be inferred via recursive inference.
We will prove this by induction on the length of the derivation.
Base case: The derivation is one step. This means that π΄ β π€ is a production, so clearly π€ β πΏ π΄ can be inferred.
Jim Anderson (modified by Nathan Otterness) 31
From Derivation to Recursive Inference
Inductive step: There is more than one step in the derivation. We can write the derivation as
π΄ β π1π2β¦ππ ββπ₯1π₯2β¦π₯π = π€
By the inductive hypothesis, we can infer that π₯π β πΏ ππfor every π. Next, since π΄ β π1π2β¦ππ is clearly a production, we can infer that π€ β πΏ π΄ .
Jim Anderson (modified by Nathan Otterness) 32
Ambiguity
A grammar is ambiguous if some word in it has multiple parse trees.
βRecall: This is equivalent to saying that some word has more than one leftmost or rightmost derivation.
Ambiguity is important to know about, because parsers (i.e. for a programming language compiler) need to determine a programβs structure from source code. This is complicated if multiple parse trees are possible.
Jim Anderson (modified by Nathan Otterness) 33
Ambiguous Grammar: Example
πΈ β πΈ + πΈ πΈ β πΈ πΈ βπΈ π’π
Jim Anderson (modified by Nathan Otterness) 34
πΈ β βπΈβ βπΈ + πΈβ βπ’π + πΈβ βπ’π + π’π
πΈ
πΈ + πΈ
β πΈ π’π
π’π
πΈ
πΈ + πΈ
π’π
β πΈ
π’π
πΈ β πΈ + πΈβ βπΈ + πΈβ βπ’π + πΈβ βπ’π + π’π
Resolving Ambiguity
πΈ β πΈ + πΈ πΈ β πΈ πΈ βπΈ π’π
Ambiguity in this grammar is caused by the lack of operator precedence.
This can be resolved by introducing more variables.
βFor example, πΈ β πΈ + πΈ βπΈ π’π, the part of our grammar causing the ambiguity, can be made unambiguous by adding a variable πΉ:πΈ β πΉ + πΉ, πΉ β βπΈ | π’π.
Section 5.4 of the book discusses this in more depth.
Jim Anderson (modified by Nathan Otterness) 35
Inherent Ambiguity
A context-free language for which all possible CFGs are ambiguous is called inherently ambiguous.
One example (from the book) is: πΏ =ππππππππ | π, π β₯ 1 βͺ ππππππππ | π, π β₯ 1 .
Proving that languages are inherently ambiguous can be quite difficult.
These languages are encountered quite rarely, so this has little practical impact.
Jim Anderson (modified by Nathan Otterness) 36