SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Introduction
 Syed M. Hashir Raza Zaidi : 023R13-07
 Davut Karasu: 023R13-30
 Umut Papatya: 023R13-31
 Sana-ur-Rehman: 023R13-39
 BSCS-(6A)
Submitted To:
Mam Safia Sultana
Introduction:
A context-free grammar (CFG) is a term used in formal
languages theory to describe a certain type of formal
grammar. A context-free grammar is a set of production
rules that describe all possible strings in a given formal
language. Production rules are simple replacements. For
example, the rule
A α
 Replaces {display style A} A with {display style alpha }α. There can
be multiple replacement rules for any given value. For example,
A α A β
 means that A {display style A}A A can be replaced with
either {display style alpha }α α or {display style beta } β β
 Context-free grammars arise in linguistics where they are used to
describe the structure of sentences and words in natural language,
and they were in fact invented by the linguist Noam Chomsky for this
purpose, but have not really lived up to their original expectation. By
contrast, in computer science, as the use of recursively-defined
concepts increased, they were used more and more. In an early
application, grammars are used to describe the structure
of programming languages. In a newer application, they are used in
an essential part of the Extensible Markup Language (XML) called
the Document Type Definition
 In formal language theory, a context-free grammar.
 CFG is said to be in Chomsky normal form (first described by Noam
Chomsky) if all of its production rules.
 A context-free grammar (CFG) is a set of recursive rewriting rules (or
productions) used to generate patterns of strings.
 A CFG consists of the following components:
 a set of terminal symbols, which are the characters of the
alphabet that appear in the strings generated by the grammar.
 a set of nonterminal symbols, which are placeholders for patterns of
terminal symbols that can be generated by the nonterminal symbols.
 a set of productions, which are rules for replacing (or rewriting)
nonterminal symbols (on the left side of the production) in a string with
other nonterminal or terminal symbols (on the right side of the
production).
 a start symbol, which is a special nonterminal symbol that appears in the
initial string generated by the grammar.
 To generate a string of terminal symbols from a CFG, we:
 Begin with a string consisting of the start symbol;
 Apply one of the productions with the start symbol on the left hand size,
replacing the start symbol with the right hand side of the production;
 Repeat the process of selecting nonterminal symbols in the string, and
replacing them with the right hand side of some corresponding
production, until all nonterminals have been replaced by terminal
symbols.
Context-Free Grammars:
 A grammar is a set of rules for putting strings together and so
corresponds to a language.
 A grammar consists of:
 a set of variables (also called nonterminals), one of which is
designated the start variable; It is customary to use upper-case
letters for variables;
 a set of terminals (from the alphabet); and
 a list of productions (also called rules).
 Example: 0n 1n Here is a grammar: S → 0S1 S → ε . S is the only
variable. The terminals are 0 and 1.
 There are two productions.
 Using a Grammar A production allows one to take a string
containing a variable and replace the variable by the RHS of the
production. String w of terminals is generated by the grammar if,
starting with the start variable, one can apply productions and end
up with w.
 The sequence of strings so obtained is a derivation of w. We focus on
a special version of grammars called a context-free grammar (CFG). A
language is context-free if it is generated by
 a CFG.
Example S → 0S1 S → ε
 The string 0011 is in the language generated. The derivation is: S =⇒
0S1 =⇒ 00S11 =⇒ 0011 For compactness,
 we write S → 0S1 | ε where the vertical bar means or.
Example:
 Palindromes Let P be language of palindromes with alphabet { a, b }.
One can determine a CFG for P by finding a recursive decomposition.
If we peel first and last symbols from a palindrome, what remains is a
palindrome; and if we wrap a palindrome with the same symbol front
and back, then it is still a palindrome.
 CFG is P → a P a | b P b | ε Actually, this generates only those of
even length. . .
Formal Definition
 One can provide a formal definition of a contextfree grammar. It is a 4-tuple (V,Σ, S, P) where:
 V is a finite set of variables;
 Σ is a finite alphabet of terminals;
 S is the start variable; and
 P is the finite set of productions. Each production has the form V → (V ∪ Σ)∗.
Further Examples:
 Even 0’s A CFG for all binary strings with an even number of 0’s. Find the decomposition. If first
symbol is 1, then even number of 0’s remain. If first symbol is 0, then go to next 0; after that again
an even number of 0’s remain.
 This yields: S → 1 S | 0 A 0 S | ε A → 1 A | ε . Alternate CFG for Even 0’s Here is another CFG for
the same language. Note that when first symbol is 0, what remains has odd number of 0’s. S → 1
S | 0 T | ε T → 1 T | 0 S
Example
 A CFG for the regular language corresponding to the Recursive Expression 00∗11∗. The
language is the concatenation of two languages: all strings of zeroes with all strings of ones. S →
CD, C → 0C | 0 ,D → 1D | 1
Example
 Complement A CFG for the complement of RE 00 ∗11 ∗ . CFGs don’t do “and”s, but they do do
“or’s” A string is not of the form 0 i 1 j where i, j > 0 is one of the following: contains 10; is only
zeroes; or is only ones. This yields CFG: S → A | B | C, A → D10 D ,D → 0 D | 1 D | ε ,B → 0 B | 0
,C → 1 C | 1
 11 Consistency and Completeness Note that to check a grammar and description match, one
must check two things: that everything the grammar generates fits the description (consistency),
and everything in the description is generated by the grammar (completeness).
Example
 Consider the CFG S → 0S1S | 1S0S | ε The string 011100 is generated:
 S =⇒ 0S1S =⇒ 01S =⇒ 011S0S =⇒ 0111S0S0S =⇒ 01110S0S =⇒ 011100S =⇒
011100
 What does this language contain?
Certainly every string generated has equal 0’s and 1’s. . .
 But can any string with equal 0’s and 1’s be generated?
Example Argument for Completeness
Yes. All strings with equal 0’s & 1’s are generated: Well, at some point,
equality between 0’s and 1’s is reached. The key is that if string starts
with 0, then equality is first reached at a 1. So the portion between first
0 and this 1 is itself an example of equality, as is the portion after this 1.
That is, one can break up string as 0 w 1 x with both w and x in the
language. The break-up of 00101101: 0 0 1 0 1 1 0 1 w x
. A Silly Language CFG This CFG generates sentences as composed
of noun- and verb-phrases: S → NP. VP .N,
P → the N V ,P → V NP, V → sings| eats N → cat | song | canary This
generates “the canary sings the song”, but also “the song eats the
cat”. This CFG generates all “legal” sentences, not just meaningful
ones.
 Practice Give grammars for the following two languages:
1. All binary strings with both an even number of zeroes and an even
number of ones.
2. All strings of the form 0a1b0c such that a + c = b.
 Practice Solutions
1) S → 0X | 1Y | ε, X → 0S | 1Z (odd zeroes, even ones) Y → 1S | 0Z
(odd ones, even zeroes) Z → 0Y | 1X (odd ones, odd zeroes)
2) S → TU , T → 0T1 | ε , U → 1U0 | ε
Summary :
 A context-free grammar (CFG) consists of a set of productions that
you use to replace a variable by a string of variables and terminals.
The language of a grammar is the set of strings it generates. A
language is context-free if there is a CFG for it.
Conteext-free Grammer

Weitere ähnliche Inhalte

Was ist angesagt?

Regular expressions
Regular expressionsRegular expressions
Regular expressionsShiraz316
 
1.5 & 1.6 regular languages & regular expression
1.5 & 1.6 regular languages & regular expression1.5 & 1.6 regular languages & regular expression
1.5 & 1.6 regular languages & regular expressionSampath Kumar S
 
Context free langauges
Context free langaugesContext free langauges
Context free langaugessudhir sharma
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 
Regular expression (compiler)
Regular expression (compiler)Regular expression (compiler)
Regular expression (compiler)Jagjit Wilku
 
Types of Language in Theory of Computation
Types of Language in Theory of ComputationTypes of Language in Theory of Computation
Types of Language in Theory of ComputationAnkur Singh
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammarmeresie tesfay
 
Lecture 1,2
Lecture 1,2Lecture 1,2
Lecture 1,2shah zeb
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler designRiazul Islam
 
3.1 intro toautomatatheory h1
3.1 intro toautomatatheory  h13.1 intro toautomatatheory  h1
3.1 intro toautomatatheory h1Rajendran
 
Chomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel KhanChomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel KhanM Khan
 

Was ist angesagt? (20)

Ch02
Ch02Ch02
Ch02
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
1.5 & 1.6 regular languages & regular expression
1.5 & 1.6 regular languages & regular expression1.5 & 1.6 regular languages & regular expression
1.5 & 1.6 regular languages & regular expression
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
To lec 04
To lec 04To lec 04
To lec 04
 
Regular expression (compiler)
Regular expression (compiler)Regular expression (compiler)
Regular expression (compiler)
 
CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Regular expression (compiler)
Regular expression (compiler)Regular expression (compiler)
Regular expression (compiler)
 
Types of Language in Theory of Computation
Types of Language in Theory of ComputationTypes of Language in Theory of Computation
Types of Language in Theory of Computation
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
 
Lecture 1,2
Lecture 1,2Lecture 1,2
Lecture 1,2
 
Regular Expression in Compiler design
Regular Expression in Compiler designRegular Expression in Compiler design
Regular Expression in Compiler design
 
JAVA Literals
JAVA LiteralsJAVA Literals
JAVA Literals
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
 
3.1 intro toautomatatheory h1
3.1 intro toautomatatheory  h13.1 intro toautomatatheory  h1
3.1 intro toautomatatheory h1
 
Dfa basics
Dfa basicsDfa basics
Dfa basics
 
Flat unit 3
Flat unit 3Flat unit 3
Flat unit 3
 
To lec 03
To lec 03To lec 03
To lec 03
 
Chomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel KhanChomsky by zeeshan khan and Raheel Khan
Chomsky by zeeshan khan and Raheel Khan
 

Ähnlich wie Conteext-free Grammer

Context free languages
Context free languagesContext free languages
Context free languagesJahurul Islam
 
Context-Free Grammars.pdf
Context-Free Grammars.pdfContext-Free Grammars.pdf
Context-Free Grammars.pdfAmanda Reznor
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptxPEzhumalai
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxviswanath kani
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauagesdanhumble
 
contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfry54321288
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchySANUC2
 
hghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggghghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghggggggggggggggggggggggggggggggggggadugnanegero
 
Presentation (5).pdf
Presentation (5).pdfPresentation (5).pdf
Presentation (5).pdfGaurav447273
 
Automata
AutomataAutomata
AutomataGaditek
 
Automata
AutomataAutomata
AutomataGaditek
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRaviAr5
 
Hw2 2017-spring
Hw2 2017-springHw2 2017-spring
Hw2 2017-spring奕安 陳
 
Regular expression
Regular expressionRegular expression
Regular expressionRajon
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal formsAkila Krishnamoorthy
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnfTaha Shakeel
 
Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Dr. Maamoun Ahmed
 

Ähnlich wie Conteext-free Grammer (20)

Context free languages
Context free languagesContext free languages
Context free languages
 
TOC question bank.pdf
TOC question bank.pdfTOC question bank.pdf
TOC question bank.pdf
 
Context free grammer.ppt
Context free grammer.pptContext free grammer.ppt
Context free grammer.ppt
 
Context-Free Grammars.pdf
Context-Free Grammars.pdfContext-Free Grammars.pdf
Context-Free Grammars.pdf
 
ContextFreeGrammars.pptx
ContextFreeGrammars.pptxContextFreeGrammars.pptx
ContextFreeGrammars.pptx
 
ContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptxContextFreeGrammars (1).pptx
ContextFreeGrammars (1).pptx
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
contextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdfcontextfreegrammars-120925004035-phpapp02.pdf
contextfreegrammars-120925004035-phpapp02.pdf
 
Chomsky hierarchy
Chomsky hierarchyChomsky hierarchy
Chomsky hierarchy
 
hghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggghghghghhghghgggggggggggggggggggggggggggggggggg
hghghghhghghgggggggggggggggggggggggggggggggggg
 
Presentation (5).pdf
Presentation (5).pdfPresentation (5).pdf
Presentation (5).pdf
 
QB104541.pdf
QB104541.pdfQB104541.pdf
QB104541.pdf
 
Automata
AutomataAutomata
Automata
 
Automata
AutomataAutomata
Automata
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
 
Hw2 2017-spring
Hw2 2017-springHw2 2017-spring
Hw2 2017-spring
 
Regular expression
Regular expressionRegular expression
Regular expression
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
 
Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5Theory of Computation - Lectures 4 and 5
Theory of Computation - Lectures 4 and 5
 

Kürzlich hochgeladen

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 

Kürzlich hochgeladen (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 

Conteext-free Grammer

  • 1. Introduction  Syed M. Hashir Raza Zaidi : 023R13-07  Davut Karasu: 023R13-30  Umut Papatya: 023R13-31  Sana-ur-Rehman: 023R13-39  BSCS-(6A) Submitted To: Mam Safia Sultana
  • 2. Introduction: A context-free grammar (CFG) is a term used in formal languages theory to describe a certain type of formal grammar. A context-free grammar is a set of production rules that describe all possible strings in a given formal language. Production rules are simple replacements. For example, the rule A α
  • 3.  Replaces {display style A} A with {display style alpha }α. There can be multiple replacement rules for any given value. For example, A α A β  means that A {display style A}A A can be replaced with either {display style alpha }α α or {display style beta } β β  Context-free grammars arise in linguistics where they are used to describe the structure of sentences and words in natural language, and they were in fact invented by the linguist Noam Chomsky for this purpose, but have not really lived up to their original expectation. By contrast, in computer science, as the use of recursively-defined concepts increased, they were used more and more. In an early application, grammars are used to describe the structure of programming languages. In a newer application, they are used in an essential part of the Extensible Markup Language (XML) called the Document Type Definition
  • 4.  In formal language theory, a context-free grammar.  CFG is said to be in Chomsky normal form (first described by Noam Chomsky) if all of its production rules.  A context-free grammar (CFG) is a set of recursive rewriting rules (or productions) used to generate patterns of strings.  A CFG consists of the following components:  a set of terminal symbols, which are the characters of the alphabet that appear in the strings generated by the grammar.  a set of nonterminal symbols, which are placeholders for patterns of terminal symbols that can be generated by the nonterminal symbols.
  • 5.  a set of productions, which are rules for replacing (or rewriting) nonterminal symbols (on the left side of the production) in a string with other nonterminal or terminal symbols (on the right side of the production).  a start symbol, which is a special nonterminal symbol that appears in the initial string generated by the grammar.  To generate a string of terminal symbols from a CFG, we:  Begin with a string consisting of the start symbol;  Apply one of the productions with the start symbol on the left hand size, replacing the start symbol with the right hand side of the production;  Repeat the process of selecting nonterminal symbols in the string, and replacing them with the right hand side of some corresponding production, until all nonterminals have been replaced by terminal symbols.
  • 6. Context-Free Grammars:  A grammar is a set of rules for putting strings together and so corresponds to a language.  A grammar consists of:  a set of variables (also called nonterminals), one of which is designated the start variable; It is customary to use upper-case letters for variables;  a set of terminals (from the alphabet); and  a list of productions (also called rules).  Example: 0n 1n Here is a grammar: S → 0S1 S → ε . S is the only variable. The terminals are 0 and 1.  There are two productions.  Using a Grammar A production allows one to take a string containing a variable and replace the variable by the RHS of the production. String w of terminals is generated by the grammar if, starting with the start variable, one can apply productions and end up with w.
  • 7.  The sequence of strings so obtained is a derivation of w. We focus on a special version of grammars called a context-free grammar (CFG). A language is context-free if it is generated by  a CFG. Example S → 0S1 S → ε  The string 0011 is in the language generated. The derivation is: S =⇒ 0S1 =⇒ 00S11 =⇒ 0011 For compactness,  we write S → 0S1 | ε where the vertical bar means or. Example:  Palindromes Let P be language of palindromes with alphabet { a, b }. One can determine a CFG for P by finding a recursive decomposition. If we peel first and last symbols from a palindrome, what remains is a palindrome; and if we wrap a palindrome with the same symbol front and back, then it is still a palindrome.  CFG is P → a P a | b P b | ε Actually, this generates only those of even length. . .
  • 8. Formal Definition  One can provide a formal definition of a contextfree grammar. It is a 4-tuple (V,Σ, S, P) where:  V is a finite set of variables;  Σ is a finite alphabet of terminals;  S is the start variable; and  P is the finite set of productions. Each production has the form V → (V ∪ Σ)∗. Further Examples:  Even 0’s A CFG for all binary strings with an even number of 0’s. Find the decomposition. If first symbol is 1, then even number of 0’s remain. If first symbol is 0, then go to next 0; after that again an even number of 0’s remain.  This yields: S → 1 S | 0 A 0 S | ε A → 1 A | ε . Alternate CFG for Even 0’s Here is another CFG for the same language. Note that when first symbol is 0, what remains has odd number of 0’s. S → 1 S | 0 T | ε T → 1 T | 0 S Example  A CFG for the regular language corresponding to the Recursive Expression 00∗11∗. The language is the concatenation of two languages: all strings of zeroes with all strings of ones. S → CD, C → 0C | 0 ,D → 1D | 1 Example  Complement A CFG for the complement of RE 00 ∗11 ∗ . CFGs don’t do “and”s, but they do do “or’s” A string is not of the form 0 i 1 j where i, j > 0 is one of the following: contains 10; is only zeroes; or is only ones. This yields CFG: S → A | B | C, A → D10 D ,D → 0 D | 1 D | ε ,B → 0 B | 0 ,C → 1 C | 1  11 Consistency and Completeness Note that to check a grammar and description match, one must check two things: that everything the grammar generates fits the description (consistency), and everything in the description is generated by the grammar (completeness).
  • 9. Example  Consider the CFG S → 0S1S | 1S0S | ε The string 011100 is generated:  S =⇒ 0S1S =⇒ 01S =⇒ 011S0S =⇒ 0111S0S0S =⇒ 01110S0S =⇒ 011100S =⇒ 011100  What does this language contain? Certainly every string generated has equal 0’s and 1’s. . .  But can any string with equal 0’s and 1’s be generated? Example Argument for Completeness Yes. All strings with equal 0’s & 1’s are generated: Well, at some point, equality between 0’s and 1’s is reached. The key is that if string starts with 0, then equality is first reached at a 1. So the portion between first 0 and this 1 is itself an example of equality, as is the portion after this 1. That is, one can break up string as 0 w 1 x with both w and x in the language. The break-up of 00101101: 0 0 1 0 1 1 0 1 w x
  • 10. . A Silly Language CFG This CFG generates sentences as composed of noun- and verb-phrases: S → NP. VP .N, P → the N V ,P → V NP, V → sings| eats N → cat | song | canary This generates “the canary sings the song”, but also “the song eats the cat”. This CFG generates all “legal” sentences, not just meaningful ones.  Practice Give grammars for the following two languages: 1. All binary strings with both an even number of zeroes and an even number of ones. 2. All strings of the form 0a1b0c such that a + c = b.  Practice Solutions 1) S → 0X | 1Y | ε, X → 0S | 1Z (odd zeroes, even ones) Y → 1S | 0Z (odd ones, even zeroes) Z → 0Y | 1X (odd ones, odd zeroes) 2) S → TU , T → 0T1 | ε , U → 1U0 | ε
  • 11. Summary :  A context-free grammar (CFG) consists of a set of productions that you use to replace a variable by a string of variables and terminals. The language of a grammar is the set of strings it generates. A language is context-free if there is a CFG for it.