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

Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free GrammarsMarina Santini
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFAkunj desai
 
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
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
Introduction to the theory of computation
Introduction to the theory of computationIntroduction to the theory of computation
Introduction to the theory of computationprasadmvreddy
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4shah zeb
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languagesparmeet834
 
2.1 & 2.2 grammar introduction – types of grammar
2.1 & 2.2 grammar introduction – types of grammar2.1 & 2.2 grammar introduction – types of grammar
2.1 & 2.2 grammar introduction – types of grammarSampath Kumar S
 

What's hot (20)

Lecture: Context-Free Grammars
Lecture: Context-Free GrammarsLecture: Context-Free Grammars
Lecture: Context-Free Grammars
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
TOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal FormTOC 7 | CFG in Chomsky Normal Form
TOC 7 | CFG in Chomsky Normal Form
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
FInite Automata
FInite AutomataFInite Automata
FInite Automata
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 
Lecture: Automata
Lecture: AutomataLecture: Automata
Lecture: Automata
 
Introduction to the theory of computation
Introduction to the theory of computationIntroduction to the theory of computation
Introduction to the theory of computation
 
Push down automata
Push down automataPush down automata
Push down automata
 
Lecture 3,4
Lecture 3,4Lecture 3,4
Lecture 3,4
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Regular Languages
Regular LanguagesRegular Languages
Regular Languages
 
2.1 & 2.2 grammar introduction – types of grammar
2.1 & 2.2 grammar introduction – types of grammar2.1 & 2.2 grammar introduction – types of grammar
2.1 & 2.2 grammar introduction – types of grammar
 

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
 
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
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! EdholeEdhole.com
 
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 grammarsMohd Arif Ansari
 

Similar to Context free grammars (20)

contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
 
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
 
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
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! Edhole
 
Lexical 2
Lexical 2Lexical 2
Lexical 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
 

Recently uploaded

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Recently uploaded (20)

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

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.