Professional Documents
Culture Documents
aa bX aXX
a
x y x
Regular Languages and CFG
• The CFG generated through this procedure
generates the same language as accepted
by the FA
• Proof
– (i) Every word accepted by FA can be generated
by CFG
– (ii) Every word generated by CFG is accepted by
FA
Regular Languages and CFG
• Example
a a,b
b
a
S- M F+
b
S → aM
S → bS Derivation of babbaaba
M →aF through CFG and
traversal through FA
M →bS
F →aF
F →bF
F→Λ
Regular Languages and CFG
• FA to CFG
– Words that contain a double aa
– All words having different first and last
letters
Regular Languages and CFG
• Solution
– Differentiate CFGs defining RLs and those defining non RLs
Regular Languages and CFGs
• Theorem
– If all the productions in a given CFG fit one
of the two forms
• Nonterminal → semiword
• Nonterminal → word
– Where word can be null, the language
generated by this CFG is regular
Regular languages and CFGs
• Proof
– Consider a general CFG of this form
• N1 → w1 N2
• N2 → w 2 N3
• N3 → w 3 N4
• N4 →w5 (Can have many more productions)
• Example
– S → aA | bB
– A → aS | a
– B → bS | b
Λ Productions
• Productions of the form
– N→Λ
– are called null (Λ) productions
• All grammars that generate the Λ string include at
least one null production
• Some grammars that do not generate Λ string still
might contain null productions
– S → aX
– X→Λ
Λ Productions
• Hazards of Λ Productions
– Create ambiguity in word derivation
• Solution
– Kill Them !!!
Killing Null Productions
• Theorem
– If L is a context free language generated by CFG
that includes Λ-productions then there is a
different CFG that has no Λ- productions that
generates exactly the same language L with the
exception of only Λ.
Killing Λ Productions
• Constructive Algorithm
– Identify Null Productions
– Remove each of them one by one
– For each NT having a null production, add
productions where the NT has been replaced
by null
• Example
– S aSa | bSb |Λ becomes
– S aSa | bSb |aa |bb
Killing Λ Productions
• Problem Identified !!!
– S a | Xb | aYa
–XY|Λ
–Yb|X
Killing Λ Productions
24
Example
Consider the CFG
S Xa
X aX | bX | Λ
X bX Xb
25
Example
S XY
X Zb
• Null-able Non-terminals are?
Y bW
Z AB • A, B, Z and W
WZ
A aA | bA | Λ
B Ba | Bb | Λ
26
S XY
X Zb
Y bW
Z AB Example Contd.
WZ
A aA | bA | Λ
B Ba | Bb | Λ