0% found this document useful (0 votes)
24 views

Module 3 CFG - Final

Uploaded by

Pooja S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Module 3 CFG - Final

Uploaded by

Pooja S
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 40

Module 3

Context-Free Grammars
(CFG) & Push Down
Automata

Context-Free Grammars, Parse Trees, Ambiguity in Grammars and Languages, Ambiguity


in Grammars and Languages, Definition of the Pushdown Automaton, The Languages
of a PDA, Equivalence of PDA's and CFG's, Deterministic Pushdown Automata.

1
Context-Free
Grammars
(CFG)

11/22/24 2
What is a grammar?

 A grammar consists of one or more variables that


represents a language.
 A grammar is defined with 4 tuples as:
G = (V, T, P, S)
Where V-> finite set of variables(non-terminal
symbols) represented with capital letters
T->finite set of terminals(input letters)
P->Set of productions rules that represents
recursive definition of language
S->Start symbol (first production rule always starts
with S)

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)

T->finite set of terminals(input letters ex. a,b,c,x,---etc)

S->Start symbol (first production rule always starts with S)

P->Set of productions rules having the form:


Ax
Where A ϵ V and
x ϵ (V ᴜ T)*

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

2. The production symbol 

3. A string of empty symbol or more terminals and


variables. This string, called the body of the
production, represents one way to form strings in
the language of the variable of the head.

Ex: SaSa, SbSb, Sϵ

or can also be written as: SaSa|bSb|ϵ


11/22/24 6
Context Free
Language
1) The grammar G=({S}, {a, b}, S, P)
Where P ={S → aSa, S → bSb, Sϵ} is a CFG. Find the
language represented by given grammar.
Solution: S → bSb
S → aSa SbaSab (since S → aSa)
SaaSaa (since S → SbabSbab (since S →
aSa) bSb)
SbabbSbbab (since S →
SaabSbaa (since S bSb)
→ bSb) Sbabbbbab (since Sϵ)
Saabbaa (since
Sϵ)

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
SaaaaSbbbb (since S
SaaaaSbbbb (since S
→ aaSbb)
→ aaSbb)
Saaaabbbb (since
Saaaaabbbbb (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

L= {ϵ, aabb, aaaabbbb, aaaaaabbbbbb,….}


n= {0, 2, 4, 6, 8, …}

G=(V,T,S,P)

P= { S ϵ
T={a,b} S= start state

S  aaSbb
}

CFG= ({S},{a,b},S,{S ϵ,S  aaSbb})


V={S}

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,{Sab,SaaSbb})

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ϵ,SaaaSbbb})

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={ Sab
Saabb
SaaaSbbb
}
V={S}
CFG=({S},{a,b},S,{Sab,Saabb,SaaaSbbb})
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= { Saaa
SaSb }
V={S}
CFG=({S},{a,b},S,{Saaa,SaSb})
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={ Sb
SaSb }
V={S}
CFG=({S},{a,b},S,{Sb,SaSb})
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

SaaSb }

CFG=({S},{a,b},S,{S ϵ,SaaSb})
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={SSS
SaSb|bSa
Sϵ }
V={S}
CFG=({S},{a,b},S,{SSS|aSb|bSa|ϵ})
11/22/24 22
Context Free Grammar Examples continue..

Solution: Let S is Start State


T={a,b}
P: { S → aSb, Saa}
V={S}

Therefor, CFG is
G=( {S}, {a,b}, S, {S → aa|aSb} )

11/22/24 23
Context Free Grammar Examples
continue..

Solution: Let S is Start State


T={a,b}
P: { S → aaSbb, Sab}
V={S}

Therefor, CFG is
G=( {S}, {a,b}, S, {S → aaSbb, Sab} )

11/22/24 24
Derivation Tree/Parse Tree

Generation of Derivation Tree


A derivation tree or parse tree is an ordered rooted
tree that graphically represents the semantic
information for a string derived from a context-free
grammar.

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

The derivation or the yield of a parse tree is the


final string obtained by concatenating the labels
of the leaves of the tree from left to right,
ignoring the Nulls(Empty strings). However, if all
the leaves are Null, derivation is Null.

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| ε

The partial derivation tree is shown as

If a partial derivation tree contains the root S, it is


called a sentential form. The above sub-tree is
also in sentential form.
11/22/24 29
Types of Derivation Tree
1. Leftmost derivation
- A leftmost derivation is obtained by applying
production to the leftmost variable in each step.

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 Xa)
X→ a+ X*X (replace X*X)
X→a+a*X (replace Xa)
X→ a+a*a (replace Xa)
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 Xa)
X → X+X*a (replace
XX+X)
X →X+a*a (replace Xa)
X→ a+a*a (replace Xa)

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

Since there are two parse trees for a single


string "a+a*a", the grammar G is
ambiguous
11/22/24 38
2. Consider the following CFG,
S -> aS|aSbS|є
Show that derivation for the string “
aab” is ambiguous.

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

You might also like