lecture 4 – outline - tau › ~bchor › cm › compute4.pdf · lecture 4 – outline so far we...
TRANSCRIPT
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,
regular languages,regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,
regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,regular expressions,
pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,
context-free languages,context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,
context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,context-free grammars,
pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Lecture 4 – OutlineSo far we saw
finite automata,regular languages,regular expressions,pumping lemma for regular languages.
Today, we introduce stronger machines andlanguages with more expressive power:
pushdown automata,context-free languages,context-free grammars,pumping lemma for context-free languages.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.1
Context-Free GrammarsThis is an example of a context free grammer, G1:
A → 0A1
A → B
B → #
Terminology:
Each line is a substitution rule or production.
Each rule has the form: symbol → string.The left-hand symbol is a variable(usually upper-case).
A string consists of variables and terminals.
One variable is the start variable.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2
Context-Free GrammarsThis is an example of a context free grammer, G1:
A → 0A1
A → B
B → #
Terminology:
Each line is a substitution rule or production.
Each rule has the form: symbol → string.The left-hand symbol is a variable(usually upper-case).
A string consists of variables and terminals.
One variable is the start variable.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2
Context-Free GrammarsThis is an example of a context free grammer, G1:
A → 0A1
A → B
B → #
Terminology:
Each line is a substitution rule or production.
Each rule has the form: symbol → string.The left-hand symbol is a variable(usually upper-case).
A string consists of variables and terminals.
One variable is the start variable.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2
Context-Free GrammarsThis is an example of a context free grammer, G1:
A → 0A1
A → B
B → #
Terminology:
Each line is a substitution rule or production.
Each rule has the form: symbol → string.The left-hand symbol is a variable(usually upper-case).
A string consists of variables and terminals.
One variable is the start variable.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.2
Rules for Generating Strings
Write down the start variable (lhs of top rule).
Pick a variable written down in current string anda derivation that starts with that variable.
Replace that variable with right-hand side of thatderivation.
Repeat until no variables remain.
Return final string (concatenation of terminals).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3
Rules for Generating Strings
Write down the start variable (lhs of top rule).
Pick a variable written down in current string anda derivation that starts with that variable.
Replace that variable with right-hand side of thatderivation.
Repeat until no variables remain.
Return final string (concatenation of terminals).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3
Rules for Generating Strings
Write down the start variable (lhs of top rule).
Pick a variable written down in current string anda derivation that starts with that variable.
Replace that variable with right-hand side of thatderivation.
Repeat until no variables remain.
Return final string (concatenation of terminals).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3
Rules for Generating Strings
Write down the start variable (lhs of top rule).
Pick a variable written down in current string anda derivation that starts with that variable.
Replace that variable with right-hand side of thatderivation.
Repeat until no variables remain.
Return final string (concatenation of terminals).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3
Rules for Generating Strings
Write down the start variable (lhs of top rule).
Pick a variable written down in current string anda derivation that starts with that variable.
Replace that variable with right-hand side of thatderivation.
Repeat until no variables remain.
Return final string (concatenation of terminals).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.3
Example
Grammar G1:
A → 0A1
A → B
B → #
Derivation with G1:
A ⇒ 0A1
⇒ 00A11
⇒ 000A111
⇒ 000B111
⇒ 000#111
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.4
Example
Grammar G1:
A → 0A1
A → B
B → #
Derivation with G1:
A ⇒ 0A1
⇒ 00A11
⇒ 000A111
⇒ 000B111
⇒ 000#111
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.4
A Parse TreeA
A
A
B
#0 10 0 1 1
Question: What strings can be generated in this wayfrom the grammar G1?
Answer: Exactly those of the form 0n#1n (n ≥ 0).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.5
A Parse TreeA
A
A
B
#0 10 0 1 1
Question: What strings can be generated in this wayfrom the grammar G1?
Answer: Exactly those of the form 0n#1n (n ≥ 0).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.5
A Parse TreeA
A
A
B
#0 10 0 1 1
Question: What strings can be generated in this wayfrom the grammar G1?
Answer: Exactly those of the form 0n#1n (n ≥ 0).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.5
Context-Free Languages
The language generated in this way is the language ofthe grammar.
For example, L(G1) is {0n#1n|n ≥ 0}.
Any language generated by a context-free grammar is
called a context-free language.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6
Context-Free Languages
The language generated in this way is the language ofthe grammar.
For example, L(G1) is {0n#1n|n ≥ 0}.
Any language generated by a context-free grammar is
called a context-free language.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6
Context-Free Languages
The language generated in this way is the language ofthe grammar.
For example, L(G1) is {0n#1n|n ≥ 0}.
Any language generated by a context-free grammar is
called a context-free language.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.6
A Useful AbbreviationRules with same variable on left hand side
A → 0A1
A → B
are written as:
A → 0A1 | B
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.7
A Useful AbbreviationRules with same variable on left hand side
A → 0A1
A → B
are written as:
A → 0A1 | B
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.7
English-like Sentences
A grammar G2 to describe a few English sentences:
< SENTENCE > → < NOUN-PHRASE >< VERB >
< NOUN-PHRASE > → < ARTICLE >< NOUN >
< NOUN > → boy | girl | flower
< ARTICLE > → a | the
< VERB > → touches | likes | sees
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.8
Deriving English-like Sentences
A specific derivation in G2:
< SENTENCE > ⇒ < NOUN-PHRASE >< VERB >
⇒ < ARTICLE >< NOUN >< VERB >
⇒ a < NOUN >< VERB >
⇒ a boy < VERB >
⇒ a boy sees
More strings in G2:
a flower sees
the girl touches
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.9
Deriving English-like Sentences
A specific derivation in G2:
< SENTENCE > ⇒ < NOUN-PHRASE >< VERB >
⇒ < ARTICLE >< NOUN >< VERB >
⇒ a < NOUN >< VERB >
⇒ a boy < VERB >
⇒ a boy sees
More strings in G2:
a flower sees
the girl touches
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.9
Derivation and Parse Tree
< SENTENCE > ⇒ < NOUN-PHRASE >< VERB >
⇒ < ARTICLE >< NOUN >< VERB >
⇒ a < NOUN >< VERB >
⇒ a boy < VERB >
⇒ a boy sees
SENTENCE
NOUN-PHRASE VERB
ARTICLE NOUN
a boy seesSlides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.10
Formal DefinitionsA context-free grammar is a 4-tuple (V, Σ, R, S)where
V is a finite set of variables,
Σ is a finite set of terminals,
R is a finite set of rules: each rule is a variableand a finite string of variables and terminals.
S is the start symbol.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11
Formal DefinitionsA context-free grammar is a 4-tuple (V, Σ, R, S)where
V is a finite set of variables,
Σ is a finite set of terminals,
R is a finite set of rules: each rule is a variableand a finite string of variables and terminals.
S is the start symbol.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11
Formal DefinitionsA context-free grammar is a 4-tuple (V, Σ, R, S)where
V is a finite set of variables,
Σ is a finite set of terminals,
R is a finite set of rules: each rule is a variableand a finite string of variables and terminals.
S is the start symbol.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11
Formal DefinitionsA context-free grammar is a 4-tuple (V, Σ, R, S)where
V is a finite set of variables,
Σ is a finite set of terminals,
R is a finite set of rules: each rule is a variableand a finite string of variables and terminals.
S is the start symbol.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.11
Formal Definitions
If u and v are strings of variables and terminals,
and A → w is a rule of the grammar, then
we say uAv yields uwv, written uAv ⇒ uwv.
We write u∗⇒ v if u = v or
u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.
for some sequence u1, u2, . . . , uk.
Definition: The language of the grammar is{
w | S ∗⇒ w}
.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12
Formal Definitions
If u and v are strings of variables and terminals,
and A → w is a rule of the grammar, then
we say uAv yields uwv, written uAv ⇒ uwv.
We write u∗⇒ v if u = v or
u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.
for some sequence u1, u2, . . . , uk.
Definition: The language of the grammar is{
w | S ∗⇒ w}
.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12
Formal Definitions
If u and v are strings of variables and terminals,
and A → w is a rule of the grammar, then
we say uAv yields uwv, written uAv ⇒ uwv.
We write u∗⇒ v if u = v or
u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.
for some sequence u1, u2, . . . , uk.
Definition: The language of the grammar is{
w | S ∗⇒ w}
.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12
Formal Definitions
If u and v are strings of variables and terminals,
and A → w is a rule of the grammar, then
we say uAv yields uwv, written uAv ⇒ uwv.
We write u∗⇒ v if u = v or
u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.
for some sequence u1, u2, . . . , uk.
Definition: The language of the grammar is{
w | S ∗⇒ w}
.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12
Formal Definitions
If u and v are strings of variables and terminals,
and A → w is a rule of the grammar, then
we say uAv yields uwv, written uAv ⇒ uwv.
We write u∗⇒ v if u = v or
u ⇒ u1 ⇒ . . . ⇒ uk ⇒ v.
for some sequence u1, u2, . . . , uk.
Definition: The language of the grammar is{
w | S ∗⇒ w}
.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.12
Example
Consider G4 = (V, {a, b} , R, S).
R (Rules): S → aSb | SS | ε .
Some words in the language: aabb, aababb.
Q.: But what is this language?
Hint: Think of parentheses.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13
Example
Consider G4 = (V, {a, b} , R, S).
R (Rules): S → aSb | SS | ε .
Some words in the language: aabb, aababb.
Q.: But what is this language?
Hint: Think of parentheses.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13
Example
Consider G4 = (V, {a, b} , R, S).
R (Rules): S → aSb | SS | ε .
Some words in the language: aabb, aababb.
Q.: But what is this language?
Hint: Think of parentheses.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13
Example
Consider G4 = (V, {a, b} , R, S).
R (Rules): S → aSb | SS | ε .
Some words in the language: aabb, aababb.
Q.: But what is this language?
Hint: Think of parentheses.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.13
Arythmetic Example
Consider (V, Σ, R,E) where
V = {E, T, F}Σ = {a, +,×, (, )}
Rules:E → E + T | TT → T × F | FF → (E) | a
Strings generated by the grammer:a + a× a and (a + a)× a.
What is the language of this grammer?Hint: arithmetic expressions.
E = expression, T = term, F = factor.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.14
Arythmetic Example
Consider (V, Σ, R,E) where
V = {E, T, F}Σ = {a, +,×, (, )}
Rules:E → E + T | TT → T × F | FF → (E) | a
Strings generated by the grammer:a + a× a and (a + a)× a.
What is the language of this grammer?Hint: arithmetic expressions.
E = expression, T = term, F = factor.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.14
Arythmetic Example
Consider (V, Σ, R,E) where
V = {E, T, F}Σ = {a, +,×, (, )}
Rules:E → E + T | TT → T × F | FF → (E) | a
Strings generated by the grammer:a + a× a and (a + a)× a.
What is the language of this grammer?Hint: arithmetic expressions.
E = expression, T = term, F = factor.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.14
Parse Tree for a + a× a
E → E + T |TT → T × F | FF → (E) | a
aXa+a
FFF
T T
TE
E
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.15
Parse Tree for a + a× a
E → E + T |TT → T × F | FF → (E) | a
aXa+a
FFF
T T
TE
E
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.15
Parse Tree for (a + a)× a
E → E + T | TT → T × F | FF → (E) | a
( a + aX)a
F F F
T T
E
E
F
T
T
E
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.16
Parse Tree for (a + a)× a
E → E + T | TT → T × F | FF → (E) | a
( a + aX)a
F F F
T T
E
E
F
T
T
E
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.16
Designing Context-Free Grammars
No recipe in general, but few rules-of-thumb
If CFG is the union of several CFGs, renamevariables (not terminals) so they are disjoint, andadd new rule S → S1 | S2 | . . . | Si.
To construct CFG for a regular language,“follow” a DFA for the language. For initial stateq0, make R0 the start variable. For state transitionδ(qi, a) = qj add rule Ri → aRj to grammer. Foreach final state qf , add rule Rf → ε to grammer.
For languages with linked substrings (like{0n#1n|n ≥ 0} ), a rule of form R → uRv maybe helpful, forcing desired relation betweensubstrings.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17
Designing Context-Free Grammars
No recipe in general, but few rules-of-thumb
If CFG is the union of several CFGs, renamevariables (not terminals) so they are disjoint, andadd new rule S → S1 | S2 | . . . | Si.
To construct CFG for a regular language,“follow” a DFA for the language. For initial stateq0, make R0 the start variable. For state transitionδ(qi, a) = qj add rule Ri → aRj to grammer. Foreach final state qf , add rule Rf → ε to grammer.
For languages with linked substrings (like{0n#1n|n ≥ 0} ), a rule of form R → uRv maybe helpful, forcing desired relation betweensubstrings.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17
Designing Context-Free Grammars
No recipe in general, but few rules-of-thumb
If CFG is the union of several CFGs, renamevariables (not terminals) so they are disjoint, andadd new rule S → S1 | S2 | . . . | Si.
To construct CFG for a regular language,“follow” a DFA for the language. For initial stateq0, make R0 the start variable. For state transitionδ(qi, a) = qj add rule Ri → aRj to grammer. Foreach final state qf , add rule Rf → ε to grammer.
For languages with linked substrings (like{0n#1n|n ≥ 0} ), a rule of form R → uRv maybe helpful, forcing desired relation betweensubstrings.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.17
Closure Properties
Regular languages are closed under
unionconcatenationstar
Context-Free Languages are closed underunion : S → S1 | S2
concatenation S → S1S2
star S → ε | SS
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18
Closure Properties
Regular languages are closed underunion
concatenationstar
Context-Free Languages are closed underunion : S → S1 | S2
concatenation S → S1S2
star S → ε | SS
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18
Closure Properties
Regular languages are closed underunionconcatenation
star
Context-Free Languages are closed underunion : S → S1 | S2
concatenation S → S1S2
star S → ε | SS
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18
Closure Properties
Regular languages are closed underunionconcatenationstar
Context-Free Languages are closed underunion : S → S1 | S2
concatenation S → S1S2
star S → ε | SS
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18
Closure Properties
Regular languages are closed underunionconcatenationstar
Context-Free Languages are closed under
union : S → S1 | S2
concatenation S → S1S2
star S → ε | SS
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18
Closure Properties
Regular languages are closed underunionconcatenationstar
Context-Free Languages are closed underunion : S → S1 | S2
concatenation S → S1S2
star S → ε | SS
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18
Closure Properties
Regular languages are closed underunionconcatenationstar
Context-Free Languages are closed underunion : S → S1 | S2
concatenation S → S1S2
star S → ε | SS
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18
Closure Properties
Regular languages are closed underunionconcatenationstar
Context-Free Languages are closed underunion : S → S1 | S2
concatenation S → S1S2
star S → ε | SS
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.18
More Closure Properties
Regular languages are also closed under
complement (reverse accept/non-accept statesof DFA)
intersection(
L1 ∩ L2 = L1 ∪ L2
)
.
What about complement and intersection ofcontext-free languages?
Not clear . . .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19
More Closure Properties
Regular languages are also closed undercomplement (reverse accept/non-accept statesof DFA)
intersection(
L1 ∩ L2 = L1 ∪ L2
)
.
What about complement and intersection ofcontext-free languages?
Not clear . . .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19
More Closure Properties
Regular languages are also closed undercomplement (reverse accept/non-accept statesof DFA)
intersection(
L1 ∩ L2 = L1 ∪ L2
)
.
What about complement and intersection ofcontext-free languages?
Not clear . . .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19
More Closure Properties
Regular languages are also closed undercomplement (reverse accept/non-accept statesof DFA)
intersection(
L1 ∩ L2 = L1 ∪ L2
)
.
What about complement and intersection ofcontext-free languages?
Not clear . . .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19
More Closure Properties
Regular languages are also closed undercomplement (reverse accept/non-accept statesof DFA)
intersection(
L1 ∩ L2 = L1 ∪ L2
)
.
What about complement and intersection ofcontext-free languages?
Not clear . . .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.19
Ambiguity
Grammar: E → E+E | E×E | (E) | a
aXa+a
EEE
E
E
aXa+a
EEE
E
E
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.20
Ambiguity
We say that a string w is derived ambiguously fromgrammer G if w has two or more parse trees thatgenerate it from G.
Ambiguity is usually not only a syntactic notion butalso a semantic one, implying multiple meanings forthe same string.
It is sometime possible to eliminate ambiguity byfinding a different context free grammer generatingthe same language. This is true for the grammerabove, which can be replaced by unambiguousgrammer from slide (14).
Some languages (e.g. {1i2j3k | i = j or j = k} are in-
herentrly ambigous.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21
Ambiguity
We say that a string w is derived ambiguously fromgrammer G if w has two or more parse trees thatgenerate it from G.
Ambiguity is usually not only a syntactic notion butalso a semantic one, implying multiple meanings forthe same string.
It is sometime possible to eliminate ambiguity byfinding a different context free grammer generatingthe same language. This is true for the grammerabove, which can be replaced by unambiguousgrammer from slide (14).
Some languages (e.g. {1i2j3k | i = j or j = k} are in-
herentrly ambigous.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21
Ambiguity
We say that a string w is derived ambiguously fromgrammer G if w has two or more parse trees thatgenerate it from G.
Ambiguity is usually not only a syntactic notion butalso a semantic one, implying multiple meanings forthe same string.
It is sometime possible to eliminate ambiguity byfinding a different context free grammer generatingthe same language. This is true for the grammerabove, which can be replaced by unambiguousgrammer from slide (14).
Some languages (e.g. {1i2j3k | i = j or j = k} are in-
herentrly ambigous.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21
Ambiguity
We say that a string w is derived ambiguously fromgrammer G if w has two or more parse trees thatgenerate it from G.
Ambiguity is usually not only a syntactic notion butalso a semantic one, implying multiple meanings forthe same string.
It is sometime possible to eliminate ambiguity byfinding a different context free grammer generatingthe same language. This is true for the grammerabove, which can be replaced by unambiguousgrammer from slide (14).
Some languages (e.g. {1i2j3k | i = j or j = k} are in-
herentrly ambigous.Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.21
Chomsky Normal Form
A simplified, canonical form of context freegrammers.Every rule has the form
A → BC
A → a
S → ε
where S is the start symbol, A, B and C are any vari-
able, except B and C not the start symbol.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.22
TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:
Add new start symbol S0.
Eliminate all ε rules of the form A → ε.
Eliminate all “unit” rules of the form A → B.
Patch up rules so that grammar generates thesame language.
Convert remaining long rules to proper form.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23
TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:
Add new start symbol S0.
Eliminate all ε rules of the form A → ε.
Eliminate all “unit” rules of the form A → B.
Patch up rules so that grammar generates thesame language.
Convert remaining long rules to proper form.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23
TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:
Add new start symbol S0.
Eliminate all ε rules of the form A → ε.
Eliminate all “unit” rules of the form A → B.
Patch up rules so that grammar generates thesame language.
Convert remaining long rules to proper form.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23
TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:
Add new start symbol S0.
Eliminate all ε rules of the form A → ε.
Eliminate all “unit” rules of the form A → B.
Patch up rules so that grammar generates thesame language.
Convert remaining long rules to proper form.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23
TheoremTheorem: Any context-free language is generated bya context-free grammar in Chomsky normal form.Basic idea:
Add new start symbol S0.
Eliminate all ε rules of the form A → ε.
Eliminate all “unit” rules of the form A → B.
Patch up rules so that grammar generates thesame language.
Convert remaining long rules to proper form.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.23
ProofAdd new start symbol S0 and rule S0 → S.
Guarantees that new start symbol does not appear on
right-hand-side of a rule.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.24
ProofEliminating ε rules.
Repeat:
remove some A → ε.
for each R → uAv, add rule R → uv.
and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.
for R → A add R → ε, except if R → ε hasalready been removed.
until all ε-rules not involving the start variable have
been removed.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25
ProofEliminating ε rules.
Repeat:
remove some A → ε.
for each R → uAv, add rule R → uv.
and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.
for R → A add R → ε, except if R → ε hasalready been removed.
until all ε-rules not involving the start variable have
been removed.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25
ProofEliminating ε rules.
Repeat:
remove some A → ε.
for each R → uAv, add rule R → uv.
and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.
for R → A add R → ε, except if R → ε hasalready been removed.
until all ε-rules not involving the start variable have
been removed.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25
ProofEliminating ε rules.
Repeat:
remove some A → ε.
for each R → uAv, add rule R → uv.
and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.
for R → A add R → ε, except if R → ε hasalready been removed.
until all ε-rules not involving the start variable have
been removed.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25
ProofEliminating ε rules.
Repeat:
remove some A → ε.
for each R → uAv, add rule R → uv.
and so on: for R → uAvAw add R → uvAw,R → uAvw, and R → uvw.
for R → A add R → ε, except if R → ε hasalready been removed.
until all ε-rules not involving the start variable have
been removed.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.25
ProofEliminate unit rules.
Repeat:
remove some A → B.
for each B → u, add rule A → u, unless this ispreviously removed unit rule. (u is a string ofvariables and terminals.)
until all unit rules have been removed.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26
ProofEliminate unit rules.
Repeat:
remove some A → B.
for each B → u, add rule A → u, unless this ispreviously removed unit rule. (u is a string ofvariables and terminals.)
until all unit rules have been removed.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26
ProofEliminate unit rules.
Repeat:
remove some A → B.
for each B → u, add rule A → u, unless this ispreviously removed unit rule. (u is a string ofvariables and terminals.)
until all unit rules have been removed.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.26
ProofFinally, convert long rules.To replace each A → u1u2 . . . uk (for k ≥ 3),introduce new non-terminals
N1, N2, . . . , Nk−1
and rules
A → u1N1
N1 → u2N2
...Nk−3 → uk−2Nk−2
Nk−2 → uk−1uk ♠
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.27
ProofFinally, convert long rules.To replace each A → u1u2 . . . uk (for k ≥ 3),introduce new non-terminals
N1, N2, . . . , Nk−1
and rules
A → u1N1
N1 → u2N2
...Nk−3 → uk−2Nk−2
Nk−2 → uk−1uk ♠
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.27
Conversion Example
Initial Grammar:
S → ASA | aB
A → B | SB → b | ε
(1) Add new start state:
S0 → S
S → ASA | aB
A → B | SB → b | ε
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.28
Conversion Example (2)
S0 → S
S → ASA | aB
A → B | SB → b | ε
(2) Remove ε-rule B → ε:
S0 → S
S → ASA | aB | aA → B | S | εB → b | ε
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.29
Conversion Example (3)
S0 → S
S → ASA | aB | aA → B | S | εB → b
(3) Remove ε-rule A → ε:
S0 → S
S → ASA | aB | a | AS | SA | SA → B | S | εB → b
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.30
Conversion Example (4)
S0 → S
S → ASA | aB | a | AS | SA | SA → B | SB → b
(4) Remove unit rule S → S
S0 → S
S → ASA | aB | a | AS | SA | SA → B | SB → b
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.31
Conversion Example (5)
S0 → S
S → ASA | aB | a | AS | SA
A → B | SB → b
(5) Remove unit rule S0 → S:
S0 → S | ASA | aB | a | AS | SA
S → ASA | aB | a | AS | SA
A → B | SB → b
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.32
Conversion Example (6)
S0 → ASA | aB | a | AS | SA
S → ASA | aB | a | AS | SA
A → B | SB → b
(6) Remove unit rule A → B:
S0 → ASA | aB | a | AS | SA
S → ASA | aB | a | AS | SA
A → B | S | bB → b
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.33
Conversion Example (7)
S0 → ASA | aB | a | AS | SA
S → ASA | aB | a | AS | SA
A → S | bB → b
Remove unit rule A → S:
S0 → ASA | aB | a | AS | SA
S → ASA | aB | a | AS | SA
A → S | b | ASA | aB | a | AS | SA
B → bSlides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.34
Conversion Example (8)
S0 → ASA | aB | a | AS | SA
S → ASA | aB | a | AS | SA
A → b | ASA | aB | a | AS | SA
B → b
(8) Final simplification – treat long rules:
S0 → AA1 | UB | a | SA | AS
S → AA1 | UB | a | SA | AS
A → b | AA1 | UB | a | SA | AS
A1 → SA
U → a
B → b√
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University. – p.35