SlideShare una empresa de Scribd logo
1 de 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!
? ?

Más contenido relacionado

La actualidad más candente

Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsRajendran
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysisDattatray Gandhmal
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automatahafizhamza0322
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designSadia Akter
 
Mealy and moore machine
Mealy and moore machineMealy and moore machine
Mealy and moore machineEhatsham Riaz
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02hamzamughal39
 
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
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite AutomataArchana Gopinath
 

La actualidad más candente (20)

Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
 
Mealy and moore machine
Mealy and moore machineMealy and moore machine
Mealy and moore machine
 
Pumping lemma for cfl
Pumping lemma for cflPumping lemma for cfl
Pumping lemma for cfl
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Theory of Automata Lesson 02
Theory of Automata Lesson 02Theory of Automata Lesson 02
Theory of Automata Lesson 02
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Yacc
YaccYacc
Yacc
 
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
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Kleene's theorem
Kleene's theoremKleene's theorem
Kleene's theorem
 
Regular Expression to Finite Automata
Regular Expression to Finite AutomataRegular Expression to Finite Automata
Regular Expression to Finite Automata
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 

Similar a 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 a 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
 

Más de Radhakrishnan Chinnusamy (10)

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

Último

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
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 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
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
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
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
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
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 

Último (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service NashikCall Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
Call Girls Service Nashik Vaishnavi 7001305949 Independent Escort Service Nashik
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
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 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
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
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...
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
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...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 

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! ? ?