SlideShare a Scribd company logo
UNIT-3
Context-Free Grammars
Prepared By:
K Lakshmi Sravani
Assistant Professor
Syllabus
Context-Free Grammars: Definition of Context-Free
Grammars,
Derivations Using a Grammar, Leftmost and Rightmost Derivations,
the Language of a Grammar, Sentential Forms, Parse
Tress,
Applications of Context-Free Grammars, Ambiguity in
Grammars
and Languages.
Push Down Automata: Definition of the Pushdown Automaton, the
Languages of a PDA, Equivalence of PDA's and CFG's,
Acceptance
by final state, Acceptance by empty stack, Deterministic Pushdown
Automata. From CFG to PDA, From PDAto CFG.
Context-Free Grammars(CFG)
● AGFG is a set of recursive rewriting productions
used to generate patterns of strings.
● It has 4 components
● G= (V,T,P,S)
● V is set of variables/Non Terminals
● T is set of terminal symbols
● P is set of productions
● S is start symbol
Example 1:
E->E+E
E->E*E
E->id
● Solution:
V={E}
T={id,+,*}
S={E}
P= {E->E+E, E->E*E, E->id}
Example 2:
● The grammar for Palindrome over input alphabet {0,1} or
L={wwR|w in {0,1}, WR is reversal of w}
● G={V,T,P,S}
● S->E, S->0, S->1, S->0S0, S->1S1
● Derive string 010010
● S
● 0S0.
● 01S10
● 010S010.
● 010S010.
010010
010010
010010
010010
Example 3
Example 4:
Derivation Tree
A derivation tree or parse tree is an ordered rooted tree that
graphically represents the semantic information a string derived
from a context-free grammar.
Representation Technique
Root vertex - Must be labeled by the start symbol
Vertex - Labeled by a non-terminal symbol.
Leaves − Labeled by a terminal symbol or ε
There are two different approaches to draw a derivation tree
Top-downApproach −
Starts with the starting symbol S
•
• Goes down to tree leaves using productions
Bottom-upApproach −
Starts from tree leaves
•
• Proceeds upward to the root which is the starting symbol S
Leftmost and Rightmost Derivation
Leftmost derivation −
Aleftmost derivation is obtained by applying production to the
leftmost variable in each step.
Rightmost derivation
Arightmost derivation is obtained by applying production to the
rightmost variable in each step.
Leftmost Derivation
Example
Let any set of production rules in a CFG be
X → X+X | X*X |X| a
The leftmost derivation for the string "a+a*a" may be
X → X+X → a+X → a + X*X → a+a*X → a+a*a
Formal Languages and Automata Theory unit 3
Rightmost Derivation
Example
Let any set of production rules in a CFG be
X → X+X | X*X |X| a
The Rightmost derivation for the string "a+a*a" may be
X → X*X → X*a → X+X*a → X+a*a → a+a*a
Formal Languages and Automata Theory unit 3
Example 2
● Construct the derivation tree for string aabbabba
from CFG given by
● S->aB|bA
● A->a|aS|bAA
● B->b|bS|aBB
Ambiguous grammar
If a context free grammar G has more than one derivation tree
for
some string w ∈ L(G), it is called an ambiguous
grammar.
There exist multiple right-most or left-most derivations for some
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.
Formal Languages and Automata Theory unit 3
Example 2
● S->aAB
● A->bC|cd
● C->cd
● B->c/d
● Check ambiguous or not
Part 2
Push Down Automata
A PDA can be formally described as a 7-tuple (Q, ∑, S/ ,, δ, q0, I/Z0, F) −
Q is the finite number of states
∑ is input alphabet
S/ is stack symbols
q0 is the initial state. (q0 ∈ Q)
I/Z0 is the initial stack top symbol (I ∈ S)
F is a set of accepting states (F ∈ Q)
δ is the transition function
•
Basically a pushdown automaton is −
"Finite state machine" + "a stack”
Apushdown automaton has three components −
• an input tape,
• a control unit, and
• a stack with infinite size.
The stack head scans the top symbol of the stack.
Astack does two operations −
Push − a new symbol is added at the top.
Pop- the top symbol is read and removed.
Structure of PDA
Example
● The following diagram shows a transition in a PDA from a state
q1 to state q2, labeled as a,b → c
● This means at state q1, if we encounter an input string ‘a’ and
top symbol of the stack is ‘b’, then we pop ‘b’, push ‘c’ on top
of the stack and move to state q2.
Two ways of Acceptability
● There are two different ways to define PDA
acceptability.
1. Final state acceptability
2. Empty Stack Acceptability
Final State Acceptability

● In final state acceptability, a PDA accepts a string when,
after reading the entire string, the PDA is in a final state.
From the starting state, we can make moves that end up
in a final state with any stack values. The stack values
are irrelevant as long as we end up in a final state.
● For a PDA (Q, ∑, S, δ, q0, I/Z0, F), the language
accepted by the set of final states F is −
● L(PDA) = {w | (q0, w, Z0) ⊢* (q, ε, x), q ∈ F}
● for any input stack string x.






Empty Stack Acceptability

● Here a PDA accepts a string when, after reading the
entire string, the PDA has emptied its stack.
● For a PDA (Q, ∑, S, δ, q0, Z0, F), the language
accepted by the empty stack is −
● L(PDA) = {w | (q0, w, Z0) ⊢* (q, ε, ε), q ∈ Q}
Examples:
Formal Languages and Automata Theory unit 3
Examples Solution
L={anb2n|n>=1}
L={n(a)<n(b) } no. of a’s less than b’s
L={x=xR ; palindrome}
L={ambmcn|m,n>=1}
Conversion of CFG to PDA
● Let L = L(G).
● Construct PDA P such that N(P) = L.
● P has:
● One state q.
● Input symbols = terminals of G.
● Stack symbols = all symbols of G.
● Start symbol = start symbol of G.
Rules:
1> Non Terminal Symbol
If A -> α is a production of G, then δ(q, ε, A) =(q, α).
2>Terminal Symbol
δ(q, a, a) = (q, ε)
Example
Example
Conversion of PDA to CFG
Example
Deterministic PDA

More Related Content

PDF
Automata theory
PDF
Formal Languages and Automata Theory unit 5
PDF
Lecture: Automata
PDF
Formal Languages and Automata Theory unit 2
PDF
Flat unit 3
PPTX
Chomsky Normal Form
PPTX
Type checking compiler construction Chapter #6
PPT
Programming in c
Automata theory
Formal Languages and Automata Theory unit 5
Lecture: Automata
Formal Languages and Automata Theory unit 2
Flat unit 3
Chomsky Normal Form
Type checking compiler construction Chapter #6
Programming in c

What's hot (20)

PDF
Ch3 4 regular expression and grammar
PPT
Turing Machine
PPTX
closure properties of regular language.pptx
PPTX
Simplification of cfg ppt
PDF
Formal Languages and Automata Theory Unit 1
PPT
NFA or Non deterministic finite automata
PPTX
Regular expressions
PPT
context free language
PPTX
Types of grammer - TOC
PDF
Theory of computation and automata
PDF
Deterministic Finite Automata (DFA)
PPTX
Automata theory - Push Down Automata (PDA)
PPTX
Automata theory -RE to NFA-ε
PPTX
Chomsky classification of Language
PPT
Turing machines
PDF
COMPILER DESIGN- Syntax Directed Translation
PPTX
Types of Parser
PPT
Regular Languages
DOC
AUTOMATA THEORY - SHORT NOTES
Ch3 4 regular expression and grammar
Turing Machine
closure properties of regular language.pptx
Simplification of cfg ppt
Formal Languages and Automata Theory Unit 1
NFA or Non deterministic finite automata
Regular expressions
context free language
Types of grammer - TOC
Theory of computation and automata
Deterministic Finite Automata (DFA)
Automata theory - Push Down Automata (PDA)
Automata theory -RE to NFA-ε
Chomsky classification of Language
Turing machines
COMPILER DESIGN- Syntax Directed Translation
Types of Parser
Regular Languages
AUTOMATA THEORY - SHORT NOTES
Ad

Similar to Formal Languages and Automata Theory unit 3 (20)

DOCX
Automata theory introduction
PPTX
Unit iv
PPTX
Theory of automata
PDF
Nondeterministic Finite Automata AFN.pdf
PPTX
INTRODUTION Formal Language and Automatic Theory.pptx
PDF
5. NFA & DFA.pdf
PDF
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
PPTX
csc218pushdownautomata-160627235627.pptx
PPTX
push down automata
PPTX
Automaton
PPTX
Finite state Transducers and mealy Machine
PPTX
Pushdown Automata toc presentation 2025.pptx
PPTX
Unit 2 Pumping lemma Unit 2 Pumping lemma
PPTX
symbolic_automata or Advanced Programming Practice.pptx
PPTX
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
PPTX
Introduction to automaton ppt
PDF
Toc e learning .pdf
PPTX
vai pdf name change kore de.pptx
PPTX
chapter 4 context-freegrammarintroduction context-freegramma
Automata theory introduction
Unit iv
Theory of automata
Nondeterministic Finite Automata AFN.pdf
INTRODUTION Formal Language and Automatic Theory.pptx
5. NFA & DFA.pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
csc218pushdownautomata-160627235627.pptx
push down automata
Automaton
Finite state Transducers and mealy Machine
Pushdown Automata toc presentation 2025.pptx
Unit 2 Pumping lemma Unit 2 Pumping lemma
symbolic_automata or Advanced Programming Practice.pptx
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Introduction to automaton ppt
Toc e learning .pdf
vai pdf name change kore de.pptx
chapter 4 context-freegrammarintroduction context-freegramma
Ad

More from Srimatre K (7)

PPTX
ML_ Unit 2_Part_B
PPTX
ML_Unit_2_Part_A
PPTX
ML_Unit_1_Part_C
PPTX
ML_Unit_1_Part_B
DOCX
Internet of things unit-1
PPTX
ML_ Unit_1_PART_A
PDF
Formal Languages and Automata Theory unit 4
ML_ Unit 2_Part_B
ML_Unit_2_Part_A
ML_Unit_1_Part_C
ML_Unit_1_Part_B
Internet of things unit-1
ML_ Unit_1_PART_A
Formal Languages and Automata Theory unit 4

Recently uploaded (20)

PDF
01-Introduction-to-Information-Management.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
master seminar digital applications in india
PDF
Complications of Minimal Access Surgery at WLH
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Institutional Correction lecture only . . .
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
01-Introduction-to-Information-Management.pdf
Cell Types and Its function , kingdom of life
Final Presentation General Medicine 03-08-2024.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Module 4: Burden of Disease Tutorial Slides S2 2025
Pharmacology of Heart Failure /Pharmacotherapy of CHF
master seminar digital applications in india
Complications of Minimal Access Surgery at WLH
VCE English Exam - Section C Student Revision Booklet
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Microbial diseases, their pathogenesis and prophylaxis
Institutional Correction lecture only . . .
Anesthesia in Laparoscopic Surgery in India
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
human mycosis Human fungal infections are called human mycosis..pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
O7-L3 Supply Chain Operations - ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf

Formal Languages and Automata Theory unit 3

  • 1. UNIT-3 Context-Free Grammars Prepared By: K Lakshmi Sravani Assistant Professor
  • 2. Syllabus Context-Free Grammars: Definition of Context-Free Grammars, Derivations Using a Grammar, Leftmost and Rightmost Derivations, the Language of a Grammar, Sentential Forms, Parse Tress, Applications of Context-Free Grammars, Ambiguity in Grammars and Languages. Push Down Automata: Definition of the Pushdown Automaton, the Languages of a PDA, Equivalence of PDA's and CFG's, Acceptance by final state, Acceptance by empty stack, Deterministic Pushdown Automata. From CFG to PDA, From PDAto CFG.
  • 3. Context-Free Grammars(CFG) ● AGFG is a set of recursive rewriting productions used to generate patterns of strings. ● It has 4 components ● G= (V,T,P,S) ● V is set of variables/Non Terminals ● T is set of terminal symbols ● P is set of productions ● S is start symbol
  • 5. Example 2: ● The grammar for Palindrome over input alphabet {0,1} or L={wwR|w in {0,1}, WR is reversal of w} ● G={V,T,P,S} ● S->E, S->0, S->1, S->0S0, S->1S1 ● Derive string 010010 ● S ● 0S0. ● 01S10 ● 010S010. ● 010S010. 010010 010010 010010 010010
  • 8. Derivation Tree A derivation tree or parse tree is an ordered rooted tree that graphically represents the semantic information a string derived from a context-free grammar. Representation Technique Root vertex - Must be labeled by the start symbol Vertex - Labeled by a non-terminal symbol. Leaves − Labeled by a terminal symbol or ε
  • 9. There are two different approaches to draw a derivation tree Top-downApproach − Starts with the starting symbol S • • Goes down to tree leaves using productions Bottom-upApproach − Starts from tree leaves • • Proceeds upward to the root which is the starting symbol S
  • 10. Leftmost and Rightmost Derivation Leftmost derivation − Aleftmost derivation is obtained by applying production to the leftmost variable in each step. Rightmost derivation Arightmost derivation is obtained by applying production to the rightmost variable in each step.
  • 11. Leftmost Derivation Example Let any set of production rules in a CFG be X → X+X | X*X |X| a The leftmost derivation for the string "a+a*a" may be X → X+X → a+X → a + X*X → a+a*X → a+a*a
  • 13. Rightmost Derivation Example Let any set of production rules in a CFG be X → X+X | X*X |X| a The Rightmost derivation for the string "a+a*a" may be X → X*X → X*a → X+X*a → X+a*a → a+a*a
  • 15. Example 2 ● Construct the derivation tree for string aabbabba from CFG given by ● S->aB|bA ● A->a|aS|bAA ● B->b|bS|aBB
  • 16. Ambiguous grammar If a context free grammar G has more than one derivation tree for some string w ∈ L(G), it is called an ambiguous grammar. There exist multiple right-most or left-most derivations for some 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.
  • 18. Example 2 ● S->aAB ● A->bC|cd ● C->cd ● B->c/d ● Check ambiguous or not
  • 20. Push Down Automata A PDA can be formally described as a 7-tuple (Q, ∑, S/ ,, δ, q0, I/Z0, F) − Q is the finite number of states ∑ is input alphabet S/ is stack symbols q0 is the initial state. (q0 ∈ Q) I/Z0 is the initial stack top symbol (I ∈ S) F is a set of accepting states (F ∈ Q) δ is the transition function •
  • 21. Basically a pushdown automaton is − "Finite state machine" + "a stack” Apushdown automaton has three components − • an input tape, • a control unit, and • a stack with infinite size. The stack head scans the top symbol of the stack. Astack does two operations − Push − a new symbol is added at the top. Pop- the top symbol is read and removed.
  • 23. Example ● The following diagram shows a transition in a PDA from a state q1 to state q2, labeled as a,b → c ● This means at state q1, if we encounter an input string ‘a’ and top symbol of the stack is ‘b’, then we pop ‘b’, push ‘c’ on top of the stack and move to state q2.
  • 24. Two ways of Acceptability ● There are two different ways to define PDA acceptability. 1. Final state acceptability 2. Empty Stack Acceptability
  • 25. Final State Acceptability
 ● In final state acceptability, a PDA accepts a string when, after reading the entire string, the PDA is in a final state. From the starting state, we can make moves that end up in a final state with any stack values. The stack values are irrelevant as long as we end up in a final state. ● For a PDA (Q, ∑, S, δ, q0, I/Z0, F), the language accepted by the set of final states F is − ● L(PDA) = {w | (q0, w, Z0) ⊢* (q, ε, x), q ∈ F} ● for any input stack string x.
  • 26. 
 
 
 Empty Stack Acceptability
 ● Here a PDA accepts a string when, after reading the entire string, the PDA has emptied its stack. ● For a PDA (Q, ∑, S, δ, q0, Z0, F), the language accepted by the empty stack is − ● L(PDA) = {w | (q0, w, Z0) ⊢* (q, ε, ε), q ∈ Q}
  • 29. Examples Solution L={anb2n|n>=1} L={n(a)<n(b) } no. of a’s less than b’s L={x=xR ; palindrome} L={ambmcn|m,n>=1}
  • 30. Conversion of CFG to PDA ● Let L = L(G). ● Construct PDA P such that N(P) = L. ● P has: ● One state q. ● Input symbols = terminals of G. ● Stack symbols = all symbols of G. ● Start symbol = start symbol of G. Rules: 1> Non Terminal Symbol If A -> α is a production of G, then δ(q, ε, A) =(q, α). 2>Terminal Symbol δ(q, a, a) = (q, ε)