SlideShare a Scribd company logo
1 of 12
Context Free Grammars

        Ronak Thakkar
          Roll no 32
    M.Sc. Computer Science
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
                               b
                          S
                          e
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
                   SA
                   SB
                   S  AB
                   A  aA
                   B  bB
                   Ae
                   Be



Notice that a has at least two left-most derivations
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

What's hot

context free language
context free languagecontext free language
context free languagekhush_boo31
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free GrammarAkhil Kaushik
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMRajendran
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler designRiazul Islam
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design MAHASREEM
 
Natural Language processing Parts of speech tagging, its classes, and how to ...
Natural Language processing Parts of speech tagging, its classes, and how to ...Natural Language processing Parts of speech tagging, its classes, and how to ...
Natural Language processing Parts of speech tagging, its classes, and how to ...Rajnish Raj
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksMohammad Vaseem Akaram
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Mohammad Ilyas Malik
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Animesh Chaturvedi
 
Turing Machine
Turing MachineTuring Machine
Turing MachineRajendran
 

What's hot (20)

context free language
context free languagecontext free language
context free language
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
POST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEMPOST’s CORRESPONDENCE PROBLEM
POST’s CORRESPONDENCE PROBLEM
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Finite Automata in compiler design
Finite Automata in compiler designFinite Automata in compiler design
Finite Automata in compiler design
 
Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: 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
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Natural Language processing Parts of speech tagging, its classes, and how to ...
Natural Language processing Parts of speech tagging, its classes, and how to ...Natural Language processing Parts of speech tagging, its classes, and how to ...
Natural Language processing Parts of speech tagging, its classes, and how to ...
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Directed Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocksDirected Acyclic Graph Representation of basic blocks
Directed Acyclic Graph Representation of basic blocks
 
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
Finite Automata: Deterministic And Non-deterministic Finite Automaton (DFA)
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Chomsky Hierarchy.ppt
Chomsky Hierarchy.pptChomsky Hierarchy.ppt
Chomsky Hierarchy.ppt
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Turing Machine
Turing MachineTuring Machine
Turing Machine
 

Viewers also liked

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
Context free grammarsContext free grammars
Context free grammarsShiraz316
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauagesdanhumble
 
Class7
 Class7 Class7
Class7issbp
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTanzeela_Hussain
 
Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfgRajendran
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesMarina Santini
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up ParsingGerwin Ocsena
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicLeyo Stephen
 
Push down automata
Push down automataPush down automata
Push down automataSomya Bagai
 

Viewers also liked (20)

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
Context free grammarsContext free grammars
Context free grammars
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Properties of cfg
Properties of cfgProperties of cfg
Properties of cfg
 
Class7
 Class7 Class7
Class7
 
Parsing
ParsingParsing
Parsing
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Normal forms cfg
Normal forms   cfgNormal forms   cfg
Normal forms cfg
 
Parsing
ParsingParsing
Parsing
 
Turing machines
Turing machinesTuring machines
Turing machines
 
Lecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular LanguagesLecture: Regular Expressions and Regular Languages
Lecture: Regular Expressions and Regular Languages
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
L3 cfg
L3 cfgL3 cfg
L3 cfg
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Grammar
GrammarGrammar
Grammar
 
Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Push down automata
Push down automataPush down automata
Push down automata
 

Similar to Context free grammars

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfry54321288
 
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.pptKarthik Rohan
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchySANUC2
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free GrammerHASHIR RAZA
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)bolovv
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler designRiazul Islam
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRaviAr5
 
Final formal languages
Final formal languagesFinal formal languages
Final formal languagesMegha Khanna
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCSR2011
 
Lecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdfLecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdfDeptii Chaudhari
 
Ldml presentation
Ldml presentationLdml presentation
Ldml presentationSean Con
 
Ldml - public
Ldml - publicLdml - public
Ldml - publicseanscon
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsgadisaAdamu
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! EdholeEdhole.com
 

Similar to Context free grammars (20)

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
 
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 grammar
Context free grammarContext free grammar
Context free grammar
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Ch2 automata.pptx
Ch2 automata.pptxCh2 automata.pptx
Ch2 automata.pptx
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchy
 
Conteext-free Grammer
Conteext-free GrammerConteext-free Grammer
Conteext-free Grammer
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
 
Final formal languages
Final formal languagesFinal formal languages
Final formal languages
 
Ambiguous grammar, dfa and slr grammar.
Ambiguous grammar, dfa and slr  grammar.Ambiguous grammar, dfa and slr  grammar.
Ambiguous grammar, dfa and slr grammar.
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
 
Lecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdfLecture Notes-Finite State Automata for NLP.pdf
Lecture Notes-Finite State Automata for NLP.pdf
 
Ldml presentation
Ldml presentationLdml presentation
Ldml presentation
 
Ldml - public
Ldml - publicLdml - public
Ldml - public
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! Edhole
 

Recently uploaded

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

Context free grammars

  • 1. Context Free Grammars Ronak Thakkar Roll no 32 M.Sc. Computer Science
  • 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 b S e
  • 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 SA SB S  AB A  aA B  bB Ae Be Notice that a has at least two left-most derivations
  • 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.