SlideShare a Scribd company logo
1 of 16
CFGs: Definition
Context Free Grammar G is defined by
G = (V, Σ, P, S)
V = variables a finite set
Σ = alphabet or terminals a finite set
P = productions a finite set
S = start variable S∈V
Productions’ form, where A∈V, α∈(V∪Σ)*
:
 A → α
Derivations
Definition. v is one-step derivable from u, written u ⇒ v, if:
• u = xαz
• v = xβz
• α  β in R
Definition. v is derivable from u, written u ⇒* v, if:
There is a chain of one-derivations of the form:
u ⇒ u1
⇒ u2
⇒ … ⇒ v
Context-Free Languages
Definition. Given a context-free grammar
G = (∑, NT, R, S), the language generated or
derived from G is the set:
L(G) = {w : }S ⇒* w
Definition. A language L is context-free if there is a
context-free grammar G = (∑, NT, R, S), such that L is
generated from G
CFGs & CFLs: Example 1
{an
bn
| n≥0}
One of our canonical non-RLs.
S → ε | a S b
Formally: G = ({S}, {a,b},
{S → ε, S → a S b}, S)
CFGs & CFLs: Non-Example
{an
bn
cn
| n≥0}
Can’t be done; CFL pumping lemma later.
Intuition: Can count to n, then can count down
from n, but forgetting n.
 I.e., a stack as a counter.
 Will see this when using a machine corresponding
to CFGs.
Parse Tree
A parse tree of a derivation is a tree in which:
• Each internal node is labeled with a nonterminal
•If a rule A  A1A2…An occurs in the derivation then A is
a parent node of nodes labeled A1, A2, …, AnS
a S
a S
S
e
b
Parse Trees
Sample derivations:
S ⇒ AB ⇒ AAB ⇒ aAB ⇒ aaB ⇒ aabB ⇒ aabb
S ⇒ AB ⇒ AbB ⇒ Abb ⇒ AAbb ⇒ Aabb ⇒ aabb
S → A | A B
A → ε | a | A b | A A
B → b | b c | B c | b B
These two derivations use same productions, but in different orders.
This ordering difference is often uninteresting.
Derivation trees give way to abstract away ordering differences.
Root label = start node.
Each interior label = variable.
Each parent/child relation = derivation step.
Each leaf label = terminal or ε.
All leaf labels together = derived string = yield.
S
A B
AA Bb
a a b
Leftmost, Rightmost Derivations
Definition. A left-most derivation of a sentential form is one
in which rules transforming the left-most nonterminal are
always applied
Definition. A right-most derivation of a sentential form is
one in which rules transforming the right-most nonterminal
are always applied
Leftmost & Rightmost Derivations
Sample derivations:
S ⇒ AB ⇒ AAB ⇒ aAB ⇒ aaB ⇒ aabB ⇒ aabb
S ⇒ AB ⇒ AbB ⇒ Abb ⇒ AAbb ⇒ Aabb ⇒ aabb
S → A | A B
A → ε | a | A b | A A
B → b | b c | B c | b B
S
A B
AA Bb
a a b
These two derivations are special.
1st
derivation is leftmost.
Always picks leftmost variable.
2nd
derivation is rightmost.
Always picks rightmost variable.
Left / Rightmost Derivations
 In proofs…
 Restrict attention to left- or rightmost derivations.
 In parsing algorithms…
 Restrict attention to left- or rightmost derivations.
 E.g., recursive descent uses leftmost; yacc uses rightmost.
Derivation Trees
Infinitely
many others
possible.
S
A B
AA b
a
a
bA
S
A
A A
AA bA
a ε
a
bA
S
A B
AA Bb
a a b
S → A | A B
A → ε | a | A b | A A
B → b | b c | B c | b B
w = aabb
Other derivation
trees for this string?
? ?
Ambiguous Grammar
S  A
S  B
S  AB
A  aA
B  bB
A  e
B  e
Definition. A grammar G is ambiguous if there is a word
w ∈ L(G) having are least two different parse trees
Notice that a has at least two left-most derivations
Ambiguity
CFG ambiguous ⇔ any of following equivalent
statements:
 ∃ string w with multiple derivation trees.
 ∃ string w with multiple leftmost derivations.
 ∃ string w with multiple rightmost derivations.
Defining ambiguity of grammar, not language.
Ambiguity & Disambiguation
Given an ambiguous grammar, would like an
equivalent unambiguous grammar.
 Allows you to know more about structure of a
given derivation.
 Simplifies inductive proofs on derivations.
 Can lead to more efficient parsing algorithms.
 In programming languages, want to impose a
canonical structure on derivations. E.g., for
1+2×3.
Strategy: Force an ordering on all derivations.
Disambiguation: Example 1
Exp → n
| Exp + Exp
| Exp × Exp
What is an equivalent
unambiguous
grammar?
Exp → Term
| Term + Exp
Term → n
| n × Term
Uses
 operator precedence
 left-associativity
?
?
Disambiguation
What is a general algorithm?
There are CFLs that are inherently ambiguous
Every CFG for this language is ambiguous.
E.g., {an
bn
cm
dm
| n≥1, m≥1} ∪ {an
bm
cm
dn
| n≥1, m≥1}.
So, can’t necessarily eliminate ambiguity!
None exists!
? ?

More Related Content

What's hot

3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cflSampath Kumar S
 
Turing Machine
Turing MachineTuring Machine
Turing MachineRajendran
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite AutomataRatnakar Mikkili
 
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
 
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
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
closure properties of regular language.pptx
closure properties of regular language.pptxclosure properties of regular language.pptx
closure properties of regular language.pptxThirumoorthy64
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 
context free language
context free languagecontext free language
context free languagekhush_boo31
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarMdImamHasan1
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmIfad Rahman
 
Generalized transition graphs
Generalized transition graphsGeneralized transition graphs
Generalized transition graphsArham Khan G
 
Equivalence and minimization of DFA
Equivalence and minimization of DFAEquivalence and minimization of DFA
Equivalence and minimization of DFABadrul Alam
 

What's hot (20)

3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl3.5 equivalence of pushdown automata and cfl
3.5 equivalence of pushdown automata and cfl
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO 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
 
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
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
closure properties of regular language.pptx
closure properties of regular language.pptxclosure properties of regular language.pptx
closure properties of regular language.pptx
 
Turing machine by_deep
Turing machine by_deepTuring machine by_deep
Turing machine by_deep
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Graph colouring
Graph colouringGraph colouring
Graph colouring
 
context free language
context free languagecontext free language
context free language
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Pda to cfg h2
Pda to cfg h2Pda to cfg h2
Pda to cfg h2
 
Ambiguous & Unambiguous Grammar
Ambiguous & Unambiguous GrammarAmbiguous & Unambiguous Grammar
Ambiguous & Unambiguous Grammar
 
Kruskal & Prim's Algorithm
Kruskal & Prim's AlgorithmKruskal & Prim's Algorithm
Kruskal & Prim's Algorithm
 
Pda
PdaPda
Pda
 
Generalized transition graphs
Generalized transition graphsGeneralized transition graphs
Generalized transition graphs
 
Equivalence and minimization of DFA
Equivalence and minimization of DFAEquivalence and minimization of DFA
Equivalence and minimization of DFA
 

Similar to Context free grammar

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfry54321288
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammarlavishka_anuj
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRaviAr5
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free GrammerHASHIR RAZA
 
Context free languages
Context free languagesContext free languages
Context free languagesJahurul Islam
 
Context free languages
Context free languagesContext free languages
Context free languagesJahurul Islam
 
Context free langauges
Context free langaugesContext free langauges
Context free langaugessudhir sharma
 
context free grammars automata therory and compiler design
context free grammars automata therory and compiler designcontext free grammars automata therory and compiler design
context free grammars automata therory and compiler designsunitachalageri1
 
163692498 grammar
163692498 grammar163692498 grammar
163692498 grammarcjsmann
 
Context Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.pptContext Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.ppt1sonalishipu
 
Open course(programming languages) 20150211
Open course(programming languages) 20150211Open course(programming languages) 20150211
Open course(programming languages) 20150211codin9cafe
 
Open course(programming languages) 20150211
Open course(programming languages) 20150211Open course(programming languages) 20150211
Open course(programming languages) 20150211JangChulho
 

Similar to Context free grammar (20)

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammar
 
Ch02
Ch02Ch02
Ch02
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
Context free languages
Context free languagesContext free languages
Context free languages
 
Context free languages
Context free languagesContext free languages
Context free languages
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
context free grammars automata therory and compiler design
context free grammars automata therory and compiler designcontext free grammars automata therory and compiler design
context free grammars automata therory and compiler design
 
PARSER .pptx
PARSER .pptxPARSER .pptx
PARSER .pptx
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Cfg part ii
Cfg   part iiCfg   part ii
Cfg part ii
 
163692498 grammar
163692498 grammar163692498 grammar
163692498 grammar
 
Ch2 automata.pptx
Ch2 automata.pptxCh2 automata.pptx
Ch2 automata.pptx
 
Context Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.pptContext Free Languages by S.Mandal-1.ppt
Context Free Languages by S.Mandal-1.ppt
 
Class5
Class5Class5
Class5
 
Open course(programming languages) 20150211
Open course(programming languages) 20150211Open course(programming languages) 20150211
Open course(programming languages) 20150211
 
Open course(programming languages) 20150211
Open course(programming languages) 20150211Open course(programming languages) 20150211
Open course(programming languages) 20150211
 
To lec 03
To lec 03To lec 03
To lec 03
 
Lex analysis
Lex analysisLex analysis
Lex analysis
 

More from Radhakrishnan Chinnusamy (11)

Unit 5_Controlling.pptx
Unit 5_Controlling.pptxUnit 5_Controlling.pptx
Unit 5_Controlling.pptx
 
Unit 3_organising.pptx
Unit 3_organising.pptxUnit 3_organising.pptx
Unit 3_organising.pptx
 
Unit 2_Planning.ppt
Unit 2_Planning.pptUnit 2_Planning.ppt
Unit 2_Planning.ppt
 
Unit 1_introduction.pptx
Unit 1_introduction.pptxUnit 1_introduction.pptx
Unit 1_introduction.pptx
 
Chapter 7 Run Time Environment
Chapter 7   Run Time EnvironmentChapter 7   Run Time Environment
Chapter 7 Run Time Environment
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Introduction to Compiler
Introduction to CompilerIntroduction to Compiler
Introduction to Compiler
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
 
Primitive Recursive Functions
Primitive Recursive FunctionsPrimitive Recursive Functions
Primitive Recursive Functions
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxupamatechverse
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxAsutosh Ranjan
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...RajaP95
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Coefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptxCoefficient of Thermal Expansion and their Importance.pptx
Coefficient of Thermal Expansion and their Importance.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
IMPLICATIONS OF THE ABOVE HOLISTIC UNDERSTANDING OF HARMONY ON PROFESSIONAL E...
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 

Context free grammar

  • 1. CFGs: Definition Context Free Grammar G is defined by G = (V, Σ, P, S) V = variables a finite set Σ = alphabet or terminals a finite set P = productions a finite set S = start variable S∈V Productions’ form, where A∈V, α∈(V∪Σ)* :  A → α
  • 2. Derivations Definition. v is one-step derivable from u, written u ⇒ v, if: • u = xαz • v = xβz • α  β in R Definition. v is derivable from u, written u ⇒* v, if: There is a chain of one-derivations of the form: u ⇒ u1 ⇒ u2 ⇒ … ⇒ v
  • 3. Context-Free Languages Definition. Given a context-free grammar G = (∑, NT, R, S), the language generated or derived from G is the set: L(G) = {w : }S ⇒* w Definition. A language L is context-free if there is a context-free grammar G = (∑, NT, R, S), such that L is generated from G
  • 4. CFGs & CFLs: Example 1 {an bn | n≥0} One of our canonical non-RLs. S → ε | a S b Formally: G = ({S}, {a,b}, {S → ε, S → a S b}, S)
  • 5. CFGs & CFLs: Non-Example {an bn cn | n≥0} Can’t be done; CFL pumping lemma later. Intuition: Can count to n, then can count down from n, but forgetting n.  I.e., a stack as a counter.  Will see this when using a machine corresponding to CFGs.
  • 6. Parse Tree A parse tree of a derivation is a tree in which: • Each internal node is labeled with a nonterminal •If a rule A  A1A2…An occurs in the derivation then A is a parent node of nodes labeled A1, A2, …, AnS a S a S S e b
  • 7. Parse Trees Sample derivations: S ⇒ AB ⇒ AAB ⇒ aAB ⇒ aaB ⇒ aabB ⇒ aabb S ⇒ AB ⇒ AbB ⇒ Abb ⇒ AAbb ⇒ Aabb ⇒ aabb S → A | A B A → ε | a | A b | A A B → b | b c | B c | b B These two derivations use same productions, but in different orders. This ordering difference is often uninteresting. Derivation trees give way to abstract away ordering differences. Root label = start node. Each interior label = variable. Each parent/child relation = derivation step. Each leaf label = terminal or ε. All leaf labels together = derived string = yield. S A B AA Bb a a b
  • 8. Leftmost, Rightmost Derivations Definition. A left-most derivation of a sentential form is one in which rules transforming the left-most nonterminal are always applied Definition. A right-most derivation of a sentential form is one in which rules transforming the right-most nonterminal are always applied
  • 9. Leftmost & Rightmost Derivations Sample derivations: S ⇒ AB ⇒ AAB ⇒ aAB ⇒ aaB ⇒ aabB ⇒ aabb S ⇒ AB ⇒ AbB ⇒ Abb ⇒ AAbb ⇒ Aabb ⇒ aabb S → A | A B A → ε | a | A b | A A B → b | b c | B c | b B S A B AA Bb a a b These two derivations are special. 1st derivation is leftmost. Always picks leftmost variable. 2nd derivation is rightmost. Always picks rightmost variable.
  • 10. Left / Rightmost Derivations  In proofs…  Restrict attention to left- or rightmost derivations.  In parsing algorithms…  Restrict attention to left- or rightmost derivations.  E.g., recursive descent uses leftmost; yacc uses rightmost.
  • 11. Derivation Trees Infinitely many others possible. S A B AA b a a bA S A A A AA bA a ε a bA S A B AA Bb a a b S → A | A B A → ε | a | A b | A A B → b | b c | B c | b B w = aabb Other derivation trees for this string? ? ?
  • 12. Ambiguous Grammar S  A S  B S  AB A  aA B  bB A  e B  e Definition. A grammar G is ambiguous if there is a word w ∈ L(G) having are least two different parse trees Notice that a has at least two left-most derivations
  • 13. Ambiguity CFG ambiguous ⇔ any of following equivalent statements:  ∃ string w with multiple derivation trees.  ∃ string w with multiple leftmost derivations.  ∃ string w with multiple rightmost derivations. Defining ambiguity of grammar, not language.
  • 14. Ambiguity & Disambiguation Given an ambiguous grammar, would like an equivalent unambiguous grammar.  Allows you to know more about structure of a given derivation.  Simplifies inductive proofs on derivations.  Can lead to more efficient parsing algorithms.  In programming languages, want to impose a canonical structure on derivations. E.g., for 1+2×3. Strategy: Force an ordering on all derivations.
  • 15. Disambiguation: Example 1 Exp → n | Exp + Exp | Exp × Exp What is an equivalent unambiguous grammar? Exp → Term | Term + Exp Term → n | n × Term Uses  operator precedence  left-associativity ? ?
  • 16. Disambiguation What is a general algorithm? There are CFLs that are inherently ambiguous Every CFG for this language is ambiguous. E.g., {an bn cm dm | n≥1, m≥1} ∪ {an bm cm dn | n≥1, m≥1}. So, can’t necessarily eliminate ambiguity! None exists! ? ?