Unit-3. Context Free Grammar
Unit-3. Context Free Grammar
CONTEXT FREE
GRAMMAR
Shyam B. Verma
Assistant Professor, UCER Prayagraj
UNIT-3
Regular and Non-Regular Grammars: Context Free
Grammar(CFG)-Definition, Derivations, Languages, Derivation Trees
and Ambiguity, Regular Grammars-Right Linear and Left Linear
grammars, Conversion of FA into CFG and Regular grammar into FA,
Simplification of CFG, Normal Forms- Chomsky Normal Form(CNF),
Greibach Normal Form (GNF), Chomsky Hierarchy, Programming
problems based on the properties of CFGs.
CONTEXT FREE GRAMMAR
DERIVATION AND LANGUAGE
A sequence of steps where non-terminals are replaced by the
right-hand side of a production is called a derivation.
If string α derives string ω, we write α ⇒* ω.
of G is the set:
L(G) = { ω ∈ Σ* | S ⇒* ω }
That is, L(G) is the set of strings derivable from the start symbol S. ω must
be in Σ* and it is made from terminals.
A language L is called a context-free language (or CFL) if there is
a CFG G such that L = L(G)
CONSTRUCT CFG
CONSTRUCT CONTEXT FREE GRAMMAR
AMBIGUOUS GRAMMAR
INHERENTLY AMBIGUOUS LANGUAGE
REDUCED GRAMMAR
A grammar obtained by deleting useless symbols, productions,
Unit productions and null productions is called reduced
grammar.
REMOVAL OF USELESS SYMBOLS AND PRODUCTIONS
Removal of useless symbols (variables and terminals) and production
rules from the grammar that can never take part in any derivation.
SUBSTITUTION RULE
IfA → α1 B α2 is a production and variable B has a
production: B → X1|X2…|Xn, then remove production
A → α1 B α2 and add the following productions:
A→ α X α
1 1 2
A→ α X α
1 2 2
.
.
.
A→ α X α
1 n 2
REMOVAL OF LEFT RECURSION
A Grammar G (V, T, P, S) is left recursive if it has a
production in the form: A → A α |β.
The above Grammar is left recursive because the
L(G).
Then, we use the algorithm for removing useless
symbols and productions. If S is found to be
useless, then L(G) is empty otherwise L(G)
contains at least one element.
DECISION PROPERTIES OF CONTEXT FREE LANGUAGES
Theorem: Given a context free grammar G=(V,Σ,S,P),
there exists an algorithm for deciding whether or not L(G)
is infinite.
Proof: We assume that G contains no ε−productions, no