CFG CFG

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CFG

Ambiguity
• Consider the CFG ({S},{0,1},R,S),
where the rules of R are
S→0|1|S+S|S*S
• Derive the string
g0*1+1
Ambiguity
S→0|1|S+S|S*S
• 0*1+1

S S

S * S S + S

0 S + S S * S 1

1 1 0 1

Different
D ff r nt par
parse tr
trees!!
Leftmost derivation & ambiguity
• A derivation of a string
g w in a
grammar G is a leftmost derivation if
everyy step
p of
f the derivation replaced
p
the leftmost variable
• A string is derived ambiguously in
CFG G if it has two or more different
leftmost derivations
• The grammar G is ambiguous if it
generates
t some string
t i ambiguously
bi l
– Some grammars are inherently
ambiguous
bi
Chomsky normal form
• Method of simplifying
p y g a CFG
Definition: A context-free grammar is in
Chomsky normal form if every rule is of
one of the following forms
A → BC
A→a
where a is any terminal and A is any
variable, and B, and C are any variables or
terminals other than the start variable
the rule S → ε is permitted,
where
h S is
i the
th start
t t variable
i bl
Theorem: If G is in CNF, w ∈ L(G) and |w| > 0,
then any derivation of w in G has length 2|w| - 1
Proof (by induction on |w|):
Base Case: If |w| = 1, then any derivation of
w must have length 1 (S → a)
Inductive Step: Assume true for any string
of length at most k ≥ 1, and let |w| = k+1
Since |w| > 1,
1 derivation starts with S → AB
y where A ⇒* x, ||x|| > 0 and B ⇒* y
So w = xy y, |y| > 0
By the inductive hypothesis, the length of
any derivation of w must be
1 + (2|x| - 1) + (2|y| - 1) = 2(|x| + |y|) - 1
CFG’ss and Chomsky normal form
CFG
Theorem: Any y context-free language
g g is
generated by a context-free
grammar
g mm in Chomskym y normalm form.
f m.
Proof idea: Convert any CFG to one in
Chomsky normal form by removing
or replacing all rules in the wrong
form
1. Add a new start symbol
2
2. Eliminate ε rules of the form A → ε
3. Eliminate unit rules of the form A → B
4
4. Convert remaining rules into proper form
Convert a CFG to Chomsky normal form
1. Add a new start symbol
- Create the following new rule
S0 → S
where S is the start symbol and S0
is not used in the CFG
Convert a CFG to Chomsky normal form
2. Eliminate all ε rules A → ε, where A
is not the start variable
- For
F each h rule
l with
ith an occurrence of
f
A on the right-hand side, add a new
rule with the A deleted
R → uAv becomes R → uAv | uv
R → uAvAw becomes R → uAvAw | uvAw | uAvw | uvw
- If we have R → A,
A replace it with
R → ε unless we had already
removed
m dR→ε
Convert a CFG to Chomsky normal form
3. Eliminate all unit rules of the form
A→B
- For
F each h rule
l B → u, adddd a new rule
l
A → u, where u is a string of
terminals and variables, unless this
rule had alreadyy been removed
m
- Repeat until all unit rules have been
replaced
Convert a CFG to Chomsky normal form
4. Convert remaining rules into proper
form
- What’s
Wh t’ left?
l ft?
- Replace
p each rule A → u1u2…uk,
where k ≥ 3 and ui is a variable or a
terminal with k
k-11 rules
A → u1A1 A1 → u2A2 … Ak-2 → uk-1uk
Example
S → S1 | S2
S1 → S1b | Ab | ε
A → aAb | ab
S2 → S2a | Ba | ε
B → bBa | ba

Step 1: Add a new start symbol


Example
S0 → S
S → S1 | S2
S1 → S1b | Ab
A → aAb | ab | ε
S2 → S2a | Ba
B → bBa | ba | ε

Step 2: Eliminate ε rules


Example
S0 → S
S → S1 | S2
S1 → S1b | Ab | b
A → aAb | ab
S2 → S2a | Ba | a
B → bBa | ba

Step 3: Eliminate all unit rules


Example
S0 → S1b | Ab | b | S2a | Ba | a
S → S1b | Ab | b | S2a | Ba | a
S1 → S1b | Ab | b
A → aAb | ab
S2 → S2a | Ba | a
B → bBa | ba

Step 4: Convert remaining rules to


proper form
f
Example
S0 → S1b | Ab | b | S2a | Ba | a
S → S1b | Ab | b | S2a | Ba | a
S1 → S1b | Ab | b
A → aA1 | ab
A1 → Ab
S2 → S2a | Ba | a
B → bB1 | ba
B1 → Ba

You might also like