Unit 3: by Kamini Sharma
Unit 3: by Kamini Sharma
By kamini sharma
Classification of Context Free Grammars
• Recursive Grammars
• 1) S->SaS
• S->b
• The language(set of strings) generated by the
above grammar is :{b, bab, babab,…}, which is
infinite.
• 2) S-> Aa
• A->Ab|c
• The language generated by the above
grammar is :{ca, cba, cbba …}, which is
infinite.
• Note: A recursive context-free grammar that
contains no useless rules necessarily produces
an infinite language.
• Non-Recursive Grammars
• S->Aa
• A->b|c
• The language generated by the above grammar is :{ba, ca},
which is finite.
• Types of Recursive Grammars
Based on the nature of the recursion in a recursive grammar, a
recursive CFG can be again divided into the following:
• Left Recursive Grammar (having left Recursion)
• Right Recursive Grammar (having right Recursion)
• General Recursive Grammar(having general Recursion)
Ambiguous Grammar :
•
A context-free grammar is called ambiguous
grammar if there exists more than one
derivation tree or parse tree.
• Example –
• S -> S + S / S * S / S / a
• G : S AB
• A aAa|b
• B bB|a
• W =abba
2. E E + E|id
W: Id+Id+id
2. Unambiguous Grammar :
•
A context-free grammar is called unambiguous
grammar if there exists one and only one
derivation tree or parse tree.
• Example –
• X -> AB
• A -> Aa / a
• B -> b
Ambiguous Grammar Unambiguous Grammar
Leftmost Derivation-
S → aS
→ aaS (Using S → aS)
→ aaaS (Using S → aS)
→ aaa∈
→ aaa
Rightmost Derivation-
S → aS
→ aaS (Using S → aS)
→ aaaS (Using S → aS)
→ aaa∈
→ aaa
Clearly,
Leftmost derivation = Rightmost derivation
Similar is the case for all other strings.
Pumping lemma for CFL Context free
language
• Lemma
• If L is a context-free language, there is a pumping
length p such that any string w ∈ L of length ≥ p can
be written as w = uvxyz, where
• vy ≠ ε,
• |vxy| ≤ p,
• and
• for all i ≥ 0, uvixyiz ∈ L.
• Show that L = {a^n b^n c^n| n>0} is not
context free.
Construction of reduced Grammar
Greibach Normal Form (GNF)