SlideShare a Scribd company logo
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

Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
hafizhamza0322
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
Tsegazeab Asgedom
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
Iffat Anjum
 
Pushdown Automata Theory
Pushdown Automata TheoryPushdown Automata Theory
Pushdown Automata Theory
Saifur Rahman
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
Akhil Kaushik
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
Riazul Islam
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
Akila Krishnamoorthy
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
Ratnakar Mikkili
 
0.0 Introduction to theory of computation
0.0 Introduction to theory of computation0.0 Introduction to theory of computation
0.0 Introduction to theory of computation
Sampath Kumar S
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
Zain Ul Abiden
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
United International University
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
Shiraz316
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
Rajendran
 
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
Sampath Kumar S
 
AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
suthi
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Assembly language
Assembly languageAssembly language
Assembly language
shashank puthran
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
Animesh Chaturvedi
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
Rabia Khalid
 

What's hot (20)

Pumping lemma Theory Of Automata
Pumping lemma Theory Of AutomataPumping lemma Theory Of Automata
Pumping lemma Theory Of Automata
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Pushdown Automata Theory
Pushdown Automata TheoryPushdown Automata Theory
Pushdown Automata Theory
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
 
0.0 Introduction to theory of computation
0.0 Introduction to theory of computation0.0 Introduction to theory of computation
0.0 Introduction to theory of computation
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Chomsky & Greibach Normal Forms
Chomsky & Greibach Normal FormsChomsky & Greibach Normal Forms
Chomsky & Greibach Normal Forms
 
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
 
AUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTESAUTOMATA THEORY - SHORT NOTES
AUTOMATA THEORY - SHORT NOTES
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Assembly language
Assembly languageAssembly language
Assembly language
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 

Similar to Context free grammar

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
ry54321288
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammarlavishka_anuj
 
Ch02
Ch02Ch02
Ch02
Hankyo
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
RaviAr5
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
HASHIR RAZA
 
Context free languages
Context free languagesContext free languages
Context free languages
Jahurul Islam
 
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
sunitachalageri1
 
PARSER .pptx
PARSER .pptxPARSER .pptx
PARSER .pptx
SHRISTEERAI1
 
Unit iii
Unit iiiUnit iii
Unit iii
TPLatchoumi
 
Cfg part ii
Cfg   part iiCfg   part ii
Cfg part ii
Kashif Ali
 
163692498 grammar
163692498 grammar163692498 grammar
163692498 grammar
cjsmann
 
Ch2 automata.pptx
Ch2 automata.pptxCh2 automata.pptx
Ch2 automata.pptx
TemesgenAzezew
 
Normal-forms-for-Context-Free-Grammars.ppt
Normal-forms-for-Context-Free-Grammars.pptNormal-forms-for-Context-Free-Grammars.ppt
Normal-forms-for-Context-Free-Grammars.ppt
Karthik Rohan
 
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
1sonalishipu
 
Class5
Class5Class5
Class5
kpcs2010
 
Open course(programming languages) 20150211
Open course(programming languages) 20150211Open course(programming languages) 20150211
Open course(programming languages) 20150211
JangChulho
 
Open course(programming languages) 20150211
Open course(programming languages) 20150211Open course(programming languages) 20150211
Open course(programming languages) 20150211
codin9cafe
 
Lex analysis
Lex analysisLex analysis
Lex analysis
Suhit Kulkarni
 
Lexical 2
Lexical 2Lexical 2

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 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
 
Normal-forms-for-Context-Free-Grammars.ppt
Normal-forms-for-Context-Free-Grammars.pptNormal-forms-for-Context-Free-Grammars.ppt
Normal-forms-for-Context-Free-Grammars.ppt
 
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
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 

More from Radhakrishnan Chinnusamy

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

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

Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
aqil azizi
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
ChristineTorrepenida1
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
veerababupersonal22
 

Recently uploaded (20)

Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdfTutorial for 16S rRNA Gene Analysis with QIIME2.pdf
Tutorial for 16S rRNA Gene Analysis with QIIME2.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Unbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptxUnbalanced Three Phase Systems and circuits.pptx
Unbalanced Three Phase Systems and circuits.pptx
 
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERSCW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
CW RADAR, FMCW RADAR, FMCW ALTIMETER, AND THEIR PARAMETERS
 

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