0% found this document useful (0 votes)
9 views68 pages

Unit-3. Context Free Grammar

This document provides a comprehensive overview of Context Free Grammar (CFG), including definitions, derivations, languages, and properties such as ambiguity and simplification. It covers the conversion of finite automata to CFGs, normal forms, closure properties of context-free languages, and decision properties, along with algorithms like the CYK algorithm for string acceptance. The document also discusses the inherent ambiguity of languages and methods for removing left recursion and useless symbols in grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views68 pages

Unit-3. Context Free Grammar

This document provides a comprehensive overview of Context Free Grammar (CFG), including definitions, derivations, languages, and properties such as ambiguity and simplification. It covers the conversion of finite automata to CFGs, normal forms, closure properties of context-free languages, and decision properties, along with algorithms like the CYK algorithm for string acceptance. The document also discusses the inherent ambiguity of languages and methods for removing left recursion and useless symbols in grammars.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 68

UNIT-3

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 α ⇒* ω.

 If G is a CFG with alphabet Σ and start symbol S, then the language

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

left of production is occurring at a first position on


the right side of production. It can eliminate left
recursion by replacing a pair of production with:
A → βA′
A′ → αA′|ϵ
 Left recursive grammar creates problem in top-
down parsing. It causes a recursive descent parser
into infinite loop.
STEPS FOR CONVERTING CFG INTO GNF

 Step 1: Convert the grammar into CNF.


 Step 2: Write G as ({A , A , …A }, T, P, S) by
1 2 n
renaming the variables with A1 as the start symbol.
 Now try to modify the grammar in the form Ai → aα
or Ai → Ajα , where j>i by applying
substitution rule.
 Apply substitution rule and left recursion

removal method to convert the grammar into


required form i.e Ai → aα
CLOSURE PROPERTIES OF CONTEXT FREE LANGUAGES

 Theorem: Show that the family of context free languages is closed


under union operation.

 Proof: Let L1 and L2 be two context free languages generated by


context free grammar G1 = (V1, Σ1, S1, P1) and G1 = (V2, Σ2, S2, P2)
respectively.

Now, we construct the grammar G as the following;- G = (V, Σ, S, P) Where,


V = V1 ∪ V2 ∪ {S}, Σ = Σ1 ∪ Σ2 and P = P1 ∪ P2 ∪ {S → S1|S2}

Now, we have to show that L(G) = L(G1) ∪ L(G2)

Therefore, G is a grammar generating the language L1 ∪ L2. Hence L is a


context free language.
CLOSURE PROPERTIES OF CONTEXT FREE LANGUAGES

 Theorem: Show that the family of context free languages


is closed under concatenation operation.
CLOSURE PROPERTIES OF CONTEXT FREE LANGUAGES

 Theorem: Show that the family of context free languages is closed


under Kleene closure operation.
CLOSURE PROPERTIES OF CONTEXT FREE LANGUAGES

 Theorem: Show that the family of context free languages is not


closed under intersection operation.
CLOSURE PROPERTIES OF CONTEXT FREE LANGUAGES

 Theorem: Show that the family of context free languages is not


closed under complement operation.
CLOSURE PROPERTIES OF CFL AND REGULAR LANGUAGES
 Given L1 is a context free language and L2 is a regular language then L1 ∩ L2,
L1-L2 are also context free language.
 The idea of the proof is to simulate a push-down automaton and a finite state
automaton in parallel and only accept if both machines accept.
DECISION PROPERTIES OF CFLS
A decision property for a class of languages is an
algorithm that takes a formal description of a language
(e.g., a PDA) and tells whether or not some property
holds.
 Given context free languages, specified in any one of the

four means, can we develop algorithms that answer the


following questions:
1. Is a given string in the language?
2. Is the language empty?
3. Is the language finite?
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 empty.
 Proof: For the simplicity, we assume that ε ∉

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

unit-productions, and no useless symbols.


 Convert the grammar into CNF.
 We draw a directed graph whose vertices are variables in G.
 If A→BC is a production, then there are directed edges from A to B
and A to C.
 L is finite if and only if the directed graph has no cycles.
DECISION PROPERTIES OF CONTEXT FREE LANGUAGES [CYK ALGORITHM]
 Theorem: Given a CFG G=(V, T, P, S) and a string w. Whether w ∈ L(G)?
 Proof: There exists an algorithm called CKY Algorithm or Cocke-Younger-
Kasami Algorithm after its inventors. It is used to decide whether a given
string belongs to the language of grammar or not. If start variable is member of
V1n then w ∈ L(G).
EXAMPLE
 For the given grammar, check the acceptance of string w = baaba
using CYK Algorithm-
S → AB / BC
A → BA / a
B → CC / b
C → AB / a

You might also like