尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
SlideShare a Scribd company logo
Normal forms for Context-
Free Grammars
Context-Free Grammar
 In linguistics and computer science, a context-free grammar
(CFG) is a formal grammar in which every production rule is of
the form
V → w
where V is a “non-terminal symbol” and w is a “string” consisting
of terminals and/or non-terminals.
 The term "context-free" expresses the fact that the non-terminal
V can always be replaced by w, regardless of the context in
which it occurs.
 A formal language is context-free if there is a context-free
grammar that generates it.
Context-Free Grammar
 Context-free grammars are powerful enough to
describe the syntax of most programming
languages; in fact, the syntax of most programming
languages is specified using context-free grammars.
 On the other hand, context-free grammars are
simple enough to allow the construction of efficient
parsing algorithms which, for a given string,
determine whether and how it can be generated
from the grammar.
Context-Free Grammar
 Not all formal languages are context-free.
 A well-known counter example is
{ an bn cn : n >= 0 }
the set of strings containing some number of
a's, followed by the same number of b's and
the same number of c's.
Context-Free Grammar
 Just as any formal grammar, a context-free
grammar G can be defined as a 4-tuple:
 G = (Vt ,Vn ,P,S) where
 Vt is a finite set of terminals
 Vn is a finite set of non-terminals
 P is a finite set of production rules
 S is an element of Vn, the distinguished
starting non-terminal.
 elements of P are of the form
Vn → ( Vt U Vn) *
 A language L is said to be a Context-Free-Language
(CFL) if its grammar is Context-Free. More precisely,
it is a language whose words, sentences and
phrases are made of symbols and words from a
Context-Free-Grammar.
 Usually, CFL is of the form L=L(G).
Example 1
 A simple context-free grammar is given as:
S → a S b | ε
 where | is used to separate multiple options
for the same non-terminal, and ε stands for
the empty string. This grammar generates the
language { an bn : n >= 0 } , which is not
regular.
Regular languages
 A regular language is a formal language (i.e., a
possibly infinite set of finite sequences of symbols
from a finite alphabet) that satisfies the following
equivalent properties:
 it can be accepted by a deterministic finite state
machine
 it can be accepted by a nondeterministic finite state
machine
 it can be accepted by an alternating finite automaton
 it can be described by a regular expression
 it can be generated by a regular grammar
 it can be generated by a prefix grammar
Regular languages
The collection of regular languages over an
alphabet Σ is defined recursively as follows:
 the empty language Ø is a regular language.
 the empty string language { ε } is a regular
language.
 For each a є Σ, the singleton language { a } is a
regular language.
 If A and B are regular languages, then A ∩ B
(union), A ○ B (concatenation), and A* (Kleene star)
are regular languages.
 No other languages over Σ are regular.
Finite languages
Finite languages are:
 A specific subset within the class of regular
languages is the finite languages - those
containing only a finite number of words.
These are obviously regular as one can
create a regular expression that is the union
of every word in the language, and thus are
regular.
Example 2
 A context-free grammar for the language consisting
of all strings over {a,b} which contain a different
number of a's to b's is
 S → U | V
 U → TaU | TaT
 V → TbV | TbT
 T → aTbT | bTaT | ε
 Here, T can generate all strings with the same
number of a's as b's, U generates all strings with
more a's than b's and V generates all strings with
fewer a's than b's.
Example 3
Another example of a context-free language
is
This is not a regular language, but it is
context free as it can be generated by the
following context-free grammar:
 S → b S bb | A
 A → a A | ε
Normal forms
 Every context-free grammar that does not generate the empty
string can be transformed into an equivalent one in Chomsky
normal form or Greibach normal form. "Equivalent" here means
that the two grammars generate the same language.
 Because of the especially simple form of production rules in
Chomsky Normal Form grammars, this normal form has both
theoretical and practical implications.
 For instance, given a context-free grammar, one can use the
Chomsky Normal Form to construct a polynomial-time algorithm
which decides whether a given string is in the language
represented by that grammar or not (the CYK algorithm).
Properties of context-free languages
 An alternative and equivalent definition of context-
free languages employs non-deterministic push-
down automata: a language is context-free if and
only if it can be accepted by such an automaton.
 A language can also be modeled as a set of all
sequences of terminals which are accepted by the
grammar. This model is helpful in understanding set
operations on languages.
 The union and concatenation of two context-free
languages is context-free, but the intersection need
not be.
 The reverse of a context-free language is context-
free, but the complement need not be.
Properties of context-free languages
 Every regular language is context-free because it
can be described by a regular grammar.
 The intersection of a context-free language and a
regular language is always context-free.
 There exist context-sensitive languages which are
not context-free.
 To prove that a given language is not context-free,
one may employ the pumping lemma for context-
free languages.
 The problem of determining if a context-sensitive
grammar describes a context-free language is
undecidable.
Normal forms for Context-Free
Grammars
The goal is to show that every CFL (without ε)
is generated by a CFG in which all
productions are of the form A  BC or A  a,
where A, B, C are variables, and a is a
terminal.
Normal forms for Context-Free
Grammars
 A number of simplifications is inevitable:
1. The elimination of useless symbols,
“variables or terminals that do not appear in
any derivation of a terminal string from the
start symbol”.
2. The elimination of ε-productions, those of the
form A  ε for some variable A.
3. The elimination of unit productions, those of
the form A  B for variables A and B.
Eliminating useless symbols
 A symbol X is useful for Grammar
G = {V, T, P, S}, if there is some derivation of
the form S ═>* a X b ═>* w , where w є T*
 X є V or X є T
 The sentential form of a X b might be the
first or last derivation
 If X is not useful, then X is useless
Eliminating useless symbols
 Characteristics of useful symbols (for instance X):
1. X is generating if X ═>* w for some terminal
string w. Every terminal is generating since w can
be that terminal itself, which is derived by 0 steps.
2. X is reachable if there is a derivation
S ═>* a X b for some a and b.
A symbol which is useful is surely to be both
generating and reachable.
Eliminating useless symbols
Eliminating the symbols which are not
generating first followed by eliminating the
symbols which are not reachable from the
remaining grammar, this will generate a
grammar consisting of only useful symbols.
Eliminating useless symbols
 Example 7.1
 If we have the following grammar:
Eliminating useless symbols
 Example 7.1
 Notice that a and b generate themselves
“terminals”, S generates a, and A
generates b. B is not generating.
 After eliminating B:
Eliminating useless symbols
 Example 7.1
 Notice that only S and a are reachable after
eliminating the non-generating B.
 A is not reachable; so it should be eliminated.
 The result :
 This production itself is a grammar that has the
same result, which is {a}, as the original
grammar.
Computing the generating and reachable
symbols
 Basis: Every Symbol of T is obviously generating; it
generates itself.
 Induction: If we have a production A → a, and every
symbol of a is already known to be generating,
then A is generating; because it generates all and
only generating symbols, even if a = ε ; since all
variables that have ε as a production body are
generating.
 Theorem: The previous algorithm finds all and only
the Generating symbols of G
Computing the generating and reachable
symbols
 Basis : For a grammar G = {V, T, P, S}
S is surely reachable.
 Induction: If we discovered that some variable A is
reachable, then for all productions with A in the
head (first part of the expression), all the symbols of
the bodies (second part of the expression) of those
productions are also reachable.
 Theorem: The above algorithm finds all and only the
Reachable symbols of G
Eliminating useless symbols
 So far, the first step, which is the elimination
of useless symbols is concluded.
 Now, for the second part, which is the
elimination of ε-productions.
Eliminating ε-productions
 The strategy is to have the following:
if L is CFG, then L – {ε} is also CFG
 This is done through discovering the nullable
variables. A variable for instance A, is
nullable if: A ═>* ε .
 Whenever A appears in a production body, A
might or might not derive ε
Eliminating ε-productions
 Basis: If A  ε is a production of G, then A
is nullable
 Induction: If there is a production
B  C1 C2 … Ck such that each C is a
variable and each C is nullable, then B is
nullable
Eliminating ε-productions
 Theorem: For any grammar G, the only nullable symbols
are the variables that derive ε in previous algorithm
 Proof:
for one step : A  ε must be a production, then this implies
that A is discovered as nullable (as in basis).
for N > 1 steps: the first step is A  C1 C2 … Ck  ε , each
Ci derives ε by a sequence < N steps.
By the induction, each Ci is discovered by the algorithm to
be nullable. So by the inductive step, A is eventually found
to be nullable.
Eliminating ε-productions
 If a grammar G1 is constructed by the
elimination of ε-productions “ using the
previous method ” of grammar G, then
L(G1) = L(G) - {ε}
Eliminating unit productions
 The last part concerns the eliminating of unit
productions
 Any production of the form A  B , where A
and B are variables, is called a unit
production.
 These production introduce extra steps in the
derivations that obviously are not needed in
there.
Eliminating unit productions
 Basis: (A, A) is a unit pair of any variable A, if
A ═>* A by 0 steps.
 Induction: Let’s (A, B) be a unit pair, and let B  C
is a production, where A, B, and C are variables,
then we can conclude that
(A, C) is also a unit pair.
 Theorem: The previous algorithm (basis and
induction) finds exactly all the unit pairs for any
grammar G.
Eliminating unit productions
 Example 7.12
Eliminating unit productions
 Example 7.12
Eliminating unit productions
 Example 7.12
After eliminating the unit productions, the generated
grammar is:
This grammar has no unit productions and still
generates the same expressions as the previous one.
Chomsky Normal Form
Conclusion of all three elimination stages:
Theorem: If G is a CFG which generates a
language that consists of at least one string
along with ε, then there is another CFG G1
such that:
L{G1} = L{G} – {ε} , “no ε-productions”, and G1
has neither unit productions nor useless
symbols
Chomsky Normal Form
 Proof: Start by performing the elimination of
ε-productions. Then perform the elimination
of unit productions, so the resulting grammar
won’t introduce any ε-productions since the
new bodies are still identical to some bodies
of the old grammar. Finally, perform the
elimination of useless symbols, and since this
eliminates productions and symbols, it will
never reintroduce any ε-productions nor unit
productions
Chomsky Normal Form
Every nonempty CFL without ε has grammar G in
which all productions are in one of the following
forms:
 A  BC , where A, B, and C are variables or
 A  a , where A is a variable and a is a terminal
 Also G doesn’t contain any useless symbols
 A grammar complying to these forms is called a
Chomsky Normal Form (CNF).
Chomsky Normal Form
 The construction of CNF is performed
through:
1. Arrangement of all bodies of length 2 or
more to contain only variables.
2. Breaking bodies of length 3 or more into a
cascade productions, where each one has a
body consisting of 2 variables.
Chomsky Normal Form
 Example 7.15
Chomsky Normal Form
 Example 7.15
 First: we introduce new variables to represent
terminals:
Chomsky Normal Form
 Example 7.15
 Second: We make all bodies either a single
terminal or multiple variables:
Chomsky Normal Form
 Example 7.15
 Last step: we make all bodies either a single
terminal or two variables:

More Related Content

Similar to Normal-forms-for-Context-Free-Grammars.ppt

Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
Leyo Stephen
 
Presentation (5).pdf
Presentation (5).pdfPresentation (5).pdf
Presentation (5).pdf
Gaurav447273
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
kenilpatel65
 
Free Ebooks Download ! Edhole
Free Ebooks Download ! EdholeFree Ebooks Download ! Edhole
Free Ebooks Download ! Edhole
Edhole.com
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! Edhole
Edhole.com
 
9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx
mainakmail2585
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
bolovv
 
Unit iii
Unit iiiUnit iii
Unit iii
TPLatchoumi
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Princess Doll
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
RaviAr5
 
Ch2 automata.pptx
Ch2 automata.pptxCh2 automata.pptx
Ch2 automata.pptx
TemesgenAzezew
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
sudhir sharma
 
theory of computation lecture 02
theory of computation lecture 02theory of computation lecture 02
theory of computation lecture 02
8threspecter
 
Thoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's ClassificationThoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's Classification
PrafullMisra
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
CSR2011
 
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
M Khan
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
Akila Krishnamoorthy
 
Introduction to Theory of computations:-
Introduction to Theory of computations:-Introduction to Theory of computations:-
Introduction to Theory of computations:-
sivamathi12
 
Automata
AutomataAutomata
Automata
Gaditek
 
Automata
AutomataAutomata
Automata
Gaditek
 

Similar to Normal-forms-for-Context-Free-Grammars.ppt (20)

Deterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministicDeterministic context free grammars &non-deterministic
Deterministic context free grammars &non-deterministic
 
Presentation (5).pdf
Presentation (5).pdfPresentation (5).pdf
Presentation (5).pdf
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
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
 
9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx9781284077247_PPTx_CH01.pptx
9781284077247_PPTx_CH01.pptx
 
Chapter Three(2)
Chapter Three(2)Chapter Three(2)
Chapter Three(2)
 
Unit iii
Unit iiiUnit iii
Unit iii
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
RegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptxRegularLanguageProperties [Autosaved].pptx
RegularLanguageProperties [Autosaved].pptx
 
Ch2 automata.pptx
Ch2 automata.pptxCh2 automata.pptx
Ch2 automata.pptx
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
theory of computation lecture 02
theory of computation lecture 02theory of computation lecture 02
theory of computation lecture 02
 
Thoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's ClassificationThoery of Computaion and Chomsky's Classification
Thoery of Computaion and Chomsky's Classification
 
Csr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminskiCsr2011 june17 15_15_kaminski
Csr2011 june17 15_15_kaminski
 
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
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Introduction to Theory of computations:-
Introduction to Theory of computations:-Introduction to Theory of computations:-
Introduction to Theory of computations:-
 
Automata
AutomataAutomata
Automata
 
Automata
AutomataAutomata
Automata
 

More from Karthik Rohan

introduction to Laravel and its Basic and origin
introduction to Laravel and its Basic and originintroduction to Laravel and its Basic and origin
introduction to Laravel and its Basic and origin
Karthik Rohan
 
unit-3-flip-flop-notes.pdf
unit-3-flip-flop-notes.pdfunit-3-flip-flop-notes.pdf
unit-3-flip-flop-notes.pdf
Karthik Rohan
 
BSc(IoT) - Digital.pptx
BSc(IoT) - Digital.pptxBSc(IoT) - Digital.pptx
BSc(IoT) - Digital.pptx
Karthik Rohan
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
Karthik Rohan
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
Karthik Rohan
 
SVM_Sample.pptx
SVM_Sample.pptxSVM_Sample.pptx
SVM_Sample.pptx
Karthik Rohan
 
neuralnetwork.pptx
neuralnetwork.pptxneuralnetwork.pptx
neuralnetwork.pptx
Karthik Rohan
 
IT Unit III.pptx
IT Unit III.pptxIT Unit III.pptx
IT Unit III.pptx
Karthik Rohan
 
13598881-introduction-to-java-lecture-one.pdf
13598881-introduction-to-java-lecture-one.pdf13598881-introduction-to-java-lecture-one.pdf
13598881-introduction-to-java-lecture-one.pdf
Karthik Rohan
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
Karthik Rohan
 
AI-UNIT 1 FINAL PPT (1).pptx
AI-UNIT 1 FINAL PPT (1).pptxAI-UNIT 1 FINAL PPT (1).pptx
AI-UNIT 1 FINAL PPT (1).pptx
Karthik Rohan
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
Karthik Rohan
 
UNIT4.2(VB).pptx
UNIT4.2(VB).pptxUNIT4.2(VB).pptx
UNIT4.2(VB).pptx
Karthik Rohan
 
BD1.pptx
BD1.pptxBD1.pptx
BD1.pptx
Karthik Rohan
 

More from Karthik Rohan (14)

introduction to Laravel and its Basic and origin
introduction to Laravel and its Basic and originintroduction to Laravel and its Basic and origin
introduction to Laravel and its Basic and origin
 
unit-3-flip-flop-notes.pdf
unit-3-flip-flop-notes.pdfunit-3-flip-flop-notes.pdf
unit-3-flip-flop-notes.pdf
 
BSc(IoT) - Digital.pptx
BSc(IoT) - Digital.pptxBSc(IoT) - Digital.pptx
BSc(IoT) - Digital.pptx
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
 
SVM_Sample.pptx
SVM_Sample.pptxSVM_Sample.pptx
SVM_Sample.pptx
 
neuralnetwork.pptx
neuralnetwork.pptxneuralnetwork.pptx
neuralnetwork.pptx
 
IT Unit III.pptx
IT Unit III.pptxIT Unit III.pptx
IT Unit III.pptx
 
13598881-introduction-to-java-lecture-one.pdf
13598881-introduction-to-java-lecture-one.pdf13598881-introduction-to-java-lecture-one.pdf
13598881-introduction-to-java-lecture-one.pdf
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
 
AI-UNIT 1 FINAL PPT (1).pptx
AI-UNIT 1 FINAL PPT (1).pptxAI-UNIT 1 FINAL PPT (1).pptx
AI-UNIT 1 FINAL PPT (1).pptx
 
Thread.pptx
Thread.pptxThread.pptx
Thread.pptx
 
UNIT4.2(VB).pptx
UNIT4.2(VB).pptxUNIT4.2(VB).pptx
UNIT4.2(VB).pptx
 
BD1.pptx
BD1.pptxBD1.pptx
BD1.pptx
 

Recently uploaded

Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
Friends of African Village Libraries
 
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
biruktesfaye27
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
TechSoup
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
sanamushtaq922
 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
nabaegha
 
How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17
Celine George
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
Kalna College
 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
MJDuyan
 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
Frederic Fovet
 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
khabri85
 
8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity
RuchiRathor2
 
(T.L.E.) Agriculture: "Ornamental Plants"
(T.L.E.) Agriculture: "Ornamental Plants"(T.L.E.) Agriculture: "Ornamental Plants"
(T.L.E.) Agriculture: "Ornamental Plants"
MJDuyan
 
Keynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse CityKeynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse City
PJ Caposey
 
Non-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech ProfessionalsNon-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech Professionals
MattVassar1
 
Slides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptxSlides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptx
shabeluno
 
The basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptxThe basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptx
heathfieldcps1
 
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT KanpurDiversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Quiz Club IIT Kanpur
 
The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
Derek Wenmoth
 
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
Nguyen Thanh Tu Collection
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
 

Recently uploaded (20)

Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
 
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
Ethiopia and Eritrea Eritrea's journey has been marked by resilience and dete...
 
Accounting for Restricted Grants When and How To Record Properly
Accounting for Restricted Grants  When and How To Record ProperlyAccounting for Restricted Grants  When and How To Record Properly
Accounting for Restricted Grants When and How To Record Properly
 
Observational Learning
Observational Learning Observational Learning
Observational Learning
 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
 
How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17How to Create User Notification in Odoo 17
How to Create User Notification in Odoo 17
 
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
220711130083 SUBHASHREE RAKSHIT  Internet resources for social science220711130083 SUBHASHREE RAKSHIT  Internet resources for social science
220711130083 SUBHASHREE RAKSHIT Internet resources for social science
 
Information and Communication Technology in Education
Information and Communication Technology in EducationInformation and Communication Technology in Education
Information and Communication Technology in Education
 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
 
Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024Brand Guideline of Bashundhara A4 Paper - 2024
Brand Guideline of Bashundhara A4 Paper - 2024
 
8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity
 
(T.L.E.) Agriculture: "Ornamental Plants"
(T.L.E.) Agriculture: "Ornamental Plants"(T.L.E.) Agriculture: "Ornamental Plants"
(T.L.E.) Agriculture: "Ornamental Plants"
 
Keynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse CityKeynote given on June 24 for MASSP at Grand Traverse City
Keynote given on June 24 for MASSP at Grand Traverse City
 
Non-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech ProfessionalsNon-Verbal Communication for Tech Professionals
Non-Verbal Communication for Tech Professionals
 
Slides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptxSlides Peluncuran Amalan Pemakanan Sihat.pptx
Slides Peluncuran Amalan Pemakanan Sihat.pptx
 
The basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptxThe basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptx
 
Diversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT KanpurDiversity Quiz Finals by Quiz Club, IIT Kanpur
Diversity Quiz Finals by Quiz Club, IIT Kanpur
 
The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
 
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
 

Normal-forms-for-Context-Free-Grammars.ppt

  • 1. Normal forms for Context- Free Grammars
  • 2. Context-Free Grammar  In linguistics and computer science, a context-free grammar (CFG) is a formal grammar in which every production rule is of the form V → w where V is a “non-terminal symbol” and w is a “string” consisting of terminals and/or non-terminals.  The term "context-free" expresses the fact that the non-terminal V can always be replaced by w, regardless of the context in which it occurs.  A formal language is context-free if there is a context-free grammar that generates it.
  • 3. Context-Free Grammar  Context-free grammars are powerful enough to describe the syntax of most programming languages; in fact, the syntax of most programming languages is specified using context-free grammars.  On the other hand, context-free grammars are simple enough to allow the construction of efficient parsing algorithms which, for a given string, determine whether and how it can be generated from the grammar.
  • 4. Context-Free Grammar  Not all formal languages are context-free.  A well-known counter example is { an bn cn : n >= 0 } the set of strings containing some number of a's, followed by the same number of b's and the same number of c's.
  • 5. Context-Free Grammar  Just as any formal grammar, a context-free grammar G can be defined as a 4-tuple:  G = (Vt ,Vn ,P,S) where  Vt is a finite set of terminals  Vn is a finite set of non-terminals  P is a finite set of production rules  S is an element of Vn, the distinguished starting non-terminal.
  • 6.  elements of P are of the form Vn → ( Vt U Vn) *  A language L is said to be a Context-Free-Language (CFL) if its grammar is Context-Free. More precisely, it is a language whose words, sentences and phrases are made of symbols and words from a Context-Free-Grammar.  Usually, CFL is of the form L=L(G).
  • 7. Example 1  A simple context-free grammar is given as: S → a S b | ε  where | is used to separate multiple options for the same non-terminal, and ε stands for the empty string. This grammar generates the language { an bn : n >= 0 } , which is not regular.
  • 8. Regular languages  A regular language is a formal language (i.e., a possibly infinite set of finite sequences of symbols from a finite alphabet) that satisfies the following equivalent properties:  it can be accepted by a deterministic finite state machine  it can be accepted by a nondeterministic finite state machine  it can be accepted by an alternating finite automaton  it can be described by a regular expression  it can be generated by a regular grammar  it can be generated by a prefix grammar
  • 9. Regular languages The collection of regular languages over an alphabet Σ is defined recursively as follows:  the empty language Ø is a regular language.  the empty string language { ε } is a regular language.  For each a є Σ, the singleton language { a } is a regular language.  If A and B are regular languages, then A ∩ B (union), A ○ B (concatenation), and A* (Kleene star) are regular languages.  No other languages over Σ are regular.
  • 10. Finite languages Finite languages are:  A specific subset within the class of regular languages is the finite languages - those containing only a finite number of words. These are obviously regular as one can create a regular expression that is the union of every word in the language, and thus are regular.
  • 11. Example 2  A context-free grammar for the language consisting of all strings over {a,b} which contain a different number of a's to b's is  S → U | V  U → TaU | TaT  V → TbV | TbT  T → aTbT | bTaT | ε  Here, T can generate all strings with the same number of a's as b's, U generates all strings with more a's than b's and V generates all strings with fewer a's than b's.
  • 12. Example 3 Another example of a context-free language is This is not a regular language, but it is context free as it can be generated by the following context-free grammar:  S → b S bb | A  A → a A | ε
  • 13. Normal forms  Every context-free grammar that does not generate the empty string can be transformed into an equivalent one in Chomsky normal form or Greibach normal form. "Equivalent" here means that the two grammars generate the same language.  Because of the especially simple form of production rules in Chomsky Normal Form grammars, this normal form has both theoretical and practical implications.  For instance, given a context-free grammar, one can use the Chomsky Normal Form to construct a polynomial-time algorithm which decides whether a given string is in the language represented by that grammar or not (the CYK algorithm).
  • 14. Properties of context-free languages  An alternative and equivalent definition of context- free languages employs non-deterministic push- down automata: a language is context-free if and only if it can be accepted by such an automaton.  A language can also be modeled as a set of all sequences of terminals which are accepted by the grammar. This model is helpful in understanding set operations on languages.  The union and concatenation of two context-free languages is context-free, but the intersection need not be.  The reverse of a context-free language is context- free, but the complement need not be.
  • 15. Properties of context-free languages  Every regular language is context-free because it can be described by a regular grammar.  The intersection of a context-free language and a regular language is always context-free.  There exist context-sensitive languages which are not context-free.  To prove that a given language is not context-free, one may employ the pumping lemma for context- free languages.  The problem of determining if a context-sensitive grammar describes a context-free language is undecidable.
  • 16. Normal forms for Context-Free Grammars The goal is to show that every CFL (without ε) is generated by a CFG in which all productions are of the form A  BC or A  a, where A, B, C are variables, and a is a terminal.
  • 17. Normal forms for Context-Free Grammars  A number of simplifications is inevitable: 1. The elimination of useless symbols, “variables or terminals that do not appear in any derivation of a terminal string from the start symbol”. 2. The elimination of ε-productions, those of the form A  ε for some variable A. 3. The elimination of unit productions, those of the form A  B for variables A and B.
  • 18. Eliminating useless symbols  A symbol X is useful for Grammar G = {V, T, P, S}, if there is some derivation of the form S ═>* a X b ═>* w , where w є T*  X є V or X є T  The sentential form of a X b might be the first or last derivation  If X is not useful, then X is useless
  • 19. Eliminating useless symbols  Characteristics of useful symbols (for instance X): 1. X is generating if X ═>* w for some terminal string w. Every terminal is generating since w can be that terminal itself, which is derived by 0 steps. 2. X is reachable if there is a derivation S ═>* a X b for some a and b. A symbol which is useful is surely to be both generating and reachable.
  • 20. Eliminating useless symbols Eliminating the symbols which are not generating first followed by eliminating the symbols which are not reachable from the remaining grammar, this will generate a grammar consisting of only useful symbols.
  • 21. Eliminating useless symbols  Example 7.1  If we have the following grammar:
  • 22. Eliminating useless symbols  Example 7.1  Notice that a and b generate themselves “terminals”, S generates a, and A generates b. B is not generating.  After eliminating B:
  • 23. Eliminating useless symbols  Example 7.1  Notice that only S and a are reachable after eliminating the non-generating B.  A is not reachable; so it should be eliminated.  The result :  This production itself is a grammar that has the same result, which is {a}, as the original grammar.
  • 24. Computing the generating and reachable symbols  Basis: Every Symbol of T is obviously generating; it generates itself.  Induction: If we have a production A → a, and every symbol of a is already known to be generating, then A is generating; because it generates all and only generating symbols, even if a = ε ; since all variables that have ε as a production body are generating.  Theorem: The previous algorithm finds all and only the Generating symbols of G
  • 25. Computing the generating and reachable symbols  Basis : For a grammar G = {V, T, P, S} S is surely reachable.  Induction: If we discovered that some variable A is reachable, then for all productions with A in the head (first part of the expression), all the symbols of the bodies (second part of the expression) of those productions are also reachable.  Theorem: The above algorithm finds all and only the Reachable symbols of G
  • 26. Eliminating useless symbols  So far, the first step, which is the elimination of useless symbols is concluded.  Now, for the second part, which is the elimination of ε-productions.
  • 27. Eliminating ε-productions  The strategy is to have the following: if L is CFG, then L – {ε} is also CFG  This is done through discovering the nullable variables. A variable for instance A, is nullable if: A ═>* ε .  Whenever A appears in a production body, A might or might not derive ε
  • 28. Eliminating ε-productions  Basis: If A  ε is a production of G, then A is nullable  Induction: If there is a production B  C1 C2 … Ck such that each C is a variable and each C is nullable, then B is nullable
  • 29. Eliminating ε-productions  Theorem: For any grammar G, the only nullable symbols are the variables that derive ε in previous algorithm  Proof: for one step : A  ε must be a production, then this implies that A is discovered as nullable (as in basis). for N > 1 steps: the first step is A  C1 C2 … Ck  ε , each Ci derives ε by a sequence < N steps. By the induction, each Ci is discovered by the algorithm to be nullable. So by the inductive step, A is eventually found to be nullable.
  • 30. Eliminating ε-productions  If a grammar G1 is constructed by the elimination of ε-productions “ using the previous method ” of grammar G, then L(G1) = L(G) - {ε}
  • 31. Eliminating unit productions  The last part concerns the eliminating of unit productions  Any production of the form A  B , where A and B are variables, is called a unit production.  These production introduce extra steps in the derivations that obviously are not needed in there.
  • 32. Eliminating unit productions  Basis: (A, A) is a unit pair of any variable A, if A ═>* A by 0 steps.  Induction: Let’s (A, B) be a unit pair, and let B  C is a production, where A, B, and C are variables, then we can conclude that (A, C) is also a unit pair.  Theorem: The previous algorithm (basis and induction) finds exactly all the unit pairs for any grammar G.
  • 35. Eliminating unit productions  Example 7.12 After eliminating the unit productions, the generated grammar is: This grammar has no unit productions and still generates the same expressions as the previous one.
  • 36. Chomsky Normal Form Conclusion of all three elimination stages: Theorem: If G is a CFG which generates a language that consists of at least one string along with ε, then there is another CFG G1 such that: L{G1} = L{G} – {ε} , “no ε-productions”, and G1 has neither unit productions nor useless symbols
  • 37. Chomsky Normal Form  Proof: Start by performing the elimination of ε-productions. Then perform the elimination of unit productions, so the resulting grammar won’t introduce any ε-productions since the new bodies are still identical to some bodies of the old grammar. Finally, perform the elimination of useless symbols, and since this eliminates productions and symbols, it will never reintroduce any ε-productions nor unit productions
  • 38. Chomsky Normal Form Every nonempty CFL without ε has grammar G in which all productions are in one of the following forms:  A  BC , where A, B, and C are variables or  A  a , where A is a variable and a is a terminal  Also G doesn’t contain any useless symbols  A grammar complying to these forms is called a Chomsky Normal Form (CNF).
  • 39. Chomsky Normal Form  The construction of CNF is performed through: 1. Arrangement of all bodies of length 2 or more to contain only variables. 2. Breaking bodies of length 3 or more into a cascade productions, where each one has a body consisting of 2 variables.
  • 40. Chomsky Normal Form  Example 7.15
  • 41. Chomsky Normal Form  Example 7.15  First: we introduce new variables to represent terminals:
  • 42. Chomsky Normal Form  Example 7.15  Second: We make all bodies either a single terminal or multiple variables:
  • 43. Chomsky Normal Form  Example 7.15  Last step: we make all bodies either a single terminal or two variables:
  翻译: