SlideShare a Scribd company logo
1 of 35
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
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
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.
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:
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

What's hot

Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5Srimatre K
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite AutomataRatnakar Mikkili
 
Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1Abhimanyu Mishra
 
Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Abhimanyu Mishra
 
Pushdown Automata Theory
Pushdown Automata TheoryPushdown Automata Theory
Pushdown Automata TheorySaifur Rahman
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDAAshish Duggal
 
Properties of Regular Expressions
Properties of Regular ExpressionsProperties of Regular Expressions
Properties of Regular ExpressionsShiraz316
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 

What's hot (20)

Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5Formal Languages and Automata Theory unit 5
Formal Languages and Automata Theory unit 5
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
3.4 deterministic pda
3.4 deterministic pda3.4 deterministic pda
3.4 deterministic pda
 
Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5
 
L3 cfg
L3 cfgL3 cfg
L3 cfg
 
Chomsky Hierarchy.ppt
Chomsky Hierarchy.pptChomsky Hierarchy.ppt
Chomsky Hierarchy.ppt
 
Pushdown Automata Theory
Pushdown Automata TheoryPushdown Automata Theory
Pushdown Automata Theory
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
FInite Automata
FInite AutomataFInite Automata
FInite Automata
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Properties of Regular Expressions
Properties of Regular ExpressionsProperties of Regular Expressions
Properties of Regular Expressions
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 

Similar to Context-Free Grammars and Pushdown Automata

Automata theory introduction
Automata theory introductionAutomata theory introduction
Automata theory introductionNAMRATA BORKAR
 
Theory of automata
Theory of automataTheory of automata
Theory of automataArslan905905
 
Nondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfNondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfSergioUlisesRojasAla
 
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdfFariyaTasneem1
 
Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Srimatre K
 
Finite state Transducers and mealy Machine
Finite state Transducers and mealy Machine Finite state Transducers and mealy Machine
Finite state Transducers and mealy Machine Nadeem Qasmi
 
Introduction to automaton ppt
Introduction to automaton pptIntroduction to automaton ppt
Introduction to automaton pptShiela Rani
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauagesdanhumble
 
Sep logic slide
Sep logic slideSep logic slide
Sep logic sliderainoftime
 
Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)Akila Krishnamoorthy
 

Similar to Context-Free Grammars and Pushdown Automata (20)

Automata theory introduction
Automata theory introductionAutomata theory introduction
Automata theory introduction
 
Automata theory
Automata theoryAutomata theory
Automata theory
 
Unit iv
Unit ivUnit iv
Unit iv
 
Theory of automata
Theory of automataTheory of automata
Theory of automata
 
Nondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfNondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdf
 
5. NFA & DFA.pdf
5. NFA & DFA.pdf5. NFA & DFA.pdf
5. NFA & DFA.pdf
 
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf@vtucode.in-module-1-21CS51-5th-semester (1).pdf
@vtucode.in-module-1-21CS51-5th-semester (1).pdf
 
push down automata
push down automatapush down automata
push down automata
 
Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1Formal Languages and Automata Theory Unit 1
Formal Languages and Automata Theory Unit 1
 
Automaton
AutomatonAutomaton
Automaton
 
Finite state Transducers and mealy Machine
Finite state Transducers and mealy Machine Finite state Transducers and mealy Machine
Finite state Transducers and mealy Machine
 
Introduction to automaton ppt
Introduction to automaton pptIntroduction to automaton ppt
Introduction to automaton ppt
 
vai pdf name change kore de.pptx
vai pdf name change kore de.pptxvai pdf name change kore de.pptx
vai pdf name change kore de.pptx
 
Pda
PdaPda
Pda
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Fsa
FsaFsa
Fsa
 
Lec1.pptx
Lec1.pptxLec1.pptx
Lec1.pptx
 
Sep logic slide
Sep logic slideSep logic slide
Sep logic slide
 
PDA (1) (1).pptx
PDA (1) (1).pptxPDA (1) (1).pptx
PDA (1) (1).pptx
 
Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)Automata theory - Push Down Automata (PDA)
Automata theory - Push Down Automata (PDA)
 

More from Srimatre K

ML_ Unit 2_Part_B
ML_ Unit 2_Part_BML_ Unit 2_Part_B
ML_ Unit 2_Part_BSrimatre K
 
ML_Unit_2_Part_A
ML_Unit_2_Part_AML_Unit_2_Part_A
ML_Unit_2_Part_ASrimatre K
 
ML_Unit_1_Part_C
ML_Unit_1_Part_CML_Unit_1_Part_C
ML_Unit_1_Part_CSrimatre K
 
ML_Unit_1_Part_B
ML_Unit_1_Part_BML_Unit_1_Part_B
ML_Unit_1_Part_BSrimatre K
 
Internet of things unit-1
Internet of things unit-1Internet of things unit-1
Internet of things unit-1Srimatre K
 
ML_ Unit_1_PART_A
ML_ Unit_1_PART_AML_ Unit_1_PART_A
ML_ Unit_1_PART_ASrimatre K
 
Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Srimatre K
 

More from Srimatre K (7)

ML_ Unit 2_Part_B
ML_ Unit 2_Part_BML_ Unit 2_Part_B
ML_ Unit 2_Part_B
 
ML_Unit_2_Part_A
ML_Unit_2_Part_AML_Unit_2_Part_A
ML_Unit_2_Part_A
 
ML_Unit_1_Part_C
ML_Unit_1_Part_CML_Unit_1_Part_C
ML_Unit_1_Part_C
 
ML_Unit_1_Part_B
ML_Unit_1_Part_BML_Unit_1_Part_B
ML_Unit_1_Part_B
 
Internet of things unit-1
Internet of things unit-1Internet of things unit-1
Internet of things unit-1
 
ML_ Unit_1_PART_A
ML_ Unit_1_PART_AML_ Unit_1_PART_A
ML_ Unit_1_PART_A
 
Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 

Context-Free Grammars and Pushdown Automata

  • 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
  • 12.
  • 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
  • 14.
  • 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.
  • 17.
  • 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}
  • 28.
  • 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, ε)