SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsRajendran
 
Knuth morris pratt string matching algo
Knuth morris pratt string matching algoKnuth morris pratt string matching algo
Knuth morris pratt string matching algosabiya sabiya
 
Properties of Regular Expressions
Properties of Regular ExpressionsProperties of Regular Expressions
Properties of Regular ExpressionsShiraz316
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite AutomataRatnakar Mikkili
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computationBipul Roy Bpl
 
2.5 ambiguity in context free grammars
2.5 ambiguity in context free grammars2.5 ambiguity in context free grammars
2.5 ambiguity in context free grammarsSampath Kumar S
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptFamiDan
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of LanguageDipankar Boruah
 
Variants of Turing Machine
Variants of Turing MachineVariants of Turing Machine
Variants of Turing MachineRajendran
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Abhimanyu Mishra
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarMdImamHasan1
 

Tendances (20)

Top down parsing
Top down parsingTop down parsing
Top down parsing
 
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
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Knuth morris pratt string matching algo
Knuth morris pratt string matching algoKnuth morris pratt string matching algo
Knuth morris pratt string matching algo
 
Properties of Regular Expressions
Properties of Regular ExpressionsProperties of Regular Expressions
Properties of Regular Expressions
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Finite automata
Finite automataFinite automata
Finite automata
 
2.5 ambiguity in context free grammars
2.5 ambiguity in context free grammars2.5 ambiguity in context free grammars
2.5 ambiguity in context free grammars
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 
Chapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.pptChapter 5 -Syntax Directed Translation - Copy.ppt
Chapter 5 -Syntax Directed Translation - Copy.ppt
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 
Pumping lemma
Pumping lemmaPumping lemma
Pumping lemma
 
Variants of Turing Machine
Variants of Turing MachineVariants of Turing Machine
Variants of Turing Machine
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
 
Finite automata
Finite automataFinite automata
Finite automata
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous Grammar
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 

Similaire à 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
 
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
 
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
 

Similaire à 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
 
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
 
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
 

Plus de 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
 
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Srimatre K
 

Plus de Srimatre K (8)

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
 
Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2Formal Languages and Automata Theory unit 2
Formal Languages and Automata Theory unit 2
 

Dernier

Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17Celine George
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptshraddhaparab530
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQuiz Club NITW
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxSayali Powar
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPCeline George
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdfMr Bounab Samir
 

Dernier (20)

Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
Mattingly "AI & Prompt Design" - Introduction to Machine Learning"
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17How to Manage Buy 3 Get 1 Free in Odoo 17
How to Manage Buy 3 Get 1 Free in Odoo 17
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
Integumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.pptIntegumentary System SMP B. Pharm Sem I.ppt
Integumentary System SMP B. Pharm Sem I.ppt
 
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITWQ-Factor General Quiz-7th April 2024, Quiz Club NITW
Q-Factor General Quiz-7th April 2024, Quiz Club NITW
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptxBIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
BIOCHEMISTRY-CARBOHYDRATE METABOLISM CHAPTER 2.pptx
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
An Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERPAn Overview of the Calendar App in Odoo 17 ERP
An Overview of the Calendar App in Odoo 17 ERP
 
MS4 level being good citizen -imperative- (1) (1).pdf
MS4 level   being good citizen -imperative- (1) (1).pdfMS4 level   being good citizen -imperative- (1) (1).pdf
MS4 level being good citizen -imperative- (1) (1).pdf
 

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