SlideShare a Scribd company logo
1 of 12
Download to read offline
RonakThakkar
Roll no 32
M.Sc. Computer Science
Context Free Grammars
What are Context Free Grammars?
In Formal Language Theory , a Context free Grammar(CFG)
is a formal grammar in which every production rule is of the
form
V w
Where V is a single nonterminal symbol and w is a string of
terminals and/or nonterminals (w can be empty)
The languages generated by context free grammars are
knows as the context free languages
What does CFG do?
A CFG provides a simple and mathematically precise
mechanism for describing the methods by which phrases in
some natural language are built from smaller blocks,
capturing the “block structure” of sentences in a natural way.
Important features of natural language syntax such as
agreement and reference is are not the part of context free
grammar , but the basic recursive structure of sentences , the
way in which clauses nest inside other clauses, and the way in
which list of adjectives and adverbs are swallowed by nouns
and verbs is described exactly.
Formal Definition of CFG
A context-free grammar G is a 4-tuple (V, ∑, R, S), where:
 V is a finite set; each element v ∈ V is called a non-terminal character or
a variable.
∑ is a finite set of terminals, disjoint from , which make up the actual
content of the sentence.
R is a finite relation from V to (V U ∑)* , where the asterisk
represents the Kleene star operation.
If (α,β) ∈ R, we write production α  β
β is called a sentential form
• S, the start symbol, used to represent the whole sentence (or
program). It must be an element of V.
Production rule notation
A production rule in R is formalized mathematically as a pair
(α,β) , where α is a non-terminal and β is a string of
variables and nonterminals; rather than using ordered pair
notation, production rules are usually written using an arrow
operator with α as its left hand side and β as its right hand
side: α  β.
It is allowed for β to be the empty string, and in this case it is
customary to denote it by . The form
ε α  is called an -
ε ε
production.
Context-Free Languages
•Given a context-free grammar
G = (V,∑,R, S), the language generated or derived from
G is the set
L(G) = {w :S ⇒* w}
A language L is context-free if there is a context-free
grammar G = (V,∑, R, S), such that L is generated from G.
Example :Well-formed
parentheses
The canonical example of a context free grammar is
parenthesis matching, which is representative of the general
case. There are two terminal symbols "(" and ")" and one
nonterminal symbol S. The production rules are
S S
→
SS (S)
→
S ()
→
The first rule allows Ss to multiply; the second rule allows Ss
to become enclosed by matching parentheses; and the third
rule terminates the recursion.
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, …, An
S
a S
a S
S
e
b
Leftmost, Rightmost Derivations
A left-most derivation of a sentential form is one in
which rules transforming the left-most nonterminal are
always applied
A right-most derivation of a sentential form is one in
which rules transforming the right-most nonterminal
are always applied
Ambiguous Grammar
. 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
S  A
S  B
S  AB
A  aA
B  bB
A  e
B  e
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.
CFG Simplification
Can’t always eliminate ambiguity.
But, CFG simplification & restriction still useful
theoretically & pragmatically.
Simpler grammars are easier to understand.
Simpler grammars can lead to faster parsing.
Restricted forms useful for some parsing algorithms.
Restricted forms can give you more knowledge about
derivations.

More Related Content

Similar to contextfreegrammars-120925004035-phpapp02.pdf

Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
bolovv
 
A195259101 22750 24_2018_grammars and languages generated by grammars
A195259101 22750 24_2018_grammars and languages generated by grammarsA195259101 22750 24_2018_grammars and languages generated by grammars
A195259101 22750 24_2018_grammars and languages generated by grammars
Mohd Arif Ansari
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
CSR2011
 
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptxCOMPILER DESIGN LECTURES -UNIT-2 ST.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
Ranjeet Reddy
 
Final formal languages
Final formal languagesFinal formal languages
Final formal languages
Megha Khanna
 

Similar to contextfreegrammars-120925004035-phpapp02.pdf (20)

Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
A195259101 22750 24_2018_grammars and languages generated by grammars
A195259101 22750 24_2018_grammars and languages generated by grammarsA195259101 22750 24_2018_grammars and languages generated by grammars
A195259101 22750 24_2018_grammars and languages generated by grammars
 
9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptxLECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
LECTURE 1 ALPHABET,STRINGS, LANGUAGE CHOMSKY TYPES OF GRAMMAR.pptx
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchy
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
 
Unit iii
Unit iiiUnit iii
Unit iii
 
compiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 partcompiler design course focus of chapter 3 and 4 part
compiler design course focus of chapter 3 and 4 part
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptxCOMPILER DESIGN LECTURES -UNIT-2 ST.pptx
COMPILER DESIGN LECTURES -UNIT-2 ST.pptx
 
theory of computation lecture 02
theory of computation lecture 02theory of computation lecture 02
theory of computation lecture 02
 
Lecture Notes-Are Natural Languages Regular.pdf
Lecture Notes-Are Natural Languages Regular.pdfLecture Notes-Are Natural Languages Regular.pdf
Lecture Notes-Are Natural Languages Regular.pdf
 
Final formal languages
Final formal languagesFinal formal languages
Final formal languages
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Basic Intentional Injuries Health Education
Basic Intentional Injuries Health EducationBasic Intentional Injuries Health Education
Basic Intentional Injuries Health Education
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 

contextfreegrammars-120925004035-phpapp02.pdf

  • 1. RonakThakkar Roll no 32 M.Sc. Computer Science Context Free Grammars
  • 2. What are Context Free Grammars? In Formal Language Theory , a Context free Grammar(CFG) is a formal grammar in which every production rule is of the form V w Where V is a single nonterminal symbol and w is a string of terminals and/or nonterminals (w can be empty) The languages generated by context free grammars are knows as the context free languages
  • 3. What does CFG do? A CFG provides a simple and mathematically precise mechanism for describing the methods by which phrases in some natural language are built from smaller blocks, capturing the “block structure” of sentences in a natural way. Important features of natural language syntax such as agreement and reference is are not the part of context free grammar , but the basic recursive structure of sentences , the way in which clauses nest inside other clauses, and the way in which list of adjectives and adverbs are swallowed by nouns and verbs is described exactly.
  • 4. Formal Definition of CFG A context-free grammar G is a 4-tuple (V, ∑, R, S), where:  V is a finite set; each element v ∈ V is called a non-terminal character or a variable. ∑ is a finite set of terminals, disjoint from , which make up the actual content of the sentence. R is a finite relation from V to (V U ∑)* , where the asterisk represents the Kleene star operation. If (α,β) ∈ R, we write production α  β β is called a sentential form • S, the start symbol, used to represent the whole sentence (or program). It must be an element of V.
  • 5. Production rule notation A production rule in R is formalized mathematically as a pair (α,β) , where α is a non-terminal and β is a string of variables and nonterminals; rather than using ordered pair notation, production rules are usually written using an arrow operator with α as its left hand side and β as its right hand side: α  β. It is allowed for β to be the empty string, and in this case it is customary to denote it by . The form ε α  is called an - ε ε production.
  • 6. Context-Free Languages •Given a context-free grammar G = (V,∑,R, S), the language generated or derived from G is the set L(G) = {w :S ⇒* w} A language L is context-free if there is a context-free grammar G = (V,∑, R, S), such that L is generated from G.
  • 7. Example :Well-formed parentheses The canonical example of a context free grammar is parenthesis matching, which is representative of the general case. There are two terminal symbols "(" and ")" and one nonterminal symbol S. The production rules are S S → SS (S) → S () → The first rule allows Ss to multiply; the second rule allows Ss to become enclosed by matching parentheses; and the third rule terminates the recursion.
  • 8. 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, …, An S a S a S S e b
  • 9. Leftmost, Rightmost Derivations A left-most derivation of a sentential form is one in which rules transforming the left-most nonterminal are always applied A right-most derivation of a sentential form is one in which rules transforming the right-most nonterminal are always applied
  • 10. Ambiguous Grammar . 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 S  A S  B S  AB A  aA B  bB A  e B  e
  • 11. 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.
  • 12. CFG Simplification Can’t always eliminate ambiguity. But, CFG simplification & restriction still useful theoretically & pragmatically. Simpler grammars are easier to understand. Simpler grammars can lead to faster parsing. Restricted forms useful for some parsing algorithms. Restricted forms can give you more knowledge about derivations.