Module 3 CFG - Final
Module 3 CFG - Final
Context-Free Grammars
(CFG) & Push Down
Automata
1
Context-Free
Grammars
(CFG)
11/22/24 2
What is a grammar?
11/22/24 3
Definition of Context-Context Free
Grammar
A Context Free Grammar is defined as:
G = (V, T, P, S)
Where V-> finite set of variables(non-terminal symbols)
represented with capital letters (e.g. A,S,B,…etc)
11/22/24 4
Example
1) The grammar G=({S}, {a, b}, S, P)
Where P ={S → aSa, S → bSb, Sϵ} is a CFG.
In this CFG,
V={S}=set of variables
T={a, b}=set of terminals
S=Start Symbol
P=Set of production rules
11/22/24 5
rules
Each production rule consists of three parts as:
1.A variable taken from V set. This variable is often
called the head of the production
11/22/24 7
The grammar G=({S}, {a, b}, S, P)
Where P ={S → aaSbb|ab|ϵ} is a CFG. Find the
language represented by given grammar.
8
Context Free Language
continue..
2) The grammar G=({S}, {a, b}, S, P)
Where P ={S → aaSbb|ab|ϵ} is a CFG. Find the
language represented by given grammar.
Solution:
S → aaSbb
S → aaSbb
SaaaaSbbbb (since S
SaaaaSbbbb (since S
→ aaSbb)
→ aaSbb)
Saaaabbbb (since
Saaaaabbbbb (since S
Sϵ)
→ ab)
11/22/24 9
L={anbn, n is even} find CFG
n= {0, 2, 4, 6, 8, …}
10
Context Free Grammar
Examples
1. L={anbn, n is even} find CFG
G=(V,T,S,P)
P= { S ϵ
T={a,b} S= start state
S aaSbb
}
11/22/24 11
L={anbn, n is odd} find CFG
n= 1,3,5,7….
12
Context Free Grammar
Examples
2. L={anbn, n is odd} find CFG
n= 1,3,5,7….
L={ab, aaabbb,aaaaabbbbb,aaaaaaabbbbbbb….}
S ϵ
G=(V,T,S,P)
T={a,b} S=start state
P={ S ab S aaSbb
S aaSbb
}
V={S}
CFG= ({S},{a,b},S,{Sab,SaaSbb})
11/22/24 13
L={anbn, n is a multiple of three}
find CFG
n= 0,3,6,9,12,..
14
Context Free Grammar
Examples
3. L={anbn, n is a multiple of three} find CFG
n= 3,6,9,12,..
L={aaabbb,aaaaaabbbbbb,aaaaaaaaabbbbbbbb
,..}
G=(V,T,S,P)
P={ S ϵ
T={a,b} S=start state
S aaaSbbb }
V={S}
CFG=({S},{a,b},S,{Sϵ,SaaaSbbb})
11/22/24 15
L={anbn, n is not a multiple of
three} find CFG
n=1,2,4,5,7,8,10,11,---
16
Context Free Grammar
Examples
4.L={anbn, n is not a multiple of three} find CFG
n=1,2,4,5,7,8,11,---
L={ab, aabb,aaaabbbb,aaaaabbbbb,---}
G=(V,T,S,P)
T={a,b} S=start state
P={ Sab
Saabb
SaaaSbbb
}
V={S}
CFG=({S},{a,b},S,{Sab,Saabb,SaaaSbbb})
11/22/24 17
Context Free Grammar
Examples
5. L={anbm, n=m+3}
m=0,1,2,3-- n=3,4,5,6,…
L={aaa,aaaab,aaaaabb,aaaaaabbb,---}
G=(V,T,P,S)
T={a,b} S=start symbol
P= { Saaa
SaSb }
V={S}
CFG=({S},{a,b},S,{Saaa,SaSb})
11/22/24 18
L={anbm, n=m-1}
19
Context Free Grammar
Examples
6. L={anbm, n=m-1}
m=1,2,3,4,-- n=0,1,2,3,4—
L={b,abb,aabbb,aaabbbb,--}
G=(V,T,P,S)
T={a,b} S=start symbol
P={ Sb
SaSb }
V={S}
CFG=({S},{a,b},S,{Sb,SaSb})
11/22/24 20
Context Free Grammar
Examples
7. L={anbm, n=2m}
m=0,1,2,3,-- n=0,2,4,6,--
L={ϵ,aab,aaaabb,aaaaaabbb,--}
G=(V,T,P,S)
P={ S ϵ
T={a,b} S=start symbol
SaaSb }
CFG=({S},{a,b},S,{S ϵ,SaaSb})
V={S}
11/22/24 21
Context Free Grammar
Examples
8. L={w,w€{a,b}* : na(w)= nb(w)}
Count of a = count of b
L={ϵ,ab,ba,abab,baba,aabb,bbaa,---}
G=(V,T,P,S)
T={a,b} S=Start symbol
P={SSS
SaSb|bSa
Sϵ }
V={S}
CFG=({S},{a,b},S,{SSS|aSb|bSa|ϵ})
11/22/24 22
Context Free Grammar Examples continue..
Therefor, CFG is
G=( {S}, {a,b}, S, {S → aa|aSb} )
11/22/24 23
Context Free Grammar Examples
continue..
Therefor, CFG is
G=( {S}, {a,b}, S, {S → aaSbb, Sab} )
11/22/24 24
Derivation Tree/Parse Tree
Representation Technique
1. Root vertex: Must be labeled by the start
symbol.
2. Vertex: Labeled by a non-terminal symbol.
3. Leaves: Labeled by a terminal symbol or ε.
11/22/24 25
Example
If S → x1x2 …… xn is a production rule in a CFG,
then the parse tree / derivation tree will be as
follows:
11/22/24 26
Derivation or Yield of a Tree
11/22/24 27
Example
1) Let a CFG {V,T,P,S} be V = {S}, T = {a, b},
Starting symbol = S, P = S → SS | aSb | ε Find the
derivation tree from the above CFG for the String
“abaabb”
Solution
S → SS
S → aSbS (replace S →aSb)
S →abS (replace S →ε)
S → abaSb (replace S →aSb)
S → abaaSbb (replace S →aSb)
S→ abaabb (replace S →ε)
11/22/24 28
Sentential Form and Partial
Derivation Tree
A partial derivation tree is a sub-tree of a
derivation tree/parse tree such that either all of
its children are in the sub-tree or none of them
are in the sub-tree.
Example
If in any CFG the productions are:
S → AB, A → aaA | ε, B →Bb| ε
2. Rightmost derivation
- A rightmost derivation is obtained by applying
production to the rightmost variable in each step.
11/22/24 30
LMD Example
1) Let any set of production
rules in a CFG be
X → X+X | X*X |X| a
over an alphabet {a}.
Find the leftmost derivation
for the string
"a+a*a“.
Solution
X → X+X
X→ a+X (replace Xa)
X→ a+ X*X (replace X*X)
X→a+a*X (replace Xa)
X→ a+a*a (replace Xa)
11/22/24 31
RMD Example
2) Let any set of production
rules in a CFG be
X → X+X | X*X |X| a
over an alphabet {a}.
Find the Rightmost
derivation for the string
"a+a*a“.
Solution
X → X*X
X→ X*a (replace Xa)
X → X+X*a (replace
XX+X)
X →X+a*a (replace Xa)
X→ a+a*a (replace Xa)
11/22/24 32
Example 3
11/22/24 33
Example 4
11/22/24 34
Example 5
Consider the following grammar
S → bB/Aa
A→b/ bS/ aAA
B → a/ aS/ bBB
Find: Leftmost and right most derivation For string
bbaababa and Also find derivation tree
11/22/24 35
Solution :
11/22/24 36
Ambiguity in Context-Free
Grammars
If a context free grammar G has more than one
derivation tree(more than 1 LMDs or 1 RMDs) for
some string w ∈ L(G), it is called an ambiguous
grammar. There exist multiple right-most or left-
most derivations for same string generated from
that grammar.
Problem
Check whether the grammar G with production
rules:
X → X+X | X*X |X| a is ambiguous or not.
Solution
Let’s find out the derivation tree for the string
"a+a*a". It has two derivations
11/22/24 37
Solution
Derivation 1:Parse tree1:Derivation 2:Parse tree2:
X → X+X X X*X
X→ a +X X X+X*X
X→ a+ X*X X a+X*X
X →a+a*X X a+ a*X
X→ a+a*a X a+a*a
11/22/24 39
3. CFG : S->SS| aSb|bSa|€
Given string w=aabb find whether this G
is ambiguous or not
11/22/24 40